Automatically connect to a CGI process and break in GDB before it exits?

倖福魔咒の 提交于 2019-12-24 10:49:01

问题


For a C application accessed via CGI-BIN, documentation online for accessing the process and breaking in GDB relies on manipulating the source code (i.e. adding an infinite loop), in order for the process to be available long enough for a developer to attach, exit the loop, and debug.

Is it feasible that a tool could monitor the process list, and attach via GDB, immediately breaking in order for a developer to achieve this without requiring source code changes?

The rough structure of what I have in mind to develop is something along the lines of:
1. My process monitors the process list on the system.
2. A process matching the name of my application, and owner Apache appears in the list.
3. My process immediately performs a 'pgrep' and 'gdb -p' command, then sending a break-point command to pause the process.
4. The developer can then access the process and look at the flow of execution.

Is this feasible as an idea or not possible due to some constraints (i.e. a race condition which may not always be fufilled?)


回答1:


Is this feasible

Sure: a trivial shell script will do:

while true; do
  PID=$(pgrep my_app)
  if [[ -n "$PID" ]]; then
    gdb -p "$PID"
  fi
done

a race condition

The problem is that between pgrep and gdb -p the application may make significant progress, or even run to completion.

The only way to avoid that is to intercept all execve system calls on the system, as Tom Tromey's preattach.stp does.



来源:https://stackoverflow.com/questions/57230391/automatically-connect-to-a-cgi-process-and-break-in-gdb-before-it-exits

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