Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | # Copyright 2022 The Dawn & Tint Authors |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 2 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | # Redistribution and use in source and binary forms, with or without |
| 4 | # modification, are permitted provided that the following conditions are met: |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 5 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | # 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | # list of conditions and the following disclaimer. |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 8 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | # this list of conditions and the following disclaimer in the documentation |
| 11 | # and/or other materials provided with the distribution. |
| 12 | # |
| 13 | # 3. Neither the name of the copyright holder nor the names of its |
| 14 | # contributors may be used to endorse or promote products derived from |
| 15 | # this software without specific prior written permission. |
| 16 | # |
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 27 | |
| 28 | # Pretty printers for the Tint project. |
| 29 | # Add a line to your ~/.gdbinit to source this file, e.g.: |
| 30 | # |
| 31 | # source /path/to/dawn/src/tint/tint_gdb.py |
| 32 | |
| 33 | import gdb |
| 34 | import gdb.printing |
| 35 | from itertools import chain |
| 36 | |
| 37 | # When debugging this module, set _DEBUGGING = True so that re-sourcing this file in gdb replaces |
| 38 | # the existing printers. |
| 39 | _DEBUGGING = True |
| 40 | |
| 41 | # Enable to display other data members along with child elements of compound data types (arrays, etc.). |
| 42 | # This is useful in debuggers like VS Code that doesn't display the `to_string()` result in the watch window. |
| 43 | # OTOH, it's less useful when using gdb/lldb's print command. |
| 44 | _DISPLAY_MEMBERS_AS_CHILDREN = False |
| 45 | |
| 46 | |
| 47 | # Tips for debugging using VS Code: |
| 48 | # - Set a breakpoint where you can view the types you want to debug/write pretty printers for. |
| 49 | # - Debug Console: source /path/to/dawn/src/tint/tint_gdb.py |
| 50 | # - To execute Python code, in the Debug Console: |
| 51 | # -exec python foo = gdb.parse_and_eval('map.set_') |
| 52 | # -exec python v = (foo['slots_']['impl_']['slice']['data'] + 8).dereference()['value'] |
| 53 | # |
| 54 | # - Useful docs: |
| 55 | # Python API: https://sourceware.org/gdb/onlinedocs/gdb/Python-API.html#Python-API |
| 56 | # Especially: |
| 57 | # Types: https://sourceware.org/gdb/onlinedocs/gdb/Types-In-Python.html#Types-In-Python |
| 58 | # Values: https://sourceware.org/gdb/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior |
| 59 | |
| 60 | |
| 61 | pp_set = gdb.printing.RegexpCollectionPrettyPrinter("tint") |
| 62 | |
| 63 | |
| 64 | class Printer(object): |
| 65 | '''Base class for Printers''' |
| 66 | |
| 67 | def __init__(self, val): |
| 68 | self.val = val |
| 69 | |
| 70 | def template_type(self, index): |
| 71 | '''Returns template type at index''' |
| 72 | return self.val.type.template_argument(index) |
| 73 | |
| 74 | |
| 75 | class UtilsSlicePrinter(Printer): |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 76 | '''Printer for tint::Slice<T>''' |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 77 | |
| 78 | def __init__(self, val): |
| 79 | super(UtilsSlicePrinter, self).__init__(val) |
| 80 | self.len = self.val['len'] |
| 81 | self.cap = self.val['cap'] |
| 82 | self.data = self.val['data'] |
| 83 | self.elem_type = self.data.type.target().unqualified() |
| 84 | |
| 85 | def length(self): |
| 86 | return self.len |
| 87 | |
| 88 | def value_at(self, index): |
| 89 | '''Returns array value at index''' |
| 90 | return (self.data + index).dereference().cast(self.elem_type) |
| 91 | |
| 92 | def to_string(self): |
| 93 | return 'length={} capacity={}'.format(self.len, self.cap) |
| 94 | |
| 95 | def members(self): |
| 96 | if _DISPLAY_MEMBERS_AS_CHILDREN: |
| 97 | return [ |
| 98 | ('length', self.len), |
| 99 | ('capacity', self.cap), |
| 100 | ] |
| 101 | else: |
| 102 | return [] |
| 103 | |
| 104 | def children(self): |
| 105 | for m in self.members(): |
| 106 | yield m |
| 107 | for i in range(self.len): |
| 108 | yield str(i), self.value_at(i) |
| 109 | |
| 110 | def display_hint(self): |
| 111 | return 'array' |
| 112 | |
| 113 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 114 | pp_set.add_printer('UtilsSlicePrinter', '^tint::Slice<.*>$', UtilsSlicePrinter) |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | class UtilsVectorPrinter(Printer): |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 118 | '''Printer for tint::Vector<T, N>''' |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 119 | |
| 120 | def __init__(self, val): |
| 121 | super(UtilsVectorPrinter, self).__init__(val) |
| 122 | self.slice = self.val['impl_']['slice'] |
| 123 | self.using_heap = self.slice['cap'] > self.template_type(1) |
| 124 | |
| 125 | def slice_printer(self): |
| 126 | return UtilsSlicePrinter(self.slice) |
| 127 | |
| 128 | def to_string(self): |
| 129 | return 'heap={} {}'.format(self.using_heap, self.slice) |
| 130 | |
| 131 | def members(self): |
| 132 | if _DISPLAY_MEMBERS_AS_CHILDREN: |
| 133 | return [ |
| 134 | ('heap', self.using_heap), |
| 135 | ] |
| 136 | else: |
| 137 | return [] |
| 138 | |
| 139 | def children(self): |
| 140 | return chain(self.members(), self.slice_printer().children()) |
| 141 | |
| 142 | def display_hint(self): |
| 143 | return 'array' |
| 144 | |
| 145 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 146 | pp_set.add_printer('UtilsVector', '^tint::Vector<.*>$', UtilsVectorPrinter) |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | class UtilsVectorRefPrinter(Printer): |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 150 | '''Printer for tint::VectorRef<T>''' |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 151 | |
| 152 | def __init__(self, val): |
| 153 | super(UtilsVectorRefPrinter, self).__init__(val) |
| 154 | self.slice = self.val['slice_'] |
| 155 | self.can_move = self.val['can_move_'] |
| 156 | |
| 157 | def to_string(self): |
| 158 | return 'can_move={} {}'.format(self.can_move, self.slice) |
| 159 | |
| 160 | def members(self): |
| 161 | if _DISPLAY_MEMBERS_AS_CHILDREN: |
| 162 | return [ |
| 163 | ('can_move', self.can_move), |
| 164 | ] |
| 165 | else: |
| 166 | return [] |
| 167 | |
| 168 | def children(self): |
| 169 | return chain(self.members(), UtilsSlicePrinter(self.slice).children()) |
| 170 | |
| 171 | def display_hint(self): |
| 172 | return 'array' |
| 173 | |
| 174 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 175 | pp_set.add_printer('UtilsVector', '^tint::VectorRef<.*>$', |
| 176 | UtilsVectorRefPrinter) |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 177 | |
| 178 | |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 179 | class UtilsHashmapBasePrinter(Printer): |
| 180 | '''Base Printer for HashmapBase-derived types''' |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 181 | |
| 182 | def __init__(self, val): |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 183 | super(UtilsHashmapBasePrinter, self).__init__(val) |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 184 | self.slice = UtilsVectorPrinter(self.val['slots_']).slice_printer() |
| 185 | self.try_read_std_optional_func = self.try_read_std_optional |
| 186 | |
| 187 | def to_string(self): |
| 188 | length = 0 |
| 189 | for slot in range(0, self.slice.length()): |
| 190 | v = self.slice.value_at(slot) |
| 191 | if v['hash'] != 0: |
| 192 | length += 1 |
| 193 | return 'length={}'.format(length) |
| 194 | |
| 195 | def children(self): |
| 196 | for slot in range(0, self.slice.length()): |
| 197 | v = self.slice.value_at(slot) |
| 198 | if v['hash'] != 0: |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 199 | entry = v['entry'] |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 200 | |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 201 | # entry is a std::optional, let's try to extract its value for display |
| 202 | kvp = self.try_read_std_optional_func(slot, entry) |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 203 | if kvp is None: |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 204 | # If we failed, just output the slot and entry as is, which will use |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 205 | # the default visualizer for each. |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 206 | kvp = slot, entry |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 207 | |
| 208 | yield str(kvp[0]), kvp[1] |
| 209 | |
| 210 | def display_hint(self): |
| 211 | return 'array' |
| 212 | |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 213 | def try_read_std_optional(self, slot, entry): |
| 214 | return None |
| 215 | |
| 216 | |
| 217 | class UtilsHashsetPrinter(UtilsHashmapBasePrinter): |
| 218 | '''Printer for Hashset<T, N, HASH, EQUAL>''' |
| 219 | |
| 220 | def try_read_std_optional(self, slot, entry): |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 221 | try: |
| 222 | # libstdc++ |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 223 | v = entry['_M_payload']['_M_payload']['_M_value'] |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 224 | return slot, v |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 225 | except: |
| 226 | return None |
| 227 | |
| 228 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 229 | pp_set.add_printer('UtilsHashset', '^tint::Hashset<.*>$', UtilsHashsetPrinter) |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 230 | |
| 231 | |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 232 | class UtilsHashmapPrinter(UtilsHashmapBasePrinter): |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 233 | '''Printer for Hashmap<K, V, N, HASH, EQUAL>''' |
| 234 | |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 235 | def try_read_std_optional(self, slot, entry): |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 236 | try: |
| 237 | # libstdc++ |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 238 | kvp = entry['_M_payload']['_M_payload']['_M_value'] |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 239 | return str(kvp['key']), kvp['value'] |
| 240 | except: |
Antonio Maiorano | c027f33 | 2022-11-07 14:00:53 +0000 | [diff] [blame] | 241 | return None |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 242 | |
| 243 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 244 | pp_set.add_printer('UtilsHashmap', '^tint::Hashmap<.*>$', UtilsHashmapPrinter) |
Antonio Maiorano | 14b5fb6 | 2022-10-27 20:17:45 +0000 | [diff] [blame] | 245 | |
| 246 | |
| 247 | gdb.printing.register_pretty_printer(gdb, pp_set, replace=_DEBUGGING) |