3 # Architecture commands for GDB, the GNU debugger.
5 # Copyright (C) 1998-2023 Free Software Foundation, Inc.
7 # This file is part of GDB.
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.
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.
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/>.
25 # All the components created in gdbarch-components.py.
29 def indentation(n_columns
):
30 """Return string with tabs and spaces to indent line to N_COLUMNS."""
31 return "\t" * (n_columns
// 8) + " " * (n_columns
% 8)
34 def join_type_and_name(t
, n
):
35 "Combine the type T and the name N into a C declaration."
36 if t
.endswith("*") or t
.endswith("&"):
42 def join_params(params
):
43 """Given a sequence of (TYPE, NAME) pairs, generate a comma-separated
44 list of declarations."""
45 params
= [join_type_and_name(p
[0], p
[1]) for p
in params
]
46 return ", ".join(params
)
50 "Base class for all components."
52 def __init__(self
, **kwargs
):
54 setattr(self
, key
, kwargs
[key
])
55 components
.append(self
)
57 # It doesn't make sense to have a check of the result value
58 # for a function or method with void return type.
59 if self
.type == "void" and self
.result_checks
:
60 raise Exception("can't have result checks with a void return type")
62 def get_predicate(self
):
63 "Return the expression used for validity checking."
64 assert self
.predicate
and not isinstance(self
.invalid
, str)
66 predicate
= f
"gdbarch->{self.name} != {self.predefault}"
67 elif isinstance(c
, Value
):
68 predicate
= f
"gdbarch->{self.name} != 0"
70 predicate
= f
"gdbarch->{self.name} != NULL"
74 class Info(_Component
):
75 "An Info component is copied from the gdbarch_info."
77 def __init__(self
, *, name
, type, printer
=None):
78 super().__init
__(name
=name
, type=type, printer
=printer
)
79 # This little hack makes the generator a bit simpler.
83 class Value(_Component
):
84 "A Value component is just a data member."
103 predefault
=predefault
,
104 postdefault
=postdefault
,
110 class Function(_Component
):
111 "A Function component is a function pointer member."
134 predefault
=predefault
,
135 postdefault
=postdefault
,
139 param_checks
=param_checks
,
140 result_checks
=result_checks
,
145 "Return the name of the function typedef to use."
146 return f
"gdbarch_{self.name}_ftype"
148 def param_list(self
):
149 "Return the formal parameter list as a string."
150 return join_params(self
.params
)
153 """Return the formal parameter list of the caller function,
154 as a string. This list includes the gdbarch."""
155 arch_arg
= ("struct gdbarch *", "gdbarch")
156 arch_tuple
= [arch_arg
]
157 return join_params(arch_tuple
+ list(self
.params
))
160 "Return the actual parameters to forward, as a string."
161 return ", ".join([p
[1] for p
in self
.params
])
164 class Method(Function
):
165 "A Method is like a Function but passes the gdbarch through."
167 def param_list(self
):
169 return self
.set_list()
173 result
= ["gdbarch"] + [p
[1] for p
in self
.params
]
174 return ", ".join(result
)
177 # Read the components.
178 with
open("gdbarch-components.py") as fd
:
181 copyright
= gdbcopyright
.copyright(
182 "gdbarch.py", "Dynamic architecture support for GDB, the GNU debugger."
187 "Filter function to only allow Info components."
188 return type(c
) is Info
192 "Filter function to omit Info components."
193 return type(c
) is not Info
196 with
open("gdbarch-gen.h", "w") as f
:
197 print(copyright
, file=f
)
200 print("/* The following are pre-initialized by GDBARCH. */", file=f
)
202 # Do Info components first.
203 for c
in filter(info
, components
):
206 f
"""extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);
207 /* set_gdbarch_{c.name}() - not applicable - pre-initialized. */""",
213 print("/* The following are initialized by the target dependent code. */", file=f
)
215 # Generate decls for accessors, setters, and predicates for all
216 # non-Info components.
217 for c
in filter(not_info
, components
):
220 comment
= c
.comment
.split("\n")
222 comment
= comment
[1:]
223 if comment
[-1] == "":
224 comment
= comment
[:-1]
225 print("/* ", file=f
, end
="")
226 print(comment
[0], file=f
, end
="")
230 textwrap
.indent("\n".join(comment
[1:]), prefix
=" "),
238 print(f
"extern bool gdbarch_{c.name}_p (struct gdbarch *gdbarch);", file=f
)
241 if isinstance(c
, Value
):
243 f
"extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);",
247 f
"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.type} {c.name});",
251 assert isinstance(c
, Function
)
253 f
"typedef {c.type} ({c.ftype()}) ({c.param_list()});",
258 f
"extern {c.type} gdbarch_{c.name} ({c.set_list()});",
262 f
"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.ftype()} *{c.name});",
266 with
open("gdbarch.c", "w") as f
:
267 print(copyright
, file=f
)
269 print("/* Maintain the struct gdbarch object. */", file=f
)
272 # The struct definition body.
274 print("struct gdbarch", file=f
)
276 print(" /* Has this architecture been fully initialized? */", file=f
)
277 print(" bool initialized_p = false;", file=f
)
279 print(" /* An obstack bound to the lifetime of the architecture. */", file=f
)
280 print(" auto_obstack obstack;", file=f
)
281 print(" /* Registry. */", file=f
)
282 print(" registry<gdbarch> registry_fields;", file=f
)
284 print(" /* basic architectural information. */", file=f
)
285 for c
in filter(info
, components
):
286 print(f
" {c.type} {c.name};", file=f
)
288 print(" /* target specific vector. */", file=f
)
289 print(" struct gdbarch_tdep_base *tdep = nullptr;", file=f
)
290 print(" gdbarch_dump_tdep_ftype *dump_tdep = nullptr;", file=f
)
292 print(" /* per-architecture data-pointers. */", file=f
)
293 print(" unsigned nr_data = 0;", file=f
)
294 print(" void **data = nullptr;", file=f
)
296 for c
in filter(not_info
, components
):
297 if isinstance(c
, Function
):
298 print(f
" gdbarch_{c.name}_ftype *", file=f
, end
="")
300 print(f
" {c.type} ", file=f
, end
="")
301 print(f
"{c.name} = ", file=f
, end
="")
302 if c
.predefault
is not None:
303 print(f
"{c.predefault};", file=f
)
304 elif isinstance(c
, Value
):
307 assert isinstance(c
, Function
)
308 print("nullptr;", file=f
)
314 print("/* Create a new ``struct gdbarch'' based on information provided by", file=f
)
315 print(" ``struct gdbarch_info''. */", file=f
)
317 print("struct gdbarch *", file=f
)
318 print("gdbarch_alloc (const struct gdbarch_info *info,", file=f
)
319 print(" struct gdbarch_tdep_base *tdep)", file=f
)
321 print(" struct gdbarch *gdbarch;", file=f
)
323 print(" gdbarch = new struct gdbarch;", file=f
)
325 print(" gdbarch->tdep = tdep;", file=f
)
327 for c
in filter(info
, components
):
328 print(f
" gdbarch->{c.name} = info->{c.name};", file=f
)
330 print(" return gdbarch;", file=f
)
336 # Post-initialization validation and updating
338 print("/* Ensure that all values in a GDBARCH are reasonable. */", file=f
)
340 print("static void", file=f
)
341 print("verify_gdbarch (struct gdbarch *gdbarch)", file=f
)
343 print(" string_file log;", file=f
)
345 print(" /* fundamental */", file=f
)
346 print(" if (gdbarch->byte_order == BFD_ENDIAN_UNKNOWN)", file=f
)
347 print(""" log.puts ("\\n\\tbyte-order");""", file=f
)
348 print(" if (gdbarch->bfd_arch_info == NULL)", file=f
)
349 print(""" log.puts ("\\n\\tbfd_arch_info");""", file=f
)
351 " /* Check those that need to be defined for the given multi-arch level. */",
354 for c
in filter(not_info
, components
):
355 if c
.invalid
is False:
356 print(f
" /* Skip verify of {c.name}, invalid_p == 0 */", file=f
)
358 print(f
" /* Skip verify of {c.name}, has predicate. */", file=f
)
359 elif isinstance(c
.invalid
, str) and c
.postdefault
is not None:
360 print(f
" if ({c.invalid})", file=f
)
361 print(f
" gdbarch->{c.name} = {c.postdefault};", file=f
)
362 elif c
.predefault
is not None and c
.postdefault
is not None:
363 print(f
" if (gdbarch->{c.name} == {c.predefault})", file=f
)
364 print(f
" gdbarch->{c.name} = {c.postdefault};", file=f
)
365 elif c
.postdefault
is not None:
366 print(f
" if (gdbarch->{c.name} == 0)", file=f
)
367 print(f
" gdbarch->{c.name} = {c.postdefault};", file=f
)
368 elif isinstance(c
.invalid
, str):
369 print(f
" if ({c.invalid})", file=f
)
370 print(f
""" log.puts ("\\n\\t{c.name}");""", file=f
)
371 elif c
.predefault
is not None:
372 print(f
" if (gdbarch->{c.name} == {c.predefault})", file=f
)
373 print(f
""" log.puts ("\\n\\t{c.name}");""", file=f
)
374 elif c
.invalid
is True:
375 print(f
" if (gdbarch->{c.name} == 0)", file=f
)
376 print(f
""" log.puts ("\\n\\t{c.name}");""", file=f
)
378 # We should not allow ourselves to simply do nothing here
379 # because no other case applies. If we end up here then
380 # either the input data needs adjusting so one of the
381 # above cases matches, or we need additional cases adding
383 raise Exception("unhandled case when generating gdbarch validation")
384 print(" if (!log.empty ())", file=f
)
386 """ internal_error (_("verify_gdbarch: the following are invalid ...%s"),""",
389 print(" log.c_str ());", file=f
)
396 print("/* Print out the details of the current architecture. */", file=f
)
398 print("void", file=f
)
399 print("gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)", file=f
)
401 print(""" const char *gdb_nm_file = "<not-defined>";""", file=f
)
403 print("#if defined (GDB_NM_FILE)", file=f
)
404 print(" gdb_nm_file = GDB_NM_FILE;", file=f
)
405 print("#endif", file=f
)
406 print(" gdb_printf (file,", file=f
)
407 print(""" "gdbarch_dump: GDB_NM_FILE = %s\\n",""", file=f
)
408 print(" gdb_nm_file);", file=f
)
411 print(" gdb_printf (file,", file=f
)
413 f
""" "gdbarch_dump: gdbarch_{c.name}_p() = %d\\n",""",
416 print(f
" gdbarch_{c.name}_p (gdbarch));", file=f
)
417 if isinstance(c
, Function
):
418 print(" gdb_printf (file,", file=f
)
419 print(f
""" "gdbarch_dump: {c.name} = <%s>\\n",""", file=f
)
421 f
" host_address_to_string (gdbarch->{c.name}));",
427 elif c
.type == "CORE_ADDR":
428 printer
= f
"core_addr_to_string_nz (gdbarch->{c.name})"
430 printer
= f
"plongest (gdbarch->{c.name})"
431 print(" gdb_printf (file,", file=f
)
432 print(f
""" "gdbarch_dump: {c.name} = %s\\n",""", file=f
)
433 print(f
" {printer});", file=f
)
434 print(" if (gdbarch->dump_tdep != NULL)", file=f
)
435 print(" gdbarch->dump_tdep (gdbarch, file);", file=f
)
439 # Bodies of setter, accessor, and predicate functions.
444 print("bool", file=f
)
445 print(f
"gdbarch_{c.name}_p (struct gdbarch *gdbarch)", file=f
)
447 print(" gdb_assert (gdbarch != NULL);", file=f
)
448 print(f
" return {c.get_predicate()};", file=f
)
450 if isinstance(c
, Function
):
453 print(f
"{c.type}", file=f
)
454 print(f
"gdbarch_{c.name} ({c.set_list()})", file=f
)
456 print(" gdb_assert (gdbarch != NULL);", file=f
)
457 print(f
" gdb_assert (gdbarch->{c.name} != NULL);", file=f
)
458 if c
.predicate
and c
.predefault
:
459 # Allow a call to a function with a predicate.
461 f
" /* Do not check predicate: {c.get_predicate()}, allow call. */",
465 for rule
in c
.param_checks
:
466 print(f
" gdb_assert ({rule});", file=f
)
467 print(" if (gdbarch_debug >= 2)", file=f
)
469 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
472 print(" ", file=f
, end
="")
475 print("auto result = ", file=f
, end
="")
477 print("return ", file=f
, end
="")
478 print(f
"gdbarch->{c.name} ({c.actuals()});", file=f
)
479 if c
.type != "void" and c
.result_checks
:
480 for rule
in c
.result_checks
:
481 print(f
" gdb_assert ({rule});", file=f
)
482 print(" return result;", file=f
)
485 print("void", file=f
)
486 setter_name
= f
"set_gdbarch_{c.name}"
487 ftype_name
= f
"gdbarch_{c.name}_ftype"
488 print(f
"{setter_name} (struct gdbarch *gdbarch,", file=f
)
489 indent_columns
= len(f
"{setter_name} (")
490 print(f
"{indentation(indent_columns)}{ftype_name} {c.name})", file=f
)
492 print(f
" gdbarch->{c.name} = {c.name};", file=f
)
494 elif isinstance(c
, Value
):
496 print(f
"{c.type}", file=f
)
497 print(f
"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f
)
499 print(" gdb_assert (gdbarch != NULL);", file=f
)
500 if c
.invalid
is False:
501 print(f
" /* Skip verify of {c.name}, invalid_p == 0 */", file=f
)
502 elif isinstance(c
.invalid
, str):
503 print(" /* Check variable is valid. */", file=f
)
504 print(f
" gdb_assert (!({c.invalid}));", file=f
)
506 print(" /* Check variable changed from pre-default. */", file=f
)
507 print(f
" gdb_assert (gdbarch->{c.name} != {c.predefault});", file=f
)
508 print(" if (gdbarch_debug >= 2)", file=f
)
510 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
513 print(f
" return gdbarch->{c.name};", file=f
)
516 print("void", file=f
)
517 setter_name
= f
"set_gdbarch_{c.name}"
518 print(f
"{setter_name} (struct gdbarch *gdbarch,", file=f
)
519 indent_columns
= len(f
"{setter_name} (")
520 print(f
"{indentation(indent_columns)}{c.type} {c.name})", file=f
)
522 print(f
" gdbarch->{c.name} = {c.name};", file=f
)
525 assert isinstance(c
, Info
)
527 print(f
"{c.type}", file=f
)
528 print(f
"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f
)
530 print(" gdb_assert (gdbarch != NULL);", file=f
)
531 print(" if (gdbarch_debug >= 2)", file=f
)
533 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
536 print(f
" return gdbarch->{c.name};", file=f
)