Search code examples
emacsgdbgud

attach to a different program after detach


I used gud-gdb in emacs. First, I attached to a program1's PID 29514

(gdb) attach 29514
Attaching to program: program1
...

Then detached it.

(gdb) detach
Detaching from program: program1, process 29514

Then I wanted to another program program2 with pid 4917.

(gdb) attach 4917
Attaching to program: program1, process 4917
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
0x00007fbfc52604c0 in ?? ()

We saw that GDB still wanted to use program1. Is there a way to let gdb clear the last detached program?


Solution

  • I reproduced this behavior using current trunk GDB.

    I believe it's a bug: the documentation says:

    "When you use attach, the debugger finds the program running in the process
    first by looking in the current working directory, then (if the program
    is not found) ..."
    

    It doesn't distinguish between the first and the second attach, and it doesn't say that GDB will not find the program again if the new process is running different program from the old one.

    You can work around this with the file command:

    (gdb) attach $PID1
    ...
    (gdb) detach
    
    (gdb) file prog2    # you shouldn't have to do this
    (gdb) attach $PID2  # works fine