More updated translations
[binutils-gdb.git] / gdb / guile / scm-symtab.c
blob2fd4b03b4c6ddd7a539e2d141a9964c81923f0fe
1 /* Scheme interface to symbol tables.
3 Copyright (C) 2008-2024 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 "symtab.h"
24 #include "source.h"
25 #include "objfiles.h"
26 #include "block.h"
27 #include "guile-internal.h"
29 /* A <gdb:symtab> smob. */
31 struct symtab_smob
33 /* This always appears first.
34 eqable_gdb_smob is used so that symtabs are eq?-able.
35 Also, a symtab object is associated with an objfile. eqable_gdb_smob
36 lets us track the lifetime of all symtabs associated with an objfile.
37 When an objfile is deleted we need to invalidate the symtab object. */
38 eqable_gdb_smob base;
40 /* The GDB symbol table structure.
41 If this is NULL the symtab is invalid. This can happen when the
42 underlying objfile is freed. */
43 struct symtab *symtab;
46 /* A <gdb:sal> smob.
47 A smob describing a gdb symtab-and-line object.
48 A sal is associated with an objfile. All access must be gated by checking
49 the validity of symtab_scm.
50 TODO: Sals are not eq?-able at the moment, or even comparable. */
52 struct sal_smob
54 /* This always appears first. */
55 gdb_smob base;
57 /* The <gdb:symtab> object of the symtab.
58 We store this instead of a pointer to the symtab_smob because it's not
59 clear GC will know the symtab_smob is referenced by us otherwise, and we
60 need quick access to symtab_smob->symtab to know if this sal is valid. */
61 SCM symtab_scm;
63 /* The GDB symbol table and line structure.
64 This object is ephemeral in GDB, so keep our own copy.
65 The symtab pointer in this struct is not usable: If the symtab is deleted
66 this pointer will not be updated. Use symtab_scm instead to determine
67 if this sal is valid. */
68 struct symtab_and_line sal;
71 static const char symtab_smob_name[] = "gdb:symtab";
72 /* "symtab-and-line" is pretty long, and "sal" is short and unique. */
73 static const char sal_smob_name[] = "gdb:sal";
75 /* The tags Guile knows the symbol table smobs by. */
76 static scm_t_bits symtab_smob_tag;
77 static scm_t_bits sal_smob_tag;
79 /* This is called when an objfile is about to be freed.
80 Invalidate the symbol table as further actions on the symbol table
81 would result in bad data. All access to st_smob->symtab should be
82 gated by stscm_get_valid_symtab_smob_arg_unsafe which will raise an
83 exception on invalid symbol tables. */
84 struct stscm_deleter
86 /* Helper function for stscm_del_objfile_symtabs to mark the symtab
87 as invalid. */
89 static int
90 stscm_mark_symtab_invalid (void **slot, void *info)
92 symtab_smob *st_smob = (symtab_smob *) *slot;
94 st_smob->symtab = NULL;
95 return 1;
98 void operator() (htab_t htab)
100 gdb_assert (htab != nullptr);
101 htab_traverse_noresize (htab, stscm_mark_symtab_invalid, NULL);
102 htab_delete (htab);
106 static const registry<objfile>::key<htab, stscm_deleter>
107 stscm_objfile_data_key;
109 /* Administrivia for symtab smobs. */
111 /* Helper function to hash a symbol_smob. */
113 static hashval_t
114 stscm_hash_symtab_smob (const void *p)
116 const symtab_smob *st_smob = (const symtab_smob *) p;
118 return htab_hash_pointer (st_smob->symtab);
121 /* Helper function to compute equality of symtab_smobs. */
123 static int
124 stscm_eq_symtab_smob (const void *ap, const void *bp)
126 const symtab_smob *a = (const symtab_smob *) ap;
127 const symtab_smob *b = (const symtab_smob *) bp;
129 return (a->symtab == b->symtab
130 && a->symtab != NULL);
133 /* Return the struct symtab pointer -> SCM mapping table.
134 It is created if necessary. */
136 static htab_t
137 stscm_objfile_symtab_map (struct symtab *symtab)
139 struct objfile *objfile = symtab->compunit ()->objfile ();
140 htab_t htab = stscm_objfile_data_key.get (objfile);
142 if (htab == NULL)
144 htab = gdbscm_create_eqable_gsmob_ptr_map (stscm_hash_symtab_smob,
145 stscm_eq_symtab_smob);
146 stscm_objfile_data_key.set (objfile, htab);
149 return htab;
152 /* The smob "free" function for <gdb:symtab>. */
154 static size_t
155 stscm_free_symtab_smob (SCM self)
157 symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (self);
159 if (st_smob->symtab != NULL)
161 htab_t htab = stscm_objfile_symtab_map (st_smob->symtab);
163 gdbscm_clear_eqable_gsmob_ptr_slot (htab, &st_smob->base);
166 /* Not necessary, done to catch bugs. */
167 st_smob->symtab = NULL;
169 return 0;
172 /* The smob "print" function for <gdb:symtab>. */
174 static int
175 stscm_print_symtab_smob (SCM self, SCM port, scm_print_state *pstate)
177 symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (self);
179 gdbscm_printf (port, "#<%s ", symtab_smob_name);
180 gdbscm_printf (port, "%s",
181 st_smob->symtab != NULL
182 ? symtab_to_filename_for_display (st_smob->symtab)
183 : "<invalid>");
184 scm_puts (">", port);
186 scm_remember_upto_here_1 (self);
188 /* Non-zero means success. */
189 return 1;
192 /* Low level routine to create a <gdb:symtab> object. */
194 static SCM
195 stscm_make_symtab_smob (void)
197 symtab_smob *st_smob = (symtab_smob *)
198 scm_gc_malloc (sizeof (symtab_smob), symtab_smob_name);
199 SCM st_scm;
201 st_smob->symtab = NULL;
202 st_scm = scm_new_smob (symtab_smob_tag, (scm_t_bits) st_smob);
203 gdbscm_init_eqable_gsmob (&st_smob->base, st_scm);
205 return st_scm;
208 /* Return non-zero if SCM is a symbol table smob. */
210 static int
211 stscm_is_symtab (SCM scm)
213 return SCM_SMOB_PREDICATE (symtab_smob_tag, scm);
216 /* (symtab? object) -> boolean */
218 static SCM
219 gdbscm_symtab_p (SCM scm)
221 return scm_from_bool (stscm_is_symtab (scm));
224 /* Create a new <gdb:symtab> object that encapsulates SYMTAB. */
227 stscm_scm_from_symtab (struct symtab *symtab)
229 htab_t htab;
230 eqable_gdb_smob **slot;
231 symtab_smob *st_smob, st_smob_for_lookup;
232 SCM st_scm;
234 /* If we've already created a gsmob for this symtab, return it.
235 This makes symtabs eq?-able. */
236 htab = stscm_objfile_symtab_map (symtab);
237 st_smob_for_lookup.symtab = symtab;
238 slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &st_smob_for_lookup.base);
239 if (*slot != NULL)
240 return (*slot)->containing_scm;
242 st_scm = stscm_make_symtab_smob ();
243 st_smob = (symtab_smob *) SCM_SMOB_DATA (st_scm);
244 st_smob->symtab = symtab;
245 gdbscm_fill_eqable_gsmob_ptr_slot (slot, &st_smob->base);
247 return st_scm;
250 /* Returns the <gdb:symtab> object in SELF.
251 Throws an exception if SELF is not a <gdb:symtab> object. */
253 static SCM
254 stscm_get_symtab_arg_unsafe (SCM self, int arg_pos, const char *func_name)
256 SCM_ASSERT_TYPE (stscm_is_symtab (self), self, arg_pos, func_name,
257 symtab_smob_name);
259 return self;
262 /* Returns a pointer to the symtab smob of SELF.
263 Throws an exception if SELF is not a <gdb:symtab> object. */
265 static symtab_smob *
266 stscm_get_symtab_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
268 SCM st_scm = stscm_get_symtab_arg_unsafe (self, arg_pos, func_name);
269 symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (st_scm);
271 return st_smob;
274 /* Return non-zero if symtab ST_SMOB is valid. */
276 static int
277 stscm_is_valid (symtab_smob *st_smob)
279 return st_smob->symtab != NULL;
282 /* Throw a Scheme error if SELF is not a valid symtab smob.
283 Otherwise return a pointer to the symtab_smob object. */
285 static symtab_smob *
286 stscm_get_valid_symtab_smob_arg_unsafe (SCM self, int arg_pos,
287 const char *func_name)
289 symtab_smob *st_smob
290 = stscm_get_symtab_smob_arg_unsafe (self, arg_pos, func_name);
292 if (!stscm_is_valid (st_smob))
294 gdbscm_invalid_object_error (func_name, arg_pos, self,
295 _("<gdb:symtab>"));
298 return st_smob;
302 /* Symbol table methods. */
304 /* (symtab-valid? <gdb:symtab>) -> boolean
305 Returns #t if SELF still exists in GDB. */
307 static SCM
308 gdbscm_symtab_valid_p (SCM self)
310 symtab_smob *st_smob
311 = stscm_get_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
313 return scm_from_bool (stscm_is_valid (st_smob));
316 /* (symtab-filename <gdb:symtab>) -> string */
318 static SCM
319 gdbscm_symtab_filename (SCM self)
321 symtab_smob *st_smob
322 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
323 struct symtab *symtab = st_smob->symtab;
325 return gdbscm_scm_from_c_string (symtab_to_filename_for_display (symtab));
328 /* (symtab-fullname <gdb:symtab>) -> string */
330 static SCM
331 gdbscm_symtab_fullname (SCM self)
333 symtab_smob *st_smob
334 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
335 struct symtab *symtab = st_smob->symtab;
337 return gdbscm_scm_from_c_string (symtab_to_fullname (symtab));
340 /* (symtab-objfile <gdb:symtab>) -> <gdb:objfile> */
342 static SCM
343 gdbscm_symtab_objfile (SCM self)
345 symtab_smob *st_smob
346 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
347 const struct symtab *symtab = st_smob->symtab;
349 return ofscm_scm_from_objfile (symtab->compunit ()->objfile ());
352 /* (symtab-global-block <gdb:symtab>) -> <gdb:block>
353 Return the GLOBAL_BLOCK of the underlying symtab. */
355 static SCM
356 gdbscm_symtab_global_block (SCM self)
358 symtab_smob *st_smob
359 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
360 const struct symtab *symtab = st_smob->symtab;
361 const struct blockvector *blockvector;
363 blockvector = symtab->compunit ()->blockvector ();
364 const struct block *block = blockvector->global_block ();
366 return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
369 /* (symtab-static-block <gdb:symtab>) -> <gdb:block>
370 Return the STATIC_BLOCK of the underlying symtab. */
372 static SCM
373 gdbscm_symtab_static_block (SCM self)
375 symtab_smob *st_smob
376 = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
377 const struct symtab *symtab = st_smob->symtab;
378 const struct blockvector *blockvector;
380 blockvector = symtab->compunit ()->blockvector ();
381 const struct block *block = blockvector->static_block ();
383 return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
386 /* Administrivia for sal (symtab-and-line) smobs. */
388 /* The smob "print" function for <gdb:sal>. */
390 static int
391 stscm_print_sal_smob (SCM self, SCM port, scm_print_state *pstate)
393 sal_smob *s_smob = (sal_smob *) SCM_SMOB_DATA (self);
395 gdbscm_printf (port, "#<%s ", symtab_smob_name);
396 scm_write (s_smob->symtab_scm, port);
397 if (s_smob->sal.line != 0)
398 gdbscm_printf (port, " line %d", s_smob->sal.line);
399 scm_puts (">", port);
401 scm_remember_upto_here_1 (self);
403 /* Non-zero means success. */
404 return 1;
407 /* Low level routine to create a <gdb:sal> object. */
409 static SCM
410 stscm_make_sal_smob (void)
412 sal_smob *s_smob
413 = (sal_smob *) scm_gc_malloc (sizeof (sal_smob), sal_smob_name);
414 SCM s_scm;
416 s_smob->symtab_scm = SCM_BOOL_F;
417 new (&s_smob->sal) symtab_and_line ();
418 s_scm = scm_new_smob (sal_smob_tag, (scm_t_bits) s_smob);
419 gdbscm_init_gsmob (&s_smob->base);
421 return s_scm;
424 /* Return non-zero if SCM is a <gdb:sal> object. */
426 static int
427 stscm_is_sal (SCM scm)
429 return SCM_SMOB_PREDICATE (sal_smob_tag, scm);
432 /* (sal? object) -> boolean */
434 static SCM
435 gdbscm_sal_p (SCM scm)
437 return scm_from_bool (stscm_is_sal (scm));
440 /* Create a new <gdb:sal> object that encapsulates SAL. */
443 stscm_scm_from_sal (struct symtab_and_line sal)
445 SCM st_scm, s_scm;
446 sal_smob *s_smob;
448 st_scm = SCM_BOOL_F;
449 if (sal.symtab != NULL)
450 st_scm = stscm_scm_from_symtab (sal.symtab);
452 s_scm = stscm_make_sal_smob ();
453 s_smob = (sal_smob *) SCM_SMOB_DATA (s_scm);
454 s_smob->symtab_scm = st_scm;
455 s_smob->sal = sal;
457 return s_scm;
460 /* Returns the <gdb:sal> object in SELF.
461 Throws an exception if SELF is not a <gdb:sal> object. */
463 static SCM
464 stscm_get_sal_arg (SCM self, int arg_pos, const char *func_name)
466 SCM_ASSERT_TYPE (stscm_is_sal (self), self, arg_pos, func_name,
467 sal_smob_name);
469 return self;
472 /* Returns a pointer to the sal smob of SELF.
473 Throws an exception if SELF is not a <gdb:sal> object. */
475 static sal_smob *
476 stscm_get_sal_smob_arg (SCM self, int arg_pos, const char *func_name)
478 SCM s_scm = stscm_get_sal_arg (self, arg_pos, func_name);
479 sal_smob *s_smob = (sal_smob *) SCM_SMOB_DATA (s_scm);
481 return s_smob;
484 /* Return non-zero if the symtab in S_SMOB is valid. */
486 static int
487 stscm_sal_is_valid (sal_smob *s_smob)
489 symtab_smob *st_smob;
491 /* If there's no symtab that's ok, the sal is still valid. */
492 if (gdbscm_is_false (s_smob->symtab_scm))
493 return 1;
495 st_smob = (symtab_smob *) SCM_SMOB_DATA (s_smob->symtab_scm);
497 return st_smob->symtab != NULL;
500 /* Throw a Scheme error if SELF is not a valid sal smob.
501 Otherwise return a pointer to the sal_smob object. */
503 static sal_smob *
504 stscm_get_valid_sal_smob_arg (SCM self, int arg_pos, const char *func_name)
506 sal_smob *s_smob = stscm_get_sal_smob_arg (self, arg_pos, func_name);
508 if (!stscm_sal_is_valid (s_smob))
510 gdbscm_invalid_object_error (func_name, arg_pos, self,
511 _("<gdb:sal>"));
514 return s_smob;
517 /* sal methods */
519 /* (sal-valid? <gdb:sal>) -> boolean
520 Returns #t if the symtab for SELF still exists in GDB. */
522 static SCM
523 gdbscm_sal_valid_p (SCM self)
525 sal_smob *s_smob = stscm_get_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
527 return scm_from_bool (stscm_sal_is_valid (s_smob));
530 /* (sal-pc <gdb:sal>) -> address */
532 static SCM
533 gdbscm_sal_pc (SCM self)
535 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
536 const struct symtab_and_line *sal = &s_smob->sal;
538 return gdbscm_scm_from_ulongest (sal->pc);
541 /* (sal-last <gdb:sal>) -> address
542 Returns #f if no ending address is recorded. */
544 static SCM
545 gdbscm_sal_last (SCM self)
547 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
548 const struct symtab_and_line *sal = &s_smob->sal;
550 if (sal->end > 0)
551 return gdbscm_scm_from_ulongest (sal->end - 1);
552 return SCM_BOOL_F;
555 /* (sal-line <gdb:sal>) -> integer
556 Returns #f if no line number is recorded. */
558 static SCM
559 gdbscm_sal_line (SCM self)
561 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
562 const struct symtab_and_line *sal = &s_smob->sal;
564 if (sal->line > 0)
565 return scm_from_int (sal->line);
566 return SCM_BOOL_F;
569 /* (sal-symtab <gdb:sal>) -> <gdb:symtab>
570 Returns #f if no symtab is recorded. */
572 static SCM
573 gdbscm_sal_symtab (SCM self)
575 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
577 return s_smob->symtab_scm;
580 /* (find-pc-line address) -> <gdb:sal> */
582 static SCM
583 gdbscm_find_pc_line (SCM pc_scm)
585 ULONGEST pc_ull;
586 symtab_and_line sal;
588 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "U", pc_scm, &pc_ull);
590 gdbscm_gdb_exception exc {};
593 CORE_ADDR pc = (CORE_ADDR) pc_ull;
595 sal = find_pc_line (pc, 0);
597 catch (const gdb_exception &except)
599 exc = unpack (except);
602 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
603 return stscm_scm_from_sal (sal);
606 /* Initialize the Scheme symbol support. */
608 static const scheme_function symtab_functions[] =
610 { "symtab?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_p),
612 Return #t if the object is a <gdb:symtab> object." },
614 { "symtab-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_valid_p),
616 Return #t if the symtab still exists in GDB.\n\
617 Symtabs are deleted when the corresponding objfile is freed." },
619 { "symtab-filename", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_filename),
621 Return the symtab's source file name." },
623 { "symtab-fullname", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_fullname),
625 Return the symtab's full source file name." },
627 { "symtab-objfile", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_objfile),
629 Return the symtab's objfile." },
631 { "symtab-global-block", 1, 0, 0,
632 as_a_scm_t_subr (gdbscm_symtab_global_block),
634 Return the symtab's global block." },
636 { "symtab-static-block", 1, 0, 0,
637 as_a_scm_t_subr (gdbscm_symtab_static_block),
639 Return the symtab's static block." },
641 { "sal?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_p),
643 Return #t if the object is a <gdb:sal> (symtab-and-line) object." },
645 { "sal-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_valid_p),
647 Return #t if the symtab for the sal still exists in GDB.\n\
648 Symtabs are deleted when the corresponding objfile is freed." },
650 { "sal-symtab", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_symtab),
652 Return the sal's symtab." },
654 { "sal-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_line),
656 Return the sal's line number, or #f if there is none." },
658 { "sal-pc", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_pc),
660 Return the sal's address." },
662 { "sal-last", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_last),
664 Return the last address specified by the sal, or #f if there is none." },
666 { "find-pc-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_find_pc_line),
668 Return the sal corresponding to the address, or #f if there isn't one.\n\
670 Arguments: address" },
672 END_FUNCTIONS
675 void
676 gdbscm_initialize_symtabs (void)
678 symtab_smob_tag
679 = gdbscm_make_smob_type (symtab_smob_name, sizeof (symtab_smob));
680 scm_set_smob_free (symtab_smob_tag, stscm_free_symtab_smob);
681 scm_set_smob_print (symtab_smob_tag, stscm_print_symtab_smob);
683 sal_smob_tag = gdbscm_make_smob_type (sal_smob_name, sizeof (sal_smob));
684 scm_set_smob_print (sal_smob_tag, stscm_print_sal_smob);
686 gdbscm_define_functions (symtab_functions, 1);