如何解决 Linux 32 位机器上的 REG_EIP 未声明(在此函数中首次使用)错误?

发布于 2024-11-02 03:18:24 字数 2334 浏览 1 评论 0 原文

在使用 gcc 编译用 C 语言编写的信号处理程序时,我在出现分段错误后显示转储的寄存器值时遇到了错误。当我尝试使用代码访问它时:

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]);
                    }
                }

我通过添加

                 #include<sys/ucontext.h> 

以及

                 #define _GNU_SOURCE
                 #ifndef REG_EIP
                 #define REG_EIP 0x23b46F
                 #endif

但是,出现的错误是:

 ‘REG_EIP’ undeclared (first use in this function) 
 (Each undeclared identifier is reported only once  for each function it appears in.)

并且所有寄存器都出现该错误,

我尝试了很多文档...但无法得到解决方案。 任何人都可以分享解决此错误的详细信息。

提前感谢所有回复者

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国产ˉ祖宗 2024-11-09 03:18:24

我相信您应该将 #define _GNU_SOURCE 作为源文件的第一行,或者最好将 -D_GNU_SOURCE 放在您的 CFLAGS 中(在命令行)。然后确保包含

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

谁的新欢旧爱 2024-11-09 03:18:24

尝试在包含 之前定义 __USE_GNU

#define __USE_GNU
#include <ucontext.h>

您不需要包含 明确地, 将执行此操作。

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.

半夏半凉 2024-11-09 03:18:24

尝试使用 32 位,因为这些是 32 位模式值。 gcc -m32 应该可以解决这个问题。

Try using 32 bit as those are 32 bit mode values. gcc -m32 should solve this.

萌︼了一个春 2024-11-09 03:18:24

对我来说,这是通过以下方式解决的:
yum 删除 openssl-devel.x86_64
yum install openssl-devel.i686

在 CentOS 6.4 (x86_64) 上

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文