Debug error before main() using GDB

寵の児 提交于 2021-02-08 08:54:25

问题


Is there anyway to debug a link error or any kind of error that may occur before the execution of the main() function using GDB?


回答1:


Is there anyway to debug a link error

Presumably you are asking about runtime link error (e.g. `error: libfoo.so: no such file or directory'), and not about (static) link step of your build process.

The trick is to set a breakpoint on exit or (exit_group on Linux) system call with e.g. catch syscall exit. You will then be stopped inside ld.so at the point where it gives up running your binary.

or any kind of error that may occur before the execution of the main() function using GDB?

Any other kind of error, e.g. SIGSEGV can be debugged "normally" -- for signal you don't need to do anything at all -- GDB will just stop. For other errors, just set a breakpoint as usual.




回答2:


On way to debug initialization code (even if you don't have symbols) goes like this:

gdb somebinary

GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04 etc.

info file

Symbols from "somebinary".

Local exec file:

`/somebinary', file type elf64-x86-64.

Entry point: 0x4045a4, etc.

break *0x4045a4
run

...Breakpoint 1, 0x00000000004045a4 in ?? ()

From here on you can proceed as usual.



来源:https://stackoverflow.com/questions/21149058/debug-error-before-main-using-gdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!