question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attach to PID does not process "Initial Commands" before attaching

See original GitHub issue

The current method used to attach to a process on the local machine is to specify the PID on the command line to the debugger. However, by doing this, the debugger will attach to the process prior to executing the initial configuration commands (specified in MI2::initCommands). This is inconsistent with remote attach operations (using target-select remote or target-select extended-remote) which only attach to the remote process after the initial configuration commands have been processed.

The problem with attaching prior to processing the initial configuration commands is that there can be spurious errors that occur because of this. For example, if a source path mapping is needed to resolve the source code, when the debugger attaches to the process, it will attempt to locate the source file, but won’t be able to find it because the source path mappings commands have not yet been processed. Additionally, this can also lead to errors attempting other commands, such as gdb-set target-async on failing with “Cannot change this setting while the inferior is running”.

The below output demonstrates both the source path mapping issue as well as the issue with the failing gdb-set target-async on command. I’d suggest that the current behavior be changed to process the initial configuration commands first, and then issue a “target-attach” command afterwards to attach to the local process.

1-gdb-set target-async on
2-list-features
3-environment-directory "/home/troy/git/hello_world_exe"
4-gdb-set substitute-path "C:\\usr\\src" "/home/troy/git/hello_world_exe/../hello_world_c"
GDB -> App: {"outOfBandRecord":[{"isStream":false,"type":"notify","asyncClass":"thread-group-added","output":[["id","i1"]]}]}
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"Attaching to process 7801\n"}]}
Attaching to process 7801
GDB -> App: {"outOfBandRecord":[{"isStream":false,"type":"notify","asyncClass":"thread-group-started","output":[["id","i1"],["pid","7801"]]}]}
GDB -> App: {"outOfBandRecord":[{"isStream":false,"type":"notify","asyncClass":"thread-created","output":[["id","1"],["group-id","i1"]]}]}
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"Reading symbols from /home/troy/git/hello_world_exe/hello_world-gcc..."}]}
Reading symbols from /home/troy/git/hello_world_exe/hello_world-gcc...
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"done.\n"}]}
done.
GDB -> App: {"outOfBandRecord":[{"isStream":false,"type":"notify","asyncClass":"library-loaded","output":[["id","/lib/x86_64-linux-gnu/libc.so.6"],["target-name","/lib/x86_64-linux-gnu/libc.so.6"],["host-name","/lib/x86_64-linux-gnu/libc.so.6"],["symbols-loaded","0"],["thread-group","i1"],["ranges",[[["from","0x00007f8d2b173360"],["to","0x00007f8d2b2ebafc"]]]]]}]}
GDB -> App: {"outOfBandRecord":[{"isStream":false,"type":"notify","asyncClass":"library-loaded","output":[["id","/lib64/ld-linux-x86-64.so.2"],["target-name","/lib64/ld-linux-x86-64.so.2"],["host-name","/lib64/ld-linux-x86-64.so.2"],["symbols-loaded","0"],["thread-group","i1"],["ranges",[[["from","0x00007f8d2b543f10"],["to","0x00007f8d2b564550"]]]]]}]}
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"Reading symbols from /lib/x86_64-linux-gnu/libc.so.6..."}]}
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.27.so..."}]}
Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.27.so...
5-thread-info
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"done.\n"}]}
done.
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"done.\n"}]}
done.
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"Reading symbols from /lib64/ld-linux-x86-64.so.2..."}]}
Reading symbols from /lib64/ld-linux-x86-64.so.2...
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.27.so..."}]}
Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.27.so...
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"done.\n"}]}
done.
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"done.\n"}]}
done.
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"console","content":"main () at hello_world.c:6\n"}]}
main () at hello_world.c:6
GDB -> App: {"outOfBandRecord":[{"isStream":true,"type":"log","content":"6\thello_world.c: No such file or directory.\n"}]}
6	hello_world.c: No such file or directory.
GDB -> App: {"outOfBandRecord":[{"isStream":false,"type":"exec","asyncClass":"stopped","output":[["frame",[["addr","0x00005615fb5f864a"],["func","main"],["args",[]],["file","hello_world.c"],["fullname","C:\\usr\\src/hello_world.c"],["line","6"]]],["thread-id","1"],["stopped-threads","all"],["core","5"]]}]}
GDB -> App: {"token":1,"outOfBandRecord":[],"resultRecords":{"resultClass":"error","results":[["msg","Cannot change this setting while the inferior is running."]]}}
WARNING: Error executing command 'gdb-set target-async on'
GDB -> App: {"token":2,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["features",["frozen-varobjs","pending-breakpoints","thread-info","data-read-memory-bytes","breakpoint-notifications","ada-task-info","language-option","info-gdb-mi-command","undefined-command-error-code","exec-run-start-option","python"]]]}}
GDB -> App: {"token":3,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["source-path","/home/troy/git/hello_world_exe:$cdir:$cwd"]]}}
GDB -> App: {"token":4,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[]}}
GDB -> App: {"token":5,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["threads",[[["id","1"],["target-id","process 7801"],["name","hello_world-gcc"],["frame",[["level","0"],["addr","0x00005615fb5f864a"],["func","main"],["args",[]],["file","hello_world.c"],["fullname","/home/troy/git/hello_world_c/hello_world.c"],["line","6"]]],["state","stopped"],["core","5"]]]],["current-thread-id","1"]]}}
6-thread-info
GDB -> App: {"token":6,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["threads",[[["id","1"],["target-id","process 7801"],["name","hello_world-gcc"],["frame",[["level","0"],["addr","0x00005615fb5f864a"],["func","main"],["args",[]],["file","hello_world.c"],["fullname","/home/troy/git/hello_world_c/hello_world.c"],["line","6"]]],["state","stopped"],["core","5"]]]],["current-thread-id","1"]]}}
7-stack-info-depth --thread 1
8-stack-info-depth --thread 1
GDB -> App: {"token":7,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["depth","1"]]}}
GDB -> App: {"token":8,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["depth","1"]]}}
9-stack-list-frames --thread 1 0 0
10-stack-list-frames --thread 1 0 0
GDB -> App: {"token":9,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["stack",[["frame",[["level","0"],["addr","0x00005615fb5f864a"],["func","main"],["file","hello_world.c"],["fullname","/home/troy/git/hello_world_c/hello_world.c"],["line","6"]]]]]]}}
GDB -> App: {"token":10,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["stack",[["frame",[["level","0"],["addr","0x00005615fb5f864a"],["func","main"],["file","hello_world.c"],["fullname","/home/troy/git/hello_world_c/hello_world.c"],["line","6"]]]]]]}}
11-thread-info
GDB -> App: {"token":11,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["threads",[[["id","1"],["target-id","process 7801"],["name","hello_world-gcc"],["frame",[["level","0"],["addr","0x00005615fb5f864a"],["func","main"],["args",[]],["file","hello_world.c"],["fullname","/home/troy/git/hello_world_c/hello_world.c"],["line","6"]]],["state","stopped"],["core","5"]]]],["current-thread-id","1"]]}}
12-thread-info
GDB -> App: {"token":12,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["threads",[[["id","1"],["target-id","process 7801"],["name","hello_world-gcc"],["frame",[["level","0"],["addr","0x00005615fb5f864a"],["func","main"],["args",[]],["file","hello_world.c"],["fullname","/home/troy/git/hello_world_c/hello_world.c"],["line","6"]]],["state","stopped"],["core","5"]]]],["current-thread-id","1"]]}}
13-stack-info-depth --thread 1
GDB -> App: {"token":13,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["depth","1"]]}}
14-stack-list-frames --thread 1 0 0
GDB -> App: {"token":14,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["stack",[["frame",[["level","0"],["addr","0x00005615fb5f864a"],["func","main"],["file","hello_world.c"],["fullname","/home/troy/git/hello_world_c/hello_world.c"],["line","6"]]]]]]}}
15-stack-info-depth --thread 1
GDB -> App: {"token":15,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["depth","1"]]}}
16-stack-list-frames --thread 1 0 0
GDB -> App: {"token":16,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["stack",[["frame",[["level","0"],["addr","0x00005615fb5f864a"],["func","main"],["file","hello_world.c"],["fullname","/home/troy/git/hello_world_c/hello_world.c"],["line","6"]]]]]]}}
17-stack-list-variables --thread 1 --frame 0 --simple-values
GDB -> App: {"token":17,"outOfBandRecord":[],"resultRecords":{"resultClass":"done","results":[["variables",[]]]}}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
GitMenschcommented, Apr 7, 2022

Thanks for your work on this extension, it helps a lot!

1reaction
WebFreak001commented, Apr 7, 2022

@brownts would you also like to have collaborator access?

Also we might want to move the repository to a GitHub team eventually, so there can be more than one owner on it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Xcode: Could not attach to pid | Apple Developer Forums
Failure Reason: attach failed (Not allowed to attach to process. Look in the console messages (Console.app), near the debugserver entries when the attached...
Read more >
How do I attach a terminal to a detached process?
Yes, it is. First, create a pipe: mkfifo /tmp/fifo . Use gdb to attach to the process: gdb -p PID. Then close stdin:...
Read more >
How to attach a process in gdb - Stack Overflow
I want to attach the child process to gdb. I run the main program in a console and open another console to find...
Read more >
Attaching to a Running Process Using Code::Blocks - dummies
Type the PID in the PID to Attach To field and then click OK. You see the Debugger tab of the Logs and...
Read more >
Attach to running processes with the Visual Studio debugger
You can attach the Visual Studio debugger to a running process on a ... you attached to by first attempting to match the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found