File: libisl-gdb.py

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (118 lines) | stat: -rw-r--r-- 3,345 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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)