Add GDB module that defines an islprint command
authorTobias Grosser <grosser@fim.uni-passau.de>
Thu, 30 Dec 2010 20:12:21 +0000 (15:12 -0500)
committerSven Verdoolaege <skimo@kotnet.org>
Fri, 31 Dec 2010 08:50:36 +0000 (09:50 +0100)
Add a python library that is installed next to libisl. GDB will automatically
load it and add a new gdbcommand called "islprint". This command can be used to
prettyprint isl values. It does not install an official pretty printer, as such
a printer will also pretty print stacktraces which may reference values already
freed.

Before:
(gdb) print stride
$1 = (isl_set *) 0x122e6c0

Now also possible:
(gdb) islprint stride
$1 = "{ MemRef_A[1] }"
(gdb) is stride
$1 = "{ MemRef_A[1] }"

Signed-off-by: Tobias Grosser <grosser@fim.uni-passau.de>
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
Makefile.am
isl.py [new file with mode: 0644]

index 578981d..54088f5 100644 (file)
@@ -197,6 +197,7 @@ EXTRA_DIST = \
        basis_reduction_templ.c \
        isl_pw_templ.c \
        isl_union_templ.c \
+       isl.py \
        doc/chicago.bst \
        doc/chicago.sty \
        doc/implementation.tex \
@@ -218,3 +219,11 @@ pkgconfig_DATA = $(pkgconfig_libfile)
 
 gitversion.h: @GIT_HEAD@
        echo '#define GIT_HEAD_ID "'@GIT_HEAD_VERSION@'"' > $@
+
+install-data-local: $(srcdir)/isl.py
+       @libisl=`sed -ne "/^library_names=/{s/.*='//;s/'$$//;s/ .*//;p}" \
+                $(builddir)/libisl.la`; \
+       case $$libisl in \
+       '') echo Cannot find isl library name. GDB bindings not installed.;; \
+       *) echo $(INSTALL_DATA) $(srcdir)/isl.py $(libdir)/$$libisl-gdb.py; \
+       $(INSTALL_DATA) $(srcdir)/isl.py $(libdir)/$$libisl-gdb.py; esac
diff --git a/isl.py b/isl.py
new file mode 100644 (file)
index 0000000..6382f63
--- /dev/null
+++ b/isl.py
@@ -0,0 +1,100 @@
+import gdb
+import re
+
+# GDB Pretty Printers for most isl objects
+class IslObjectPrinter:
+       """Print an isl object"""
+       def __init__ (self, val, type):
+                self.val = val
+                self.type = type
+
+       def to_string (self):
+                # Cast val to a void pointer to stop gdb using this pretty
+                # printer for the pointer which would lead to an infinite loop.
+                void_ptr = gdb.lookup_type('void').pointer()
+                value = str(self.val.cast(void_ptr))
+                printer = gdb.parse_and_eval("isl_printer_to_str(isl_"
+                                             + str(self.type)
+                                            + "_get_ctx(" + value + "))")
+                printer = gdb.parse_and_eval("isl_printer_print_"
+                                             + str(self.type) + "("
+                                             + str(printer) + ", "
+                                             + value + ")")
+                string = gdb.parse_and_eval("(char*)isl_printer_get_str("
+                                            + str(printer) + ")")
+                gdb.parse_and_eval("isl_printer_free(" + str(printer) + ")")
+                return string
+
+       def display_hint (self):
+                return 'string'
+
+class IslIntPrinter:
+       """Print an isl_int """
+       def __init__ (self, val):
+                self.val = val
+
+       def to_string (self):
+                # Cast val to a void pointer to stop gdb using this pretty
+                # printer for the pointer which would lead to an infinite loop.
+                void_ptr = gdb.lookup_type('void').pointer()
+                value = str(self.val.cast(void_ptr))
+
+                context = gdb.parse_and_eval("isl_ctx_alloc()")
+                printer = gdb.parse_and_eval("isl_printer_to_str("
+                                             + str(context) + ")")
+                printer = gdb.parse_and_eval("isl_printer_print_isl_int("
+                                             + str(printer) + ", "
+                                             + value + ")")
+                string = gdb.parse_and_eval("(char*)isl_printer_get_str("
+                                            + str(printer) + ")")
+                gdb.parse_and_eval("isl_printer_free(" + str(printer) + ")")
+                gdb.parse_and_eval("isl_ctx_free(" + str(context) + ")")
+                return string
+
+       def display_hint (self):
+                return 'string'
+
+class IslPrintCommand (gdb.Command):
+        """Print an isl value."""
+        def __init__ (self):
+                super (IslPrintCommand, self).__init__ ("islprint",
+                                                        gdb.COMMAND_OBSCURE)
+        def invoke (self, arg, from_tty):
+                arg = gdb.parse_and_eval(arg);
+                printer = str_lookup_function(arg)
+
+                if printer == None:
+                        print "No isl printer for this type"
+                        return
+
+                print printer.to_string()
+
+IslPrintCommand()
+
+def str_lookup_function (val):
+       if val.type.code != gdb.TYPE_CODE_PTR:
+               if str(val.type) == "isl_int":
+                       return IslIntPrinter(val)
+                else:
+                        return None
+
+       lookup_tag = val.type.target()
+       regex = re.compile ("^isl_(.*)$")
+
+       if lookup_tag == None:
+               return None
+
+       m = regex.match (str(lookup_tag))
+
+       if m:
+                # Those types of printers defined in isl.
+                if m.group(1) in ["basic_set", "set", "union_set", "basic_map",
+                                  "map", "union_map", "qpolynomial",
+                                  "pw_qpolynomial", "pw_qpolynomial_fold",
+                                  "union_pw_qpolynomial",
+                                  "union_pw_qpolynomial_fold"]:
+                        return IslObjectPrinter(val, m.group(1))
+        return None
+
+# Do not register the pretty printer.
+# gdb.current_objfile().pretty_printers.append(str_lookup_function)