public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
* [RFC][5/5] Sample python scripts
@ 2009-08-23 15:37 Oguz Kayral
  0 siblings, 0 replies; only message in thread
From: Oguz Kayral @ 2009-08-23 15:37 UTC (permalink / raw)
  To: archer

These 2 new files are sample python script for demonstrating the new
functions of gdb python scripting. check_segfault adds a handler for
sigsegv signal stops and executes a backtrace when a sigsegv event
occurs. test_events is just for printing the info inside events,
attaches itself to every current event.

check_segfault.py:

# Copyright (C) 2009 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/>.

import gdb

def sigsegv_handler (event):
    if (event.stop_signal == "SIGSEGV"):
        print "event type : %s" % (event.event_type)
        print "stop reason: %s" % (event.stop_reason)
        print "stop signal: %s" % (event.stop_signal)
        gdb.execute ("backtrace")

class Check_Segfault (gdb.Command):
    """Check segfault."""

    def __init__ (self):
        gdb.Command.__init__ (self, "check_segfault", gdb.COMMAND_STACK)

    def invoke (self, arg, from_tty):
        gdb.selected_thread().signal_stop_eventregistry.connect
(sigsegv_handler)
        print "SIGSEGV handler registered."

Check_Segfault ()


test_events.py:
# Copyright (C) 2009 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/>.

import gdb

def signal_stop_handler (event):
    print "event type : %s" % (event.event_type)
    print "stop reason: %s" % (event.stop_reason)
    print "stop signal: %s" % (event.stop_signal)

def breakpoint_stop_handler (event):
    print "event type       : %s" % (event.event_type)
    print "stop reason      : %s" % (event.stop_reason)
    print "breakpoint number: %s" % (event.breakpoint.number)

def exit_handler (event):
    print "event type: %s" % (event.event_type)
    print "exit code : %d" % (event.exit_code)

def continue_handler (event):
    print "event_type: %s" % (event.event_type)

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.selected_thread().signal_stop_eventregistry.connect
(signal_stop_handler)
        gdb.selected_thread().breakpoint_stop_eventregistry.connect
(breakpoint_stop_handler)
        gdb.selected_thread().exited_eventregistry.connect (exit_handler)
        gdb.selected_thread().continue_eventregistry.connect (continue_handler)

        print "Event testers registered."

Test_Events ()

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-08-23 15:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-23 15:37 [RFC][5/5] Sample python scripts Oguz Kayral

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).