1 /* Scheme interface to symbol tables.
3 Copyright (C) 2008-2014 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 static const struct objfile_data
*stscm_objfile_data_key
;
82 /* Administrivia for symtab smobs. */
84 /* Helper function to hash a symbol_smob. */
87 stscm_hash_symtab_smob (const void *p
)
89 const symtab_smob
*st_smob
= p
;
91 return htab_hash_pointer (st_smob
->symtab
);
94 /* Helper function to compute equality of symtab_smobs. */
97 stscm_eq_symtab_smob (const void *ap
, const void *bp
)
99 const symtab_smob
*a
= ap
;
100 const symtab_smob
*b
= bp
;
102 return (a
->symtab
== b
->symtab
103 && a
->symtab
!= NULL
);
106 /* Return the struct symtab pointer -> SCM mapping table.
107 It is created if necessary. */
110 stscm_objfile_symtab_map (struct symtab
*symtab
)
112 struct objfile
*objfile
= symtab
->objfile
;
113 htab_t htab
= objfile_data (objfile
, stscm_objfile_data_key
);
117 htab
= gdbscm_create_eqable_gsmob_ptr_map (stscm_hash_symtab_smob
,
118 stscm_eq_symtab_smob
);
119 set_objfile_data (objfile
, stscm_objfile_data_key
, htab
);
125 /* The smob "mark" function for <gdb:symtab>. */
128 stscm_mark_symtab_smob (SCM self
)
133 /* The smob "free" function for <gdb:symtab>. */
136 stscm_free_symtab_smob (SCM self
)
138 symtab_smob
*st_smob
= (symtab_smob
*) SCM_SMOB_DATA (self
);
140 if (st_smob
->symtab
!= NULL
)
142 htab_t htab
= stscm_objfile_symtab_map (st_smob
->symtab
);
144 gdbscm_clear_eqable_gsmob_ptr_slot (htab
, &st_smob
->base
);
147 /* Not necessary, done to catch bugs. */
148 st_smob
->symtab
= NULL
;
153 /* The smob "print" function for <gdb:symtab>. */
156 stscm_print_symtab_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
158 symtab_smob
*st_smob
= (symtab_smob
*) SCM_SMOB_DATA (self
);
160 gdbscm_printf (port
, "#<%s ", symtab_smob_name
);
161 gdbscm_printf (port
, "%s",
162 st_smob
->symtab
!= NULL
163 ? symtab_to_filename_for_display (st_smob
->symtab
)
165 scm_puts (">", port
);
167 scm_remember_upto_here_1 (self
);
169 /* Non-zero means success. */
173 /* Low level routine to create a <gdb:symtab> object. */
176 stscm_make_symtab_smob (void)
178 symtab_smob
*st_smob
= (symtab_smob
*)
179 scm_gc_malloc (sizeof (symtab_smob
), symtab_smob_name
);
182 st_smob
->symtab
= NULL
;
183 st_scm
= scm_new_smob (symtab_smob_tag
, (scm_t_bits
) st_smob
);
184 gdbscm_init_eqable_gsmob (&st_smob
->base
, st_scm
);
189 /* Return non-zero if SCM is a symbol table smob. */
192 stscm_is_symtab (SCM scm
)
194 return SCM_SMOB_PREDICATE (symtab_smob_tag
, scm
);
197 /* (symtab? object) -> boolean */
200 gdbscm_symtab_p (SCM scm
)
202 return scm_from_bool (stscm_is_symtab (scm
));
205 /* Create a new <gdb:symtab> object that encapsulates SYMTAB. */
208 stscm_scm_from_symtab (struct symtab
*symtab
)
211 eqable_gdb_smob
**slot
;
212 symtab_smob
*st_smob
, st_smob_for_lookup
;
215 /* If we've already created a gsmob for this symtab, return it.
216 This makes symtabs eq?-able. */
217 htab
= stscm_objfile_symtab_map (symtab
);
218 st_smob_for_lookup
.symtab
= symtab
;
219 slot
= gdbscm_find_eqable_gsmob_ptr_slot (htab
, &st_smob_for_lookup
.base
);
221 return (*slot
)->containing_scm
;
223 st_scm
= stscm_make_symtab_smob ();
224 st_smob
= (symtab_smob
*) SCM_SMOB_DATA (st_scm
);
225 st_smob
->symtab
= symtab
;
226 gdbscm_fill_eqable_gsmob_ptr_slot (slot
, &st_smob
->base
);
231 /* Returns the <gdb:symtab> object in SELF.
232 Throws an exception if SELF is not a <gdb:symtab> object. */
235 stscm_get_symtab_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
237 SCM_ASSERT_TYPE (stscm_is_symtab (self
), self
, arg_pos
, func_name
,
243 /* Returns a pointer to the symtab smob of SELF.
244 Throws an exception if SELF is not a <gdb:symtab> object. */
247 stscm_get_symtab_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
249 SCM st_scm
= stscm_get_symtab_arg_unsafe (self
, arg_pos
, func_name
);
250 symtab_smob
*st_smob
= (symtab_smob
*) SCM_SMOB_DATA (st_scm
);
255 /* Return non-zero if symtab ST_SMOB is valid. */
258 stscm_is_valid (symtab_smob
*st_smob
)
260 return st_smob
->symtab
!= NULL
;
263 /* Throw a Scheme error if SELF is not a valid symtab smob.
264 Otherwise return a pointer to the symtab_smob object. */
267 stscm_get_valid_symtab_smob_arg_unsafe (SCM self
, int arg_pos
,
268 const char *func_name
)
271 = stscm_get_symtab_smob_arg_unsafe (self
, arg_pos
, func_name
);
273 if (!stscm_is_valid (st_smob
))
275 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
282 /* Helper function for stscm_del_objfile_symtabs to mark the symtab
286 stscm_mark_symtab_invalid (void **slot
, void *info
)
288 symtab_smob
*st_smob
= (symtab_smob
*) *slot
;
290 st_smob
->symtab
= NULL
;
294 /* This function is called when an objfile is about to be freed.
295 Invalidate the symbol table as further actions on the symbol table
296 would result in bad data. All access to st_smob->symtab should be
297 gated by stscm_get_valid_symtab_smob_arg_unsafe which will raise an
298 exception on invalid symbol tables. */
301 stscm_del_objfile_symtabs (struct objfile
*objfile
, void *datum
)
307 htab_traverse_noresize (htab
, stscm_mark_symtab_invalid
, NULL
);
312 /* Symbol table methods. */
314 /* (symtab-valid? <gdb:symtab>) -> boolean
315 Returns #t if SELF still exists in GDB. */
318 gdbscm_symtab_valid_p (SCM self
)
321 = stscm_get_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
323 return scm_from_bool (stscm_is_valid (st_smob
));
326 /* (symtab-filename <gdb:symtab>) -> string */
329 gdbscm_symtab_filename (SCM self
)
332 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
333 struct symtab
*symtab
= st_smob
->symtab
;
335 return gdbscm_scm_from_c_string (symtab_to_filename_for_display (symtab
));
338 /* (symtab-fullname <gdb:symtab>) -> string */
341 gdbscm_symtab_fullname (SCM self
)
344 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
345 struct symtab
*symtab
= st_smob
->symtab
;
347 return gdbscm_scm_from_c_string (symtab_to_fullname (symtab
));
350 /* (symtab-objfile <gdb:symtab>) -> <gdb:objfile> */
353 gdbscm_symtab_objfile (SCM self
)
356 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
357 const struct symtab
*symtab
= st_smob
->symtab
;
359 return ofscm_scm_from_objfile (symtab
->objfile
);
362 /* (symtab-global-block <gdb:symtab>) -> <gdb:block>
363 Return the GLOBAL_BLOCK of the underlying symtab. */
366 gdbscm_symtab_global_block (SCM self
)
369 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
370 const struct symtab
*symtab
= st_smob
->symtab
;
371 const struct blockvector
*blockvector
;
372 const struct block
*block
;
374 blockvector
= BLOCKVECTOR (symtab
);
375 block
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
377 return bkscm_scm_from_block (block
, symtab
->objfile
);
380 /* (symtab-static-block <gdb:symtab>) -> <gdb:block>
381 Return the STATIC_BLOCK of the underlying symtab. */
384 gdbscm_symtab_static_block (SCM self
)
387 = stscm_get_valid_symtab_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
388 const struct symtab
*symtab
= st_smob
->symtab
;
389 const struct blockvector
*blockvector
;
390 const struct block
*block
;
392 blockvector
= BLOCKVECTOR (symtab
);
393 block
= BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
);
395 return bkscm_scm_from_block (block
, symtab
->objfile
);
398 /* Administrivia for sal (symtab-and-line) smobs. */
400 /* The smob "mark" function for <gdb:sal>. */
403 stscm_mark_sal_smob (SCM self
)
405 sal_smob
*s_smob
= (sal_smob
*) SCM_SMOB_DATA (self
);
407 return s_smob
->symtab_scm
;
410 /* The smob "free" function for <gdb:sal>. */
413 stscm_free_sal_smob (SCM self
)
415 sal_smob
*s_smob
= (sal_smob
*) SCM_SMOB_DATA (self
);
417 /* Not necessary, done to catch bugs. */
418 s_smob
->symtab_scm
= SCM_UNDEFINED
;
423 /* The smob "print" function for <gdb:sal>. */
426 stscm_print_sal_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
428 sal_smob
*s_smob
= (sal_smob
*) SCM_SMOB_DATA (self
);
429 symtab_smob
*st_smob
= (symtab_smob
*) SCM_SMOB_DATA (s_smob
->symtab_scm
);
431 gdbscm_printf (port
, "#<%s ", symtab_smob_name
);
432 scm_write (s_smob
->symtab_scm
, port
);
433 if (s_smob
->sal
.line
!= 0)
434 gdbscm_printf (port
, " line %d", s_smob
->sal
.line
);
435 scm_puts (">", port
);
437 scm_remember_upto_here_1 (self
);
439 /* Non-zero means success. */
443 /* Low level routine to create a <gdb:sal> object. */
446 stscm_make_sal_smob (void)
449 = (sal_smob
*) scm_gc_malloc (sizeof (sal_smob
), sal_smob_name
);
452 s_smob
->symtab_scm
= SCM_BOOL_F
;
453 memset (&s_smob
->sal
, 0, sizeof (s_smob
->sal
));
454 s_scm
= scm_new_smob (sal_smob_tag
, (scm_t_bits
) s_smob
);
455 gdbscm_init_gsmob (&s_smob
->base
);
460 /* Return non-zero if SCM is a <gdb:sal> object. */
463 stscm_is_sal (SCM scm
)
465 return SCM_SMOB_PREDICATE (sal_smob_tag
, scm
);
468 /* (sal? object) -> boolean */
471 gdbscm_sal_p (SCM scm
)
473 return scm_from_bool (stscm_is_sal (scm
));
476 /* Create a new <gdb:sal> object that encapsulates SAL. */
479 stscm_scm_from_sal (struct symtab_and_line sal
)
485 if (sal
.symtab
!= NULL
)
486 st_scm
= stscm_scm_from_symtab (sal
.symtab
);
488 s_scm
= stscm_make_sal_smob ();
489 s_smob
= (sal_smob
*) SCM_SMOB_DATA (s_scm
);
490 s_smob
->symtab_scm
= st_scm
;
496 /* Returns the <gdb:sal> object in SELF.
497 Throws an exception if SELF is not a <gdb:sal> object. */
500 stscm_get_sal_arg (SCM self
, int arg_pos
, const char *func_name
)
502 SCM_ASSERT_TYPE (stscm_is_sal (self
), self
, arg_pos
, func_name
,
508 /* Returns a pointer to the sal smob of SELF.
509 Throws an exception if SELF is not a <gdb:sal> object. */
512 stscm_get_sal_smob_arg (SCM self
, int arg_pos
, const char *func_name
)
514 SCM s_scm
= stscm_get_sal_arg (self
, arg_pos
, func_name
);
515 sal_smob
*s_smob
= (sal_smob
*) SCM_SMOB_DATA (s_scm
);
520 /* Return non-zero if the symtab in S_SMOB is valid. */
523 stscm_sal_is_valid (sal_smob
*s_smob
)
525 symtab_smob
*st_smob
;
527 /* If there's no symtab that's ok, the sal is still valid. */
528 if (gdbscm_is_false (s_smob
->symtab_scm
))
531 st_smob
= (symtab_smob
*) SCM_SMOB_DATA (s_smob
->symtab_scm
);
533 return st_smob
->symtab
!= NULL
;
536 /* Throw a Scheme error if SELF is not a valid sal smob.
537 Otherwise return a pointer to the sal_smob object. */
540 stscm_get_valid_sal_smob_arg (SCM self
, int arg_pos
, const char *func_name
)
542 sal_smob
*s_smob
= stscm_get_sal_smob_arg (self
, arg_pos
, func_name
);
544 if (!stscm_sal_is_valid (s_smob
))
546 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
555 /* (sal-valid? <gdb:sal>) -> boolean
556 Returns #t if the symtab for SELF still exists in GDB. */
559 gdbscm_sal_valid_p (SCM self
)
561 sal_smob
*s_smob
= stscm_get_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
563 return scm_from_bool (stscm_sal_is_valid (s_smob
));
566 /* (sal-pc <gdb:sal>) -> address */
569 gdbscm_sal_pc (SCM self
)
571 sal_smob
*s_smob
= stscm_get_valid_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
572 const struct symtab_and_line
*sal
= &s_smob
->sal
;
574 return gdbscm_scm_from_ulongest (sal
->pc
);
577 /* (sal-last <gdb:sal>) -> address
578 Returns #f if no ending address is recorded. */
581 gdbscm_sal_last (SCM self
)
583 sal_smob
*s_smob
= stscm_get_valid_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
584 const struct symtab_and_line
*sal
= &s_smob
->sal
;
587 return gdbscm_scm_from_ulongest (sal
->end
- 1);
591 /* (sal-line <gdb:sal>) -> integer
592 Returns #f if no line number is recorded. */
595 gdbscm_sal_line (SCM self
)
597 sal_smob
*s_smob
= stscm_get_valid_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
598 const struct symtab_and_line
*sal
= &s_smob
->sal
;
601 return scm_from_int (sal
->line
);
605 /* (sal-symtab <gdb:sal>) -> <gdb:symtab>
606 Returns #f if no symtab is recorded. */
609 gdbscm_sal_symtab (SCM self
)
611 sal_smob
*s_smob
= stscm_get_valid_sal_smob_arg (self
, SCM_ARG1
, FUNC_NAME
);
612 const struct symtab_and_line
*sal
= &s_smob
->sal
;
614 return s_smob
->symtab_scm
;
617 /* (find-pc-line address) -> <gdb:sal> */
620 gdbscm_find_pc_line (SCM pc_scm
)
623 struct symtab_and_line sal
;
624 volatile struct gdb_exception except
;
626 init_sal (&sal
); /* -Wall */
628 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, NULL
, "U", pc_scm
, &pc_ull
);
630 TRY_CATCH (except
, RETURN_MASK_ALL
)
632 CORE_ADDR pc
= (CORE_ADDR
) pc_ull
;
634 sal
= find_pc_line (pc
, 0);
636 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
638 return stscm_scm_from_sal (sal
);
641 /* Initialize the Scheme symbol support. */
643 static const scheme_function symtab_functions
[] =
645 { "symtab?", 1, 0, 0, gdbscm_symtab_p
,
647 Return #t if the object is a <gdb:symtab> object." },
649 { "symtab-valid?", 1, 0, 0, gdbscm_symtab_valid_p
,
651 Return #t if the symtab still exists in GDB.\n\
652 Symtabs are deleted when the corresponding objfile is freed." },
654 { "symtab-filename", 1, 0, 0, gdbscm_symtab_filename
,
656 Return the symtab's source file name." },
658 { "symtab-fullname", 1, 0, 0, gdbscm_symtab_fullname
,
660 Return the symtab's full source file name." },
662 { "symtab-objfile", 1, 0, 0, gdbscm_symtab_objfile
,
664 Return the symtab's objfile." },
666 { "symtab-global-block", 1, 0, 0, gdbscm_symtab_global_block
,
668 Return the symtab's global block." },
670 { "symtab-static-block", 1, 0, 0, gdbscm_symtab_static_block
,
672 Return the symtab's static block." },
674 { "sal?", 1, 0, 0, gdbscm_sal_p
,
676 Return #t if the object is a <gdb:sal> (symtab-and-line) object." },
678 { "sal-valid?", 1, 0, 0, gdbscm_sal_valid_p
,
680 Return #t if the symtab for the sal still exists in GDB.\n\
681 Symtabs are deleted when the corresponding objfile is freed." },
683 { "sal-symtab", 1, 0, 0, gdbscm_sal_symtab
,
685 Return the sal's symtab." },
687 { "sal-line", 1, 0, 0, gdbscm_sal_line
,
689 Return the sal's line number, or #f if there is none." },
691 { "sal-pc", 1, 0, 0, gdbscm_sal_pc
,
693 Return the sal's address." },
695 { "sal-last", 1, 0, 0, gdbscm_sal_last
,
697 Return the last address specified by the sal, or #f if there is none." },
699 { "find-pc-line", 1, 0, 0, gdbscm_find_pc_line
,
701 Return the sal corresponding to the address, or #f if there isn't one.\n\
703 Arguments: address" },
709 gdbscm_initialize_symtabs (void)
712 = gdbscm_make_smob_type (symtab_smob_name
, sizeof (symtab_smob
));
713 scm_set_smob_mark (symtab_smob_tag
, stscm_mark_symtab_smob
);
714 scm_set_smob_free (symtab_smob_tag
, stscm_free_symtab_smob
);
715 scm_set_smob_print (symtab_smob_tag
, stscm_print_symtab_smob
);
717 sal_smob_tag
= gdbscm_make_smob_type (sal_smob_name
, sizeof (sal_smob
));
718 scm_set_smob_mark (sal_smob_tag
, stscm_mark_sal_smob
);
719 scm_set_smob_free (sal_smob_tag
, stscm_free_sal_smob
);
720 scm_set_smob_print (sal_smob_tag
, stscm_print_sal_smob
);
722 gdbscm_define_functions (symtab_functions
, 1);
724 /* Register an objfile "free" callback so we can properly
725 invalidate symbol tables, and symbol table and line data
726 structures when an object file that is about to be deleted. */
727 stscm_objfile_data_key
728 = register_objfile_data_with_cleanup (NULL
, stscm_del_objfile_symtabs
);