scm-block.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /* Scheme interface to blocks.
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* See README file in this directory for implementation notes, coding
  15. conventions, et.al. */
  16. #include "defs.h"
  17. #include "block.h"
  18. #include "dictionary.h"
  19. #include "objfiles.h"
  20. #include "source.h"
  21. #include "symtab.h"
  22. #include "guile-internal.h"
  23. /* A smob describing a gdb block. */
  24. struct block_smob
  25. {
  26. /* This always appears first.
  27. We want blocks to be eq?-able. And we need to be able to invalidate
  28. blocks when the associated objfile is deleted. */
  29. eqable_gdb_smob base;
  30. /* The GDB block structure that represents a frame's code block. */
  31. const struct block *block;
  32. /* The backing object file. There is no direct relationship in GDB
  33. between a block and an object file. When a block is created also
  34. store a pointer to the object file for later use. */
  35. struct objfile *objfile;
  36. };
  37. /* To iterate over block symbols from Scheme we need to store
  38. struct block_iterator somewhere. This is stored in the "progress" field
  39. of <gdb:iterator>. We store the block object in iterator_smob.object,
  40. so we don't store it here.
  41. Remember: While iterating over block symbols, you must continually check
  42. whether the block is still valid. */
  43. struct block_syms_progress_smob
  44. {
  45. /* This always appears first. */
  46. gdb_smob base;
  47. /* The iterator for that block. */
  48. struct block_iterator iter;
  49. /* Has the iterator been initialized flag. */
  50. int initialized_p;
  51. };
  52. static const char block_smob_name[] = "gdb:block";
  53. static const char block_syms_progress_smob_name[] = "gdb:block-symbols-iterator";
  54. /* The tag Guile knows the block smobs by. */
  55. static scm_t_bits block_smob_tag;
  56. static scm_t_bits block_syms_progress_smob_tag;
  57. /* The "next!" block syms iterator method. */
  58. static SCM bkscm_next_symbol_x_proc;
  59. static const struct objfile_data *bkscm_objfile_data_key;
  60. /* Administrivia for block smobs. */
  61. /* Helper function to hash a block_smob. */
  62. static hashval_t
  63. bkscm_hash_block_smob (const void *p)
  64. {
  65. const block_smob *b_smob = (const block_smob *) p;
  66. return htab_hash_pointer (b_smob->block);
  67. }
  68. /* Helper function to compute equality of block_smobs. */
  69. static int
  70. bkscm_eq_block_smob (const void *ap, const void *bp)
  71. {
  72. const block_smob *a = (const block_smob *) ap;
  73. const block_smob *b = (const block_smob *) bp;
  74. return (a->block == b->block
  75. && a->block != NULL);
  76. }
  77. /* Return the struct block pointer -> SCM mapping table.
  78. It is created if necessary. */
  79. static htab_t
  80. bkscm_objfile_block_map (struct objfile *objfile)
  81. {
  82. htab_t htab = (htab_t) objfile_data (objfile, bkscm_objfile_data_key);
  83. if (htab == NULL)
  84. {
  85. htab = gdbscm_create_eqable_gsmob_ptr_map (bkscm_hash_block_smob,
  86. bkscm_eq_block_smob);
  87. set_objfile_data (objfile, bkscm_objfile_data_key, htab);
  88. }
  89. return htab;
  90. }
  91. /* The smob "free" function for <gdb:block>. */
  92. static size_t
  93. bkscm_free_block_smob (SCM self)
  94. {
  95. block_smob *b_smob = (block_smob *) SCM_SMOB_DATA (self);
  96. if (b_smob->block != NULL)
  97. {
  98. htab_t htab = bkscm_objfile_block_map (b_smob->objfile);
  99. gdbscm_clear_eqable_gsmob_ptr_slot (htab, &b_smob->base);
  100. }
  101. /* Not necessary, done to catch bugs. */
  102. b_smob->block = NULL;
  103. b_smob->objfile = NULL;
  104. return 0;
  105. }
  106. /* The smob "print" function for <gdb:block>. */
  107. static int
  108. bkscm_print_block_smob (SCM self, SCM port, scm_print_state *pstate)
  109. {
  110. block_smob *b_smob = (block_smob *) SCM_SMOB_DATA (self);
  111. const struct block *b = b_smob->block;
  112. gdbscm_printf (port, "#<%s", block_smob_name);
  113. if (BLOCK_SUPERBLOCK (b) == NULL)
  114. gdbscm_printf (port, " global");
  115. else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (b)) == NULL)
  116. gdbscm_printf (port, " static");
  117. if (BLOCK_FUNCTION (b) != NULL)
  118. gdbscm_printf (port, " %s", BLOCK_FUNCTION (b)->print_name ());
  119. gdbscm_printf (port, " %s-%s",
  120. hex_string (BLOCK_START (b)), hex_string (BLOCK_END (b)));
  121. scm_puts (">", port);
  122. scm_remember_upto_here_1 (self);
  123. /* Non-zero means success. */
  124. return 1;
  125. }
  126. /* Low level routine to create a <gdb:block> object. */
  127. static SCM
  128. bkscm_make_block_smob (void)
  129. {
  130. block_smob *b_smob = (block_smob *)
  131. scm_gc_malloc (sizeof (block_smob), block_smob_name);
  132. SCM b_scm;
  133. b_smob->block = NULL;
  134. b_smob->objfile = NULL;
  135. b_scm = scm_new_smob (block_smob_tag, (scm_t_bits) b_smob);
  136. gdbscm_init_eqable_gsmob (&b_smob->base, b_scm);
  137. return b_scm;
  138. }
  139. /* Returns non-zero if SCM is a <gdb:block> object. */
  140. static int
  141. bkscm_is_block (SCM scm)
  142. {
  143. return SCM_SMOB_PREDICATE (block_smob_tag, scm);
  144. }
  145. /* (block? scm) -> boolean */
  146. static SCM
  147. gdbscm_block_p (SCM scm)
  148. {
  149. return scm_from_bool (bkscm_is_block (scm));
  150. }
  151. /* Return the existing object that encapsulates BLOCK, or create a new
  152. <gdb:block> object. */
  153. SCM
  154. bkscm_scm_from_block (const struct block *block, struct objfile *objfile)
  155. {
  156. htab_t htab;
  157. eqable_gdb_smob **slot;
  158. block_smob *b_smob, b_smob_for_lookup;
  159. SCM b_scm;
  160. /* If we've already created a gsmob for this block, return it.
  161. This makes blocks eq?-able. */
  162. htab = bkscm_objfile_block_map (objfile);
  163. b_smob_for_lookup.block = block;
  164. slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &b_smob_for_lookup.base);
  165. if (*slot != NULL)
  166. return (*slot)->containing_scm;
  167. b_scm = bkscm_make_block_smob ();
  168. b_smob = (block_smob *) SCM_SMOB_DATA (b_scm);
  169. b_smob->block = block;
  170. b_smob->objfile = objfile;
  171. gdbscm_fill_eqable_gsmob_ptr_slot (slot, &b_smob->base);
  172. return b_scm;
  173. }
  174. /* Returns the <gdb:block> object in SELF.
  175. Throws an exception if SELF is not a <gdb:block> object. */
  176. static SCM
  177. bkscm_get_block_arg_unsafe (SCM self, int arg_pos, const char *func_name)
  178. {
  179. SCM_ASSERT_TYPE (bkscm_is_block (self), self, arg_pos, func_name,
  180. block_smob_name);
  181. return self;
  182. }
  183. /* Returns a pointer to the block smob of SELF.
  184. Throws an exception if SELF is not a <gdb:block> object. */
  185. static block_smob *
  186. bkscm_get_block_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
  187. {
  188. SCM b_scm = bkscm_get_block_arg_unsafe (self, arg_pos, func_name);
  189. block_smob *b_smob = (block_smob *) SCM_SMOB_DATA (b_scm);
  190. return b_smob;
  191. }
  192. /* Returns non-zero if block B_SMOB is valid. */
  193. static int
  194. bkscm_is_valid (block_smob *b_smob)
  195. {
  196. return b_smob->block != NULL;
  197. }
  198. /* Returns the block smob in SELF, verifying it's valid.
  199. Throws an exception if SELF is not a <gdb:block> object or is invalid. */
  200. static block_smob *
  201. bkscm_get_valid_block_smob_arg_unsafe (SCM self, int arg_pos,
  202. const char *func_name)
  203. {
  204. block_smob *b_smob
  205. = bkscm_get_block_smob_arg_unsafe (self, arg_pos, func_name);
  206. if (!bkscm_is_valid (b_smob))
  207. {
  208. gdbscm_invalid_object_error (func_name, arg_pos, self,
  209. _("<gdb:block>"));
  210. }
  211. return b_smob;
  212. }
  213. /* Returns the block smob contained in SCM or NULL if SCM is not a
  214. <gdb:block> object.
  215. If there is an error a <gdb:exception> object is stored in *EXCP. */
  216. static block_smob *
  217. bkscm_get_valid_block (SCM scm, int arg_pos, const char *func_name, SCM *excp)
  218. {
  219. block_smob *b_smob;
  220. if (!bkscm_is_block (scm))
  221. {
  222. *excp = gdbscm_make_type_error (func_name, arg_pos, scm,
  223. block_smob_name);
  224. return NULL;
  225. }
  226. b_smob = (block_smob *) SCM_SMOB_DATA (scm);
  227. if (!bkscm_is_valid (b_smob))
  228. {
  229. *excp = gdbscm_make_invalid_object_error (func_name, arg_pos, scm,
  230. _("<gdb:block>"));
  231. return NULL;
  232. }
  233. return b_smob;
  234. }
  235. /* Returns the struct block that is wrapped by BLOCK_SCM.
  236. If BLOCK_SCM is not a block, or is an invalid block, then NULL is returned
  237. and a <gdb:exception> object is stored in *EXCP. */
  238. const struct block *
  239. bkscm_scm_to_block (SCM block_scm, int arg_pos, const char *func_name,
  240. SCM *excp)
  241. {
  242. block_smob *b_smob;
  243. b_smob = bkscm_get_valid_block (block_scm, arg_pos, func_name, excp);
  244. if (b_smob != NULL)
  245. return b_smob->block;
  246. return NULL;
  247. }
  248. /* Helper function for bkscm_del_objfile_blocks to mark the block
  249. as invalid. */
  250. static int
  251. bkscm_mark_block_invalid (void **slot, void *info)
  252. {
  253. block_smob *b_smob = (block_smob *) *slot;
  254. b_smob->block = NULL;
  255. b_smob->objfile = NULL;
  256. return 1;
  257. }
  258. /* This function is called when an objfile is about to be freed.
  259. Invalidate the block as further actions on the block would result
  260. in bad data. All access to b_smob->block should be gated by
  261. checks to ensure the block is (still) valid. */
  262. static void
  263. bkscm_del_objfile_blocks (struct objfile *objfile, void *datum)
  264. {
  265. htab_t htab = (htab_t) datum;
  266. if (htab != NULL)
  267. {
  268. htab_traverse_noresize (htab, bkscm_mark_block_invalid, NULL);
  269. htab_delete (htab);
  270. }
  271. }
  272. /* Block methods. */
  273. /* (block-valid? <gdb:block>) -> boolean
  274. Returns #t if SELF still exists in GDB. */
  275. static SCM
  276. gdbscm_block_valid_p (SCM self)
  277. {
  278. block_smob *b_smob
  279. = bkscm_get_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  280. return scm_from_bool (bkscm_is_valid (b_smob));
  281. }
  282. /* (block-start <gdb:block>) -> address */
  283. static SCM
  284. gdbscm_block_start (SCM self)
  285. {
  286. block_smob *b_smob
  287. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  288. const struct block *block = b_smob->block;
  289. return gdbscm_scm_from_ulongest (BLOCK_START (block));
  290. }
  291. /* (block-end <gdb:block>) -> address */
  292. static SCM
  293. gdbscm_block_end (SCM self)
  294. {
  295. block_smob *b_smob
  296. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  297. const struct block *block = b_smob->block;
  298. return gdbscm_scm_from_ulongest (BLOCK_END (block));
  299. }
  300. /* (block-function <gdb:block>) -> <gdb:symbol> */
  301. static SCM
  302. gdbscm_block_function (SCM self)
  303. {
  304. block_smob *b_smob
  305. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  306. const struct block *block = b_smob->block;
  307. struct symbol *sym;
  308. sym = BLOCK_FUNCTION (block);
  309. if (sym != NULL)
  310. return syscm_scm_from_symbol (sym);
  311. return SCM_BOOL_F;
  312. }
  313. /* (block-superblock <gdb:block>) -> <gdb:block> */
  314. static SCM
  315. gdbscm_block_superblock (SCM self)
  316. {
  317. block_smob *b_smob
  318. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  319. const struct block *block = b_smob->block;
  320. const struct block *super_block;
  321. super_block = BLOCK_SUPERBLOCK (block);
  322. if (super_block)
  323. return bkscm_scm_from_block (super_block, b_smob->objfile);
  324. return SCM_BOOL_F;
  325. }
  326. /* (block-global-block <gdb:block>) -> <gdb:block>
  327. Returns the global block associated to this block. */
  328. static SCM
  329. gdbscm_block_global_block (SCM self)
  330. {
  331. block_smob *b_smob
  332. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  333. const struct block *block = b_smob->block;
  334. const struct block *global_block;
  335. global_block = block_global_block (block);
  336. return bkscm_scm_from_block (global_block, b_smob->objfile);
  337. }
  338. /* (block-static-block <gdb:block>) -> <gdb:block>
  339. Returns the static block associated to this block.
  340. Returns #f if we cannot get the static block (this is the global block). */
  341. static SCM
  342. gdbscm_block_static_block (SCM self)
  343. {
  344. block_smob *b_smob
  345. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  346. const struct block *block = b_smob->block;
  347. const struct block *static_block;
  348. if (BLOCK_SUPERBLOCK (block) == NULL)
  349. return SCM_BOOL_F;
  350. static_block = block_static_block (block);
  351. return bkscm_scm_from_block (static_block, b_smob->objfile);
  352. }
  353. /* (block-global? <gdb:block>) -> boolean
  354. Returns #t if this block object is a global block. */
  355. static SCM
  356. gdbscm_block_global_p (SCM self)
  357. {
  358. block_smob *b_smob
  359. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  360. const struct block *block = b_smob->block;
  361. return scm_from_bool (BLOCK_SUPERBLOCK (block) == NULL);
  362. }
  363. /* (block-static? <gdb:block>) -> boolean
  364. Returns #t if this block object is a static block. */
  365. static SCM
  366. gdbscm_block_static_p (SCM self)
  367. {
  368. block_smob *b_smob
  369. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  370. const struct block *block = b_smob->block;
  371. if (BLOCK_SUPERBLOCK (block) != NULL
  372. && BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
  373. return SCM_BOOL_T;
  374. return SCM_BOOL_F;
  375. }
  376. /* (block-symbols <gdb:block>) -> list of <gdb:symbol objects
  377. Returns a list of symbols of the block. */
  378. static SCM
  379. gdbscm_block_symbols (SCM self)
  380. {
  381. block_smob *b_smob
  382. = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  383. const struct block *block = b_smob->block;
  384. struct block_iterator iter;
  385. struct symbol *sym;
  386. SCM result;
  387. result = SCM_EOL;
  388. sym = block_iterator_first (block, &iter);
  389. while (sym != NULL)
  390. {
  391. SCM s_scm = syscm_scm_from_symbol (sym);
  392. result = scm_cons (s_scm, result);
  393. sym = block_iterator_next (&iter);
  394. }
  395. return scm_reverse_x (result, SCM_EOL);
  396. }
  397. /* The <gdb:block-symbols-iterator> object,
  398. for iterating over all symbols in a block. */
  399. /* The smob "print" function for <gdb:block-symbols-iterator>. */
  400. static int
  401. bkscm_print_block_syms_progress_smob (SCM self, SCM port,
  402. scm_print_state *pstate)
  403. {
  404. block_syms_progress_smob *i_smob
  405. = (block_syms_progress_smob *) SCM_SMOB_DATA (self);
  406. gdbscm_printf (port, "#<%s", block_syms_progress_smob_name);
  407. if (i_smob->initialized_p)
  408. {
  409. switch (i_smob->iter.which)
  410. {
  411. case GLOBAL_BLOCK:
  412. case STATIC_BLOCK:
  413. {
  414. struct compunit_symtab *cust;
  415. gdbscm_printf (port, " %s",
  416. i_smob->iter.which == GLOBAL_BLOCK
  417. ? "global" : "static");
  418. if (i_smob->iter.idx != -1)
  419. gdbscm_printf (port, " @%d", i_smob->iter.idx);
  420. cust = (i_smob->iter.idx == -1
  421. ? i_smob->iter.d.compunit_symtab
  422. : i_smob->iter.d.compunit_symtab->includes[i_smob->iter.idx]);
  423. gdbscm_printf (port, " %s",
  424. symtab_to_filename_for_display
  425. (cust->primary_filetab ()));
  426. break;
  427. }
  428. case FIRST_LOCAL_BLOCK:
  429. gdbscm_printf (port, " single block");
  430. break;
  431. }
  432. }
  433. else
  434. gdbscm_printf (port, " !initialized");
  435. scm_puts (">", port);
  436. scm_remember_upto_here_1 (self);
  437. /* Non-zero means success. */
  438. return 1;
  439. }
  440. /* Low level routine to create a <gdb:block-symbols-progress> object. */
  441. static SCM
  442. bkscm_make_block_syms_progress_smob (void)
  443. {
  444. block_syms_progress_smob *i_smob = (block_syms_progress_smob *)
  445. scm_gc_malloc (sizeof (block_syms_progress_smob),
  446. block_syms_progress_smob_name);
  447. SCM smob;
  448. memset (&i_smob->iter, 0, sizeof (i_smob->iter));
  449. i_smob->initialized_p = 0;
  450. smob = scm_new_smob (block_syms_progress_smob_tag, (scm_t_bits) i_smob);
  451. gdbscm_init_gsmob (&i_smob->base);
  452. return smob;
  453. }
  454. /* Returns non-zero if SCM is a <gdb:block-symbols-progress> object. */
  455. static int
  456. bkscm_is_block_syms_progress (SCM scm)
  457. {
  458. return SCM_SMOB_PREDICATE (block_syms_progress_smob_tag, scm);
  459. }
  460. /* (block-symbols-progress? scm) -> boolean */
  461. static SCM
  462. bkscm_block_syms_progress_p (SCM scm)
  463. {
  464. return scm_from_bool (bkscm_is_block_syms_progress (scm));
  465. }
  466. /* (make-block-symbols-iterator <gdb:block>) -> <gdb:iterator>
  467. Return a <gdb:iterator> object for iterating over the symbols of SELF. */
  468. static SCM
  469. gdbscm_make_block_syms_iter (SCM self)
  470. {
  471. /* Call for side effects. */
  472. bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  473. SCM progress, iter;
  474. progress = bkscm_make_block_syms_progress_smob ();
  475. iter = gdbscm_make_iterator (self, progress, bkscm_next_symbol_x_proc);
  476. return iter;
  477. }
  478. /* Returns the next symbol in the iteration through the block's dictionary,
  479. or (end-of-iteration).
  480. This is the iterator_smob.next_x method. */
  481. static SCM
  482. gdbscm_block_next_symbol_x (SCM self)
  483. {
  484. SCM progress, iter_scm, block_scm;
  485. iterator_smob *iter_smob;
  486. block_smob *b_smob;
  487. const struct block *block;
  488. block_syms_progress_smob *p_smob;
  489. struct symbol *sym;
  490. iter_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  491. iter_smob = (iterator_smob *) SCM_SMOB_DATA (iter_scm);
  492. block_scm = itscm_iterator_smob_object (iter_smob);
  493. b_smob = bkscm_get_valid_block_smob_arg_unsafe (block_scm,
  494. SCM_ARG1, FUNC_NAME);
  495. block = b_smob->block;
  496. progress = itscm_iterator_smob_progress (iter_smob);
  497. SCM_ASSERT_TYPE (bkscm_is_block_syms_progress (progress),
  498. progress, SCM_ARG1, FUNC_NAME,
  499. block_syms_progress_smob_name);
  500. p_smob = (block_syms_progress_smob *) SCM_SMOB_DATA (progress);
  501. if (!p_smob->initialized_p)
  502. {
  503. sym = block_iterator_first (block, &p_smob->iter);
  504. p_smob->initialized_p = 1;
  505. }
  506. else
  507. sym = block_iterator_next (&p_smob->iter);
  508. if (sym == NULL)
  509. return gdbscm_end_of_iteration ();
  510. return syscm_scm_from_symbol (sym);
  511. }
  512. /* (lookup-block address) -> <gdb:block>
  513. Returns the innermost lexical block containing the specified pc value,
  514. or #f if there is none. */
  515. static SCM
  516. gdbscm_lookup_block (SCM pc_scm)
  517. {
  518. CORE_ADDR pc;
  519. const struct block *block = NULL;
  520. struct compunit_symtab *cust = NULL;
  521. gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "U", pc_scm, &pc);
  522. gdbscm_gdb_exception exc {};
  523. try
  524. {
  525. cust = find_pc_compunit_symtab (pc);
  526. if (cust != NULL && cust->objfile () != NULL)
  527. block = block_for_pc (pc);
  528. }
  529. catch (const gdb_exception &except)
  530. {
  531. exc = unpack (except);
  532. }
  533. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  534. if (cust == NULL || cust->objfile () == NULL)
  535. {
  536. gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, pc_scm,
  537. _("cannot locate object file for block"));
  538. }
  539. if (block != NULL)
  540. return bkscm_scm_from_block (block, cust->objfile ());
  541. return SCM_BOOL_F;
  542. }
  543. /* Initialize the Scheme block support. */
  544. static const scheme_function block_functions[] =
  545. {
  546. { "block?", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_p),
  547. "\
  548. Return #t if the object is a <gdb:block> object." },
  549. { "block-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_valid_p),
  550. "\
  551. Return #t if the block is valid.\n\
  552. A block becomes invalid when its objfile is freed." },
  553. { "block-start", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_start),
  554. "\
  555. Return the start address of the block." },
  556. { "block-end", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_end),
  557. "\
  558. Return the end address of the block." },
  559. { "block-function", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_function),
  560. "\
  561. Return the gdb:symbol object of the function containing the block\n\
  562. or #f if the block does not live in any function." },
  563. { "block-superblock", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_superblock),
  564. "\
  565. Return the superblock (parent block) of the block." },
  566. { "block-global-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_global_block),
  567. "\
  568. Return the global block of the block." },
  569. { "block-static-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_static_block),
  570. "\
  571. Return the static block of the block." },
  572. { "block-global?", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_global_p),
  573. "\
  574. Return #t if block is a global block." },
  575. { "block-static?", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_static_p),
  576. "\
  577. Return #t if block is a static block." },
  578. { "block-symbols", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_symbols),
  579. "\
  580. Return a list of all symbols (as <gdb:symbol> objects) in the block." },
  581. { "make-block-symbols-iterator", 1, 0, 0,
  582. as_a_scm_t_subr (gdbscm_make_block_syms_iter),
  583. "\
  584. Return a <gdb:iterator> object for iterating over all symbols in the block." },
  585. { "block-symbols-progress?", 1, 0, 0,
  586. as_a_scm_t_subr (bkscm_block_syms_progress_p),
  587. "\
  588. Return #t if the object is a <gdb:block-symbols-progress> object." },
  589. { "lookup-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_lookup_block),
  590. "\
  591. Return the innermost GDB block containing the address or #f if none found.\n\
  592. \n\
  593. Arguments:\n\
  594. address: the address to lookup" },
  595. END_FUNCTIONS
  596. };
  597. void
  598. gdbscm_initialize_blocks (void)
  599. {
  600. block_smob_tag
  601. = gdbscm_make_smob_type (block_smob_name, sizeof (block_smob));
  602. scm_set_smob_free (block_smob_tag, bkscm_free_block_smob);
  603. scm_set_smob_print (block_smob_tag, bkscm_print_block_smob);
  604. block_syms_progress_smob_tag
  605. = gdbscm_make_smob_type (block_syms_progress_smob_name,
  606. sizeof (block_syms_progress_smob));
  607. scm_set_smob_print (block_syms_progress_smob_tag,
  608. bkscm_print_block_syms_progress_smob);
  609. gdbscm_define_functions (block_functions, 1);
  610. /* This function is "private". */
  611. bkscm_next_symbol_x_proc
  612. = scm_c_define_gsubr ("%block-next-symbol!", 1, 0, 0,
  613. as_a_scm_t_subr (gdbscm_block_next_symbol_x));
  614. scm_set_procedure_property_x (bkscm_next_symbol_x_proc,
  615. gdbscm_documentation_symbol,
  616. gdbscm_scm_from_c_string ("\
  617. Internal function to assist the block symbols iterator."));
  618. }
  619. void _initialize_scm_block ();
  620. void
  621. _initialize_scm_block ()
  622. {
  623. /* Register an objfile "free" callback so we can properly
  624. invalidate blocks when an object file is about to be deleted. */
  625. bkscm_objfile_data_key
  626. = register_objfile_data_with_cleanup (NULL, bkscm_del_objfile_blocks);
  627. }