scm-symtab.c revision 1.1.1.8 1 /* Scheme interface to symbol tables.
2
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
22
23 #include "symtab.h"
24 #include "source.h"
25 #include "objfiles.h"
26 #include "block.h"
27 #include "guile-internal.h"
28
29 /* A <gdb:symtab> smob. */
30
31 struct symtab_smob
32 {
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;
39
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;
44 };
45
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. */
51
52 struct sal_smob
53 {
54 /* This always appears first. */
55 gdb_smob base;
56
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;
62
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;
69 };
70
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";
74
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;
78
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
85 {
86 /* Helper function for stscm_del_objfile_symtabs to mark the symtab
87 as invalid. */
88
89 static int
90 stscm_mark_symtab_invalid (void **slot, void *info)
91 {
92 symtab_smob *st_smob = (symtab_smob *) *slot;
93
94 st_smob->symtab = NULL;
95 return 1;
96 }
97
98 void operator() (htab_t htab)
99 {
100 gdb_assert (htab != nullptr);
101 htab_traverse_noresize (htab, stscm_mark_symtab_invalid, NULL);
102 htab_delete (htab);
103 }
104 };
105
106 static const registry<objfile>::key<htab, stscm_deleter>
107 stscm_objfile_data_key;
108
109 /* Administrivia for symtab smobs. */
111
112 /* Helper function to hash a symbol_smob. */
113
114 static hashval_t
115 stscm_hash_symtab_smob (const void *p)
116 {
117 const symtab_smob *st_smob = (const symtab_smob *) p;
118
119 return htab_hash_pointer (st_smob->symtab);
120 }
121
122 /* Helper function to compute equality of symtab_smobs. */
123
124 static int
125 stscm_eq_symtab_smob (const void *ap, const void *bp)
126 {
127 const symtab_smob *a = (const symtab_smob *) ap;
128 const symtab_smob *b = (const symtab_smob *) bp;
129
130 return (a->symtab == b->symtab
131 && a->symtab != NULL);
132 }
133
134 /* Return the struct symtab pointer -> SCM mapping table.
135 It is created if necessary. */
136
137 static htab_t
138 stscm_objfile_symtab_map (struct symtab *symtab)
139 {
140 struct objfile *objfile = symtab->compunit ()->objfile ();
141 htab_t htab = stscm_objfile_data_key.get (objfile);
142
143 if (htab == NULL)
144 {
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);
148 }
149
150 return htab;
151 }
152
153 /* The smob "free" function for <gdb:symtab>. */
154
155 static size_t
156 stscm_free_symtab_smob (SCM self)
157 {
158 symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (self);
159
160 if (st_smob->symtab != NULL)
161 {
162 htab_t htab = stscm_objfile_symtab_map (st_smob->symtab);
163
164 gdbscm_clear_eqable_gsmob_ptr_slot (htab, &st_smob->base);
165 }
166
167 /* Not necessary, done to catch bugs. */
168 st_smob->symtab = NULL;
169
170 return 0;
171 }
172
173 /* The smob "print" function for <gdb:symtab>. */
174
175 static int
176 stscm_print_symtab_smob (SCM self, SCM port, scm_print_state *pstate)
177 {
178 symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (self);
179
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)
184 : "<invalid>");
185 scm_puts (">", port);
186
187 scm_remember_upto_here_1 (self);
188
189 /* Non-zero means success. */
190 return 1;
191 }
192
193 /* Low level routine to create a <gdb:symtab> object. */
194
195 static SCM
196 stscm_make_symtab_smob (void)
197 {
198 symtab_smob *st_smob = (symtab_smob *)
199 scm_gc_malloc (sizeof (symtab_smob), symtab_smob_name);
200 SCM st_scm;
201
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);
205
206 return st_scm;
207 }
208
209 /* Return non-zero if SCM is a symbol table smob. */
210
211 static int
212 stscm_is_symtab (SCM scm)
213 {
214 return SCM_SMOB_PREDICATE (symtab_smob_tag, scm);
215 }
216
217 /* (symtab? object) -> boolean */
218
219 static SCM
220 gdbscm_symtab_p (SCM scm)
221 {
222 return scm_from_bool (stscm_is_symtab (scm));
223 }
224
225 /* Create a new <gdb:symtab> object that encapsulates SYMTAB. */
226
227 SCM
228 stscm_scm_from_symtab (struct symtab *symtab)
229 {
230 htab_t htab;
231 eqable_gdb_smob **slot;
232 symtab_smob *st_smob, st_smob_for_lookup;
233 SCM st_scm;
234
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);
240 if (*slot != NULL)
241 return (*slot)->containing_scm;
242
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);
247
248 return st_scm;
249 }
250
251 /* Returns the <gdb:symtab> object in SELF.
252 Throws an exception if SELF is not a <gdb:symtab> object. */
253
254 static SCM
255 stscm_get_symtab_arg_unsafe (SCM self, int arg_pos, const char *func_name)
256 {
257 SCM_ASSERT_TYPE (stscm_is_symtab (self), self, arg_pos, func_name,
258 symtab_smob_name);
259
260 return self;
261 }
262
263 /* Returns a pointer to the symtab smob of SELF.
264 Throws an exception if SELF is not a <gdb:symtab> object. */
265
266 static symtab_smob *
267 stscm_get_symtab_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
268 {
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);
271
272 return st_smob;
273 }
274
275 /* Return non-zero if symtab ST_SMOB is valid. */
276
277 static int
278 stscm_is_valid (symtab_smob *st_smob)
279 {
280 return st_smob->symtab != NULL;
281 }
282
283 /* Throw a Scheme error if SELF is not a valid symtab smob.
284 Otherwise return a pointer to the symtab_smob object. */
285
286 static symtab_smob *
287 stscm_get_valid_symtab_smob_arg_unsafe (SCM self, int arg_pos,
288 const char *func_name)
289 {
290 symtab_smob *st_smob
291 = stscm_get_symtab_smob_arg_unsafe (self, arg_pos, func_name);
292
293 if (!stscm_is_valid (st_smob))
294 {
295 gdbscm_invalid_object_error (func_name, arg_pos, self,
296 _("<gdb:symtab>"));
297 }
298
299 return st_smob;
300 }
301
302
303 /* Symbol table methods. */
305
306 /* (symtab-valid? <gdb:symtab>) -> boolean
307 Returns #t if SELF still exists in GDB. */
308
309 static SCM
310 gdbscm_symtab_valid_p (SCM self)
311 {
312 symtab_smob *st_smob
313 = stscm_get_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
314
315 return scm_from_bool (stscm_is_valid (st_smob));
316 }
317
318 /* (symtab-filename <gdb:symtab>) -> string */
319
320 static SCM
321 gdbscm_symtab_filename (SCM self)
322 {
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;
326
327 return gdbscm_scm_from_c_string (symtab_to_filename_for_display (symtab));
328 }
329
330 /* (symtab-fullname <gdb:symtab>) -> string */
331
332 static SCM
333 gdbscm_symtab_fullname (SCM self)
334 {
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;
338
339 return gdbscm_scm_from_c_string (symtab_to_fullname (symtab));
340 }
341
342 /* (symtab-objfile <gdb:symtab>) -> <gdb:objfile> */
343
344 static SCM
345 gdbscm_symtab_objfile (SCM self)
346 {
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;
350
351 return ofscm_scm_from_objfile (symtab->compunit ()->objfile ());
352 }
353
354 /* (symtab-global-block <gdb:symtab>) -> <gdb:block>
355 Return the GLOBAL_BLOCK of the underlying symtab. */
356
357 static SCM
358 gdbscm_symtab_global_block (SCM self)
359 {
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;
364
365 blockvector = symtab->compunit ()->blockvector ();
366 const struct block *block = blockvector->global_block ();
367
368 return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
369 }
370
371 /* (symtab-static-block <gdb:symtab>) -> <gdb:block>
372 Return the STATIC_BLOCK of the underlying symtab. */
373
374 static SCM
375 gdbscm_symtab_static_block (SCM self)
376 {
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;
381
382 blockvector = symtab->compunit ()->blockvector ();
383 const struct block *block = blockvector->static_block ();
384
385 return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
386 }
387
388 /* Administrivia for sal (symtab-and-line) smobs. */
390
391 /* The smob "print" function for <gdb:sal>. */
392
393 static int
394 stscm_print_sal_smob (SCM self, SCM port, scm_print_state *pstate)
395 {
396 sal_smob *s_smob = (sal_smob *) SCM_SMOB_DATA (self);
397
398 gdbscm_printf (port, "#<%s ", symtab_smob_name);
399 scm_write (s_smob->symtab_scm, port);
400 if (s_smob->sal.line != 0)
401 gdbscm_printf (port, " line %d", s_smob->sal.line);
402 scm_puts (">", port);
403
404 scm_remember_upto_here_1 (self);
405
406 /* Non-zero means success. */
407 return 1;
408 }
409
410 /* Low level routine to create a <gdb:sal> object. */
411
412 static SCM
413 stscm_make_sal_smob (void)
414 {
415 sal_smob *s_smob
416 = (sal_smob *) scm_gc_malloc (sizeof (sal_smob), sal_smob_name);
417 SCM s_scm;
418
419 s_smob->symtab_scm = SCM_BOOL_F;
420 new (&s_smob->sal) symtab_and_line ();
421 s_scm = scm_new_smob (sal_smob_tag, (scm_t_bits) s_smob);
422 gdbscm_init_gsmob (&s_smob->base);
423
424 return s_scm;
425 }
426
427 /* Return non-zero if SCM is a <gdb:sal> object. */
428
429 static int
430 stscm_is_sal (SCM scm)
431 {
432 return SCM_SMOB_PREDICATE (sal_smob_tag, scm);
433 }
434
435 /* (sal? object) -> boolean */
436
437 static SCM
438 gdbscm_sal_p (SCM scm)
439 {
440 return scm_from_bool (stscm_is_sal (scm));
441 }
442
443 /* Create a new <gdb:sal> object that encapsulates SAL. */
444
445 SCM
446 stscm_scm_from_sal (struct symtab_and_line sal)
447 {
448 SCM st_scm, s_scm;
449 sal_smob *s_smob;
450
451 st_scm = SCM_BOOL_F;
452 if (sal.symtab != NULL)
453 st_scm = stscm_scm_from_symtab (sal.symtab);
454
455 s_scm = stscm_make_sal_smob ();
456 s_smob = (sal_smob *) SCM_SMOB_DATA (s_scm);
457 s_smob->symtab_scm = st_scm;
458 s_smob->sal = sal;
459
460 return s_scm;
461 }
462
463 /* Returns the <gdb:sal> object in SELF.
464 Throws an exception if SELF is not a <gdb:sal> object. */
465
466 static SCM
467 stscm_get_sal_arg (SCM self, int arg_pos, const char *func_name)
468 {
469 SCM_ASSERT_TYPE (stscm_is_sal (self), self, arg_pos, func_name,
470 sal_smob_name);
471
472 return self;
473 }
474
475 /* Returns a pointer to the sal smob of SELF.
476 Throws an exception if SELF is not a <gdb:sal> object. */
477
478 static sal_smob *
479 stscm_get_sal_smob_arg (SCM self, int arg_pos, const char *func_name)
480 {
481 SCM s_scm = stscm_get_sal_arg (self, arg_pos, func_name);
482 sal_smob *s_smob = (sal_smob *) SCM_SMOB_DATA (s_scm);
483
484 return s_smob;
485 }
486
487 /* Return non-zero if the symtab in S_SMOB is valid. */
488
489 static int
490 stscm_sal_is_valid (sal_smob *s_smob)
491 {
492 symtab_smob *st_smob;
493
494 /* If there's no symtab that's ok, the sal is still valid. */
495 if (gdbscm_is_false (s_smob->symtab_scm))
496 return 1;
497
498 st_smob = (symtab_smob *) SCM_SMOB_DATA (s_smob->symtab_scm);
499
500 return st_smob->symtab != NULL;
501 }
502
503 /* Throw a Scheme error if SELF is not a valid sal smob.
504 Otherwise return a pointer to the sal_smob object. */
505
506 static sal_smob *
507 stscm_get_valid_sal_smob_arg (SCM self, int arg_pos, const char *func_name)
508 {
509 sal_smob *s_smob = stscm_get_sal_smob_arg (self, arg_pos, func_name);
510
511 if (!stscm_sal_is_valid (s_smob))
512 {
513 gdbscm_invalid_object_error (func_name, arg_pos, self,
514 _("<gdb:sal>"));
515 }
516
517 return s_smob;
518 }
519
520 /* sal methods */
522
523 /* (sal-valid? <gdb:sal>) -> boolean
524 Returns #t if the symtab for SELF still exists in GDB. */
525
526 static SCM
527 gdbscm_sal_valid_p (SCM self)
528 {
529 sal_smob *s_smob = stscm_get_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
530
531 return scm_from_bool (stscm_sal_is_valid (s_smob));
532 }
533
534 /* (sal-pc <gdb:sal>) -> address */
535
536 static SCM
537 gdbscm_sal_pc (SCM self)
538 {
539 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
540 const struct symtab_and_line *sal = &s_smob->sal;
541
542 return gdbscm_scm_from_ulongest (sal->pc);
543 }
544
545 /* (sal-last <gdb:sal>) -> address
546 Returns #f if no ending address is recorded. */
547
548 static SCM
549 gdbscm_sal_last (SCM self)
550 {
551 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
552 const struct symtab_and_line *sal = &s_smob->sal;
553
554 if (sal->end > 0)
555 return gdbscm_scm_from_ulongest (sal->end - 1);
556 return SCM_BOOL_F;
557 }
558
559 /* (sal-line <gdb:sal>) -> integer
560 Returns #f if no line number is recorded. */
561
562 static SCM
563 gdbscm_sal_line (SCM self)
564 {
565 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
566 const struct symtab_and_line *sal = &s_smob->sal;
567
568 if (sal->line > 0)
569 return scm_from_int (sal->line);
570 return SCM_BOOL_F;
571 }
572
573 /* (sal-symtab <gdb:sal>) -> <gdb:symtab>
574 Returns #f if no symtab is recorded. */
575
576 static SCM
577 gdbscm_sal_symtab (SCM self)
578 {
579 sal_smob *s_smob = stscm_get_valid_sal_smob_arg (self, SCM_ARG1, FUNC_NAME);
580
581 return s_smob->symtab_scm;
582 }
583
584 /* (find-pc-line address) -> <gdb:sal> */
585
586 static SCM
587 gdbscm_find_pc_line (SCM pc_scm)
588 {
589 ULONGEST pc_ull;
590 symtab_and_line sal;
591
592 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "U", pc_scm, &pc_ull);
593
594 gdbscm_gdb_exception exc {};
595 try
596 {
597 CORE_ADDR pc = (CORE_ADDR) pc_ull;
598
599 sal = find_pc_line (pc, 0);
600 }
601 catch (const gdb_exception &except)
602 {
603 exc = unpack (except);
604 }
605
606 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
607 return stscm_scm_from_sal (sal);
608 }
609
610 /* Initialize the Scheme symbol support. */
612
613 static const scheme_function symtab_functions[] =
614 {
615 { "symtab?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_p),
616 "\
617 Return #t if the object is a <gdb:symtab> object." },
618
619 { "symtab-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_valid_p),
620 "\
621 Return #t if the symtab still exists in GDB.\n\
622 Symtabs are deleted when the corresponding objfile is freed." },
623
624 { "symtab-filename", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_filename),
625 "\
626 Return the symtab's source file name." },
627
628 { "symtab-fullname", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_fullname),
629 "\
630 Return the symtab's full source file name." },
631
632 { "symtab-objfile", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_objfile),
633 "\
634 Return the symtab's objfile." },
635
636 { "symtab-global-block", 1, 0, 0,
637 as_a_scm_t_subr (gdbscm_symtab_global_block),
638 "\
639 Return the symtab's global block." },
640
641 { "symtab-static-block", 1, 0, 0,
642 as_a_scm_t_subr (gdbscm_symtab_static_block),
643 "\
644 Return the symtab's static block." },
645
646 { "sal?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_p),
647 "\
648 Return #t if the object is a <gdb:sal> (symtab-and-line) object." },
649
650 { "sal-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_valid_p),
651 "\
652 Return #t if the symtab for the sal still exists in GDB.\n\
653 Symtabs are deleted when the corresponding objfile is freed." },
654
655 { "sal-symtab", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_symtab),
656 "\
657 Return the sal's symtab." },
658
659 { "sal-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_line),
660 "\
661 Return the sal's line number, or #f if there is none." },
662
663 { "sal-pc", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_pc),
664 "\
665 Return the sal's address." },
666
667 { "sal-last", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_last),
668 "\
669 Return the last address specified by the sal, or #f if there is none." },
670
671 { "find-pc-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_find_pc_line),
672 "\
673 Return the sal corresponding to the address, or #f if there isn't one.\n\
674 \n\
675 Arguments: address" },
676
677 END_FUNCTIONS
678 };
679
680 void
681 gdbscm_initialize_symtabs (void)
682 {
683 symtab_smob_tag
684 = gdbscm_make_smob_type (symtab_smob_name, sizeof (symtab_smob));
685 scm_set_smob_free (symtab_smob_tag, stscm_free_symtab_smob);
686 scm_set_smob_print (symtab_smob_tag, stscm_print_symtab_smob);
687
688 sal_smob_tag = gdbscm_make_smob_type (sal_smob_name, sizeof (sal_smob));
689 scm_set_smob_print (sal_smob_tag, stscm_print_sal_smob);
690
691 gdbscm_define_functions (symtab_functions, 1);
692 }
693