1 /* Scheme interface to blocks.
3 Copyright (C) 2008-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
25 #include "dictionary.h"
29 #include "guile-internal.h"
31 /* A smob describing a gdb block. */
35 /* This always appears first.
36 We want blocks to be eq?-able. And we need to be able to invalidate
37 blocks when the associated objfile is deleted. */
40 /* The GDB block structure that represents a frame's code block. */
41 const struct block
*block
;
43 /* The backing object file. There is no direct relationship in GDB
44 between a block and an object file. When a block is created also
45 store a pointer to the object file for later use. */
46 struct objfile
*objfile
;
49 /* To iterate over block symbols from Scheme we need to store
50 struct block_iterator somewhere. This is stored in the "progress" field
51 of <gdb:iterator>. We store the block object in iterator_smob.object,
52 so we don't store it here.
54 Remember: While iterating over block symbols, you must continually check
55 whether the block is still valid. */
57 struct block_syms_progress_smob
59 /* This always appears first. */
62 /* The iterator for that block. */
63 struct block_iterator iter
;
65 /* Has the iterator been initialized flag. */
69 static const char block_smob_name
[] = "gdb:block";
70 static const char block_syms_progress_smob_name
[] = "gdb:block-symbols-iterator";
72 /* The tag Guile knows the block smobs by. */
73 static scm_t_bits block_smob_tag
;
74 static scm_t_bits block_syms_progress_smob_tag
;
76 /* The "next!" block syms iterator method. */
77 static SCM bkscm_next_symbol_x_proc
;
79 /* This is called when an objfile is about to be freed.
80 Invalidate the block as further actions on the block would result
81 in bad data. All access to b_smob->block should be gated by
82 checks to ensure the block is (still) valid. */
85 /* Helper function for bkscm_del_objfile_blocks to mark the block
89 bkscm_mark_block_invalid (void **slot
, void *info
)
91 block_smob
*b_smob
= (block_smob
*) *slot
;
94 b_smob
->objfile
= NULL
;
98 void operator() (htab_t htab
)
100 gdb_assert (htab
!= nullptr);
101 htab_traverse_noresize (htab
, bkscm_mark_block_invalid
, NULL
);
106 static const registry
<objfile
>::key
<htab
, bkscm_deleter
>
107 bkscm_objfile_data_key
;
109 /* Administrivia for block smobs. */
111 /* Helper function to hash a block_smob. */
114 bkscm_hash_block_smob (const void *p
)
116 const block_smob
*b_smob
= (const block_smob
*) p
;
118 return htab_hash_pointer (b_smob
->block
);
121 /* Helper function to compute equality of block_smobs. */
124 bkscm_eq_block_smob (const void *ap
, const void *bp
)
126 const block_smob
*a
= (const block_smob
*) ap
;
127 const block_smob
*b
= (const block_smob
*) bp
;
129 return (a
->block
== b
->block
130 && a
->block
!= NULL
);
133 /* Return the struct block pointer -> SCM mapping table.
134 It is created if necessary. */
137 bkscm_objfile_block_map (struct objfile
*objfile
)
139 htab_t htab
= bkscm_objfile_data_key
.get (objfile
);
143 htab
= gdbscm_create_eqable_gsmob_ptr_map (bkscm_hash_block_smob
,
144 bkscm_eq_block_smob
);
145 bkscm_objfile_data_key
.set (objfile
, htab
);
151 /* The smob "free" function for <gdb:block>. */
154 bkscm_free_block_smob (SCM self
)
156 block_smob
*b_smob
= (block_smob
*) SCM_SMOB_DATA (self
);
158 if (b_smob
->block
!= NULL
)
160 htab_t htab
= bkscm_objfile_block_map (b_smob
->objfile
);
162 gdbscm_clear_eqable_gsmob_ptr_slot (htab
, &b_smob
->base
);
165 /* Not necessary, done to catch bugs. */
166 b_smob
->block
= NULL
;
167 b_smob
->objfile
= NULL
;
172 /* The smob "print" function for <gdb:block>. */
175 bkscm_print_block_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
177 block_smob
*b_smob
= (block_smob
*) SCM_SMOB_DATA (self
);
178 const struct block
*b
= b_smob
->block
;
180 gdbscm_printf (port
, "#<%s", block_smob_name
);
182 if (b
->superblock () == NULL
)
183 gdbscm_printf (port
, " global");
184 else if (b
->superblock ()->superblock () == NULL
)
185 gdbscm_printf (port
, " static");
187 if (b
->function () != NULL
)
188 gdbscm_printf (port
, " %s", b
->function ()->print_name ());
190 gdbscm_printf (port
, " %s-%s",
191 hex_string (b
->start ()), hex_string (b
->end ()));
193 scm_puts (">", port
);
195 scm_remember_upto_here_1 (self
);
197 /* Non-zero means success. */
201 /* Low level routine to create a <gdb:block> object. */
204 bkscm_make_block_smob (void)
206 block_smob
*b_smob
= (block_smob
*)
207 scm_gc_malloc (sizeof (block_smob
), block_smob_name
);
210 b_smob
->block
= NULL
;
211 b_smob
->objfile
= NULL
;
212 b_scm
= scm_new_smob (block_smob_tag
, (scm_t_bits
) b_smob
);
213 gdbscm_init_eqable_gsmob (&b_smob
->base
, b_scm
);
218 /* Returns non-zero if SCM is a <gdb:block> object. */
221 bkscm_is_block (SCM scm
)
223 return SCM_SMOB_PREDICATE (block_smob_tag
, scm
);
226 /* (block? scm) -> boolean */
229 gdbscm_block_p (SCM scm
)
231 return scm_from_bool (bkscm_is_block (scm
));
234 /* Return the existing object that encapsulates BLOCK, or create a new
235 <gdb:block> object. */
238 bkscm_scm_from_block (const struct block
*block
, struct objfile
*objfile
)
241 eqable_gdb_smob
**slot
;
242 block_smob
*b_smob
, b_smob_for_lookup
;
245 /* If we've already created a gsmob for this block, return it.
246 This makes blocks eq?-able. */
247 htab
= bkscm_objfile_block_map (objfile
);
248 b_smob_for_lookup
.block
= block
;
249 slot
= gdbscm_find_eqable_gsmob_ptr_slot (htab
, &b_smob_for_lookup
.base
);
251 return (*slot
)->containing_scm
;
253 b_scm
= bkscm_make_block_smob ();
254 b_smob
= (block_smob
*) SCM_SMOB_DATA (b_scm
);
255 b_smob
->block
= block
;
256 b_smob
->objfile
= objfile
;
257 gdbscm_fill_eqable_gsmob_ptr_slot (slot
, &b_smob
->base
);
262 /* Returns the <gdb:block> object in SELF.
263 Throws an exception if SELF is not a <gdb:block> object. */
266 bkscm_get_block_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
268 SCM_ASSERT_TYPE (bkscm_is_block (self
), self
, arg_pos
, func_name
,
274 /* Returns a pointer to the block smob of SELF.
275 Throws an exception if SELF is not a <gdb:block> object. */
278 bkscm_get_block_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
280 SCM b_scm
= bkscm_get_block_arg_unsafe (self
, arg_pos
, func_name
);
281 block_smob
*b_smob
= (block_smob
*) SCM_SMOB_DATA (b_scm
);
286 /* Returns non-zero if block B_SMOB is valid. */
289 bkscm_is_valid (block_smob
*b_smob
)
291 return b_smob
->block
!= NULL
;
294 /* Returns the block smob in SELF, verifying it's valid.
295 Throws an exception if SELF is not a <gdb:block> object or is invalid. */
298 bkscm_get_valid_block_smob_arg_unsafe (SCM self
, int arg_pos
,
299 const char *func_name
)
302 = bkscm_get_block_smob_arg_unsafe (self
, arg_pos
, func_name
);
304 if (!bkscm_is_valid (b_smob
))
306 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
313 /* Returns the block smob contained in SCM or NULL if SCM is not a
315 If there is an error a <gdb:exception> object is stored in *EXCP. */
318 bkscm_get_valid_block (SCM scm
, int arg_pos
, const char *func_name
, SCM
*excp
)
322 if (!bkscm_is_block (scm
))
324 *excp
= gdbscm_make_type_error (func_name
, arg_pos
, scm
,
329 b_smob
= (block_smob
*) SCM_SMOB_DATA (scm
);
330 if (!bkscm_is_valid (b_smob
))
332 *excp
= gdbscm_make_invalid_object_error (func_name
, arg_pos
, scm
,
340 /* Returns the struct block that is wrapped by BLOCK_SCM.
341 If BLOCK_SCM is not a block, or is an invalid block, then NULL is returned
342 and a <gdb:exception> object is stored in *EXCP. */
345 bkscm_scm_to_block (SCM block_scm
, int arg_pos
, const char *func_name
,
350 b_smob
= bkscm_get_valid_block (block_scm
, arg_pos
, func_name
, excp
);
353 return b_smob
->block
;
360 /* (block-valid? <gdb:block>) -> boolean
361 Returns #t if SELF still exists in GDB. */
364 gdbscm_block_valid_p (SCM self
)
367 = bkscm_get_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
369 return scm_from_bool (bkscm_is_valid (b_smob
));
372 /* (block-start <gdb:block>) -> address */
375 gdbscm_block_start (SCM self
)
378 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
379 const struct block
*block
= b_smob
->block
;
381 return gdbscm_scm_from_ulongest (block
->start ());
384 /* (block-end <gdb:block>) -> address */
387 gdbscm_block_end (SCM self
)
390 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
391 const struct block
*block
= b_smob
->block
;
393 return gdbscm_scm_from_ulongest (block
->end ());
396 /* (block-function <gdb:block>) -> <gdb:symbol> */
399 gdbscm_block_function (SCM self
)
402 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
403 const struct block
*block
= b_smob
->block
;
406 sym
= block
->function ();
409 return syscm_scm_from_symbol (sym
);
413 /* (block-superblock <gdb:block>) -> <gdb:block> */
416 gdbscm_block_superblock (SCM self
)
419 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
420 const struct block
*block
= b_smob
->block
;
421 const struct block
*super_block
;
423 super_block
= block
->superblock ();
426 return bkscm_scm_from_block (super_block
, b_smob
->objfile
);
430 /* (block-global-block <gdb:block>) -> <gdb:block>
431 Returns the global block associated to this block. */
434 gdbscm_block_global_block (SCM self
)
437 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
438 const struct block
*block
= b_smob
->block
;
439 const struct block
*global_block
;
441 global_block
= block_global_block (block
);
443 return bkscm_scm_from_block (global_block
, b_smob
->objfile
);
446 /* (block-static-block <gdb:block>) -> <gdb:block>
447 Returns the static block associated to this block.
448 Returns #f if we cannot get the static block (this is the global block). */
451 gdbscm_block_static_block (SCM self
)
454 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
455 const struct block
*block
= b_smob
->block
;
456 const struct block
*static_block
;
458 if (block
->superblock () == NULL
)
461 static_block
= block_static_block (block
);
463 return bkscm_scm_from_block (static_block
, b_smob
->objfile
);
466 /* (block-global? <gdb:block>) -> boolean
467 Returns #t if this block object is a global block. */
470 gdbscm_block_global_p (SCM self
)
473 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
474 const struct block
*block
= b_smob
->block
;
476 return scm_from_bool (block
->superblock () == NULL
);
479 /* (block-static? <gdb:block>) -> boolean
480 Returns #t if this block object is a static block. */
483 gdbscm_block_static_p (SCM self
)
486 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
487 const struct block
*block
= b_smob
->block
;
489 if (block
->superblock () != NULL
490 && block
->superblock ()->superblock () == NULL
)
495 /* (block-symbols <gdb:block>) -> list of <gdb:symbol objects
496 Returns a list of symbols of the block. */
499 gdbscm_block_symbols (SCM self
)
502 = bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
503 const struct block
*block
= b_smob
->block
;
504 struct block_iterator iter
;
510 sym
= block_iterator_first (block
, &iter
);
514 SCM s_scm
= syscm_scm_from_symbol (sym
);
516 result
= scm_cons (s_scm
, result
);
517 sym
= block_iterator_next (&iter
);
520 return scm_reverse_x (result
, SCM_EOL
);
523 /* The <gdb:block-symbols-iterator> object,
524 for iterating over all symbols in a block. */
526 /* The smob "print" function for <gdb:block-symbols-iterator>. */
529 bkscm_print_block_syms_progress_smob (SCM self
, SCM port
,
530 scm_print_state
*pstate
)
532 block_syms_progress_smob
*i_smob
533 = (block_syms_progress_smob
*) SCM_SMOB_DATA (self
);
535 gdbscm_printf (port
, "#<%s", block_syms_progress_smob_name
);
537 if (i_smob
->initialized_p
)
539 switch (i_smob
->iter
.which
)
544 struct compunit_symtab
*cust
;
546 gdbscm_printf (port
, " %s",
547 i_smob
->iter
.which
== GLOBAL_BLOCK
548 ? "global" : "static");
549 if (i_smob
->iter
.idx
!= -1)
550 gdbscm_printf (port
, " @%d", i_smob
->iter
.idx
);
551 cust
= (i_smob
->iter
.idx
== -1
552 ? i_smob
->iter
.d
.compunit_symtab
553 : i_smob
->iter
.d
.compunit_symtab
->includes
[i_smob
->iter
.idx
]);
554 gdbscm_printf (port
, " %s",
555 symtab_to_filename_for_display
556 (cust
->primary_filetab ()));
559 case FIRST_LOCAL_BLOCK
:
560 gdbscm_printf (port
, " single block");
565 gdbscm_printf (port
, " !initialized");
567 scm_puts (">", port
);
569 scm_remember_upto_here_1 (self
);
571 /* Non-zero means success. */
575 /* Low level routine to create a <gdb:block-symbols-progress> object. */
578 bkscm_make_block_syms_progress_smob (void)
580 block_syms_progress_smob
*i_smob
= (block_syms_progress_smob
*)
581 scm_gc_malloc (sizeof (block_syms_progress_smob
),
582 block_syms_progress_smob_name
);
585 memset (&i_smob
->iter
, 0, sizeof (i_smob
->iter
));
586 i_smob
->initialized_p
= 0;
587 smob
= scm_new_smob (block_syms_progress_smob_tag
, (scm_t_bits
) i_smob
);
588 gdbscm_init_gsmob (&i_smob
->base
);
593 /* Returns non-zero if SCM is a <gdb:block-symbols-progress> object. */
596 bkscm_is_block_syms_progress (SCM scm
)
598 return SCM_SMOB_PREDICATE (block_syms_progress_smob_tag
, scm
);
601 /* (block-symbols-progress? scm) -> boolean */
604 bkscm_block_syms_progress_p (SCM scm
)
606 return scm_from_bool (bkscm_is_block_syms_progress (scm
));
609 /* (make-block-symbols-iterator <gdb:block>) -> <gdb:iterator>
610 Return a <gdb:iterator> object for iterating over the symbols of SELF. */
613 gdbscm_make_block_syms_iter (SCM self
)
615 /* Call for side effects. */
616 bkscm_get_valid_block_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
619 progress
= bkscm_make_block_syms_progress_smob ();
621 iter
= gdbscm_make_iterator (self
, progress
, bkscm_next_symbol_x_proc
);
626 /* Returns the next symbol in the iteration through the block's dictionary,
627 or (end-of-iteration).
628 This is the iterator_smob.next_x method. */
631 gdbscm_block_next_symbol_x (SCM self
)
633 SCM progress
, iter_scm
, block_scm
;
634 iterator_smob
*iter_smob
;
636 const struct block
*block
;
637 block_syms_progress_smob
*p_smob
;
640 iter_scm
= itscm_get_iterator_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
641 iter_smob
= (iterator_smob
*) SCM_SMOB_DATA (iter_scm
);
643 block_scm
= itscm_iterator_smob_object (iter_smob
);
644 b_smob
= bkscm_get_valid_block_smob_arg_unsafe (block_scm
,
645 SCM_ARG1
, FUNC_NAME
);
646 block
= b_smob
->block
;
648 progress
= itscm_iterator_smob_progress (iter_smob
);
650 SCM_ASSERT_TYPE (bkscm_is_block_syms_progress (progress
),
651 progress
, SCM_ARG1
, FUNC_NAME
,
652 block_syms_progress_smob_name
);
653 p_smob
= (block_syms_progress_smob
*) SCM_SMOB_DATA (progress
);
655 if (!p_smob
->initialized_p
)
657 sym
= block_iterator_first (block
, &p_smob
->iter
);
658 p_smob
->initialized_p
= 1;
661 sym
= block_iterator_next (&p_smob
->iter
);
664 return gdbscm_end_of_iteration ();
666 return syscm_scm_from_symbol (sym
);
669 /* (lookup-block address) -> <gdb:block>
670 Returns the innermost lexical block containing the specified pc value,
671 or #f if there is none. */
674 gdbscm_lookup_block (SCM pc_scm
)
677 const struct block
*block
= NULL
;
678 struct compunit_symtab
*cust
= NULL
;
680 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, NULL
, "U", pc_scm
, &pc
);
682 gdbscm_gdb_exception exc
{};
685 cust
= find_pc_compunit_symtab (pc
);
687 if (cust
!= NULL
&& cust
->objfile () != NULL
)
688 block
= block_for_pc (pc
);
690 catch (const gdb_exception
&except
)
692 exc
= unpack (except
);
695 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
696 if (cust
== NULL
|| cust
->objfile () == NULL
)
698 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, pc_scm
,
699 _("cannot locate object file for block"));
703 return bkscm_scm_from_block (block
, cust
->objfile ());
707 /* Initialize the Scheme block support. */
709 static const scheme_function block_functions
[] =
711 { "block?", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_p
),
713 Return #t if the object is a <gdb:block> object." },
715 { "block-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_valid_p
),
717 Return #t if the block is valid.\n\
718 A block becomes invalid when its objfile is freed." },
720 { "block-start", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_start
),
722 Return the start address of the block." },
724 { "block-end", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_end
),
726 Return the end address of the block." },
728 { "block-function", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_function
),
730 Return the gdb:symbol object of the function containing the block\n\
731 or #f if the block does not live in any function." },
733 { "block-superblock", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_superblock
),
735 Return the superblock (parent block) of the block." },
737 { "block-global-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_global_block
),
739 Return the global block of the block." },
741 { "block-static-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_static_block
),
743 Return the static block of the block." },
745 { "block-global?", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_global_p
),
747 Return #t if block is a global block." },
749 { "block-static?", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_static_p
),
751 Return #t if block is a static block." },
753 { "block-symbols", 1, 0, 0, as_a_scm_t_subr (gdbscm_block_symbols
),
755 Return a list of all symbols (as <gdb:symbol> objects) in the block." },
757 { "make-block-symbols-iterator", 1, 0, 0,
758 as_a_scm_t_subr (gdbscm_make_block_syms_iter
),
760 Return a <gdb:iterator> object for iterating over all symbols in the block." },
762 { "block-symbols-progress?", 1, 0, 0,
763 as_a_scm_t_subr (bkscm_block_syms_progress_p
),
765 Return #t if the object is a <gdb:block-symbols-progress> object." },
767 { "lookup-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_lookup_block
),
769 Return the innermost GDB block containing the address or #f if none found.\n\
772 address: the address to lookup" },
778 gdbscm_initialize_blocks (void)
781 = gdbscm_make_smob_type (block_smob_name
, sizeof (block_smob
));
782 scm_set_smob_free (block_smob_tag
, bkscm_free_block_smob
);
783 scm_set_smob_print (block_smob_tag
, bkscm_print_block_smob
);
785 block_syms_progress_smob_tag
786 = gdbscm_make_smob_type (block_syms_progress_smob_name
,
787 sizeof (block_syms_progress_smob
));
788 scm_set_smob_print (block_syms_progress_smob_tag
,
789 bkscm_print_block_syms_progress_smob
);
791 gdbscm_define_functions (block_functions
, 1);
793 /* This function is "private". */
794 bkscm_next_symbol_x_proc
795 = scm_c_define_gsubr ("%block-next-symbol!", 1, 0, 0,
796 as_a_scm_t_subr (gdbscm_block_next_symbol_x
));
797 scm_set_procedure_property_x (bkscm_next_symbol_x_proc
,
798 gdbscm_documentation_symbol
,
799 gdbscm_scm_from_c_string ("\
800 Internal function to assist the block symbols iterator."));