Is there any way to tell gdb to wait for a process to start and attach to it?

后端 未结 6 1761
独厮守ぢ
独厮守ぢ 2020-12-24 01:44

I have a process that is called by another process which is called by another process and so on ad nauseum. It\'s a child process in a long tool chain.

This process

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 02:14

    I've been facing a similar problem with something I'm trying to debug, and I came up with a solution using ldpreload, but after seeing Joeys answer I think I'll try that first. In case it's helpful to anyone else though here's the idea:

    Create an LD_PRELOAD library to hook the exec* calls (there's plenty of guides on how to do this around, but if I do this I'll update my answer with the code), check the path used when passing through the exec* call, if it's our target then output a message with the PID to stderr and go into an infinite loop (with sleep to avoid massive CPU usage). Then you can attach with gdb and modify the register used in the loop to continue execution.

    This may involve some inline ASM to make sure the compiler doesn't optimise the infinite loop in such a way that makes it hard to break out of. A more eloquent way of doing it would be to find a way to detect that gdb has attached then trigger a breakpoint ("asm("int3");" should do the trick on the latter).

提交回复
热议问题