]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbarch.py
3 # Architecture commands for GDB, the GNU debugger.
5 # Copyright (C) 1998-2022 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.
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("&"):
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
)
44 "Base class for all components."
46 def __init__(self
, **kwargs
):
48 setattr(self
, key
, kwargs
[key
])
49 components
.append(self
)
51 def get_predicate(self
):
52 "Return the expression used for validity checking."
53 assert self
.predicate
and not isinstance(self
.invalid
, str)
55 predicate
= f
"gdbarch->{self.name} != {self.predefault}"
56 elif isinstance(c
, Value
):
57 predicate
= f
"gdbarch->{self.name} != 0"
59 predicate
= f
"gdbarch->{self.name} != NULL"
63 class Info(_Component
):
64 "An Info component is copied from the gdbarch_info."
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.
72 class Value(_Component
):
73 "A Value component is just a data member."
92 predefault
=predefault
,
93 postdefault
=postdefault
,
99 class Function(_Component
):
100 "A Function component is a function pointer member."
120 predefault
=predefault
,
121 postdefault
=postdefault
,
128 "Return the name of the function typedef to use."
129 return f
"gdbarch_{self.name}_ftype"
131 def param_list(self
):
132 "Return the formal parameter list as a string."
133 return join_params(self
.params
)
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
))
143 "Return the actual parameters to forward, as a string."
144 return ", ".join([p
[1] for p
in self
.params
])
147 class Method(Function
):
148 "A Method is like a Function but passes the gdbarch through."
150 def param_list(self
):
152 return self
.set_list()
156 result
= ["gdbarch"] + [p
[1] for p
in self
.params
]
157 return ", ".join(result
)
160 # Read the components.
161 with
open("gdbarch-components.py") as fd
:
164 copyright
= gdbcopyright
.copyright(
165 "gdbarch.py", "Dynamic architecture support for GDB, the GNU debugger."
170 "Filter function to only allow Info components."
171 return type(c
) is Info
175 "Filter function to omit Info components."
176 return type(c
) is not Info
179 with
open("gdbarch-gen.h", "w") as f
:
180 print(copyright
, file=f
)
183 print("/* The following are pre-initialized by GDBARCH. */", file=f
)
185 # Do Info components first.
186 for c
in filter(info
, components
):
189 f
"""extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);
190 /* set_gdbarch_{c.name}() - not applicable - pre-initialized. */""",
196 print("/* The following are initialized by the target dependent code. */", file=f
)
198 # Generate decls for accessors, setters, and predicates for all
199 # non-Info components.
200 for c
in filter(not_info
, components
):
203 comment
= c
.comment
.split("\n")
205 comment
= comment
[1:]
206 if comment
[-1] == "":
207 comment
= comment
[:-1]
208 print("/* ", file=f
, end
="")
209 print(comment
[0], file=f
, end
="")
213 textwrap
.indent("\n".join(comment
[1:]), prefix
=" "),
221 print(f
"extern bool gdbarch_{c.name}_p (struct gdbarch *gdbarch);", file=f
)
224 if isinstance(c
, Value
):
226 f
"extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);",
230 f
"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.type} {c.name});",
234 assert isinstance(c
, Function
)
236 f
"typedef {c.type} ({c.ftype()}) ({c.param_list()});",
240 f
"extern {c.type} gdbarch_{c.name} ({c.set_list()});",
244 f
"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.ftype()} *{c.name});",
248 with
open("gdbarch.c", "w") as f
:
249 print(copyright
, file=f
)
251 print("/* Maintain the struct gdbarch object. */", file=f
)
254 # The struct definition body.
256 print("struct gdbarch", file=f
)
258 print(" /* Has this architecture been fully initialized? */", file=f
)
259 print(" int initialized_p;", file=f
)
261 print(" /* An obstack bound to the lifetime of the architecture. */", file=f
)
262 print(" struct obstack *obstack;", 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
)
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
)
272 print(" /* per-architecture data-pointers. */", file=f
)
273 print(" unsigned nr_data;", file=f
)
274 print(" void **data;", file=f
)
276 for c
in filter(not_info
, components
):
277 if isinstance(c
, Value
):
278 print(f
" {c.type} {c.name};", file=f
)
280 assert isinstance(c
, Function
)
281 print(f
" gdbarch_{c.name}_ftype *{c.name};", file=f
)
287 print("/* Create a new ``struct gdbarch'' based on information provided by", file=f
)
288 print(" ``struct gdbarch_info''. */", 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
)
294 print(" struct gdbarch *gdbarch;", file=f
)
297 " /* Create an obstack for allocating all the per-architecture memory,", file=f
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
)
306 print(" alloc_gdbarch_data (gdbarch);", file=f
)
308 print(" gdbarch->tdep = tdep;", file=f
)
310 for c
in filter(info
, components
):
311 print(f
" gdbarch->{c.name} = info->{c.name};", 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
)
319 print(" return gdbarch;", file=f
)
325 # Post-initialization validation and updating
327 print("/* Ensure that all values in a GDBARCH are reasonable. */", file=f
)
329 print("static void", file=f
)
330 print("verify_gdbarch (struct gdbarch *gdbarch)", file=f
)
332 print(" string_file log;", 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
)
340 " /* Check those that need to be defined for the given multi-arch level. */",
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
)
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
)
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
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
)
383 print("/* Print out the details of the current architecture. */", file=f
)
385 print("void", file=f
)
386 print("gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)", file=f
)
388 print(""" const char *gdb_nm_file = "<not-defined>";""", 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
)
398 print(" fprintf_filtered (file,", file=f
)
400 f
""" "gdbarch_dump: gdbarch_{c.name}_p() = %d\\n",""",
403 print(f
" gdbarch_{c.name}_p (gdbarch));", file=f
)
404 if isinstance(c
, Function
):
405 print(" fprintf_filtered (file,", file=f
)
407 f
""" "gdbarch_dump: {c.name} = <%s>\\n",""", file=f
410 f
" host_address_to_string (gdbarch->{c.name}));",
416 elif c
.type == "CORE_ADDR":
417 printer
= f
"core_addr_to_string_nz (gdbarch->{c.name})"
419 printer
= f
"plongest (gdbarch->{c.name})"
420 print(" fprintf_filtered (file,", file=f
)
422 f
""" "gdbarch_dump: {c.name} = %s\\n",""", file=f
424 print(f
" {printer});", file=f
)
425 print(" if (gdbarch->dump_tdep != NULL)", file=f
)
426 print(" gdbarch->dump_tdep (gdbarch, file);", file=f
)
430 # Bodies of setter, accessor, and predicate functions.
435 print("bool", file=f
)
436 print(f
"gdbarch_{c.name}_p (struct gdbarch *gdbarch)", file=f
)
438 print(" gdb_assert (gdbarch != NULL);", file=f
)
439 print(f
" return {c.get_predicate()};", file=f
)
441 if isinstance(c
, Function
):
443 print(f
"{c.type}", file=f
)
444 print(f
"gdbarch_{c.name} ({c.set_list()})", 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.
451 f
" /* Do not check predicate: {c.get_predicate()}, allow call. */",
454 print(" if (gdbarch_debug >= 2)", file=f
)
456 f
""" fprintf_unfiltered (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
459 print(" ", file=f
, end
="")
461 print("return ", file=f
, end
="")
462 print(f
"gdbarch->{c.name} ({c.actuals()});", file=f
)
465 print("void", file=f
)
466 print(f
"set_gdbarch_{c.name} (struct gdbarch *gdbarch,", file=f
)
468 f
" {' ' * len(c.name)} gdbarch_{c.name}_ftype {c.name})",
472 print(f
" gdbarch->{c.name} = {c.name};", file=f
)
474 elif isinstance(c
, Value
):
476 print(f
"{c.type}", file=f
)
477 print(f
"gdbarch_{c.name} (struct gdbarch *gdbarch)", 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
)
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
)
490 f
""" fprintf_unfiltered (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
493 print(f
" return gdbarch->{c.name};", 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
)
500 print(f
" gdbarch->{c.name} = {c.name};", file=f
)
503 assert isinstance(c
, Info
)
505 print(f
"{c.type}", file=f
)
506 print(f
"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f
)
508 print(" gdb_assert (gdbarch != NULL);", file=f
)
509 print(" if (gdbarch_debug >= 2)", file=f
)
511 f
""" fprintf_unfiltered (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
514 print(f
" return gdbarch->{c.name};", file=f
)