25

I am trying to debug with gdbserver. after I terminat the gdb client on the host I see that the gdbserver is still listening :

Remote side has terminated connection.  GDBserver will reopen the connection.
Listening on port 5004

I tried to exit gdbserver with everything I have found anywhere no luck: quit,exit,q, monitor exit,Esc,Cnt+c... nothing kills it. Moreover, when I opened another terminal and looked for the process running gdbserver (with the commands ps,top) I couldn't find it there... my question is - How to terminate gdbserver ?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
yehudahs
  • 2,488
  • 8
  • 34
  • 54

8 Answers8

26

Give command

monitor exit

from your host gdb before terminating the client. If you have already terminated it, just attach with another one.

Konstantin Shemyak
  • 2,369
  • 5
  • 21
  • 41
4

monitor exit step-by-step

https://stackoverflow.com/a/23647002/895245 mentions it, but this is the full setup you need.

Remote:

# pwd contains cross-compiled ./myexec
gdbserver --multi :1234

Local:

# pwd also contains the same cross-compiled ./myexec
gdb -ex 'target extended-remote 192.168.0.1:1234' \
    -ex 'set remote exec-file ./myexec' \
    --args ./myexec arg1
(gdb) r
[Inferior 1 (process 1234) exited normally]
(gdb) monitor exit

Tested in Ubuntu 14.04.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
2

gdbserver runs on the target, not the host.

Terminating it is target dependent. For example, if your target is UNIX-ish, you could remote login and use ps and kill from a target shell.

For any type of target, rebooting should kill gdbserver.

(If this isn't enough to answer your question, include more information about the target in the question.)

Paul Beusterien
  • 27,542
  • 6
  • 83
  • 139
  • yes - I tried all the above on the target... and yes - tried to find the gdbserver process on the target with ps and top and no luck...my machine is "Linux version 2.6.32-71.el6.x86_64 (mockbuild@x86-007.build.bos.redhat.com) (gcc version 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC) ) #1 SMP Wed Sep 1 01:33:01 EDT 2010" – yehudahs Jan 01 '14 at 17:11
2

on linux write:

ps -ef |grep gdbserver

Now find the pid of the gdbserver process and then

kill -9 <pid>
Innovation
  • 1,514
  • 17
  • 32
  • 5
    yes - I know how to kill the process. But I want to exit from within the gdbserver in the normal way. like exiting gdb with q... – yehudahs Jan 21 '14 at 10:09
  • 1
    Looks like this is the only way to terminate it from the remote target. And `ctrl+z` doesn't work either, so you have to login from another session to do this. – wisbucky Oct 11 '17 at 00:17
1

Here is a script which I'm using to start gdb server via ssh and kill it when necessary with ctrl+c

#!/usr/bin/env bash

trap stop_gdb_server INT

function stop_gdb_server {
    ssh remote-srv-name "pkill gdbserver"
    echo "GDB server killed"
}

ssh remote-srv-name "cd /path/to/project/dir/ && gdbserver localhost:6789 my-executable"
Ivan Talalaev
  • 6,014
  • 9
  • 40
  • 49
0

quit [expression]


q To exit GDB, use the quit command (abbreviated q), or type an end-of-file character (usually C-d). If you do not supply expression, GDB will terminate normally; otherwise it will terminate using the result of expression as the error code.

wsha
  • 874
  • 2
  • 9
  • 25
0

I'm going to write another answer because none of these answers made any sense to me in my context/use case.

So, suppose you're in a normal desktop environment and you've opened a terminal and run something like:

gdbserver localhost:5000 ./myapp

Maybe you did this to debug from VS Code or something. In any case, now you've got this process running in your terminal that won't go away no matter how many times you mash Ctrl+C. How do you get rid of it?

The answer: in a new terminal, run:

gdb -ex 'target extended-remote localhost:5000'

gdb might bother you with some nonsense you don't care about - just mash the enter key until you see a prompt that says (gdb). Now type monitor exit at this internal gdb command prompt. The gdbserver in the first terminal window should immediately quit (you won't get any feedback in the second terminal).

To quit gdb in the second terminal, type the command q. You might have to do it a couple of times. Or just kill the terminal.

Jack M
  • 4,769
  • 6
  • 43
  • 67
-2

gdbserver should exit when your target exits. The question is how your target is exiting: does it

  1. do nothing: just fall through
  2. return 0 in main
  3. exit(0) in main

From the debug sessions I've been running, in the first case, gdbserver will not exit. It will just hang around forever and you have to kill it. In the latter two cases, gdbserver will exit.

cup
  • 7,589
  • 4
  • 19
  • 42