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