This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[python] a sample function


I went ahead and added "require function" and wrote a couple useful
convenience functions.  "caller_is" is the function that got me
started hacking python-gdb earlier this year...

Thiago, it should be simple to drop your function in here now :-)

Tom

2008-11-23  Tom Tromey  <tromey@redhat.com>

	* Makefile.in (PY_FILES): Add caller_is.py.
	* python/lib/gdb/function/caller_is.py: New file.
	* python/lib/gdb/command/require.py: Add "require function".

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 26e4aaf..6ef02bf 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1917,12 +1917,13 @@ python-value.o: $(srcdir)/python/python-value.c
 # first.  These are maintained by hand because that is simpler than
 # writing portable sh to make the __init__.py files, and the result is
 # faster.
-PY_DIRS = gdb gdb/command gdb/libstdcxx gdb/libstdcxx/v6
+PY_DIRS = gdb gdb/command gdb/function gdb/libstdcxx gdb/libstdcxx/v6
 
 # All python library files, with the "python/lib" stripped off.
 # Note that we should only install files in the "gdb" module.
 PY_FILES = gdb/backtrace.py gdb/command/alias.py \
-    gdb/command/backtrace.py gdb/command/require.py gdb/FrameIterator.py \
+    gdb/command/backtrace.py gdb/command/require.py \
+    gdb/function/caller_is.py gdb/FrameIterator.py \
     gdb/__init__.py gdb/libstdcxx/v6/printers.py
 
 # Install the Python library.  Python library files go under
diff --git a/gdb/python/lib/gdb/command/require.py b/gdb/python/lib/gdb/command/require.py
index 2112b9a..36b63e5 100644
--- a/gdb/python/lib/gdb/command/require.py
+++ b/gdb/python/lib/gdb/command/require.py
@@ -54,4 +54,4 @@ class RequireSubcommand (gdb.Command):
 
 RequireCommand()
 RequireSubcommand("command")
-# Don't install a "require function" command until we have one.
+RequireSubcommand("function")
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..8afa721
--- /dev/null
+++ b/gdb/python/lib/gdb/function/caller_is.py
@@ -0,0 +1,58 @@
+# 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's 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.get_current_frame ()
+        while nframes > 0:
+            frame = frame.get_prev ()
+            nframes = nframes - 1
+        return frame.get_name () == name.string ()
+
+class CallerMatches (gdb.Function):
+    """Return True if the calling function's 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.get_current_frame ()
+        while nframes > 0:
+            frame = frame.get_prev ()
+            nframes = nframes - 1
+        return re.match (name.string (), frame.get_name ()) is not None
+
+CallerIs()
+CallerMatches()


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]