I am trying to compile the code from the following link to print the backtrace when a signal is generated:
http://www.linuxjournal.com/article/6391?page=0,1 (from article http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/063/6391/6391l3.html)
I made the necessary changes (REG_EIP -> REG_RIP).
I also changed "#include <ucontext.h>
" to "#include <sys/ucontext.h>
" to debug my issue which I will explain below.
The top of the file is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <execinfo.h>
/* get REG_EIP from ucontext.h */
#define __USE_GNU
#include <sys/ucontext.h>
...
With the code as is, I get the following error:
# gcc ./st2.c -rdynamic -o st2
./st2.c: In function ‘bt_sighandler’:
./st2.c:22: error: ‘REG_RIP’ undeclared (first use in this function)
./st2.c:22: error: (Each undeclared identifier is reported only once
./st2.c:22: error: for each function it appears in.)
However, when I copy the line "#define __USE_GNU" to the top of "/usr/include/sys/ucontext.h" (which I know is a very bad idea and is only temporary) as follows:
#ifndef _SYS_UCONTEXT_H
#define _SYS_UCONTEXT_H 1
#define __USE_GNU
#include <features.h>
#include <signal.h>
#include <bits/wordsize.h>
............
#endif // _SYS_UCONTEXT_H
My program compiles and run correctly.
I am baffled why the #define in my program does not "flow" into the header file "sys/ucontext.h", and adding #define directly into sys/ucontext.h makes a difference. Any help would be very appreciated.j
Thank you, Ahmed.