1 /* Scheme interface to symbol tables.
3 Copyright (C) 2008-2023 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. */
28 #include "guile-internal.h"
30 /* A <gdb:symtab> smob. */
34 /* This always appears first.
35 eqable_gdb_smob is used so that symtabs are eq?-able.
36 Also, a symtab object is associated with an objfile. eqable_gdb_smob
37 lets us track the lifetime of all symtabs associated with an objfile.
38 When an objfile is deleted we need to invalidate the symtab object. */
41 /* The GDB symbol table structure.
42 If this is NULL the symtab is invalid. This can happen when the
43 underlying objfile is freed. */
44 struct symtab
*symtab
;
48 A smob describing a gdb symtab-and-line object.
49 A sal is associated with an objfile. All access must be gated by checking
50 the validity of symtab_scm.
51 TODO: Sals are not eq?-able at the moment, or even comparable. */
55 /* This always appears first. */
58 /* The <gdb:symtab> object of the symtab.
59 We store this instead of a pointer to the symtab_smob because it's not
60 clear GC will know the symtab_smob is referenced by us otherwise, and we
61 need quick access to symtab_smob->symtab to know if this sal is valid. */
64 /* The GDB symbol table and line structure.
65 This object is ephemeral in GDB, so keep our own copy.
66 The symtab pointer in this struct is not usable: If the symtab is deleted
67 this pointer will not be updated. Use symtab_scm instead to determine
68 if this sal is valid. */
69 struct symtab_and_line sal
;
72 static const char symtab_smob_name
[] = "gdb:symtab";
73 /* "symtab-and-line" is pretty long, and "sal" is short and unique. */
74 static const char sal_smob_name
[] = "gdb:sal";
76 /* The tags Guile knows the symbol table smobs by. */
77 static scm_t_bits symtab_smob_tag
;
78 static scm_t_bits sal_smob_tag
;
80 /* This is called when an objfile is about to be freed.
81 Invalidate the symbol table as further actions on the symbol table
82 would result in bad data. All access to st_smob->symtab should be
83 gated by stscm_get_valid_symtab_smob_arg_unsafe which will raise an
84 exception on invalid symbol tables. */
87 /* Helper function for stscm_del_objfile_symtabs to mark the symtab
91 stscm_mark_symtab_invalid (void **slot
, void *info
)
93 symtab_smob
*st_smob
= (symtab_smob
*) *slot
;
95 st_smob
->symtab
= NULL
;
99 void operator() (htab_t htab
)
101 gdb_assert (htab
!= nullptr);
102 htab_traverse_noresize (htab
, stscm_mark_symtab_invalid
, NULL
);
107 static const registry
<objfile
>::key
<htab
, stscm_deleter
>
108 stscm_objfile_data_key
;
110 /* Administrivia for symtab smobs. */
112 /* Helper function to hash a symbol_smob. */
115 stscm_hash_symtab_smob (const void *p
)
117 const symtab_smob
*st_smob
= (const symtab_smob
*) p
;
119 return htab_hash_pointer (st_smob
->symtab
);
122 /* Helper function to compute equality of symtab_smobs. */
125 stscm_eq_symtab_smob (const void *ap
, const void *bp
)
127 const symtab_smob
*a
= (const symtab_smob
*) ap
;
128 const symtab_smob
*b
= (const symtab_smob
*) bp
;
130 return (a
->symtab
== b
->symtab
131 && a
->symtab
!= NULL
);
134 /* Return the struct symtab pointer -> SCM mapping table.
135 It is created if necessary. */
138 stscm_objfile_symtab_map (struct symtab
*symtab
)
140 struct objfile
*objfile
= symtab
->compunit ()->objfile ();
141 htab_t htab
= stscm_objfile_data_key
.get (objfile
);
145 htab
= gdbscm_create_eqable_gsmob_ptr_map (stscm_hash_symtab_smob
,
146 stscm_eq_symtab_smob
);
147 stscm_objfile_data_key
.set (objfile
, htab
);
153 /* The smob "free" function for <gdb:symtab>. */
156 stscm_free_symtab_smob (SCM self
)
158 symtab_smob
*st_smob
= (symtab_smob
*) SCM_SMOB_DATA (self
);
160 if (st_smob
->symtab
!= NULL
)
162 htab_t htab
= stscm_objfile_symtab_map (st_smob
->symtab
);
164 gdbscm_clear_eqable_gsmob_ptr_slot (htab
, &st_smob
->base
);
167 /* Not necessary, done to catch bugs. */
168 st_smob
->symtab
= NULL
;
173 /* The smob "print" function for <gdb:symtab>. */
176 stscm_print_symtab_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
178 symtab_smob
*st_smob
= (symtab_smob
*) SCM_SMOB_DATA (self
);
180 gdbscm_printf (port
, "#<%s ", symtab_smob_name
);
181 gdbscm_printf (port
, "%s",
182 st_smob
->symtab
!= NULL
183 ? symtab_to_filename_for_display (st_smob
->symtab
)
185 scm_puts (">", port
);
187 scm_remember_upto_here_1 (self
);
189 /* Non-zero means success. */
193 /* Low level routine to create a <gdb:symtab> object. */
196 stscm_make_symtab_smob (void)
198 symtab_smob
*st_smob
= (symtab_smob
*)
199 scm_gc_malloc (sizeof (symtab_smob
), symtab_smob_name
);
202 st_smob
->symtab
= NULL
;
203 st_scm
= scm_new_smob (symtab_smob_tag
, (scm_t_bits
) st_smob
);
204 gdbscm_init_eqable_gsmob (&st_smob
->base
, st_scm
);
209 /* Return non-zero if SCM is a symbol table smob. */
212 stscm_is_symtab (SCM scm
)
214 return SCM_SMOB_PREDICATE (symtab_smob_tag
, scm
);
217 /* (symtab? object) -> boolean */
220 gdbscm_symtab_p (SCM scm
)
222 return scm_from_bool (stscm_is_symtab (scm
));
225 /* Create a new <gdb:symtab> object that encapsulates SYMTAB. */
228 stscm_scm_from_symtab (struct symtab
*symtab
)
231 eqable_gdb_smob
**slot
;
232 symtab_smob
*st_smob
, st_smob_for_lookup
;
235 /* If we've already created a gsmob for this symtab, return it.
236 This makes symtabs eq?-able. */
237 htab
= stscm_objfile_symtab_map (symtab
);
238 st_smob_for_lookup
.symtab
= symtab
;
239 slot
= gdbscm_find_eqable_gsmob_ptr_slot (htab
, &st_smob_for_lookup
.base
);
241 return (*slot
)->containing_scm
;
243 st_scm
= stscm_make_symtab_smob ();
244 st_smob
= (symtab_smob
*) SCM_SMOB_DATA (st_scm
);
245 st_smob
->symtab
= symtab
;
246 gdbscm_fill_eqable_gsmob_ptr_slot (slot
, &st_smob
->base
);
251 /* Returns the <gdb:symtab> object in SELF.
252 Throws an exception if SELF is not a <gdb:symtab> object. */
255 stscm_get_symtab_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
257 SCM_ASSERT_TYPE (stscm_is_symtab (self
), self
, arg_pos
, func_name
,
263 /* Returns a pointer to the symtab smob of SELF.
264 Throws an exception if SELF is not a <gdb:symtab> object. */
267 stscm_get_symtab_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
269 SCM st_scm
= stscm_get_symtab_arg_unsafe (self
, arg_pos
, func_name
);
270 symtab_smob
*st_smob
= (symtab_smob
*) SCM_SMOB_DATA (st_scm
);
275 /* Return non-zero if symtab ST_SMOB is valid. */
278 stscm_is_valid (symtab_smob
*st_smob
)
280 return st_smob
->symtab
!= NULL
;
283 /* Throw a Scheme error if SELF is not a valid symtab smob.
284 Otherwise return a pointer to the symtab_smob object. */
287 stscm_get_valid_symtab_smob_arg_unsafe (SCM self
, int arg_pos
,
288 const char *func_name
)
291 = stscm_get_symtab_smob_arg_unsafe (self
, arg_pos
, func_name
);
293 if (!stscm_is_valid (st_smob
))
295 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
303 /* Symbol table methods. */
305 /* (symtab-valid? <gdb:symtab>) -> boolean
306 Returns #t if SELF still exists in GDB. */
309 gdbscm_symtab_valid_p (SCM self
)
312 = stscm_get_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
314 return scm_from_bool (stscm_is_valid (st_smob
));
317 /* (symtab-filename <gdb:symtab>) -> string */
320 gdbscm_symtab_filename (SCM self
)
323 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
324 struct symtab
*symtab
= st_smob
->symtab
;
326 return gdbscm_scm_from_c_string (symtab_to_filename_for_display (symtab
));
329 /* (symtab-fullname <gdb:symtab>) -> string */
332 gdbscm_symtab_fullname (SCM self
)
335 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
336 struct symtab
*symtab
= st_smob
->symtab
;
338 return gdbscm_scm_from_c_string (symtab_to_fullname (symtab
));
341 /* (symtab-objfile <gdb:symtab>) -> <gdb:objfile> */
344 gdbscm_symtab_objfile (SCM self
)
347 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
348 const struct symtab
*symtab
= st_smob
->symtab
;
350 return ofscm_scm_from_objfile (symtab
->compunit ()->objfile ());
353 /* (symtab-global-block <gdb:symtab>) -> <gdb:block>
354 Return the GLOBAL_BLOCK of the underlying symtab. */
357 gdbscm_symtab_global_block (SCM self
)
360 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
361 const struct symtab
*symtab
= st_smob
->symtab
;
362 const struct blockvector
*blockvector
;
364 blockvector
= symtab
->compunit ()->blockvector ();
365 const struct block
*block
= blockvector
->global_block ();
367 return bkscm_scm_from_block (block
, symtab
->compunit ()->objfile ());
370 /* (symtab-static-block <gdb:symtab>) -> <gdb:block>
371 Return the STATIC_BLOCK of the underlying symtab. */
374 gdbscm_symtab_static_block (SCM self
)
377 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
378 const struct symtab
*symtab
= st_smob
->symtab
;
379 const struct blockvector
*blockvector
;
381 blockvector
= symtab
->compunit ()->blockvector ();
382 const struct block
*block
= blockvector
->static_block ();
384 return bkscm_scm_from_block (block
, symtab
->compunit ()->objfile ());
387 /* Administrivia for sal (symtab-and-line) smobs. */
389 /* The smob "print" function for <gdb:sal>. */
392 stscm_print_sal_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
394 sal_smob
*s_smob
= (sal_smob
*) SCM_SMOB_DATA (self
);
396 gdbscm_printf (port
, "#<%s ", symtab_smob_name
);
397 scm_write (s_smob
->symtab_scm
, port
);
398 if (s_smob
->sal
.line
!= 0)
399 gdbscm_printf (port
, " line %d", s_smob
->sal
.line
);
400 scm_puts (">", port
);
402 scm_remember_upto_here_1 (self
);
404 /* Non-zero means success. */
408 /* Low level routine to create a <gdb:sal> object. */
411 stscm_make_sal_smob (void)
414 = (sal_smob
*) scm_gc_malloc (sizeof (sal_smob
), sal_smob_name
);
417 s_smob
->symtab_scm
= SCM_BOOL_F
;
418 new (&s_smob
->sal
) symtab_and_line ();
419 s_scm
= scm_new_smob (sal_smob_tag
, (scm_t_bits
) s_smob
);
420 gdbscm_init_gsmob (&s_smob
->base
);
425 /* Return non-zero if SCM is a <gdb:sal> object. */
428 stscm_is_sal (SCM scm
)
430 return SCM_SMOB_PREDICATE (sal_smob_tag
, scm
);
433 /* (sal? object) -> boolean */
436 gdbscm_sal_p (SCM scm
)
438 return scm_from_bool (stscm_is_sal (scm
));
441 /* Create a new <gdb:sal> object that encapsulates SAL. */
444 stscm_scm_from_sal (struct symtab_and_line sal
)
450 if (sal
.symtab
!= NULL
)
451 st_scm
= stscm_scm_from_symtab (sal
.symtab
);
453 s_scm
= stscm_make_sal_smob ();
454 s_smob
= (sal_smob
*) SCM_SMOB_DATA (s_scm
);
455 s_smob
->symtab_scm
= st_scm
;
461 /* Returns the <gdb:sal> object in SELF.
462 Throws an exception if SELF is not a <gdb:sal> object. */
465 stscm_get_sal_arg (SCM self
, int arg_pos
, const char *func_name
)
467 SCM_ASSERT_TYPE (stscm_is_sal (self
), self
, arg_pos
, func_name
,
473 /* Returns a pointer to the sal smob of SELF.
474 Throws an exception if SELF is not a <gdb:sal> object. */
477 stscm_get_sal_smob_arg (SCM self
, int arg_pos
, const char *func_name
)
479 SCM s_scm
= stscm_get_sal_arg (self
, arg_pos
, func_name
);
480 sal_smob
*s_smob
= (sal_smob
*) SCM_SMOB_DATA (s_scm
);
485 /* Return non-zero if the symtab in S_SMOB is valid. */
488 stscm_sal_is_valid (sal_smob
*s_smob
)
490 symtab_smob
*st_smob
;
492 /* If there's no symtab that's ok, the sal is still valid. */
493 if (gdbscm_is_false (s_smob
->symtab_scm
))
496 st_smob
= (symtab_smob
*) SCM_SMOB_DATA (s_smob
->symtab_scm
);
498 return st_smob
->symtab
!= NULL
;
501 /* Throw a Scheme error if SELF is not a valid sal smob.
502 Otherwise return a pointer to the sal_smob object. */
505 stscm_get_valid_sal_smob_arg (SCM self
, int arg_pos
, const char *func_name
)
507 sal_smob
*s_smob
= stscm_get_sal_smob_arg (self
, arg_pos
, func_name
);
509 if (!stscm_sal_is_valid (s_smob
))
511 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
520 /* (sal-valid? <gdb:sal>) -> boolean
521 Returns #t if the symtab for SELF still exists in GDB. */
524 gdbscm_sal_valid_p (SCM self
)
526 sal_smob
*s_smob
= stscm_get_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
528 return scm_from_bool (stscm_sal_is_valid (s_smob
));
531 /* (sal-pc <gdb:sal>) -> address */
534 gdbscm_sal_pc (SCM self
)
536 sal_smob
*s_smob
= stscm_get_valid_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
537 const struct symtab_and_line
*sal
= &s_smob
->sal
;
539 return gdbscm_scm_from_ulongest (sal
->pc
);
542 /* (sal-last <gdb:sal>) -> address
543 Returns #f if no ending address is recorded. */
546 gdbscm_sal_last (SCM self
)
548 sal_smob
*s_smob
= stscm_get_valid_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
549 const struct symtab_and_line
*sal
= &s_smob
->sal
;
552 return gdbscm_scm_from_ulongest (sal
->end
- 1);
556 /* (sal-line <gdb:sal>) -> integer
557 Returns #f if no line number is recorded. */
560 gdbscm_sal_line (SCM self
)
562 sal_smob
*s_smob
= stscm_get_valid_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
563 const struct symtab_and_line
*sal
= &s_smob
->sal
;
566 return scm_from_int (sal
->line
);
570 /* (sal-symtab <gdb:sal>) -> <gdb:symtab>
571 Returns #f if no symtab is recorded. */
574 gdbscm_sal_symtab (SCM self
)
576 sal_smob
*s_smob
= stscm_get_valid_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
578 return s_smob
->symtab_scm
;
581 /* (find-pc-line address) -> <gdb:sal> */
584 gdbscm_find_pc_line (SCM pc_scm
)
589 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, NULL
, "U", pc_scm
, &pc_ull
);
591 gdbscm_gdb_exception exc
{};
594 CORE_ADDR pc
= (CORE_ADDR
) pc_ull
;
596 sal
= find_pc_line (pc
, 0);
598 catch (const gdb_exception
&except
)
600 exc
= unpack (except
);
603 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
604 return stscm_scm_from_sal (sal
);
607 /* Initialize the Scheme symbol support. */
609 static const scheme_function symtab_functions
[] =
611 { "symtab?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_p
),
613 Return #t if the object is a <gdb:symtab> object." },
615 { "symtab-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_valid_p
),
617 Return #t if the symtab still exists in GDB.\n\
618 Symtabs are deleted when the corresponding objfile is freed." },
620 { "symtab-filename", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_filename
),
622 Return the symtab's source file name." },
624 { "symtab-fullname", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_fullname
),
626 Return the symtab's full source file name." },
628 { "symtab-objfile", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_objfile
),
630 Return the symtab's objfile." },
632 { "symtab-global-block", 1, 0, 0,
633 as_a_scm_t_subr (gdbscm_symtab_global_block
),
635 Return the symtab's global block." },
637 { "symtab-static-block", 1, 0, 0,
638 as_a_scm_t_subr (gdbscm_symtab_static_block
),
640 Return the symtab's static block." },
642 { "sal?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_p
),
644 Return #t if the object is a <gdb:sal> (symtab-and-line) object." },
646 { "sal-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_valid_p
),
648 Return #t if the symtab for the sal still exists in GDB.\n\
649 Symtabs are deleted when the corresponding objfile is freed." },
651 { "sal-symtab", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_symtab
),
653 Return the sal's symtab." },
655 { "sal-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_line
),
657 Return the sal's line number, or #f if there is none." },
659 { "sal-pc", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_pc
),
661 Return the sal's address." },
663 { "sal-last", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_last
),
665 Return the last address specified by the sal, or #f if there is none." },
667 { "find-pc-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_find_pc_line
),
669 Return the sal corresponding to the address, or #f if there isn't one.\n\
671 Arguments: address" },
677 gdbscm_initialize_symtabs (void)
680 = gdbscm_make_smob_type (symtab_smob_name
, sizeof (symtab_smob
));
681 scm_set_smob_free (symtab_smob_tag
, stscm_free_symtab_smob
);
682 scm_set_smob_print (symtab_smob_tag
, stscm_print_symtab_smob
);
684 sal_smob_tag
= gdbscm_make_smob_type (sal_smob_name
, sizeof (sal_smob
));
685 scm_set_smob_print (sal_smob_tag
, stscm_print_sal_smob
);
687 gdbscm_define_functions (symtab_functions
, 1);