Wait and attach to a process when it starts?

Sysprogs forums Forums VisualGDB Wait and attach to a process when it starts?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #8968
    BitFlipper
    Participant

    Hi,

    The program I need to debug starts up as part of another sequence of operations. So it isn’t the 1st process that starts up, and hence I can’t tell GDB to start a new instance, neither can I tell it to attach to an existing process because it only starts up later at a non-deterministic time.

    I know that attaching to `pidof <program_name>’ works, but is there a way to let VGDB/GDB wait around and attach to a process the instance it starts up?

    I’ve seen some hacks where one creates an infinite loop in the program, then once it starts up, attach to it and then use the debugger to break it out of the loop. But there has to be a better way.

    This would be similar to what the VS plugin Entrian Attach does on Windows.

    • This topic was modified 7 years, 8 months ago by BitFlipper.
    #8970
    support
    Keymaster

    Hi,

    Yes, you can achieve this by making a small script that waits for the process to start and then gets its PID and outputs it, then add this script as a custom pre-debug action that will set the AttachPID variable.

    Then you can select “Attach to existing instance -> specify PID -> 0” on the Debug Settings page (VisualGDB will show a hint that $(AttachPID) will be used to specify the PID).

    So each time you start debugging, VisualGDB will launch your script that will locate the process, store the PID output by it in $(AttachPID) and immediately attach to it as if you had selected the process manually.

    #8974
    BitFlipper
    Participant

    OK thanks I’ll give this a try!

    A follow up question… I’m not familiar with how this works, but if GDB starts a script (say python), that eventually causes the program to run, can I simply specify the script as the program to start, and will GDB eventually hit the breakpoints in the program? IOW, will the script’s sub-processes (and sub-sub-sub-etc-processes) still be considered to be debugged?

    • This reply was modified 7 years, 8 months ago by BitFlipper.
    • This reply was modified 7 years, 8 months ago by BitFlipper.
    #8980
    support
    Keymaster

    Hi,

    Unfortunately that won’t work reliably. GDB has support for multiple inferiors (i.e. debugging multiple process at the same time), but it’s very unreliable when it comes to handling events from the processes and stopping them. The solution with the custom script should be the easiest.

    To make it more clear please refer to the screenshot below for details:

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.