Reputation: 41
I am trying to automate a GDB Debugging session, I want to know whether is there any command or any other way in GDB which will help me know whether a program is running or stopped ?
Upvotes: 4
Views: 1264
Reputation: 5258
The question is a bit ambiguous, since "a program is running or stopped" could mean several different things:
I don't know which of three you are asking for, so here's a GDB script which defines (using Python) convenience functions for all three:
python
class HasProcess(gdb.Function):
"""Do we have a running process?
Meaning the program we are debugging has been started but not yet terminated.
"""
def __init__(self):
super (HasProcess, self).__init__ ("has_process")
def invoke(self):
inferior = gdb.selected_inferior()
return inferior is not None and inferior.is_valid() and len(inferior.threads()) > 0
HasProcess()
class IsAnyThreadRunning(gdb.Function):
"""Is any thread in the process running?
"""
def __init__(self):
super (IsAnyThreadRunning, self).__init__ ("is_any_thread_running")
def invoke(self):
inferior = gdb.selected_inferior()
if inferior is None or not inferior.is_valid():
return False
threads = inferior.threads()
return any([th.is_running() for th in inferior.threads()])
IsAnyThreadRunning()
class IsThreadRunning(gdb.Function):
"""Is the selected thread running?
"""
def __init__(self):
super (IsThreadRunning, self).__init__ ("is_thread_running")
def invoke(self):
thread = gdb.selected_thread()
return thread is not None and thread.is_valid() and thread.is_running()
IsThreadRunning()
end
If you save that as a file utils.gdb
, you can load that into GDB using source utils.gdb
. After doing that, if you type help function
, it will show you have three new convenience functions defined:
function has_process -- Do we have a running process?
function is_any_thread_running -- Is any thread in the process running?
function is_thread_running -- Is the selected thread running?
So now you can put stuff like this in your GDB command scripts:
while $has_process()
next
end
quit
And it will keep on running next
until the process terminates, at which point it will quit. If you'd done while 1
, it would work until the process terminates, but then the next
would fail with an error.
I'm rather surprised GDB doesn't seem to have any convenience functions like these out of the box – it seems like a rather obvious feature, which many people have sought over the years, and I'm surprised nobody has implemented it in the core yet. But until that happens, this script works.
Note I'm using GDB 12.1 with Python 3.10.6. If you use an older version of GDB (especially one which uses Python 2.x instead), it may not work – although possibly you could make it work with some tinkering.
Also, I am only doing local debugging on Linux x86-64. If you have some unusual target (e.g. remote debugging of a bare metal embedded system), it might not work – however, any such issues can likely be overcome with some modifications to the Python code.
Upvotes: 0
Reputation: 41
I added a new command into gdb to know whether the program is running or stopped.
if(is_running (inferior_ptid))
{
fprintf_filtered (gdb_stdout, "running\n");
}
else
{
fprintf_filtered (gdb_stdout, "stopped\n");
}
Upvotes: 0
Reputation: 7228
Use gdb.selected_inferior().threads()[0].is_running()
from the GDB Python API:
$ gdb -q /bin/true
(gdb) python from __future__ import print_function
(gdb) python print([ t.is_running() for t in gdb.selected_inferior().threads() ])
[True]
Upvotes: 2