]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-nested-maps.py
8d65fd8423a7d6e5c6836e0e8a387e775d517472
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-nested-maps.py
1 # Copyright (C) 2019-2025 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file is part of the GDB testsuite. It tests GDB's printing of
17 # nested map like structures.
18
19 import re
20
21 import gdb
22
23
24 def _iterator1(pointer, len):
25 while len > 0:
26 map = pointer.dereference()
27 yield ("", map["name"])
28 yield ("", map.dereference())
29 pointer += 1
30 len -= 1
31
32
33 def _iterator2(pointer1, pointer2, len):
34 while len > 0:
35 yield ("", pointer1.dereference())
36 yield ("", pointer2.dereference())
37 pointer1 += 1
38 pointer2 += 1
39 len -= 1
40
41
42 class pp_map(object):
43 def __init__(self, val):
44 self.val = val
45
46 def to_string(self):
47 if self.val["show_header"] == 0:
48 return None
49 else:
50 return "pp_map"
51
52 def children(self):
53 return _iterator2(self.val["keys"], self.val["values"], self.val["length"])
54
55 def display_hint(self):
56 return "map"
57
58
59 class pp_map_map(object):
60 def __init__(self, val):
61 self.val = val
62
63 def to_string(self):
64 if self.val["show_header"] == 0:
65 return None
66 else:
67 return "pp_map_map"
68
69 def children(self):
70 return _iterator1(self.val["values"], self.val["length"])
71
72 def display_hint(self):
73 return "map"
74
75
76 def lookup_function(val):
77 "Look-up and return a pretty-printer that can print val."
78
79 # Get the type.
80 type = val.type
81
82 # If it points to a reference, get the reference.
83 if type.code == gdb.TYPE_CODE_REF:
84 type = type.target()
85
86 # Get the unqualified type, stripped of typedefs.
87 type = type.unqualified().strip_typedefs()
88
89 # Get the type name.
90 typename = type.tag
91
92 if typename is None:
93 return None
94
95 # Iterate over local dictionary of types to determine
96 # if a printer is registered for that type. Return an
97 # instantiation of the printer if found.
98 for function in pretty_printers_dict:
99 if function.match(typename):
100 return pretty_printers_dict[function](val)
101
102 # Cannot find a pretty printer. Return None.
103 return None
104
105
106 # Lookup a printer for VAL in the typedefs dict.
107 def lookup_typedefs_function(val):
108 "Look-up and return a pretty-printer that can print val (typedefs)."
109
110 # Get the type.
111 type = val.type
112
113 if type is None or type.name is None or type.code != gdb.TYPE_CODE_TYPEDEF:
114 return None
115
116 # Iterate over local dictionary of typedef types to determine if a
117 # printer is registered for that type. Return an instantiation of
118 # the printer if found.
119 for function in typedefs_pretty_printers_dict:
120 if function.match(type.name):
121 return typedefs_pretty_printers_dict[function](val)
122
123 # Cannot find a pretty printer.
124 return None
125
126
127 def register_pretty_printers():
128 pretty_printers_dict[re.compile("^struct map_t$")] = pp_map
129 pretty_printers_dict[re.compile("^map_t$")] = pp_map
130 pretty_printers_dict[re.compile("^struct map_map_t$")] = pp_map_map
131 pretty_printers_dict[re.compile("^map_map_t$")] = pp_map_map
132
133
134 # Dict for struct types with typedefs fully stripped.
135 pretty_printers_dict = {}
136 # Dict for typedef types.
137 typedefs_pretty_printers_dict = {}
138
139 register_pretty_printers()
140 gdb.pretty_printers.append(lookup_function)
141 gdb.pretty_printers.append(lookup_typedefs_function)