用__builtin_return_address获得程序运行栈情况

#include 
#include 
#include 

#define MAX_LEVEL 4
void sigfunc(int signo)
{
    printf("%s(0): %p\n", __func__, __builtin_return_address(0));
    exit(1);
}

int b()
{
    printf("%s(0): %p\n", __func__, __builtin_return_address(0));
    printf("%s(1): %p\n", __func__, __builtin_return_address(1));
    while(1)
    {
        sleep(1);
    }
}

int a(int temp)
{
    temp += 1;
    printf("%s(0): %p\n", __func__, __builtin_return_address(0));

    b();

    return temp;
}

int main()
{
	signal(SIGINT, sigfunc);
	a(123);

    return 0;
}

执行后:
a(0): 0x400675
b(0): 0x400653
b(1): 0x400675
^Csigfunc(0): 0x30bda33140

用gdb调试:
gdb e
...
(gdb) l *0x400675
0x400675 is in main (e.c:37).
32    int main()
33    {
34        signal(SIGINT, sigfunc);
35        a(123);
36    
37        return 0;
38    }


你可能感兴趣的:(C,programming,signal,include)