gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / gdb / guile / scm-symtab.c
blob518ceeaa15dc687e6b7578ec4e6ec9db8cea1caa
1 /* Scheme interface to symbol tables.
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. */
23 #include "defs.h"
24 #include "symtab.h"
25 #include "source.h"
26 #include "objfiles.h"
27 #include "block.h"
28 #include "guile-internal.h"
30 /* A <gdb:symtab> smob. */
32 struct 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. */
39 eqable_gdb_smob base;
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;
47 /* A <gdb:sal> smob.
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. */
53 struct sal_smob
55 /* This always appears first. */
56 gdb_smob base;
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. */
62 SCM symtab_scm;
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. */
86 static hashval_t
87 stscm_hash_symtab_smob (const void *p)
89 const symtab_smob *st_smob = (const symtab_smob *) p;
91 return htab_hash_pointer (st_smob->symtab);
94 /* Helper function to compute equality of symtab_smobs. */
96 static int
97 stscm_eq_symtab_smob (const void *ap, const void *bp)
99 const symtab_smob *a = (const symtab_smob *) ap;
100 const symtab_smob *b = (const symtab_smob *) 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. */
109 static htab_t
110 stscm_objfile_symtab_map (struct symtab *symtab)
112 struct objfile *objfile = symtab->compunit ()->objfile ();
113 htab_t htab = (htab_t) objfile_data (objfile, stscm_objfile_data_key);
115 if (htab == NULL)
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);
122 return htab;
125 /* The smob "free" function for <gdb:symtab>. */
127 static size_t
128 stscm_free_symtab_smob (SCM self)
130 symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (self);
132 if (st_smob->symtab != NULL)
134 htab_t htab = stscm_objfile_symtab_map (st_smob->symtab);
136 gdbscm_clear_eqable_gsmob_ptr_slot (htab, &st_smob->base);
139 /* Not necessary, done to catch bugs. */
140 st_smob->symtab = NULL;
142 return 0;
145 /* The smob "print" function for <gdb:symtab>. */
147 static int
148 stscm_print_symtab_smob (SCM self, SCM port, scm_print_state *pstate)
150 symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (self);
152 gdbscm_printf (port, "#<%s ", symtab_smob_name);
153 gdbscm_printf (port, "%s",
154 st_smob->symtab != NULL
155 ? symtab_to_filename_for_display (st_smob->symtab)
156 : "<invalid>");
157 scm_puts (">", port);
159 scm_remember_upto_here_1 (self);
161 /* Non-zero means success. */
162 return 1;
165 /* Low level routine to create a <gdb:symtab> object. */
167 static SCM
168 stscm_make_symtab_smob (void)
170 symtab_smob *st_smob = (symtab_smob *)
171 scm_gc_malloc (sizeof (symtab_smob), symtab_smob_name);
172 SCM st_scm;
174 st_smob->symtab = NULL;
175 st_scm = scm_new_smob (symtab_smob_tag, (scm_t_bits) st_smob);
176 gdbscm_init_eqable_gsmob (&st_smob->base, st_scm);
178 return st_scm;
181 /* Return non-zero if SCM is a symbol table smob. */
183 static int
184 stscm_is_symtab (SCM scm)
186 return SCM_SMOB_PREDICATE (symtab_smob_tag, scm);
189 /* (symtab? object) -> boolean */
191 static SCM
192 gdbscm_symtab_p (SCM scm)
194 return scm_from_bool (stscm_is_symtab (scm));
197 /* Create a new <gdb:symtab> object that encapsulates SYMTAB. */
200 stscm_scm_from_symtab (struct symtab *symtab)
202 htab_t htab;
203 eqable_gdb_smob **slot;
204 symtab_smob *st_smob, st_smob_for_lookup;
205 SCM st_scm;
207 /* If we've already created a gsmob for this symtab, return it.
208 This makes symtabs eq?-able. */
209 htab = stscm_objfile_symtab_map (symtab);
210 st_smob_for_lookup.symtab = symtab;
211 slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &st_smob_for_lookup.base);
212 if (*slot != NULL)
213 return (*slot)->containing_scm;
215 st_scm = stscm_make_symtab_smob ();
216 st_smob = (symtab_smob *) SCM_SMOB_DATA (st_scm);
217 st_smob->symtab = symtab;
218 gdbscm_fill_eqable_gsmob_ptr_slot (slot, &st_smob->base);
220 return st_scm;
223 /* Returns the <gdb:symtab> object in SELF.
224 Throws an exception if SELF is not a <gdb:symtab> object. */
226 static SCM
227 stscm_get_symtab_arg_unsafe (SCM self, int arg_pos, const char *func_name)
229 SCM_ASSERT_TYPE (stscm_is_symtab (self), self, arg_pos, func_name,
230 symtab_smob_name);
232 return self;
235 /* Returns a pointer to the symtab smob of SELF.
236 Throws an exception if SELF is not a <gdb:symtab> object. */
238 static symtab_smob *
239 stscm_get_symtab_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
241 SCM st_scm = stscm_get_symtab_arg_unsafe (self, arg_pos, func_name);
242 symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (st_scm);
244 return st_smob;
247 /* Return non-zero if symtab ST_SMOB is valid. */
249 static int
250 stscm_is_valid (symtab_smob *st_smob)
252 return st_smob->symtab != NULL;
255 /* Throw a Scheme error if SELF is not a valid symtab smob.
256 Otherwise return a pointer to the symtab_smob object. */
258 static symtab_smob *
259 stscm_get_valid_symtab_smob_arg_unsafe (SCM self, int arg_pos,
260 const char *func_name)
262 symtab_smob *st_smob
263 = stscm_get_symtab_smob_arg_unsafe (self, arg_pos, func_name);
265 if (!stscm_is_valid (st_smob))
267 gdbscm_invalid_object_error (func_name, arg_pos, self,
268 _("<gdb:symtab>"));
271 return st_smob;
274 /* Helper function for stscm_del_objfile_symtabs to mark the symtab
275 as invalid. */
277 static int
278 stscm_mark_symtab_invalid (void **slot, void *info)
280 symtab_smob *st_smob = (symtab_smob *) *slot;
282 st_smob->symtab = NULL;
283 return 1;
286 /* This function is called when an objfile is about to be freed.
287 Invalidate the symbol table as further actions on the symbol table
288 would result in bad data. All access to st_smob->symtab should be
289 gated by stscm_get_valid_symtab_smob_arg_unsafe which will raise an
290 exception on invalid symbol tables. */
292 static void
293 stscm_del_objfile_symtabs (struct objfile *objfile, void *datum)
295 htab_t htab = (htab_t) datum;
297 if (htab != NULL)
299 htab_traverse_noresize (htab, stscm_mark_symtab_invalid, NULL);
300 htab_delete (htab);
304 /* Symbol table methods. */
306 /* (symtab-valid? <gdb:symtab>) -> boolean
307 Returns #t if SELF still exists in GDB. */
309 static SCM
310 gdbscm_symtab_valid_p (SCM self)
312 symtab_smob *st_smob
313 = stscm_get_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
315 return scm_from_bool (stscm_is_valid (st_smob));
318 /* (symtab-filename <gdb:symtab>) -> string */
320 static SCM
321 gdbscm_symtab_filename (SCM self)
323 symtab_smob *st_smob
324 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
325 struct symtab *symtab = st_smob->symtab;
327 return gdbscm_scm_from_c_string (symtab_to_filename_for_display (symtab));
330 /* (symtab-fullname <gdb:symtab>) -> string */
332 static SCM
333 gdbscm_symtab_fullname (SCM self)
335 symtab_smob *st_smob
336 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
337 struct symtab *symtab = st_smob->symtab;
339 return gdbscm_scm_from_c_string (symtab_to_fullname (symtab));
342 /* (symtab-objfile <gdb:symtab>) -> <gdb:objfile> */
344 static SCM
345 gdbscm_symtab_objfile (SCM self)
347 symtab_smob *st_smob
348 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
349 const struct symtab *symtab = st_smob->symtab;
351 return ofscm_scm_from_objfile (symtab->compunit ()->objfile ());
354 /* (symtab-global-block <gdb:symtab>) -> <gdb:block>
355 Return the GLOBAL_BLOCK of the underlying symtab. */
357 static SCM
358 gdbscm_symtab_global_block (SCM self)
360 symtab_smob *st_smob
361 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
362 const struct symtab *symtab = st_smob->symtab;
363 const struct blockvector *blockvector;
365 blockvector = symtab->compunit ()->blockvector ();
366 const struct block *block = blockvector->global_block ();
368 return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
371 /* (symtab-static-block <gdb:symtab>) -> <gdb:block>
372 Return the STATIC_BLOCK of the underlying symtab. */
374 static SCM
375 gdbscm_symtab_static_block (SCM self)
377 symtab_smob *st_smob
378 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
379 const struct symtab *symtab = st_smob->symtab;
380 const struct blockvector *blockvector;
382 blockvector = symtab->compunit ()->blockvector ();
383 const struct block *block = blockvector->static_block ();
385 return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
388 /* Administrivia for sal (symtab-and-line) smobs. */
390 /* The smob "print" function for <gdb:sal>. */
392 static int
393 stscm_print_sal_smob (SCM self, SCM port, scm_print_state *pstate)
395 sal_smob *s_smob = (sal_smob *) SCM_SMOB_DATA (self);
397 gdbscm_printf (port, "#<%s ", symtab_smob_name);
398 scm_write (s_smob->symtab_scm, port);
399 if (s_smob->sal.line != 0)
400 gdbscm_printf (port, " line %d", s_smob->sal.line);
401 scm_puts (">", port);
403 scm_remember_upto_here_1 (self);
405 /* Non-zero means success. */
406 return 1;
409 /* Low level routine to create a <gdb:sal> object. */
411 static SCM
412 stscm_make_sal_smob (void)
414 sal_smob *s_smob
415 = (sal_smob *) scm_gc_malloc (sizeof (sal_smob), sal_smob_name);
416 SCM s_scm;
418 s_smob->symtab_scm = SCM_BOOL_F;
419 new (&s_smob->sal) symtab_and_line ();
420 s_scm = scm_new_smob (sal_smob_tag, (scm_t_bits) s_smob);
421 gdbscm_init_gsmob (&s_smob->base);
423 return s_scm;
426 /* Return non-zero if SCM is a <gdb:sal> object. */
428 static int
429 stscm_is_sal (SCM scm)
431 return SCM_SMOB_PREDICATE (sal_smob_tag, scm);
434 /* (sal? object) -> boolean */
436 static SCM
437 gdbscm_sal_p (SCM scm)
439 return scm_from_bool (stscm_is_sal (scm));
442 /* Create a new <gdb:sal> object that encapsulates SAL. */
445 stscm_scm_from_sal (struct symtab_and_line sal)
447 SCM st_scm, s_scm;
448 sal_smob *s_smob;
450 st_scm = SCM_BOOL_F;
451 if (sal.symtab != NULL)
452 st_scm = stscm_scm_from_symtab (sal.symtab);
454 s_scm = stscm_make_sal_smob ();
455 s_smob = (sal_smob *) SCM_SMOB_DATA (s_scm);
456 s_smob->symtab_scm = st_scm;
457 s_smob->sal = sal;
459 return s_scm;
462 /* Returns the <gdb:sal> object in SELF.
463 Throws an exception if SELF is not a <gdb:sal> object. */
465 static SCM
466 stscm_get_sal_arg (SCM self, int arg_pos, const char *func_name)
468 SCM_ASSERT_TYPE (stscm_is_sal (self), self, arg_pos, func_name,
469 sal_smob_name);
471 return self;
474 /* Returns a pointer to the sal smob of SELF.
475 Throws an exception if SELF is not a <gdb:sal> object. */
477 static sal_smob *
478 stscm_get_sal_smob_arg (SCM self, int arg_pos, const char *func_name)
480 SCM s_scm = stscm_get_sal_arg (self, arg_pos, func_name);
481 sal_smob *s_smob = (sal_smob *) SCM_SMOB_DATA (s_scm);
483 return s_smob;
486 /* Return non-zero if the symtab in S_SMOB is valid. */
488 static int
489 stscm_sal_is_valid (sal_smob *s_smob)
491 symtab_smob *st_smob;
493 /* If there's no symtab that's ok, the sal is still valid. */
494 if (gdbscm_is_false (s_smob->symtab_scm))
495 return 1;
497 st_smob = (symtab_smob *) SCM_SMOB_DATA (s_smob->symtab_scm);
499 return st_smob->symtab != NULL;
502 /* Throw a Scheme error if SELF is not a valid sal smob.
503 Otherwise return a pointer to the sal_smob object. */
505 static sal_smob *
506 stscm_get_valid_sal_smob_arg (SCM self, int arg_pos, const char *func_name)
508 sal_smob *s_smob = stscm_get_sal_smob_arg (self, arg_pos, func_name);
510 if (!stscm_sal_is_valid (s_smob))
512 gdbscm_invalid_object_error (func_name, arg_pos, self,
513 _("<gdb:sal>"));
516 return s_smob;
519 /* sal methods */
521 /* (sal-valid? <gdb:sal>) -> boolean
522 Returns #t if the symtab for SELF still exists in GDB. */
524 static SCM
525 gdbscm_sal_valid_p (SCM self)
527 sal_smob *s_smob = stscm_get_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
529 return scm_from_bool (stscm_sal_is_valid (s_smob));
532 /* (sal-pc <gdb:sal>) -> address */
534 static SCM
535 gdbscm_sal_pc (SCM self)
537 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
538 const struct symtab_and_line *sal = &s_smob->sal;
540 return gdbscm_scm_from_ulongest (sal->pc);
543 /* (sal-last <gdb:sal>) -> address
544 Returns #f if no ending address is recorded. */
546 static SCM
547 gdbscm_sal_last (SCM self)
549 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
550 const struct symtab_and_line *sal = &s_smob->sal;
552 if (sal->end > 0)
553 return gdbscm_scm_from_ulongest (sal->end - 1);
554 return SCM_BOOL_F;
557 /* (sal-line <gdb:sal>) -> integer
558 Returns #f if no line number is recorded. */
560 static SCM
561 gdbscm_sal_line (SCM self)
563 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
564 const struct symtab_and_line *sal = &s_smob->sal;
566 if (sal->line > 0)
567 return scm_from_int (sal->line);
568 return SCM_BOOL_F;
571 /* (sal-symtab <gdb:sal>) -> <gdb:symtab>
572 Returns #f if no symtab is recorded. */
574 static SCM
575 gdbscm_sal_symtab (SCM self)
577 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
579 return s_smob->symtab_scm;
582 /* (find-pc-line address) -> <gdb:sal> */
584 static SCM
585 gdbscm_find_pc_line (SCM pc_scm)
587 ULONGEST pc_ull;
588 symtab_and_line sal;
590 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "U", pc_scm, &pc_ull);
592 gdbscm_gdb_exception exc {};
595 CORE_ADDR pc = (CORE_ADDR) pc_ull;
597 sal = find_pc_line (pc, 0);
599 catch (const gdb_exception &except)
601 exc = unpack (except);
604 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
605 return stscm_scm_from_sal (sal);
608 /* Initialize the Scheme symbol support. */
610 static const scheme_function symtab_functions[] =
612 { "symtab?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_p),
614 Return #t if the object is a <gdb:symtab> object." },
616 { "symtab-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_valid_p),
618 Return #t if the symtab still exists in GDB.\n\
619 Symtabs are deleted when the corresponding objfile is freed." },
621 { "symtab-filename", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_filename),
623 Return the symtab's source file name." },
625 { "symtab-fullname", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_fullname),
627 Return the symtab's full source file name." },
629 { "symtab-objfile", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_objfile),
631 Return the symtab's objfile." },
633 { "symtab-global-block", 1, 0, 0,
634 as_a_scm_t_subr (gdbscm_symtab_global_block),
636 Return the symtab's global block." },
638 { "symtab-static-block", 1, 0, 0,
639 as_a_scm_t_subr (gdbscm_symtab_static_block),
641 Return the symtab's static block." },
643 { "sal?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_p),
645 Return #t if the object is a <gdb:sal> (symtab-and-line) object." },
647 { "sal-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_valid_p),
649 Return #t if the symtab for the sal still exists in GDB.\n\
650 Symtabs are deleted when the corresponding objfile is freed." },
652 { "sal-symtab", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_symtab),
654 Return the sal's symtab." },
656 { "sal-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_line),
658 Return the sal's line number, or #f if there is none." },
660 { "sal-pc", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_pc),
662 Return the sal's address." },
664 { "sal-last", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_last),
666 Return the last address specified by the sal, or #f if there is none." },
668 { "find-pc-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_find_pc_line),
670 Return the sal corresponding to the address, or #f if there isn't one.\n\
672 Arguments: address" },
674 END_FUNCTIONS
677 void
678 gdbscm_initialize_symtabs (void)
680 symtab_smob_tag
681 = gdbscm_make_smob_type (symtab_smob_name, sizeof (symtab_smob));
682 scm_set_smob_free (symtab_smob_tag, stscm_free_symtab_smob);
683 scm_set_smob_print (symtab_smob_tag, stscm_print_symtab_smob);
685 sal_smob_tag = gdbscm_make_smob_type (sal_smob_name, sizeof (sal_smob));
686 scm_set_smob_print (sal_smob_tag, stscm_print_sal_smob);
688 gdbscm_define_functions (symtab_functions, 1);
691 void _initialize_scm_symtab ();
692 void
693 _initialize_scm_symtab ()
695 /* Register an objfile "free" callback so we can properly
696 invalidate symbol tables, and symbol table and line data
697 structures when an object file that is about to be deleted. */
698 stscm_objfile_data_key
699 = register_objfile_data_with_cleanup (NULL, stscm_del_objfile_symtabs);