]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbarch.py
gdb/gdbarch: compare some fields against 0 verify_gdbarch
[thirdparty/binutils-gdb.git] / gdb / gdbarch.py
1 #!/usr/bin/env python3
2
3 # Architecture commands for GDB, the GNU debugger.
4 #
5 # Copyright (C) 1998-2022 Free Software Foundation, Inc.
6 #
7 # This file is part of GDB.
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 import textwrap
23 import gdbcopyright
24
25 # All the components created in gdbarch-components.py.
26 components = []
27
28 def join_type_and_name(t, n):
29 "Combine the type T and the name N into a C declaration."
30 if t.endswith("*") or t.endswith("&"):
31 return t + n
32 else:
33 return t + " " + n
34
35
36 def join_params(params):
37 """Given a sequence of (TYPE, NAME) pairs, generate a comma-separated
38 list of declarations."""
39 params = [join_type_and_name(p[0], p[1]) for p in params]
40 return ", ".join(params)
41
42
43 class _Component:
44 "Base class for all components."
45
46 def __init__(self, **kwargs):
47 for key in kwargs:
48 setattr(self, key, kwargs[key])
49 components.append(self)
50
51 def get_predicate(self):
52 "Return the expression used for validity checking."
53 assert self.predicate and not isinstance(self.invalid, str)
54 if self.predefault:
55 predicate = f"gdbarch->{self.name} != {self.predefault}"
56 elif isinstance(c, Value):
57 predicate = f"gdbarch->{self.name} != 0"
58 else:
59 predicate = f"gdbarch->{self.name} != NULL"
60 return predicate
61
62
63 class Info(_Component):
64 "An Info component is copied from the gdbarch_info."
65
66 def __init__(self, *, name, type, printer=None):
67 super().__init__(name=name, type=type, printer=printer)
68 # This little hack makes the generator a bit simpler.
69 self.predicate = None
70
71
72 class Value(_Component):
73 "A Value component is just a data member."
74
75 def __init__(
76 self,
77 *,
78 name,
79 type,
80 comment=None,
81 predicate=None,
82 predefault=None,
83 postdefault=None,
84 invalid=None,
85 printer=None,
86 ):
87 super().__init__(
88 comment=comment,
89 name=name,
90 type=type,
91 predicate=predicate,
92 predefault=predefault,
93 postdefault=postdefault,
94 invalid=invalid,
95 printer=printer,
96 )
97
98
99 class Function(_Component):
100 "A Function component is a function pointer member."
101
102 def __init__(
103 self,
104 *,
105 name,
106 type,
107 params,
108 comment=None,
109 predicate=None,
110 predefault=None,
111 postdefault=None,
112 invalid=None,
113 printer=None,
114 ):
115 super().__init__(
116 comment=comment,
117 name=name,
118 type=type,
119 predicate=predicate,
120 predefault=predefault,
121 postdefault=postdefault,
122 invalid=invalid,
123 printer=printer,
124 params=params,
125 )
126
127 def ftype(self):
128 "Return the name of the function typedef to use."
129 return f"gdbarch_{self.name}_ftype"
130
131 def param_list(self):
132 "Return the formal parameter list as a string."
133 return join_params(self.params)
134
135 def set_list(self):
136 """Return the formal parameter list of the caller function,
137 as a string. This list includes the gdbarch."""
138 arch_arg = ("struct gdbarch *", "gdbarch")
139 arch_tuple = [arch_arg]
140 return join_params(arch_tuple + list(self.params))
141
142 def actuals(self):
143 "Return the actual parameters to forward, as a string."
144 return ", ".join([p[1] for p in self.params])
145
146
147 class Method(Function):
148 "A Method is like a Function but passes the gdbarch through."
149
150 def param_list(self):
151 "See superclass."
152 return self.set_list()
153
154 def actuals(self):
155 "See superclass."
156 result = ["gdbarch"] + [p[1] for p in self.params]
157 return ", ".join(result)
158
159
160 # Read the components.
161 with open("gdbarch-components.py") as fd:
162 exec(fd.read())
163
164 copyright = gdbcopyright.copyright(
165 "gdbarch.py", "Dynamic architecture support for GDB, the GNU debugger."
166 )
167
168
169 def info(c):
170 "Filter function to only allow Info components."
171 return type(c) is Info
172
173
174 def not_info(c):
175 "Filter function to omit Info components."
176 return type(c) is not Info
177
178
179 with open("gdbarch-gen.h", "w") as f:
180 print(copyright, file=f)
181 print(file=f)
182 print(file=f)
183 print("/* The following are pre-initialized by GDBARCH. */", file=f)
184
185 # Do Info components first.
186 for c in filter(info, components):
187 print(file=f)
188 print(
189 f"""extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);
190 /* set_gdbarch_{c.name}() - not applicable - pre-initialized. */""",
191 file=f,
192 )
193
194 print(file=f)
195 print(file=f)
196 print("/* The following are initialized by the target dependent code. */", file=f)
197
198 # Generate decls for accessors, setters, and predicates for all
199 # non-Info components.
200 for c in filter(not_info, components):
201 if c.comment:
202 print(file=f)
203 comment = c.comment.split("\n")
204 if comment[0] == "":
205 comment = comment[1:]
206 if comment[-1] == "":
207 comment = comment[:-1]
208 print("/* ", file=f, end="")
209 print(comment[0], file=f, end="")
210 if len(comment) > 1:
211 print(file=f)
212 print(
213 textwrap.indent("\n".join(comment[1:]), prefix=" "),
214 end="",
215 file=f,
216 )
217 print(" */", file=f)
218
219 if c.predicate:
220 print(file=f)
221 print(f"extern bool gdbarch_{c.name}_p (struct gdbarch *gdbarch);", file=f)
222
223 print(file=f)
224 if isinstance(c, Value):
225 print(
226 f"extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);",
227 file=f,
228 )
229 print(
230 f"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.type} {c.name});",
231 file=f,
232 )
233 else:
234 assert isinstance(c, Function)
235 print(
236 f"typedef {c.type} ({c.ftype()}) ({c.param_list()});",
237 file=f,
238 )
239 print(
240 f"extern {c.type} gdbarch_{c.name} ({c.set_list()});",
241 file=f,
242 )
243 print(
244 f"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.ftype()} *{c.name});",
245 file=f,
246 )
247
248 with open("gdbarch.c", "w") as f:
249 print(copyright, file=f)
250 print(file=f)
251 print("/* Maintain the struct gdbarch object. */", file=f)
252 print(file=f)
253 #
254 # The struct definition body.
255 #
256 print("struct gdbarch", file=f)
257 print("{", file=f)
258 print(" /* Has this architecture been fully initialized? */", file=f)
259 print(" int initialized_p;", file=f)
260 print(file=f)
261 print(" /* An obstack bound to the lifetime of the architecture. */", file=f)
262 print(" struct obstack *obstack;", file=f)
263 print(file=f)
264 print(" /* basic architectural information. */", file=f)
265 for c in filter(info, components):
266 print(f" {c.type} {c.name};", file=f)
267 print(file=f)
268 print(" /* target specific vector. */", file=f)
269 print(" struct gdbarch_tdep *tdep;", file=f)
270 print(" gdbarch_dump_tdep_ftype *dump_tdep;", file=f)
271 print(file=f)
272 print(" /* per-architecture data-pointers. */", file=f)
273 print(" unsigned nr_data;", file=f)
274 print(" void **data;", file=f)
275 print(file=f)
276 for c in filter(not_info, components):
277 if isinstance(c, Value):
278 print(f" {c.type} {c.name};", file=f)
279 else:
280 assert isinstance(c, Function)
281 print(f" gdbarch_{c.name}_ftype *{c.name};", file=f)
282 print("};", file=f)
283 print(file=f)
284 #
285 # Initialization.
286 #
287 print("/* Create a new ``struct gdbarch'' based on information provided by", file=f)
288 print(" ``struct gdbarch_info''. */", file=f)
289 print(file=f)
290 print("struct gdbarch *", file=f)
291 print("gdbarch_alloc (const struct gdbarch_info *info,", file=f)
292 print(" struct gdbarch_tdep *tdep)", file=f)
293 print("{", file=f)
294 print(" struct gdbarch *gdbarch;", file=f)
295 print("", file=f)
296 print(
297 " /* Create an obstack for allocating all the per-architecture memory,", file=f
298 )
299 print(" then use that to allocate the architecture vector. */", file=f)
300 print(" struct obstack *obstack = XNEW (struct obstack);", file=f)
301 print(" obstack_init (obstack);", file=f)
302 print(" gdbarch = XOBNEW (obstack, struct gdbarch);", file=f)
303 print(" memset (gdbarch, 0, sizeof (*gdbarch));", file=f)
304 print(" gdbarch->obstack = obstack;", file=f)
305 print(file=f)
306 print(" alloc_gdbarch_data (gdbarch);", file=f)
307 print(file=f)
308 print(" gdbarch->tdep = tdep;", file=f)
309 print(file=f)
310 for c in filter(info, components):
311 print(f" gdbarch->{c.name} = info->{c.name};", file=f)
312 print(file=f)
313 print(" /* Force the explicit initialization of these. */", file=f)
314 for c in filter(not_info, components):
315 if c.predefault and c.predefault != "0":
316 print(f" gdbarch->{c.name} = {c.predefault};", file=f)
317 print(" /* gdbarch_alloc() */", file=f)
318 print(file=f)
319 print(" return gdbarch;", file=f)
320 print("}", file=f)
321 print(file=f)
322 print(file=f)
323 print(file=f)
324 #
325 # Post-initialization validation and updating
326 #
327 print("/* Ensure that all values in a GDBARCH are reasonable. */", file=f)
328 print(file=f)
329 print("static void", file=f)
330 print("verify_gdbarch (struct gdbarch *gdbarch)", file=f)
331 print("{", file=f)
332 print(" string_file log;", file=f)
333 print(file=f)
334 print(" /* fundamental */", file=f)
335 print(" if (gdbarch->byte_order == BFD_ENDIAN_UNKNOWN)", file=f)
336 print(""" log.puts ("\\n\\tbyte-order");""", file=f)
337 print(" if (gdbarch->bfd_arch_info == NULL)", file=f)
338 print(""" log.puts ("\\n\\tbfd_arch_info");""", file=f)
339 print(
340 " /* Check those that need to be defined for the given multi-arch level. */",
341 file=f,
342 )
343 for c in filter(not_info, components):
344 if c.invalid is False:
345 print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f)
346 elif c.predicate:
347 print(f" /* Skip verify of {c.name}, has predicate. */", file=f)
348 elif isinstance(c.invalid, str) and c.postdefault is not None:
349 print(f" if ({c.invalid})", file=f)
350 print(f" gdbarch->{c.name} = {c.postdefault};", file=f)
351 elif c.predefault is not None and c.postdefault is not None:
352 print(f" if (gdbarch->{c.name} == {c.predefault})", file=f)
353 print(f" gdbarch->{c.name} = {c.postdefault};", file=f)
354 elif c.postdefault is not None:
355 print(f" if (gdbarch->{c.name} == 0)", file=f)
356 print(f" gdbarch->{c.name} = {c.postdefault};", file=f)
357 elif isinstance(c.invalid, str):
358 print(f" if ({c.invalid})", file=f)
359 print(f""" log.puts ("\\n\\t{c.name}");""", file=f)
360 elif c.predefault is not None:
361 print(f" if (gdbarch->{c.name} == {c.predefault})", file=f)
362 print(f""" log.puts ("\\n\\t{c.name}");""", file=f)
363 elif c.invalid is True:
364 print(f" if (gdbarch->{c.name} == 0)", file=f)
365 print(f""" log.puts ("\\n\\t{c.name}");""", file=f)
366 else:
367 # We should not allow ourselves to simply do nothing here
368 # because no other case applies. If we end up here then
369 # either the input data needs adjusting so one of the
370 # above cases matches, or we need additional cases adding
371 # here.
372 raise Exception("unhandled case when generating gdbarch validation")
373 print(" if (!log.empty ())", file=f)
374 print(" internal_error (__FILE__, __LINE__,", file=f)
375 print(""" _("verify_gdbarch: the following are invalid ...%s"),""", file=f)
376 print(" log.c_str ());", file=f)
377 print("}", file=f)
378 print(file=f)
379 print(file=f)
380 #
381 # Dumping.
382 #
383 print("/* Print out the details of the current architecture. */", file=f)
384 print(file=f)
385 print("void", file=f)
386 print("gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)", file=f)
387 print("{", file=f)
388 print(""" const char *gdb_nm_file = "<not-defined>";""", file=f)
389 print(file=f)
390 print("#if defined (GDB_NM_FILE)", file=f)
391 print(" gdb_nm_file = GDB_NM_FILE;", file=f)
392 print("#endif", file=f)
393 print(" fprintf_filtered (file,", file=f)
394 print(""" "gdbarch_dump: GDB_NM_FILE = %s\\n",""", file=f)
395 print(" gdb_nm_file);", file=f)
396 for c in components:
397 if c.predicate:
398 print(" fprintf_filtered (file,", file=f)
399 print(
400 f""" "gdbarch_dump: gdbarch_{c.name}_p() = %d\\n",""",
401 file=f,
402 )
403 print(f" gdbarch_{c.name}_p (gdbarch));", file=f)
404 if isinstance(c, Function):
405 print(" fprintf_filtered (file,", file=f)
406 print(
407 f""" "gdbarch_dump: {c.name} = <%s>\\n",""", file=f
408 )
409 print(
410 f" host_address_to_string (gdbarch->{c.name}));",
411 file=f,
412 )
413 else:
414 if c.printer:
415 printer = c.printer
416 elif c.type == "CORE_ADDR":
417 printer = f"core_addr_to_string_nz (gdbarch->{c.name})"
418 else:
419 printer = f"plongest (gdbarch->{c.name})"
420 print(" fprintf_filtered (file,", file=f)
421 print(
422 f""" "gdbarch_dump: {c.name} = %s\\n",""", file=f
423 )
424 print(f" {printer});", file=f)
425 print(" if (gdbarch->dump_tdep != NULL)", file=f)
426 print(" gdbarch->dump_tdep (gdbarch, file);", file=f)
427 print("}", file=f)
428 print(file=f)
429 #
430 # Bodies of setter, accessor, and predicate functions.
431 #
432 for c in components:
433 if c.predicate:
434 print(file=f)
435 print("bool", file=f)
436 print(f"gdbarch_{c.name}_p (struct gdbarch *gdbarch)", file=f)
437 print("{", file=f)
438 print(" gdb_assert (gdbarch != NULL);", file=f)
439 print(f" return {c.get_predicate()};", file=f)
440 print("}", file=f)
441 if isinstance(c, Function):
442 print(file=f)
443 print(f"{c.type}", file=f)
444 print(f"gdbarch_{c.name} ({c.set_list()})", file=f)
445 print("{", file=f)
446 print(" gdb_assert (gdbarch != NULL);", file=f)
447 print(f" gdb_assert (gdbarch->{c.name} != NULL);", file=f)
448 if c.predicate and c.predefault:
449 # Allow a call to a function with a predicate.
450 print(
451 f" /* Do not check predicate: {c.get_predicate()}, allow call. */",
452 file=f,
453 )
454 print(" if (gdbarch_debug >= 2)", file=f)
455 print(
456 f""" fprintf_unfiltered (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
457 file=f,
458 )
459 print(" ", file=f, end="")
460 if c.type != "void":
461 print("return ", file=f, end="")
462 print(f"gdbarch->{c.name} ({c.actuals()});", file=f)
463 print("}", file=f)
464 print(file=f)
465 print("void", file=f)
466 print(f"set_gdbarch_{c.name} (struct gdbarch *gdbarch,", file=f)
467 print(
468 f" {' ' * len(c.name)} gdbarch_{c.name}_ftype {c.name})",
469 file=f,
470 )
471 print("{", file=f)
472 print(f" gdbarch->{c.name} = {c.name};", file=f)
473 print("}", file=f)
474 elif isinstance(c, Value):
475 print(file=f)
476 print(f"{c.type}", file=f)
477 print(f"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f)
478 print("{", file=f)
479 print(" gdb_assert (gdbarch != NULL);", file=f)
480 if c.invalid is False:
481 print(f" /* Skip verify of {c.name}, invalid_p == 0 */", file=f)
482 elif isinstance(c.invalid, str):
483 print(" /* Check variable is valid. */", file=f)
484 print(f" gdb_assert (!({c.invalid}));", file=f)
485 elif c.predefault:
486 print(" /* Check variable changed from pre-default. */", file=f)
487 print(f" gdb_assert (gdbarch->{c.name} != {c.predefault});", file=f)
488 print(" if (gdbarch_debug >= 2)", file=f)
489 print(
490 f""" fprintf_unfiltered (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
491 file=f,
492 )
493 print(f" return gdbarch->{c.name};", file=f)
494 print("}", file=f)
495 print(file=f)
496 print("void", file=f)
497 print(f"set_gdbarch_{c.name} (struct gdbarch *gdbarch,", file=f)
498 print(f" {' ' * len(c.name)} {c.type} {c.name})", file=f)
499 print("{", file=f)
500 print(f" gdbarch->{c.name} = {c.name};", file=f)
501 print("}", file=f)
502 else:
503 assert isinstance(c, Info)
504 print(file=f)
505 print(f"{c.type}", file=f)
506 print(f"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f)
507 print("{", file=f)
508 print(" gdb_assert (gdbarch != NULL);", file=f)
509 print(" if (gdbarch_debug >= 2)", file=f)
510 print(
511 f""" fprintf_unfiltered (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
512 file=f,
513 )
514 print(f" return gdbarch->{c.name};", file=f)
515 print("}", file=f)