diff mbox

[doc,RFA,PR,15276] Add $_caller_is, et. al.

Message ID m3zjejhre2.fsf@seba.sebabeach.org
State New
Headers show

Commit Message

Doug Evans Sept. 1, 2014, 11:07 p.m. UTC
Hi.

Redhat has $caller_is and $caller_matches in their tree.
I've had a use for them from time to time, and I've finally gone ahead
and finished the patch so that it can be submitted.

I've also had a use for $_any_caller_is/$_any_caller_matches so I've
added those too.

Redhat names these functions $caller_is and $caller_matches
but convention in the FSF tree is to prepend the name with "_"
so I've done that to be consistent.

Jan: I'm happy to add a Redhat person to the ChangeLog entry,
but I'm not sure who wrote this.  Was it you or Tom or ...?

2014-09-01  Doug Evans  <xdje42@gmail.com>
	    ??? <???@redhat.com>

        PR 15276
	* NEWS: Mention $_caller_is, $_caller_matches, $_any_caller_is,
	$_any_caller_matches.
	* data-directory/Makefile.in (PYTHON_FILE_LIST): Add caller_is.py.
	* python/lib/gdb/function/caller_is.py: New file.

	testsuite/
	* gdb.python/py-caller-is.c: New file.
	* gdb.python/py-caller-is.exp: New file.

	doc/
	* gdb.texinfo (Convenience Funs): Document $_caller_is,
	$_caller_matches, $_any_caller_is, $_any_caller_matches.

Comments

Sergio Durigan Junior Sept. 2, 2014, 1:34 a.m. UTC | #1
On Monday, September 01 2014, Doug Evans wrote:

> Jan: I'm happy to add a Redhat person to the ChangeLog entry,
> but I'm not sure who wrote this.  Was it you or Tom or ...?

Hello Doug,

According to 5bbc9e9236e752b546f99749a2ebc758c5fb7fcf on archer.git, the
author was Tom.
Jan Kratochvil Sept. 2, 2014, 7:43 a.m. UTC | #2
On Tue, 02 Sep 2014 01:07:01 +0200, Doug Evans wrote:
> Jan: I'm happy to add a Redhat person to the ChangeLog entry,
> but I'm not sure who wrote this.  Was it you or Tom or ...?

Tom:
	https://sourceware.org/git/?p=archer.git;a=blame;f=gdb/python/lib/gdb/function/caller_is.py;h=2b9c5c74a83569b60923431943aa834e97c5d7f9;hb=refs/heads/tromey/python

BTW s/Redhat/Red Hat/.


Jan
Eli Zaretskii Sept. 2, 2014, 3:28 p.m. UTC | #3
> From: Doug Evans <xdje42@gmail.com>
> Date: Mon, 01 Sep 2014 16:07:01 -0700
> 
> Redhat has $caller_is and $caller_matches in their tree.
> I've had a use for them from time to time, and I've finally gone ahead
> and finished the patch so that it can be submitted.
> 
> I've also had a use for $_any_caller_is/$_any_caller_matches so I've
> added those too.

OK for the documentation parts.

Thanks.
diff mbox

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index d603cf7..f57eccd 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,13 @@ 
 
 *** Changes since GDB 7.8
 
+* New Python-based convenience functions:
+
+  ** $_caller_is(name [, number_of_frames])
+  ** $_caller_matches(regexp [, number_of_frames])
+  ** $_any_caller_is(name [, number_of_frames])
+  ** $_any_caller_matches(regexp [, number_of_frames])
+
 * On resume, GDB now always passes the signal the program had stopped
   for to the thread the signal was sent to, even if the user changed
   threads before resuming.  Previously GDB would often (but not
diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
index 509f888..1e8cd4b 100644
--- a/gdb/data-directory/Makefile.in
+++ b/gdb/data-directory/Makefile.in
@@ -73,6 +73,7 @@  PYTHON_FILE_LIST = \
 	gdb/command/prompt.py \
 	gdb/command/explore.py \
 	gdb/function/__init__.py \
+	gdb/function/caller_is.py \
 	gdb/function/strfns.py
 
 @HAVE_PYTHON_TRUE@PYTHON_FILES = $(PYTHON_FILE_LIST)
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 8d9148c..facbd16 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -10098,6 +10098,70 @@  Otherwise it returns zero.
 @findex $_strlen@r{, convenience function}
 Returns the length of string @var{str}.
 
+@item $_caller_is(@var{name}@r{[}, @var{number_of_frames}@r{]})
+@findex $_caller_is@r{, convenience function}
+Returns one if the calling function's name is equal to @var{name}.
+Otherwise it returns zero.
+
+If the optional argument @var{number_of_frames} is provided,
+it is the number of frames up in the stack to look.
+The default is 1.
+
+Example:
+
+@smallexample
+(gdb) backtrace
+#0  bottom_func ()
+    at testsuite/gdb.python/py-caller-is.c:21
+#1  0x00000000004005a0 in middle_func ()
+    at testsuite/gdb.python/py-caller-is.c:27
+#2  0x00000000004005ab in top_func ()
+    at testsuite/gdb.python/py-caller-is.c:33
+#3  0x00000000004005b6 in main ()
+    at testsuite/gdb.python/py-caller-is.c:39
+(gdb) print $_caller_is ("middle_func")
+$1 = 1
+(gdb) print $_caller_is ("top_func", 2)
+$1 = 1
+@end smallexample
+
+@item $_caller_matches(@var{regexp}@r{[}, @var{number_of_frames}@r{]})
+@findex $_caller_matches@r{, convenience function}
+Returns one if the calling function's name matches the regular expression
+@var{regexp}.  Otherwise it returns zero.
+
+If the optional argument @var{number_of_frames} is provided,
+it is the number of frames up in the stack to look.
+The default is 1.
+
+@item $_any_caller_is(@var{name}@r{[}, @var{number_of_frames}@r{]})
+@findex $_any_caller_is@r{, convenience function}
+Returns one if any calling function's name is equal to @var{name}.
+Otherwise it returns zero.
+
+If the optional argument @var{number_of_frames} is provided,
+it is the number of frames up in the stack to look.
+The default is 1.
+
+This function differs from @code{$_caller_is} in that this function
+checks all stack frames from the immediate caller to the frame specified
+by @var{number_of_frames}, whereas @code{$_caller_is} only checks the
+frame specified by @var{number_of_frames}.
+
+@item $_any_caller_matches(@var{regexp}@r{[}, @var{number_of_frames}@r{]})
+@findex $_any_caller_matches@r{, convenience function}
+Returns one if any calling function's name matches the regular expression
+@var{regexp}.  Otherwise it returns zero.
+
+If the optional argument @var{number_of_frames} is provided,
+it is the number of frames up in the stack to look.
+The default is 1.
+
+This function differs from @code{$_caller_matches} in that this function
+checks all stack frames from the immediate caller to the frame specified
+by @var{number_of_frames}, whereas @code{$_caller_matches} only checks the
+frame specified by @var{number_of_frames}.
+
 @end table
 
 @value{GDBN} provides the ability to list and get help on
diff --git a/gdb/python/lib/gdb/function/caller_is.py b/gdb/python/lib/gdb/function/caller_is.py
new file mode 100644
index 0000000..58257df
--- /dev/null
+++ b/gdb/python/lib/gdb/function/caller_is.py
@@ -0,0 +1,156 @@ 
+# Caller-is functions.
+# Copyright (C) 2008, 2014 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):
+    """Check the calling function's name.
+
+Usage:
+  $_caller_is(name [, number_of_frames])
+
+Arguments:
+
+  name: The name of the function to search for.
+
+  number_of_frames: How many stack frames to traverse back from the currently
+    selected frame to compare with.  If the value is greater than the depth of
+    the stack from that point then the result is False.
+    The default is 1.
+
+Returns:
+  True if the function's name at the specified frame is equal to name.
+"""
+
+    def __init__ (self):
+        super (CallerIs, self).__init__ ("_caller_is")
+
+    def invoke (self, name, nframes = 1):
+        assert nframes > 0
+        frame = gdb.selected_frame ()
+        while nframes > 0:
+            frame = frame.older ()
+            if frame is None:
+                return False
+            nframes = nframes - 1
+        return frame.name () == name.string ()
+
+class CallerMatches (gdb.Function):
+    """Compare the calling function's name with a regexp.
+
+Usage:
+  $_caller_matches(regex [, number_of_frames])
+
+Arguments:
+
+  regex: The regular expression to compare the function's name with.
+
+  number_of_frames: How many stack frames to traverse back from the currently
+    selected frame to compare with.  If the value is greater than the depth of
+    the stack from that point then the result is False.
+    The default is 1.
+
+Returns:
+  True if the function's name at the specified frame matches regex.
+"""
+
+    def __init__ (self):
+        super (CallerMatches, self).__init__ ("_caller_matches")
+
+    def invoke (self, name, nframes = 1):
+        assert nframes > 0
+        frame = gdb.selected_frame ()
+        while nframes > 0:
+            frame = frame.older ()
+            if frame is None:
+                return False
+            nframes = nframes - 1
+        return re.match (name.string (), frame.name ()) is not None
+
+class AnyCallerIs (gdb.Function):
+    """Check all calling function's names.
+
+Usage:
+  $_any_caller_is(name [, number_of_frames])
+
+Arguments:
+
+  name: The name of the function to search for.
+
+  number_of_frames: How many stack frames to traverse back from the currently
+    selected frame to compare with.  If the value is greater than the depth of
+    the stack from that point then the result is False.
+    The default is 1.
+
+Returns:
+  True if any function's name is equal to name.
+"""
+
+    def __init__ (self):
+        super (AnyCallerIs, self).__init__ ("_any_caller_is")
+
+    def invoke (self, name, nframes = 1):
+        assert nframes > 0
+        frame = gdb.selected_frame ()
+        while nframes > 0:
+            frame = frame.older ()
+            if frame is None:
+                return False
+            if frame.name () == name.string ():
+                return True 
+            nframes = nframes - 1
+        return False
+
+class AnyCallerMatches (gdb.Function):
+    """Compare all calling function's names with a regexp.
+
+Usage:
+  $_any_caller_matches(regex [, number_of_frames])
+
+Arguments:
+
+  regex: The regular expression to compare the function's name with.
+
+  number_of_frames: How many stack frames to traverse back from the currently
+    selected frame to compare with.  If the value is greater than the depth of
+    the stack from that point then the result is False.
+    The default is 1.
+
+Returns:
+  True if any function's name matches regex.
+"""
+
+    def __init__ (self):
+        super (AnyCallerMatches, self).__init__ ("_any_caller_matches")
+
+    def invoke (self, name, nframes = 1):
+        assert nframes > 0
+        frame = gdb.selected_frame ()
+        name_re = re.compile (name.string ())
+        while nframes > 0:
+            frame = frame.older ()
+            if frame is None:
+                return False
+            if name_re.match (frame.name ()) is not None:
+                return True
+            nframes = nframes - 1
+        return False
+
+CallerIs()
+CallerMatches()
+AnyCallerIs()
+AnyCallerMatches()
diff --git a/gdb/testsuite/gdb.python/py-caller-is.c b/gdb/testsuite/gdb.python/py-caller-is.c
new file mode 100644
index 0000000..2c86388
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-caller-is.c
@@ -0,0 +1,41 @@ 
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2012-2014 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/>.  */
+
+static void
+bottom_func (void)
+{
+  return; /* Break bottom_func here.  */
+}
+
+static void
+middle_func (void)
+{
+  bottom_func ();
+}
+
+static void
+top_func (void)
+{
+  middle_func ();
+}
+
+int
+main ()
+{
+  top_func ();
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.python/py-caller-is.exp b/gdb/testsuite/gdb.python/py-caller-is.exp
new file mode 100644
index 0000000..bed158b
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-caller-is.exp
@@ -0,0 +1,65 @@ 
+# Copyright (C) 2012-2014 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 the convenience
+# functions in caller_is.py.
+
+load_lib gdb-python.exp
+
+standard_testfile
+
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
+    return -1
+}
+
+if ![runto_main] {
+    return 0
+}
+
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+gdb_breakpoint "bottom_func"
+gdb_continue_to_breakpoint "bottom_func"
+
+proc test_all_caller_is_fns { } {
+    gdb_test "p \$_caller_is (\"middle_func\")" " = 1"
+    gdb_test "p \$_caller_is (\"top_func\")" " = 0"
+    gdb_test "p \$_caller_is (\"middle_func\", 2)" " = 0"
+    gdb_test "p \$_caller_is (\"top_func\", 2)" " = 1"
+    gdb_test "p \$_caller_is (\"foo\", 100)" " = 0"
+
+    gdb_test "p \$_caller_matches (\"^middle_\")" " = 1"
+    gdb_test "p \$_caller_matches (\"^top_\")" " = 0"
+    gdb_test "p \$_caller_matches (\"^middle_\", 2)" " = 0"
+    gdb_test "p \$_caller_matches (\"^top_f\", 2)" " = 1"
+    gdb_test "p \$_caller_matches (\"foo\", 100)" " = 0"
+
+    gdb_test "p \$_any_caller_is (\"middle_func\")" " = 1"
+    gdb_test "p \$_any_caller_is (\"top_func\")" " = 0"
+    gdb_test "p \$_any_caller_is (\"middle_func\", 2)" " = 1"
+    gdb_test "p \$_any_caller_is (\"top_func\", 2)" " = 1"
+    gdb_test "p \$_any_caller_is (\"main\", 100)" " = 1"
+    gdb_test "p \$_any_caller_is (\"foo\", 100)" " = 0"
+
+    gdb_test "p \$_any_caller_matches (\"^middle_\")" " = 1"
+    gdb_test "p \$_any_caller_matches (\"^top_\")" " = 0"
+    gdb_test "p \$_any_caller_matches (\"^middle_\", 2)" " = 1"
+    gdb_test "p \$_any_caller_matches (\"^top_f\", 2)" " = 1"
+    gdb_test "p \$_any_caller_matches (\"^main\", 100)" " = 1"
+    gdb_test "p \$_any_caller_matches (\"foo\", 100)" " = 0"
+}
+
+test_all_caller_is_fns