获取有关分段错误或崩溃的指令指针(对于 x86 JIT 编译器项目)?

发布于 2024-11-29 22:45:28 字数 374 浏览 2 评论 0原文

我正在实现生成 x86 代码的 JavaScript JIT 编译器的后端。有时,由于错误,我会遇到分段错误。追溯造成这些问题的原因可能非常困难。因此,我一直想知道是否有一些“简单”的方法来捕获分段错误和其他此类崩溃,并获取导致错误的指令的地址。这样,我可以将地址映射回已编译的 x86 程序集,甚至映射回源代码。

这需要在 Linux 上运行,但最好在任何兼容 POSIX 的系统上运行。在最坏的情况下,如果我无法捕获 seg 错误并在运行的 JIT 中获取 IP,我希望能够将其捕获到外部(内核日志?),并且可能只是让编译器转储一个大文件地址到指令的映射,我可以将其与 Python 脚本或其他东西相匹配。

任何想法/建议都会受到赞赏。如果您曾经参与过自己的编译器项目,请随意分享您自己的调试技巧。

I'm implementing the backend for a JavaScript JIT compiler that produces x86 code. Sometimes, as the result of bugs, I get segmentation faults. It can be quite difficult to trace back what caused them. Hence, I've been wondering if there would be some "easy" way to trap segmentation faults and other such crashes, and get the address of the instruction that caused the fault. This way, I could map the address back to compiled x86 assembly, or even back to source code.

This needs to work on Linux, but ideally on any POSIX compliant system. In the worst case, if I can't catch the seg fault and get the IP in my running JIT, I'd like to be able to trap it outside (kernel log?), and perhaps just have the compiler dump a big file with mappings of addresses to instructions, which I could match with a Python script or something.

Any ideas/suggestions are appreciated. Feel free to share your own debugging tips if you've ever worked on a compiler project of your own.

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

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

发布评论

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

评论(1

紫轩蝶泪 2024-12-06 22:45:28

如果使用sigaction,则可以定义一个带有 3 个参数的信号处理程序:

void (*sa_sigaction)(int signum, siginfo_t *info, void *ucontext)

传递给信号处理程序的第三个参数是指向操作系统和体系结构特定数据结构的指针。在 Linux 上,它是在 头文件中定义的 ucontext_t。其中,uc_mcontext 是一个 mcontext_t(机器上下文),对于 x86,它包含 gregs 中信号发出时的所有寄存器。这样就可以访问

ucontext->uc_mcontext.gregs[REG_EIP]  (32 bit mode)
ucontext->uc_mcontext.gregs[REG_RIP]  (64 bit mode)

到出错指令的指令指针。

If you use sigaction, you can define a signal handler that takes 3 arguments:

void (*sa_sigaction)(int signum, siginfo_t *info, void *ucontext)

The third argument passed to the signal handler is a pointer to an OS and architecture specific data structure. On linux, its a ucontext_t which is defined in the <sys/ucontext.h> header file. Within that, uc_mcontext is an mcontext_t (machine context) which for x86 contains all the registers at the time of the signal in gregs. So you can access

ucontext->uc_mcontext.gregs[REG_EIP]  (32 bit mode)
ucontext->uc_mcontext.gregs[REG_RIP]  (64 bit mode)

to get the instruction pointer of the faulting instruction.

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