问题
I have been coming across errors in compilation of my signal handler program written in C language with gcc in displaying the dumped register values after occurance of Segmentation fault. When i tried to access it using the code:
void print_registers(FILE *fd, ucontext_t *ctx, bool fpu = false)
{
const char *flags_str[] = {
"CF", 0, "PF", 0, "AF", 0, "ZF", "SF", "TP", "IF", "DF",
"OF", 0, 0, "NT", 0, "RF", "VM", "AC", "VIF", "VIP", "ID"
};
greg_t *regs = ctx->uc_mcontext.gregs;
void *eip[1] = { (void*)regs[REG_EIP] };
char **symbol = backtrace_symbols(eip, 1);
fprintf(fd, "Registers:\neip is at ");
backtrace_symbols_fd(eip, 1, fd->_fileno);
size_type flags = regs[REG_EFL];
fprintf(fd, "eflags: %x [ ", flags);
for (size_type i = 0; i < sizeof(flags_str) / sizeof(flags_str[0]); ++i) {
if (!flags_str[i]) continue;
if (flags & (1 << i)) fprintf(fd, "%s ", flags_str[i]);
}
size_type iopl = (flags & 0x3000) >> 12;
fprintf(fd, "] iopl: %i\n"
"eax: %08x\tebx: %08x\tecx: %08x\tedx: %08x\n"
"esi: %08x\tedi: %08x\tebp: %08x\tesp: %08x\n"
"cs: %04x\tgs: %04x\tfs: %04x\n"
"ds: %04x\tes: %04x\tss: %04x\n",
iopl,
regs[REG_EAX], regs[REG_EBX], regs[REG_ECX], regs[REG_EDX],
regs[REG_ESI], regs[REG_EDI], regs[REG_EBP], regs[REG_ESP],
regs[REG_CS], regs[REG_GS], regs[REG_FS],
regs[REG_DS], regs[REG_ES], regs[REG_SS]);
}
}
I tried the code by adding
#include<sys/ucontext.h>
as well as
#define _GNU_SOURCE
#ifndef REG_EIP
#define REG_EIP 0x23b46F
#endif
But, the error appearing is:
‘REG_EIP’ undeclared (first use in this function)
(Each undeclared identifier is reported only once for each function it appears in.)
and the error is appearing for all registers
I tried many documents...but couldn't get the solution. Can anybody share the details for resolving this error.
Advance thanks to all repliers
回答1:
Try defining __USE_GNU
before including <ucontext.h
:
#define __USE_GNU
#include <ucontext.h>
You don't need to include <sys/ucontext.h>
explicitly, <ucontext.h>
will do that.
回答2:
I believe you should either have #define _GNU_SOURCE
as the first line of your source file, or better put -D_GNU_SOURCE
in your CFLAGS
(on the command line). Then make sure you include <signal.h>
and <ucontext.h>
.
回答3:
Try using 32 bit as those are 32 bit mode values. gcc -m32 should solve this.
回答4:
For me, this was resolved with: yum remove openssl-devel.x86_64 yum install openssl-devel.i686
on CentOS 6.4 (x86_64)
Hope this helps.
来源:https://stackoverflow.com/questions/5679267/how-to-resolve-reg-eip-undeclared-first-use-in-this-function-error-on-linux-3