Commit | Line | Data |
---|---|---|
ed3ef339 DE |
1 | /* Scheme interface to symbol tables. |
2 | ||
61baf725 | 3 | Copyright (C) 2008-2017 Free Software Foundation, Inc. |
ed3ef339 DE |
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 | typedef struct | |
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 | } symtab_smob; | |
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 | typedef struct | |
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 | } sal_smob; | |
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 | static const struct objfile_data *stscm_objfile_data_key; | |
81 | \f | |
82 | /* Administrivia for symtab smobs. */ | |
83 | ||
84 | /* Helper function to hash a symbol_smob. */ | |
85 | ||
86 | static hashval_t | |
87 | stscm_hash_symtab_smob (const void *p) | |
88 | { | |
9a3c8263 | 89 | const symtab_smob *st_smob = (const symtab_smob *) p; |
ed3ef339 DE |
90 | |
91 | return htab_hash_pointer (st_smob->symtab); | |
92 | } | |
93 | ||
94 | /* Helper function to compute equality of symtab_smobs. */ | |
95 | ||
96 | static int | |
97 | stscm_eq_symtab_smob (const void *ap, const void *bp) | |
98 | { | |
9a3c8263 SM |
99 | const symtab_smob *a = (const symtab_smob *) ap; |
100 | const symtab_smob *b = (const symtab_smob *) bp; | |
ed3ef339 DE |
101 | |
102 | return (a->symtab == b->symtab | |
103 | && a->symtab != NULL); | |
104 | } | |
105 | ||
106 | /* Return the struct symtab pointer -> SCM mapping table. | |
107 | It is created if necessary. */ | |
108 | ||
109 | static htab_t | |
110 | stscm_objfile_symtab_map (struct symtab *symtab) | |
111 | { | |
eb822aa6 | 112 | struct objfile *objfile = SYMTAB_OBJFILE (symtab); |
9a3c8263 | 113 | htab_t htab = (htab_t) objfile_data (objfile, stscm_objfile_data_key); |
ed3ef339 DE |
114 | |
115 | if (htab == NULL) | |
116 | { | |
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); | |
120 | } | |
121 | ||
122 | return htab; | |
123 | } | |
124 | ||
ed3ef339 DE |
125 | /* The smob "free" function for <gdb:symtab>. */ |
126 | ||
127 | static size_t | |
128 | stscm_free_symtab_smob (SCM self) | |
129 | { | |
130 | symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (self); | |
131 | ||
132 | if (st_smob->symtab != NULL) | |
133 | { | |
134 | htab_t htab = stscm_objfile_symtab_map (st_smob->symtab); | |
135 | ||
136 | gdbscm_clear_eqable_gsmob_ptr_slot (htab, &st_smob->base); | |
137 | } | |
138 | ||
139 | /* Not necessary, done to catch bugs. */ | |
140 | st_smob->symtab = NULL; | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | /* The smob "print" function for <gdb:symtab>. */ | |
146 | ||
147 | static int | |
148 | stscm_print_symtab_smob (SCM self, SCM port, scm_print_state *pstate) | |
149 | { | |
150 | symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (self); | |
151 | ||
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); | |
158 | ||
159 | scm_remember_upto_here_1 (self); | |
160 | ||
161 | /* Non-zero means success. */ | |
162 | return 1; | |
163 | } | |
164 | ||
165 | /* Low level routine to create a <gdb:symtab> object. */ | |
166 | ||
167 | static SCM | |
168 | stscm_make_symtab_smob (void) | |
169 | { | |
170 | symtab_smob *st_smob = (symtab_smob *) | |
171 | scm_gc_malloc (sizeof (symtab_smob), symtab_smob_name); | |
172 | SCM st_scm; | |
173 | ||
174 | st_smob->symtab = NULL; | |
175 | st_scm = scm_new_smob (symtab_smob_tag, (scm_t_bits) st_smob); | |
1254eefc | 176 | gdbscm_init_eqable_gsmob (&st_smob->base, st_scm); |
ed3ef339 DE |
177 | |
178 | return st_scm; | |
179 | } | |
180 | ||
181 | /* Return non-zero if SCM is a symbol table smob. */ | |
182 | ||
183 | static int | |
184 | stscm_is_symtab (SCM scm) | |
185 | { | |
186 | return SCM_SMOB_PREDICATE (symtab_smob_tag, scm); | |
187 | } | |
188 | ||
189 | /* (symtab? object) -> boolean */ | |
190 | ||
191 | static SCM | |
192 | gdbscm_symtab_p (SCM scm) | |
193 | { | |
194 | return scm_from_bool (stscm_is_symtab (scm)); | |
195 | } | |
196 | ||
197 | /* Create a new <gdb:symtab> object that encapsulates SYMTAB. */ | |
198 | ||
199 | SCM | |
200 | stscm_scm_from_symtab (struct symtab *symtab) | |
201 | { | |
202 | htab_t htab; | |
203 | eqable_gdb_smob **slot; | |
204 | symtab_smob *st_smob, st_smob_for_lookup; | |
205 | SCM st_scm; | |
206 | ||
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; | |
214 | ||
215 | st_scm = stscm_make_symtab_smob (); | |
216 | st_smob = (symtab_smob *) SCM_SMOB_DATA (st_scm); | |
217 | st_smob->symtab = symtab; | |
1254eefc | 218 | gdbscm_fill_eqable_gsmob_ptr_slot (slot, &st_smob->base); |
ed3ef339 DE |
219 | |
220 | return st_scm; | |
221 | } | |
222 | ||
223 | /* Returns the <gdb:symtab> object in SELF. | |
224 | Throws an exception if SELF is not a <gdb:symtab> object. */ | |
225 | ||
226 | static SCM | |
227 | stscm_get_symtab_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
228 | { | |
229 | SCM_ASSERT_TYPE (stscm_is_symtab (self), self, arg_pos, func_name, | |
230 | symtab_smob_name); | |
231 | ||
232 | return self; | |
233 | } | |
234 | ||
235 | /* Returns a pointer to the symtab smob of SELF. | |
236 | Throws an exception if SELF is not a <gdb:symtab> object. */ | |
237 | ||
238 | static symtab_smob * | |
239 | stscm_get_symtab_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
240 | { | |
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); | |
243 | ||
244 | return st_smob; | |
245 | } | |
246 | ||
247 | /* Return non-zero if symtab ST_SMOB is valid. */ | |
248 | ||
249 | static int | |
250 | stscm_is_valid (symtab_smob *st_smob) | |
251 | { | |
252 | return st_smob->symtab != NULL; | |
253 | } | |
254 | ||
255 | /* Throw a Scheme error if SELF is not a valid symtab smob. | |
256 | Otherwise return a pointer to the symtab_smob object. */ | |
257 | ||
258 | static symtab_smob * | |
259 | stscm_get_valid_symtab_smob_arg_unsafe (SCM self, int arg_pos, | |
260 | const char *func_name) | |
261 | { | |
262 | symtab_smob *st_smob | |
263 | = stscm_get_symtab_smob_arg_unsafe (self, arg_pos, func_name); | |
264 | ||
265 | if (!stscm_is_valid (st_smob)) | |
266 | { | |
267 | gdbscm_invalid_object_error (func_name, arg_pos, self, | |
268 | _("<gdb:symtab>")); | |
269 | } | |
270 | ||
271 | return st_smob; | |
272 | } | |
273 | ||
274 | /* Helper function for stscm_del_objfile_symtabs to mark the symtab | |
275 | as invalid. */ | |
276 | ||
277 | static int | |
278 | stscm_mark_symtab_invalid (void **slot, void *info) | |
279 | { | |
280 | symtab_smob *st_smob = (symtab_smob *) *slot; | |
281 | ||
282 | st_smob->symtab = NULL; | |
283 | return 1; | |
284 | } | |
285 | ||
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. */ | |
291 | ||
292 | static void | |
293 | stscm_del_objfile_symtabs (struct objfile *objfile, void *datum) | |
294 | { | |
9a3c8263 | 295 | htab_t htab = (htab_t) datum; |
ed3ef339 DE |
296 | |
297 | if (htab != NULL) | |
298 | { | |
299 | htab_traverse_noresize (htab, stscm_mark_symtab_invalid, NULL); | |
300 | htab_delete (htab); | |
301 | } | |
302 | } | |
303 | \f | |
304 | /* 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 | ||
eb822aa6 | 351 | return ofscm_scm_from_objfile (SYMTAB_OBJFILE (symtab)); |
ed3ef339 DE |
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 | const struct block *block; | |
365 | ||
439247b6 | 366 | blockvector = SYMTAB_BLOCKVECTOR (symtab); |
ed3ef339 DE |
367 | block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK); |
368 | ||
eb822aa6 | 369 | return bkscm_scm_from_block (block, SYMTAB_OBJFILE (symtab)); |
ed3ef339 DE |
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 | const struct block *block; | |
383 | ||
439247b6 | 384 | blockvector = SYMTAB_BLOCKVECTOR (symtab); |
ed3ef339 DE |
385 | block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK); |
386 | ||
eb822aa6 | 387 | return bkscm_scm_from_block (block, SYMTAB_OBJFILE (symtab)); |
ed3ef339 DE |
388 | } |
389 | \f | |
390 | /* Administrivia for sal (symtab-and-line) smobs. */ | |
391 | ||
ed3ef339 DE |
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 | symtab_smob *st_smob = (symtab_smob *) SCM_SMOB_DATA (s_smob->symtab_scm); | |
399 | ||
400 | gdbscm_printf (port, "#<%s ", symtab_smob_name); | |
401 | scm_write (s_smob->symtab_scm, port); | |
402 | if (s_smob->sal.line != 0) | |
403 | gdbscm_printf (port, " line %d", s_smob->sal.line); | |
404 | scm_puts (">", port); | |
405 | ||
406 | scm_remember_upto_here_1 (self); | |
407 | ||
408 | /* Non-zero means success. */ | |
409 | return 1; | |
410 | } | |
411 | ||
412 | /* Low level routine to create a <gdb:sal> object. */ | |
413 | ||
414 | static SCM | |
415 | stscm_make_sal_smob (void) | |
416 | { | |
417 | sal_smob *s_smob | |
418 | = (sal_smob *) scm_gc_malloc (sizeof (sal_smob), sal_smob_name); | |
419 | SCM s_scm; | |
420 | ||
421 | s_smob->symtab_scm = SCM_BOOL_F; | |
422 | memset (&s_smob->sal, 0, sizeof (s_smob->sal)); | |
423 | s_scm = scm_new_smob (sal_smob_tag, (scm_t_bits) s_smob); | |
424 | gdbscm_init_gsmob (&s_smob->base); | |
425 | ||
426 | return s_scm; | |
427 | } | |
428 | ||
429 | /* Return non-zero if SCM is a <gdb:sal> object. */ | |
430 | ||
431 | static int | |
432 | stscm_is_sal (SCM scm) | |
433 | { | |
434 | return SCM_SMOB_PREDICATE (sal_smob_tag, scm); | |
435 | } | |
436 | ||
437 | /* (sal? object) -> boolean */ | |
438 | ||
439 | static SCM | |
440 | gdbscm_sal_p (SCM scm) | |
441 | { | |
442 | return scm_from_bool (stscm_is_sal (scm)); | |
443 | } | |
444 | ||
445 | /* Create a new <gdb:sal> object that encapsulates SAL. */ | |
446 | ||
447 | SCM | |
448 | stscm_scm_from_sal (struct symtab_and_line sal) | |
449 | { | |
450 | SCM st_scm, s_scm; | |
451 | sal_smob *s_smob; | |
452 | ||
453 | st_scm = SCM_BOOL_F; | |
454 | if (sal.symtab != NULL) | |
455 | st_scm = stscm_scm_from_symtab (sal.symtab); | |
456 | ||
457 | s_scm = stscm_make_sal_smob (); | |
458 | s_smob = (sal_smob *) SCM_SMOB_DATA (s_scm); | |
459 | s_smob->symtab_scm = st_scm; | |
460 | s_smob->sal = sal; | |
461 | ||
462 | return s_scm; | |
463 | } | |
464 | ||
465 | /* Returns the <gdb:sal> object in SELF. | |
466 | Throws an exception if SELF is not a <gdb:sal> object. */ | |
467 | ||
468 | static SCM | |
469 | stscm_get_sal_arg (SCM self, int arg_pos, const char *func_name) | |
470 | { | |
471 | SCM_ASSERT_TYPE (stscm_is_sal (self), self, arg_pos, func_name, | |
472 | sal_smob_name); | |
473 | ||
474 | return self; | |
475 | } | |
476 | ||
477 | /* Returns a pointer to the sal smob of SELF. | |
478 | Throws an exception if SELF is not a <gdb:sal> object. */ | |
479 | ||
480 | static sal_smob * | |
481 | stscm_get_sal_smob_arg (SCM self, int arg_pos, const char *func_name) | |
482 | { | |
483 | SCM s_scm = stscm_get_sal_arg (self, arg_pos, func_name); | |
484 | sal_smob *s_smob = (sal_smob *) SCM_SMOB_DATA (s_scm); | |
485 | ||
486 | return s_smob; | |
487 | } | |
488 | ||
489 | /* Return non-zero if the symtab in S_SMOB is valid. */ | |
490 | ||
491 | static int | |
492 | stscm_sal_is_valid (sal_smob *s_smob) | |
493 | { | |
494 | symtab_smob *st_smob; | |
495 | ||
496 | /* If there's no symtab that's ok, the sal is still valid. */ | |
497 | if (gdbscm_is_false (s_smob->symtab_scm)) | |
498 | return 1; | |
499 | ||
500 | st_smob = (symtab_smob *) SCM_SMOB_DATA (s_smob->symtab_scm); | |
501 | ||
502 | return st_smob->symtab != NULL; | |
503 | } | |
504 | ||
505 | /* Throw a Scheme error if SELF is not a valid sal smob. | |
506 | Otherwise return a pointer to the sal_smob object. */ | |
507 | ||
508 | static sal_smob * | |
509 | stscm_get_valid_sal_smob_arg (SCM self, int arg_pos, const char *func_name) | |
510 | { | |
511 | sal_smob *s_smob = stscm_get_sal_smob_arg (self, arg_pos, func_name); | |
512 | ||
513 | if (!stscm_sal_is_valid (s_smob)) | |
514 | { | |
515 | gdbscm_invalid_object_error (func_name, arg_pos, self, | |
516 | _("<gdb:sal>")); | |
517 | } | |
518 | ||
519 | return s_smob; | |
520 | } | |
521 | \f | |
522 | /* 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 | const struct symtab_and_line *sal = &s_smob->sal; | |
582 | ||
583 | return s_smob->symtab_scm; | |
584 | } | |
585 | ||
586 | /* (find-pc-line address) -> <gdb:sal> */ | |
587 | ||
588 | static SCM | |
589 | gdbscm_find_pc_line (SCM pc_scm) | |
590 | { | |
591 | ULONGEST pc_ull; | |
592 | struct symtab_and_line sal; | |
ed3ef339 DE |
593 | |
594 | init_sal (&sal); /* -Wall */ | |
595 | ||
596 | gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "U", pc_scm, &pc_ull); | |
597 | ||
492d29ea | 598 | TRY |
ed3ef339 DE |
599 | { |
600 | CORE_ADDR pc = (CORE_ADDR) pc_ull; | |
601 | ||
602 | sal = find_pc_line (pc, 0); | |
603 | } | |
492d29ea PA |
604 | CATCH (except, RETURN_MASK_ALL) |
605 | { | |
606 | GDBSCM_HANDLE_GDB_EXCEPTION (except); | |
607 | } | |
608 | END_CATCH | |
ed3ef339 DE |
609 | |
610 | return stscm_scm_from_sal (sal); | |
611 | } | |
612 | \f | |
613 | /* Initialize the Scheme symbol support. */ | |
614 | ||
615 | static const scheme_function symtab_functions[] = | |
616 | { | |
72e02483 | 617 | { "symtab?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_p), |
ed3ef339 DE |
618 | "\ |
619 | Return #t if the object is a <gdb:symtab> object." }, | |
620 | ||
72e02483 | 621 | { "symtab-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_valid_p), |
ed3ef339 DE |
622 | "\ |
623 | Return #t if the symtab still exists in GDB.\n\ | |
624 | Symtabs are deleted when the corresponding objfile is freed." }, | |
625 | ||
72e02483 | 626 | { "symtab-filename", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_filename), |
ed3ef339 DE |
627 | "\ |
628 | Return the symtab's source file name." }, | |
629 | ||
72e02483 | 630 | { "symtab-fullname", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_fullname), |
ed3ef339 DE |
631 | "\ |
632 | Return the symtab's full source file name." }, | |
633 | ||
72e02483 | 634 | { "symtab-objfile", 1, 0, 0, as_a_scm_t_subr (gdbscm_symtab_objfile), |
ed3ef339 DE |
635 | "\ |
636 | Return the symtab's objfile." }, | |
637 | ||
72e02483 PA |
638 | { "symtab-global-block", 1, 0, 0, |
639 | as_a_scm_t_subr (gdbscm_symtab_global_block), | |
ed3ef339 DE |
640 | "\ |
641 | Return the symtab's global block." }, | |
642 | ||
72e02483 PA |
643 | { "symtab-static-block", 1, 0, 0, |
644 | as_a_scm_t_subr (gdbscm_symtab_static_block), | |
ed3ef339 DE |
645 | "\ |
646 | Return the symtab's static block." }, | |
647 | ||
72e02483 | 648 | { "sal?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_p), |
ed3ef339 DE |
649 | "\ |
650 | Return #t if the object is a <gdb:sal> (symtab-and-line) object." }, | |
651 | ||
72e02483 | 652 | { "sal-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_valid_p), |
ed3ef339 DE |
653 | "\ |
654 | Return #t if the symtab for the sal still exists in GDB.\n\ | |
655 | Symtabs are deleted when the corresponding objfile is freed." }, | |
656 | ||
72e02483 | 657 | { "sal-symtab", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_symtab), |
ed3ef339 DE |
658 | "\ |
659 | Return the sal's symtab." }, | |
660 | ||
72e02483 | 661 | { "sal-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_line), |
ed3ef339 DE |
662 | "\ |
663 | Return the sal's line number, or #f if there is none." }, | |
664 | ||
72e02483 | 665 | { "sal-pc", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_pc), |
ed3ef339 DE |
666 | "\ |
667 | Return the sal's address." }, | |
668 | ||
72e02483 | 669 | { "sal-last", 1, 0, 0, as_a_scm_t_subr (gdbscm_sal_last), |
ed3ef339 DE |
670 | "\ |
671 | Return the last address specified by the sal, or #f if there is none." }, | |
672 | ||
72e02483 | 673 | { "find-pc-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_find_pc_line), |
ed3ef339 DE |
674 | "\ |
675 | Return the sal corresponding to the address, or #f if there isn't one.\n\ | |
676 | \n\ | |
677 | Arguments: address" }, | |
678 | ||
679 | END_FUNCTIONS | |
680 | }; | |
681 | ||
682 | void | |
683 | gdbscm_initialize_symtabs (void) | |
684 | { | |
685 | symtab_smob_tag | |
686 | = gdbscm_make_smob_type (symtab_smob_name, sizeof (symtab_smob)); | |
ed3ef339 DE |
687 | scm_set_smob_free (symtab_smob_tag, stscm_free_symtab_smob); |
688 | scm_set_smob_print (symtab_smob_tag, stscm_print_symtab_smob); | |
689 | ||
690 | sal_smob_tag = gdbscm_make_smob_type (sal_smob_name, sizeof (sal_smob)); | |
ed3ef339 DE |
691 | scm_set_smob_print (sal_smob_tag, stscm_print_sal_smob); |
692 | ||
693 | gdbscm_define_functions (symtab_functions, 1); | |
694 | ||
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); | |
700 | } |