gdbarch.py 18 KB

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