Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve REG_EIP undeclared (First use in this function ) error on Linux 32 bit machine?

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

like image 488
vlc Avatar asked Apr 15 '11 15:04

vlc


2 Answers

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>.

like image 130
R.. GitHub STOP HELPING ICE Avatar answered Dec 02 '22 03:12

R.. GitHub STOP HELPING ICE


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.

like image 42
Mat Avatar answered Dec 02 '22 03:12

Mat