3 # Architecture commands for GDB, the GNU debugger.
5 # Copyright (C) 1998-2024 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/>.
24 # gdbarch_components is imported only for its side-effect of filling
25 # `gdbarch_types.components`.
26 import gdbarch_components
# noqa: F401 # type: ignore
28 from gdbarch_types
import Component
, Function
, Info
, Value
, components
31 def indentation(n_columns
: int):
32 """Return string with tabs and spaces to indent line to N_COLUMNS."""
33 return "\t" * (n_columns
// 8) + " " * (n_columns
% 8)
36 copyright
= gdbcopyright
.copyright(
37 "gdbarch.py", "Dynamic architecture support for GDB, the GNU debugger."
41 def info(c
: Component
):
42 "Filter function to only allow Info components."
43 return type(c
) is Info
46 def not_info(c
: Component
):
47 "Filter function to omit Info components."
48 return type(c
) is not Info
51 with
open("gdbarch-gen.h", "w") as f
:
52 print(copyright
, file=f
)
56 print("/* The following are pre-initialized by GDBARCH. */", file=f
)
58 # Do Info components first.
59 for c
in filter(info
, components
):
62 f
"""extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);
63 /* set_gdbarch_{c.name}() - not applicable - pre-initialized. */""",
69 print("/* The following are initialized by the target dependent code. */", file=f
)
71 # Generate decls for accessors, setters, and predicates for all
72 # non-Info components.
73 for c
in filter(not_info
, components
):
76 comment
= c
.comment
.split("\n")
80 comment
= comment
[:-1]
81 print("/* ", file=f
, end
="")
82 print(comment
[0], file=f
, end
="")
86 textwrap
.indent("\n".join(comment
[1:]), prefix
=" "),
94 print(f
"extern bool gdbarch_{c.name}_p (struct gdbarch *gdbarch);", file=f
)
97 if isinstance(c
, Value
):
99 f
"extern {c.type} gdbarch_{c.name} (struct gdbarch *gdbarch);",
103 f
"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.type} {c.name});",
107 assert isinstance(c
, Function
)
109 f
"typedef {c.type} ({c.ftype()}) ({c.param_list()});",
114 f
"extern {c.type} gdbarch_{c.name} ({c.set_list()});",
118 f
"extern void set_gdbarch_{c.name} (struct gdbarch *gdbarch, {c.ftype()} *{c.name});",
122 with
open("gdbarch-gen.c", "w") as f
:
123 print(copyright
, file=f
)
126 print("/* Maintain the struct gdbarch object. */", file=f
)
129 # The struct definition body.
131 print("struct gdbarch", file=f
)
133 print(" /* Has this architecture been fully initialized? */", file=f
)
134 print(" bool initialized_p = false;", file=f
)
136 print(" /* An obstack bound to the lifetime of the architecture. */", file=f
)
137 print(" auto_obstack obstack;", file=f
)
138 print(" /* Registry. */", file=f
)
139 print(" registry<gdbarch> registry_fields;", file=f
)
141 print(" /* basic architectural information. */", file=f
)
142 for c
in filter(info
, components
):
143 print(f
" {c.type} {c.name};", file=f
)
145 print(" /* target specific vector. */", file=f
)
146 print(" gdbarch_tdep_up tdep;", file=f
)
147 print(" gdbarch_dump_tdep_ftype *dump_tdep = nullptr;", file=f
)
149 for c
in filter(not_info
, components
):
150 if isinstance(c
, Function
):
151 print(f
" gdbarch_{c.name}_ftype *", file=f
, end
="")
153 print(f
" {c.type} ", file=f
, end
="")
154 print(f
"{c.name} = ", file=f
, end
="")
155 if c
.predefault
is not None:
156 print(f
"{c.predefault};", file=f
)
157 elif isinstance(c
, Value
):
160 assert isinstance(c
, Function
)
161 print("nullptr;", file=f
)
167 print("/* Create a new ``struct gdbarch'' based on information provided by", file=f
)
168 print(" ``struct gdbarch_info''. */", file=f
)
170 print("struct gdbarch *", file=f
)
171 print("gdbarch_alloc (const struct gdbarch_info *info,", file=f
)
172 print(" gdbarch_tdep_up tdep)", file=f
)
174 print(" struct gdbarch *gdbarch;", file=f
)
176 print(" gdbarch = new struct gdbarch;", file=f
)
178 print(" gdbarch->tdep = std::move (tdep);", file=f
)
180 for c
in filter(info
, components
):
181 print(f
" gdbarch->{c.name} = info->{c.name};", file=f
)
183 print(" return gdbarch;", file=f
)
189 # Post-initialization validation and updating
191 print("/* Ensure that all values in a GDBARCH are reasonable. */", file=f
)
193 print("static void", file=f
)
194 print("verify_gdbarch (struct gdbarch *gdbarch)", file=f
)
196 print(" string_file log;", file=f
)
198 print(" /* fundamental */", file=f
)
199 print(" if (gdbarch->byte_order == BFD_ENDIAN_UNKNOWN)", file=f
)
200 print(""" log.puts ("\\n\\tbyte-order");""", file=f
)
201 print(" if (gdbarch->bfd_arch_info == NULL)", file=f
)
202 print(""" log.puts ("\\n\\tbfd_arch_info");""", file=f
)
204 " /* Check those that need to be defined for the given multi-arch level. */",
207 for c
in filter(not_info
, components
):
208 # An opportunity to write in the 'postdefault' value. We
209 # change field's value to the postdefault if its current value
210 # is not different to the initial value of the field.
211 if c
.postdefault
is not None:
212 init_value
= c
.predefault
or "0"
213 print(f
" if (gdbarch->{c.name} == {init_value})", file=f
)
214 print(f
" gdbarch->{c.name} = {c.postdefault};", file=f
)
216 # Now validate the value.
217 if isinstance(c
.invalid
, str):
218 print(f
" if ({c.invalid})", file=f
)
219 print(f
""" log.puts ("\\n\\t{c.name}");""", file=f
)
221 print(f
" /* Skip verify of {c.name}, has predicate. */", file=f
)
223 if c
.postdefault
is not None:
224 # This component has its 'invalid' field set to True, but
225 # also has a postdefault. This makes no sense, the
226 # postdefault will have been applied above, so this field
227 # will not have a zero value.
229 f
"component {c.name} has postdefault and invalid set to True"
232 init_value
= c
.predefault
or "0"
233 print(f
" if (gdbarch->{c.name} == {init_value})", file=f
)
234 print(f
""" log.puts ("\\n\\t{c.name}");""", file=f
)
236 print(f
" /* Skip verify of {c.name}, invalid_p == 0. */", file=f
)
237 print(" if (!log.empty ())", file=f
)
239 """ internal_error (_("verify_gdbarch: the following are invalid ...%s"),""",
242 print(" log.c_str ());", file=f
)
249 print("/* Print out the details of the current architecture. */", file=f
)
251 print("void", file=f
)
252 print("gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)", file=f
)
254 print(""" const char *gdb_nm_file = "<not-defined>";""", file=f
)
256 print("#if defined (GDB_NM_FILE)", file=f
)
257 print(" gdb_nm_file = GDB_NM_FILE;", file=f
)
258 print("#endif", file=f
)
259 print(" gdb_printf (file,", file=f
)
260 print(""" "gdbarch_dump: GDB_NM_FILE = %s\\n",""", file=f
)
261 print(" gdb_nm_file);", file=f
)
264 print(" gdb_printf (file,", file=f
)
266 f
""" "gdbarch_dump: gdbarch_{c.name}_p() = %d\\n",""",
269 print(f
" gdbarch_{c.name}_p (gdbarch));", file=f
)
270 if isinstance(c
, Function
):
271 print(" gdb_printf (file,", file=f
)
272 print(f
""" "gdbarch_dump: {c.name} = <%s>\\n",""", file=f
)
274 f
" host_address_to_string (gdbarch->{c.name}));",
280 elif c
.type == "CORE_ADDR":
281 printer
= f
"core_addr_to_string_nz (gdbarch->{c.name})"
283 printer
= f
"plongest (gdbarch->{c.name})"
284 print(" gdb_printf (file,", file=f
)
285 print(f
""" "gdbarch_dump: {c.name} = %s\\n",""", file=f
)
286 print(f
" {printer});", file=f
)
287 print(" if (gdbarch->dump_tdep != NULL)", file=f
)
288 print(" gdbarch->dump_tdep (gdbarch, file);", file=f
)
292 # Bodies of setter, accessor, and predicate functions.
297 print("bool", file=f
)
298 print(f
"gdbarch_{c.name}_p (struct gdbarch *gdbarch)", file=f
)
300 print(" gdb_assert (gdbarch != NULL);", file=f
)
301 print(f
" return {c.get_predicate()};", file=f
)
303 if isinstance(c
, Function
):
306 print(f
"{c.type}", file=f
)
307 print(f
"gdbarch_{c.name} ({c.set_list()})", file=f
)
309 print(" gdb_assert (gdbarch != NULL);", file=f
)
310 print(f
" gdb_assert (gdbarch->{c.name} != NULL);", file=f
)
311 if c
.predicate
and c
.predefault
:
312 # Allow a call to a function with a predicate.
314 f
" /* Do not check predicate: {c.get_predicate()}, allow call. */",
318 for rule
in c
.param_checks
:
319 print(f
" gdb_assert ({rule});", file=f
)
320 print(" if (gdbarch_debug >= 2)", file=f
)
322 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
325 print(" ", file=f
, end
="")
328 print("auto result = ", file=f
, end
="")
330 print("return ", file=f
, end
="")
331 print(f
"gdbarch->{c.name} ({c.actuals()});", file=f
)
332 if c
.type != "void" and c
.result_checks
:
333 for rule
in c
.result_checks
:
334 print(f
" gdb_assert ({rule});", file=f
)
335 print(" return result;", file=f
)
338 print("void", file=f
)
339 setter_name
= f
"set_gdbarch_{c.name}"
340 ftype_name
= f
"gdbarch_{c.name}_ftype"
341 print(f
"{setter_name} (struct gdbarch *gdbarch,", file=f
)
342 indent_columns
= len(f
"{setter_name} (")
343 print(f
"{indentation(indent_columns)}{ftype_name} {c.name})", file=f
)
345 print(f
" gdbarch->{c.name} = {c.name};", file=f
)
347 elif isinstance(c
, Value
):
349 print(f
"{c.type}", file=f
)
350 print(f
"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f
)
352 print(" gdb_assert (gdbarch != NULL);", file=f
)
353 if isinstance(c
.invalid
, str):
354 print(" /* Check variable is valid. */", file=f
)
355 print(f
" gdb_assert (!({c.invalid}));", file=f
)
357 print(" /* Check predicate was used. */", file=f
)
358 print(f
" gdb_assert (gdbarch_{c.name}_p (gdbarch));", file=f
)
359 elif c
.invalid
or c
.postdefault
is not None:
360 init_value
= c
.predefault
or "0"
361 print(" /* Check variable changed from its initial value. */", file=f
)
362 print(f
" gdb_assert (gdbarch->{c.name} != {init_value});", file=f
)
364 print(f
" /* Skip verify of {c.name}, invalid_p == 0. */", file=f
)
365 print(" if (gdbarch_debug >= 2)", file=f
)
367 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
370 print(f
" return gdbarch->{c.name};", file=f
)
373 print("void", file=f
)
374 setter_name
= f
"set_gdbarch_{c.name}"
375 print(f
"{setter_name} (struct gdbarch *gdbarch,", file=f
)
376 indent_columns
= len(f
"{setter_name} (")
377 print(f
"{indentation(indent_columns)}{c.type} {c.name})", file=f
)
379 print(f
" gdbarch->{c.name} = {c.name};", file=f
)
382 assert isinstance(c
, Info
)
384 print(f
"{c.type}", file=f
)
385 print(f
"gdbarch_{c.name} (struct gdbarch *gdbarch)", file=f
)
387 print(" gdb_assert (gdbarch != NULL);", file=f
)
388 print(" if (gdbarch_debug >= 2)", file=f
)
390 f
""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
393 print(f
" return gdbarch->{c.name};", file=f
)