binutils-gdb/gdb/testsuite/gdb.python/py-events.py
Andrew Burgess b1f0f28418 gdb/python: add a new gdb_exiting event
Add a new event, gdb.events.gdb_exiting, which is called once GDB
decides it is going to exit.

This event is not triggered in the case that GDB performs a hard
abort, for example, when handling an internal error and the user
decides to quit the debug session, or if GDB hits an unexpected,
fatal, signal.

This event is triggered if the user just types 'quit' at the command
prompt, or if GDB is run with '-batch' and has processed all of the
required commands.

The new event type is gdb.GdbExitingEvent, and it has a single
attribute exit_code, which is the value that GDB is about to exit
with.

The event is triggered before GDB starts dismantling any of its own
internal state, so, my expectation is that most Python calls should
work just fine at this point.

When considering this functionality I wondered about using the
'atexit' Python module.  However, this is triggered when the Python
environment is shut down, which is done from a final cleanup.  At
this point we don't know for sure what other GDB state has already
been cleaned up.
2021-10-05 10:05:40 +01:00

159 lines
5.3 KiB
Python

# Copyright (C) 2010-2021 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This file is part of the GDB testsuite. It tests python pretty
# printers.
import gdb
def signal_stop_handler(event):
if isinstance(event, gdb.StopEvent):
print("event type: stop")
if isinstance(event, gdb.SignalEvent):
print("stop reason: signal")
print("stop signal: %s" % (event.stop_signal))
if event.inferior_thread is not None:
print("thread num: %s" % (event.inferior_thread.num))
def breakpoint_stop_handler(event):
if isinstance(event, gdb.StopEvent):
print("event type: stop")
if isinstance(event, gdb.BreakpointEvent):
print("stop reason: breakpoint")
print("first breakpoint number: %s" % (event.breakpoint.number))
for bp in event.breakpoints:
print("breakpoint number: %s" % (bp.number))
if event.inferior_thread is not None:
print("thread num: %s" % (event.inferior_thread.num))
else:
print("all threads stopped")
def exit_handler(event):
assert isinstance(event, gdb.ExitedEvent)
print("event type: exit")
if hasattr(event, 'exit_code'):
print("exit code: %d" % (event.exit_code))
else:
print("exit code: not-present")
print("exit inf: %d" % (event.inferior.num))
print("exit pid: %d" % (event.inferior.pid))
print("dir ok: %s" % str("exit_code" in dir(event)))
def continue_handler(event):
assert isinstance(event, gdb.ContinueEvent)
print("event type: continue")
if event.inferior_thread is not None:
print("thread num: %s" % (event.inferior_thread.num))
def new_objfile_handler(event):
assert isinstance(event, gdb.NewObjFileEvent)
print("event type: new_objfile")
print("new objfile name: %s" % (event.new_objfile.filename))
def clear_objfiles_handler(event):
assert isinstance(event, gdb.ClearObjFilesEvent)
print("event type: clear_objfiles")
print("progspace: %s" % (event.progspace.filename))
def inferior_call_handler(event):
if isinstance(event, gdb.InferiorCallPreEvent):
print("event type: pre-call")
elif isinstance(event, gdb.InferiorCallPostEvent):
print("event type: post-call")
else:
assert False
print("ptid: %s" % (event.ptid,))
print("address: 0x%x" % (event.address))
def register_changed_handler(event):
assert isinstance(event, gdb.RegisterChangedEvent)
print("event type: register-changed")
assert isinstance(event.frame, gdb.Frame)
print("frame: %s" % (event.frame))
print("num: %s" % (event.regnum))
def memory_changed_handler(event):
assert isinstance(event, gdb.MemoryChangedEvent)
print("event type: memory-changed")
print("address: %s" % (event.address))
print("length: %s" % (event.length))
class test_events(gdb.Command):
"""Test events."""
def __init__(self):
gdb.Command.__init__(self, "test-events", gdb.COMMAND_STACK)
def invoke(self, arg, from_tty):
gdb.events.stop.connect(signal_stop_handler)
gdb.events.stop.connect(breakpoint_stop_handler)
gdb.events.exited.connect(exit_handler)
gdb.events.cont.connect(continue_handler)
gdb.events.inferior_call.connect(inferior_call_handler)
gdb.events.memory_changed.connect(memory_changed_handler)
gdb.events.register_changed.connect(register_changed_handler)
print("Event testers registered.")
test_events()
class test_newobj_events(gdb.Command):
"""NewObj events."""
def __init__(self):
gdb.Command.__init__(self, "test-objfile-events", gdb.COMMAND_STACK)
def invoke(self, arg, from_tty):
gdb.events.new_objfile.connect(new_objfile_handler)
gdb.events.clear_objfiles.connect(clear_objfiles_handler)
print("Object file events registered.")
test_newobj_events()
def gdb_exiting_handler(event, throw_error):
assert isinstance(event, gdb.GdbExitingEvent)
if throw_error:
raise gdb.GdbError("error from gdb_exiting_handler")
else:
print("event type: gdb-exiting")
print("exit code: %d" % (event.exit_code))
class test_exiting_event(gdb.Command):
"""GDB Exiting event."""
def __init__(self):
gdb.Command.__init__(self, "test-exiting-event", gdb.COMMAND_STACK)
def invoke(self, arg, from_tty):
if arg == "normal":
gdb.events.gdb_exiting.connect(lambda e: gdb_exiting_handler(e,False))
elif arg == "error":
gdb.events.gdb_exiting.connect(lambda e: gdb_exiting_handler(e,True))
else:
raise gdb.GdbError("invalid or missing argument")
print("GDB exiting event registered.")
test_exiting_event()