GDB aborts an exception thrown by a call from a specific function

I would like to use GDB to break when an exception is thrown only when the stack passes a certain function.

My use case is that I have a Thread class whose doRun () function is called on a new thread. This thread catches any exception that bubbles upwards, but I would like to be able to break when the exception is thrown (not caught).

I know GDB can do "reverse debugging" (an awesome concept), so this can potentially be used, but I would like something more general purpose - in fact, I would like this solution to find its way to mine. gdbinit.

+3


source to share


2 answers


Recent versions of gdb have some handy functions that are useful for doing this, such as "$ _any_caller_matches". They are written in Python, so even if your gdb doesn't have them built in, you can grab the code and just put it in your gdb.

You would use it like (untested, but you get the idea):



catch throw if $_any_caller_matches("Thread::doRun")

      

+6


source


It is not displayed. $caller_matches

(or its sister-function $caller_is

) are included in the basic GDB installation.


Go ahead and add the source code to python GDB functions folder.

This folder is usually found in /usr/share/gdb/python/gdb/function

; the filename should be caller_is.py

.



Note that in use, the $caller_matches

base implementation uses re.match

, so make sure the string you pass works with this function.

In addition, both functions have an optional second parameter, which defaults to 1, which specifies how far along the stack to move (watch). This means that if you omit it, it will only check the caller's direct of the current function. If you want to check specific stack positions (for example, if you want to check the caller grandpa), use 2

, 3

etc.

I've included the source below.

# Caller-is functions.

# Copyright (C) 2008 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
import re

class CallerIs (gdb.Function):
    """Return True if the calling function name is equal to a string.
This function takes one or two arguments.
The first argument is the name of a function; if the calling function's
name is equal to this argument, this function returns True.
The optional second argument tells this function how many stack frames
to traverse to find the calling function.  The default is 1."""

    def __init__ (self):
        super (CallerIs, self).__init__ ("caller_is")

    def invoke (self, name, nframes = 1):
        frame = gdb.selected_frame ()
        while nframes > 0:
            frame = frame.older ()
            nframes = nframes - 1
        return frame.name () == name.string ()

class CallerMatches (gdb.Function):
    """Return True if the calling function name matches a string.
This function takes one or two arguments.
The first argument is a regular expression; if the calling function's
name is matched by this argument, this function returns True.
The optional second argument tells this function how many stack frames
to traverse to find the calling function.  The default is 1."""

    def __init__ (self):
        super (CallerMatches, self).__init__ ("caller_matches")

    def invoke (self, name, nframes = 1):
        frame = gdb.selected_frame ()
        while nframes > 0:
            frame = frame.older ()
            nframes = nframes - 1
        return re.match (name.string (), frame.name ()) is not None

CallerIs()
CallerMatches()

      

+5


source







All Articles