获取有关分段错误或崩溃的指令指针(对于 x86 JIT 编译器项目)?
我正在实现生成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果使用
sigaction
,则可以定义一个带有 3 个参数的信号处理程序:传递给信号处理程序的第三个参数是指向操作系统和体系结构特定数据结构的指针。在 Linux 上,它是在
头文件中定义的ucontext_t
。其中,uc_mcontext
是一个mcontext_t
(机器上下文),对于 x86,它包含gregs
中信号发出时的所有寄存器。这样就可以访问到出错指令的指令指针。
If you use
sigaction
, you can define a signal handler that takes 3 arguments: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 anmcontext_t
(machine context) which for x86 contains all the registers at the time of the signal ingregs
. So you can accessto get the instruction pointer of the faulting instruction.