user5118
user5118

Reputation: 41

GDB command to know whether the program is running or stopped

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

Answers (3)

Simon Kissane
Simon Kissane

Reputation: 5258

The question is a bit ambiguous, since "a program is running or stopped" could mean several different things:

  • whether you are debugging a running program – as opposed to when you've started GDB but you haven't started running the program being debugged yet (or attached to any process or remote target), or when after the program you are running has terminated
  • whether the selected thread is running or stopped, irrespective of the state of any other thread in the current inferior
  • whether at least one thread is running in the current inferior, even i the selected thread is stopped

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

user5118
user5118

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

scottt
scottt

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]

References

Upvotes: 2

Related Questions