]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-nested-maps.py
ltmain.sh: allow more flags at link-time
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-nested-maps.py
CommitLineData
1d506c26 1# Copyright (C) 2019-2024 Free Software Foundation, Inc.
2e62ab40
AB
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
19import re
c2cf30e7 20
2e62ab40
AB
21import gdb
22
13123da8
SM
23
24def _iterator1(pointer, len):
2e62ab40
AB
25 while len > 0:
26 map = pointer.dereference()
13123da8
SM
27 yield ("", map["name"])
28 yield ("", map.dereference())
2e62ab40
AB
29 pointer += 1
30 len -= 1
31
13123da8
SM
32
33def _iterator2(pointer1, pointer2, len):
2e62ab40
AB
34 while len > 0:
35 yield ("", pointer1.dereference())
36 yield ("", pointer2.dereference())
37 pointer1 += 1
38 pointer2 += 1
39 len -= 1
40
13123da8
SM
41
42class pp_map(object):
2e62ab40
AB
43 def __init__(self, val):
44 self.val = val
45
46 def to_string(self):
13123da8 47 if self.val["show_header"] == 0:
2e62ab40
AB
48 return None
49 else:
50 return "pp_map"
51
52 def children(self):
13123da8
SM
53 return _iterator2(self.val["keys"], self.val["values"], self.val["length"])
54
55 def display_hint(self):
56 return "map"
2e62ab40 57
2e62ab40 58
13123da8 59class pp_map_map(object):
2e62ab40
AB
60 def __init__(self, val):
61 self.val = val
62
63 def to_string(self):
13123da8 64 if self.val["show_header"] == 0:
2e62ab40
AB
65 return None
66 else:
67 return "pp_map_map"
68
69 def children(self):
13123da8 70 return _iterator1(self.val["values"], self.val["length"])
2e62ab40 71
13123da8
SM
72 def display_hint(self):
73 return "map"
2e62ab40 74
13123da8
SM
75
76def lookup_function(val):
2e62ab40
AB
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:
13123da8 84 type = type.target()
2e62ab40
AB
85
86 # Get the unqualified type, stripped of typedefs.
13123da8 87 type = type.unqualified().strip_typedefs()
2e62ab40
AB
88
89 # Get the type name.
90 typename = type.tag
91
f9e59d06 92 if typename is None:
2e62ab40
AB
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:
13123da8
SM
99 if function.match(typename):
100 return pretty_printers_dict[function](val)
2e62ab40
AB
101
102 # Cannot find a pretty printer. Return None.
103 return None
104
13123da8 105
2e62ab40 106# Lookup a printer for VAL in the typedefs dict.
13123da8 107def lookup_typedefs_function(val):
2e62ab40
AB
108 "Look-up and return a pretty-printer that can print val (typedefs)."
109
110 # Get the type.
111 type = val.type
112
f9e59d06 113 if type is None or type.name is None or type.code != gdb.TYPE_CODE_TYPEDEF:
2e62ab40
AB
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:
13123da8
SM
120 if function.match(type.name):
121 return typedefs_pretty_printers_dict[function](val)
2e62ab40
AB
122
123 # Cannot find a pretty printer.
124 return None
125
13123da8
SM
126
127def 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
2e62ab40
AB
133
134# Dict for struct types with typedefs fully stripped.
135pretty_printers_dict = {}
136# Dict for typedef types.
137typedefs_pretty_printers_dict = {}
138
13123da8
SM
139register_pretty_printers()
140gdb.pretty_printers.append(lookup_function)
141gdb.pretty_printers.append(lookup_typedefs_function)