Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Joel Brobecker <brobecker@adacore.com>
To: gdb-patches@sourceware.org
Cc: Joel Brobecker <brobecker@adacore.com>
Subject: [RFA/python:2/2] First script in GDB python library - command/pahole.py
Date: Thu, 27 May 2010 00:09:00 -0000	[thread overview]
Message-ID: <1274918921-23200-3-git-send-email-brobecker@adacore.com> (raw)
In-Reply-To: <1274918921-23200-1-git-send-email-brobecker@adacore.com>

This patch makes use of the work done in the previous patch that allows
to store the GDB python library at a global location.  It updates the
Makefile.in, adding install-python and uninstall-python targets. And
it also introduces the first script in the GDB python library: pahole.py.

Pretty much 100% of the code comes from Archer, and I think that
Tom is the original author of that code. I only tweaked it a little
to make it work at the FSF level (there are some API differences
between the Archer tree and the FSF tree).

2010-05-22  Tom Tromey  <tromey@redhat.com>
            Joel Brobecker  <brobecker@adacore.com>

        * Makefile.in (GDB_PYTHONDIR, PY_FILES): New variabls.
        (install-python, uninstall-python): New rules.
        * python/lib/gdb/__init__.py, python/lib/gdb/command/__init__.py,
        python/lib/gdb/command/pahole.py: New files.

Tested on x86_64-linux.

---
 gdb/Makefile.in                        |   32 +++++++++++++
 gdb/python/lib/gdb/__init__.py         |   16 ++++++
 gdb/python/lib/gdb/command/__init__.py |   16 ++++++
 gdb/python/lib/gdb/command/pahole.py   |   81 ++++++++++++++++++++++++++++++++
 4 files changed, 145 insertions(+), 0 deletions(-)
 create mode 100644 gdb/python/lib/gdb/__init__.py
 create mode 100644 gdb/python/lib/gdb/command/__init__.py
 create mode 100644 gdb/python/lib/gdb/command/pahole.py

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 754671f..e61ed4f 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -169,6 +169,9 @@ TARGET_SYSTEM_ROOT_DEFINE = @TARGET_SYSTEM_ROOT_DEFINE@
 # Did the user give us a --with-gdb-datadir option?
 GDB_DATADIR = @GDB_DATADIR@
 
+# The location where the GDB python library is located.
+GDB_PYTHONDIR = @GDB_PYTHONDIR@
+
 # Helper code from gnulib.
 LIBGNU = gnulib/libgnu.a
 INCGNU = -I$(srcdir)/gnulib -Ignulib
@@ -2057,6 +2060,35 @@ py-value.o: $(srcdir)/python/py-value.c
 	$(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-value.c
 	$(POSTCOMPILE)
 
+# All python library files, with the "python/lib" stripped off.
+# Note that we should only install files in the "gdb" module.
+PY_FILES = gdb/command/pahole.py gdb/command/__init__.py \
+    gdb/__init__.py
+
+# Install the Python library.  Python library files go under
+# $(GDB_PYTHONDIR).
+.PHONY: install-python
+install-python:
+	files='$(PY_FILES)'; for file in $$files; do \
+	  dir=`echo "$$file" | sed 's,/[^/]*$$,,'`; \
+	  $(SHELL) $(srcdir)/../mkinstalldirs $(DESTDIR)$(GDB_PYTHONDIR)/$$dir; \
+	  $(INSTALL_DATA) $(srcdir)/python/lib/$$file $(DESTDIR)$(GDB_PYTHONDIR)/$$file; \
+	done
+
+# Other packages may have their files installed in $(GDB_PYTHONDIR).
+.PHONY: uninstall-python
+uninstall-python:
+	files='$(PY_FILES)'; for file in $$files; do \
+	  slashdir=`echo "/$$file" | sed 's,/[^/]*$$,,'`; \
+	  rm -f $(DESTDIR)$(GDB_PYTHONDIR)/$$file; \
+	  while test "x$$file" != "x$$slashdir"; do \
+	    rmdir 2>/dev/null "$(DESTDIR)$(GDB_PYTHONDIR)$$slashdir"; \
+	    file="$$slashdir"; \
+	    slashdir=`echo "$$file" | sed 's,/[^/]*$$,,'`; \
+	  done \
+	done
+
+
 #
 # Dependency tracking.  Most of this is conditional on GNU Make being
 # found by configure; if GNU Make is not found, we fall back to a
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
new file mode 100644
index 0000000..4263473
--- /dev/null
+++ b/gdb/python/lib/gdb/__init__.py
@@ -0,0 +1,16 @@
+# Copyright (C) 2010 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/>.
+
+
diff --git a/gdb/python/lib/gdb/command/__init__.py b/gdb/python/lib/gdb/command/__init__.py
new file mode 100644
index 0000000..4263473
--- /dev/null
+++ b/gdb/python/lib/gdb/command/__init__.py
@@ -0,0 +1,16 @@
+# Copyright (C) 2010 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/>.
+
+
diff --git a/gdb/python/lib/gdb/command/pahole.py b/gdb/python/lib/gdb/command/pahole.py
new file mode 100644
index 0000000..32e694d
--- /dev/null
+++ b/gdb/python/lib/gdb/command/pahole.py
@@ -0,0 +1,81 @@
+# pahole command for gdb
+
+# Copyright (C) 2008, 2010 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
+
+class Pahole(gdb.Command):
+    """Show the holes in a structure.
+This command takes a single argument, a type name.
+It prints the type and displays comments showing where holes are."""
+
+    def __init__(self):
+        super(Pahole, self).__init__("pahole", gdb.COMMAND_NONE,
+                                     gdb.COMPLETE_SYMBOL)
+
+    @staticmethod
+    def strip(type):
+        while type.code == gdb.TYPE_CODE_TYPEDEF:
+            type = type.target()
+        return type
+
+    def pahole(self, type, level, name):
+        if name is None:
+            name = ''
+        tag = type.tag
+        if tag is None:
+            tag = ''
+        print '%sstruct %s {' % (' ' * (2 * level), tag)
+        bitpos = 0
+        for field in type.fields():
+            # Skip static fields.
+            if not hasattr(field, ('bitpos')):
+                continue
+
+            ftype = self.strip(field.type)
+
+            if bitpos != field.bitpos:
+                hole = field.bitpos - bitpos
+                print ' /* XXX %d bit hole, try to pack */' % hole
+                bitpos = field.bitpos
+            if field.bitsize > 0:
+                fieldsize = field.bitsize
+            else:
+                # TARGET_CHAR_BIT here...
+                fieldsize = 8 * ftype.sizeof
+
+            # TARGET_CHAR_BIT
+            print ' /* %3d %3d */' % (int(bitpos / 8), int(fieldsize / 8)),
+            bitpos = bitpos + fieldsize
+
+            if ftype.code == gdb.TYPE_CODE_STRUCT:
+                self.pahole(ftype, level + 1, field.name)
+            else:
+                print ' ' * (2 + 2 * level),
+                print '%s %s' % (str(ftype), field.name)
+
+        print ' ' * (14 + 2 * level),
+        print '} %s' % name
+
+    def invoke(self, arg, from_tty):
+        type = gdb.lookup_type(arg)
+        type = self.strip(type)
+        if type.code != gdb.TYPE_CODE_STRUCT:
+            raise TypeError, '%s is not a struct type' % arg
+        print ' ' * 14,
+        self.pahole(type, 0, '')
+
+Pahole()
-- 
1.7.1


  reply	other threads:[~2010-05-27  0:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-27  0:09 Add support for the GDB Python Library Joel Brobecker
2010-05-27  0:09 ` Joel Brobecker [this message]
2010-05-27 20:37   ` [RFA/python:2/2] First script in GDB python library - command/pahole.py Tom Tromey
2010-05-28  1:34     ` Joel Brobecker
2010-06-03  0:11     ` Joel Brobecker
2010-06-03  6:28       ` Doug Evans
2010-06-03 15:35         ` Joel Brobecker
2010-07-11 18:49           ` Tom Tromey
2010-05-27  0:25 ` [RFA/python:1/2] Add support for --with-pythondir Joel Brobecker
2010-05-27  6:49   ` Doug Evans
2010-05-27 20:32   ` Tom Tromey
2010-05-28  9:55     ` Doug Evans
2010-05-28 17:20       ` Joel Brobecker
2010-05-30 17:10         ` Doug Evans
2010-06-01 20:15           ` Joel Brobecker
2010-06-01 20:39             ` Doug Evans
2010-06-01 20:53               ` Joel Brobecker
2010-05-27  1:54 ` Add support for the GDB Python Library Doug Evans
2010-05-27  3:42   ` Doug Evans
2010-05-27 15:17   ` Joel Brobecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1274918921-23200-3-git-send-email-brobecker@adacore.com \
    --to=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox