scm-symbol.c revision 1.4 1 1.1 christos /* Scheme interface to symbols.
2 1.1 christos
3 1.4 christos Copyright (C) 2008-2016 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos /* See README file in this directory for implementation notes, coding
21 1.1 christos conventions, et.al. */
22 1.1 christos
23 1.1 christos #include "defs.h"
24 1.1 christos #include "block.h"
25 1.1 christos #include "frame.h"
26 1.1 christos #include "symtab.h"
27 1.1 christos #include "objfiles.h"
28 1.1 christos #include "value.h"
29 1.1 christos #include "guile-internal.h"
30 1.1 christos
31 1.1 christos /* The <gdb:symbol> smob. */
32 1.1 christos
33 1.1 christos typedef struct
34 1.1 christos {
35 1.1 christos /* This always appears first. */
36 1.1 christos eqable_gdb_smob base;
37 1.1 christos
38 1.1 christos /* The GDB symbol structure this smob is wrapping. */
39 1.1 christos struct symbol *symbol;
40 1.1 christos } symbol_smob;
41 1.1 christos
42 1.1 christos static const char symbol_smob_name[] = "gdb:symbol";
43 1.1 christos
44 1.1 christos /* The tag Guile knows the symbol smob by. */
45 1.1 christos static scm_t_bits symbol_smob_tag;
46 1.1 christos
47 1.1 christos /* Keywords used in argument passing. */
48 1.1 christos static SCM block_keyword;
49 1.1 christos static SCM domain_keyword;
50 1.1 christos static SCM frame_keyword;
51 1.1 christos
52 1.1 christos static const struct objfile_data *syscm_objfile_data_key;
53 1.1 christos static struct gdbarch_data *syscm_gdbarch_data_key;
54 1.1 christos
55 1.1 christos struct syscm_gdbarch_data
56 1.1 christos {
57 1.1 christos /* Hash table to implement eqable gdbarch symbols. */
58 1.1 christos htab_t htab;
59 1.1 christos };
60 1.1 christos
61 1.1 christos /* Administrivia for symbol smobs. */
63 1.1 christos
64 1.1 christos /* Helper function to hash a symbol_smob. */
65 1.1 christos
66 1.1 christos static hashval_t
67 1.1 christos syscm_hash_symbol_smob (const void *p)
68 1.4 christos {
69 1.1 christos const symbol_smob *s_smob = (const symbol_smob *) p;
70 1.1 christos
71 1.1 christos return htab_hash_pointer (s_smob->symbol);
72 1.1 christos }
73 1.1 christos
74 1.1 christos /* Helper function to compute equality of symbol_smobs. */
75 1.1 christos
76 1.1 christos static int
77 1.1 christos syscm_eq_symbol_smob (const void *ap, const void *bp)
78 1.4 christos {
79 1.4 christos const symbol_smob *a = (const symbol_smob *) ap;
80 1.1 christos const symbol_smob *b = (const symbol_smob *) bp;
81 1.1 christos
82 1.1 christos return (a->symbol == b->symbol
83 1.1 christos && a->symbol != NULL);
84 1.1 christos }
85 1.1 christos
86 1.1 christos static void *
87 1.1 christos syscm_init_arch_symbols (struct gdbarch *gdbarch)
88 1.1 christos {
89 1.1 christos struct syscm_gdbarch_data *data
90 1.1 christos = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct syscm_gdbarch_data);
91 1.1 christos
92 1.1 christos data->htab = gdbscm_create_eqable_gsmob_ptr_map (syscm_hash_symbol_smob,
93 1.1 christos syscm_eq_symbol_smob);
94 1.1 christos return data;
95 1.1 christos }
96 1.1 christos
97 1.1 christos /* Return the struct symbol pointer -> SCM mapping table.
98 1.1 christos It is created if necessary. */
99 1.1 christos
100 1.1 christos static htab_t
101 1.1 christos syscm_get_symbol_map (struct symbol *symbol)
102 1.1 christos {
103 1.1 christos htab_t htab;
104 1.1 christos
105 1.1 christos if (SYMBOL_OBJFILE_OWNED (symbol))
106 1.1 christos {
107 1.1 christos struct objfile *objfile = symbol_objfile (symbol);
108 1.4 christos
109 1.1 christos htab = (htab_t) objfile_data (objfile, syscm_objfile_data_key);
110 1.1 christos if (htab == NULL)
111 1.1 christos {
112 1.1 christos htab = gdbscm_create_eqable_gsmob_ptr_map (syscm_hash_symbol_smob,
113 1.1 christos syscm_eq_symbol_smob);
114 1.1 christos set_objfile_data (objfile, syscm_objfile_data_key, htab);
115 1.1 christos }
116 1.1 christos }
117 1.1 christos else
118 1.1 christos {
119 1.4 christos struct gdbarch *gdbarch = symbol_arch (symbol);
120 1.4 christos struct syscm_gdbarch_data *data
121 1.1 christos = (struct syscm_gdbarch_data *) gdbarch_data (gdbarch,
122 1.1 christos syscm_gdbarch_data_key);
123 1.1 christos
124 1.1 christos htab = data->htab;
125 1.1 christos }
126 1.1 christos
127 1.1 christos return htab;
128 1.1 christos }
129 1.1 christos
130 1.1 christos /* The smob "free" function for <gdb:symbol>. */
131 1.1 christos
132 1.1 christos static size_t
133 1.1 christos syscm_free_symbol_smob (SCM self)
134 1.1 christos {
135 1.1 christos symbol_smob *s_smob = (symbol_smob *) SCM_SMOB_DATA (self);
136 1.1 christos
137 1.1 christos if (s_smob->symbol != NULL)
138 1.1 christos {
139 1.1 christos htab_t htab = syscm_get_symbol_map (s_smob->symbol);
140 1.1 christos
141 1.1 christos gdbscm_clear_eqable_gsmob_ptr_slot (htab, &s_smob->base);
142 1.1 christos }
143 1.1 christos
144 1.1 christos /* Not necessary, done to catch bugs. */
145 1.1 christos s_smob->symbol = NULL;
146 1.1 christos
147 1.1 christos return 0;
148 1.1 christos }
149 1.1 christos
150 1.1 christos /* The smob "print" function for <gdb:symbol>. */
151 1.1 christos
152 1.1 christos static int
153 1.1 christos syscm_print_symbol_smob (SCM self, SCM port, scm_print_state *pstate)
154 1.1 christos {
155 1.1 christos symbol_smob *s_smob = (symbol_smob *) SCM_SMOB_DATA (self);
156 1.1 christos
157 1.1 christos if (pstate->writingp)
158 1.1 christos gdbscm_printf (port, "#<%s ", symbol_smob_name);
159 1.1 christos gdbscm_printf (port, "%s",
160 1.1 christos s_smob->symbol != NULL
161 1.1 christos ? SYMBOL_PRINT_NAME (s_smob->symbol)
162 1.1 christos : "<invalid>");
163 1.1 christos if (pstate->writingp)
164 1.1 christos scm_puts (">", port);
165 1.1 christos
166 1.1 christos scm_remember_upto_here_1 (self);
167 1.1 christos
168 1.1 christos /* Non-zero means success. */
169 1.1 christos return 1;
170 1.1 christos }
171 1.1 christos
172 1.1 christos /* Low level routine to create a <gdb:symbol> object. */
173 1.1 christos
174 1.1 christos static SCM
175 1.1 christos syscm_make_symbol_smob (void)
176 1.1 christos {
177 1.1 christos symbol_smob *s_smob = (symbol_smob *)
178 1.1 christos scm_gc_malloc (sizeof (symbol_smob), symbol_smob_name);
179 1.1 christos SCM s_scm;
180 1.1 christos
181 1.1 christos s_smob->symbol = NULL;
182 1.1 christos s_scm = scm_new_smob (symbol_smob_tag, (scm_t_bits) s_smob);
183 1.1 christos gdbscm_init_eqable_gsmob (&s_smob->base, s_scm);
184 1.1 christos
185 1.1 christos return s_scm;
186 1.1 christos }
187 1.1 christos
188 1.1 christos /* Return non-zero if SCM is a symbol smob. */
189 1.1 christos
190 1.1 christos int
191 1.1 christos syscm_is_symbol (SCM scm)
192 1.1 christos {
193 1.1 christos return SCM_SMOB_PREDICATE (symbol_smob_tag, scm);
194 1.1 christos }
195 1.1 christos
196 1.1 christos /* (symbol? object) -> boolean */
197 1.1 christos
198 1.1 christos static SCM
199 1.1 christos gdbscm_symbol_p (SCM scm)
200 1.1 christos {
201 1.1 christos return scm_from_bool (syscm_is_symbol (scm));
202 1.1 christos }
203 1.1 christos
204 1.1 christos /* Return the existing object that encapsulates SYMBOL, or create a new
205 1.1 christos <gdb:symbol> object. */
206 1.1 christos
207 1.1 christos SCM
208 1.1 christos syscm_scm_from_symbol (struct symbol *symbol)
209 1.1 christos {
210 1.1 christos htab_t htab;
211 1.1 christos eqable_gdb_smob **slot;
212 1.1 christos symbol_smob *s_smob, s_smob_for_lookup;
213 1.1 christos SCM s_scm;
214 1.1 christos
215 1.1 christos /* If we've already created a gsmob for this symbol, return it.
216 1.1 christos This makes symbols eq?-able. */
217 1.1 christos htab = syscm_get_symbol_map (symbol);
218 1.1 christos s_smob_for_lookup.symbol = symbol;
219 1.1 christos slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &s_smob_for_lookup.base);
220 1.1 christos if (*slot != NULL)
221 1.1 christos return (*slot)->containing_scm;
222 1.1 christos
223 1.1 christos s_scm = syscm_make_symbol_smob ();
224 1.1 christos s_smob = (symbol_smob *) SCM_SMOB_DATA (s_scm);
225 1.1 christos s_smob->symbol = symbol;
226 1.1 christos gdbscm_fill_eqable_gsmob_ptr_slot (slot, &s_smob->base);
227 1.1 christos
228 1.1 christos return s_scm;
229 1.1 christos }
230 1.1 christos
231 1.1 christos /* Returns the <gdb:symbol> object in SELF.
232 1.1 christos Throws an exception if SELF is not a <gdb:symbol> object. */
233 1.1 christos
234 1.1 christos static SCM
235 1.1 christos syscm_get_symbol_arg_unsafe (SCM self, int arg_pos, const char *func_name)
236 1.1 christos {
237 1.1 christos SCM_ASSERT_TYPE (syscm_is_symbol (self), self, arg_pos, func_name,
238 1.1 christos symbol_smob_name);
239 1.1 christos
240 1.1 christos return self;
241 1.1 christos }
242 1.1 christos
243 1.1 christos /* Returns a pointer to the symbol smob of SELF.
244 1.1 christos Throws an exception if SELF is not a <gdb:symbol> object. */
245 1.1 christos
246 1.1 christos static symbol_smob *
247 1.1 christos syscm_get_symbol_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
248 1.1 christos {
249 1.1 christos SCM s_scm = syscm_get_symbol_arg_unsafe (self, arg_pos, func_name);
250 1.1 christos symbol_smob *s_smob = (symbol_smob *) SCM_SMOB_DATA (s_scm);
251 1.1 christos
252 1.1 christos return s_smob;
253 1.1 christos }
254 1.1 christos
255 1.1 christos /* Return non-zero if symbol S_SMOB is valid. */
256 1.1 christos
257 1.1 christos static int
258 1.1 christos syscm_is_valid (symbol_smob *s_smob)
259 1.1 christos {
260 1.1 christos return s_smob->symbol != NULL;
261 1.1 christos }
262 1.1 christos
263 1.1 christos /* Throw a Scheme error if SELF is not a valid symbol smob.
264 1.1 christos Otherwise return a pointer to the symbol smob. */
265 1.1 christos
266 1.1 christos static symbol_smob *
267 1.1 christos syscm_get_valid_symbol_smob_arg_unsafe (SCM self, int arg_pos,
268 1.1 christos const char *func_name)
269 1.1 christos {
270 1.1 christos symbol_smob *s_smob
271 1.1 christos = syscm_get_symbol_smob_arg_unsafe (self, arg_pos, func_name);
272 1.1 christos
273 1.1 christos if (!syscm_is_valid (s_smob))
274 1.1 christos {
275 1.1 christos gdbscm_invalid_object_error (func_name, arg_pos, self,
276 1.1 christos _("<gdb:symbol>"));
277 1.1 christos }
278 1.1 christos
279 1.1 christos return s_smob;
280 1.1 christos }
281 1.1 christos
282 1.1 christos /* Throw a Scheme error if SELF is not a valid symbol smob.
283 1.1 christos Otherwise return a pointer to the symbol struct. */
284 1.1 christos
285 1.1 christos struct symbol *
286 1.1 christos syscm_get_valid_symbol_arg_unsafe (SCM self, int arg_pos,
287 1.1 christos const char *func_name)
288 1.1 christos {
289 1.1 christos symbol_smob *s_smob = syscm_get_valid_symbol_smob_arg_unsafe (self, arg_pos,
290 1.1 christos func_name);
291 1.1 christos
292 1.1 christos return s_smob->symbol;
293 1.1 christos }
294 1.1 christos
295 1.1 christos /* Helper function for syscm_del_objfile_symbols to mark the symbol
296 1.1 christos as invalid. */
297 1.1 christos
298 1.1 christos static int
299 1.1 christos syscm_mark_symbol_invalid (void **slot, void *info)
300 1.1 christos {
301 1.1 christos symbol_smob *s_smob = (symbol_smob *) *slot;
302 1.1 christos
303 1.1 christos s_smob->symbol = NULL;
304 1.1 christos return 1;
305 1.1 christos }
306 1.1 christos
307 1.1 christos /* This function is called when an objfile is about to be freed.
308 1.1 christos Invalidate the symbol as further actions on the symbol would result
309 1.1 christos in bad data. All access to s_smob->symbol should be gated by
310 1.1 christos syscm_get_valid_symbol_smob_arg_unsafe which will raise an exception on
311 1.1 christos invalid symbols. */
312 1.1 christos
313 1.1 christos static void
314 1.1 christos syscm_del_objfile_symbols (struct objfile *objfile, void *datum)
315 1.4 christos {
316 1.1 christos htab_t htab = (htab_t) datum;
317 1.1 christos
318 1.1 christos if (htab != NULL)
319 1.1 christos {
320 1.1 christos htab_traverse_noresize (htab, syscm_mark_symbol_invalid, NULL);
321 1.1 christos htab_delete (htab);
322 1.1 christos }
323 1.1 christos }
324 1.1 christos
325 1.1 christos /* Symbol methods. */
327 1.1 christos
328 1.1 christos /* (symbol-valid? <gdb:symbol>) -> boolean
329 1.1 christos Returns #t if SELF still exists in GDB. */
330 1.1 christos
331 1.1 christos static SCM
332 1.1 christos gdbscm_symbol_valid_p (SCM self)
333 1.1 christos {
334 1.1 christos symbol_smob *s_smob
335 1.1 christos = syscm_get_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
336 1.1 christos
337 1.1 christos return scm_from_bool (syscm_is_valid (s_smob));
338 1.1 christos }
339 1.1 christos
340 1.1 christos /* (symbol-type <gdb:symbol>) -> <gdb:type>
341 1.1 christos Return the type of SELF, or #f if SELF has no type. */
342 1.1 christos
343 1.1 christos static SCM
344 1.1 christos gdbscm_symbol_type (SCM self)
345 1.1 christos {
346 1.1 christos symbol_smob *s_smob
347 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
348 1.1 christos const struct symbol *symbol = s_smob->symbol;
349 1.1 christos
350 1.1 christos if (SYMBOL_TYPE (symbol) == NULL)
351 1.1 christos return SCM_BOOL_F;
352 1.1 christos
353 1.1 christos return tyscm_scm_from_type (SYMBOL_TYPE (symbol));
354 1.1 christos }
355 1.1 christos
356 1.1 christos /* (symbol-symtab <gdb:symbol>) -> <gdb:symtab> | #f
357 1.1 christos Return the symbol table of SELF.
358 1.1 christos If SELF does not have a symtab (it is arch-owned) return #f. */
359 1.1 christos
360 1.1 christos static SCM
361 1.1 christos gdbscm_symbol_symtab (SCM self)
362 1.1 christos {
363 1.1 christos symbol_smob *s_smob
364 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
365 1.1 christos const struct symbol *symbol = s_smob->symbol;
366 1.1 christos
367 1.1 christos if (!SYMBOL_OBJFILE_OWNED (symbol))
368 1.1 christos return SCM_BOOL_F;
369 1.1 christos return stscm_scm_from_symtab (symbol_symtab (symbol));
370 1.1 christos }
371 1.1 christos
372 1.1 christos /* (symbol-name <gdb:symbol>) -> string */
373 1.1 christos
374 1.1 christos static SCM
375 1.1 christos gdbscm_symbol_name (SCM self)
376 1.1 christos {
377 1.1 christos symbol_smob *s_smob
378 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
379 1.1 christos const struct symbol *symbol = s_smob->symbol;
380 1.1 christos
381 1.1 christos return gdbscm_scm_from_c_string (SYMBOL_NATURAL_NAME (symbol));
382 1.1 christos }
383 1.1 christos
384 1.1 christos /* (symbol-linkage-name <gdb:symbol>) -> string */
385 1.1 christos
386 1.1 christos static SCM
387 1.1 christos gdbscm_symbol_linkage_name (SCM self)
388 1.1 christos {
389 1.1 christos symbol_smob *s_smob
390 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
391 1.1 christos const struct symbol *symbol = s_smob->symbol;
392 1.1 christos
393 1.1 christos return gdbscm_scm_from_c_string (SYMBOL_LINKAGE_NAME (symbol));
394 1.1 christos }
395 1.1 christos
396 1.1 christos /* (symbol-print-name <gdb:symbol>) -> string */
397 1.1 christos
398 1.1 christos static SCM
399 1.1 christos gdbscm_symbol_print_name (SCM self)
400 1.1 christos {
401 1.1 christos symbol_smob *s_smob
402 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
403 1.1 christos const struct symbol *symbol = s_smob->symbol;
404 1.1 christos
405 1.1 christos return gdbscm_scm_from_c_string (SYMBOL_PRINT_NAME (symbol));
406 1.1 christos }
407 1.1 christos
408 1.1 christos /* (symbol-addr-class <gdb:symbol>) -> integer */
409 1.1 christos
410 1.1 christos static SCM
411 1.1 christos gdbscm_symbol_addr_class (SCM self)
412 1.1 christos {
413 1.1 christos symbol_smob *s_smob
414 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
415 1.1 christos const struct symbol *symbol = s_smob->symbol;
416 1.1 christos
417 1.1 christos return scm_from_int (SYMBOL_CLASS (symbol));
418 1.1 christos }
419 1.1 christos
420 1.1 christos /* (symbol-argument? <gdb:symbol>) -> boolean */
421 1.1 christos
422 1.1 christos static SCM
423 1.1 christos gdbscm_symbol_argument_p (SCM self)
424 1.1 christos {
425 1.1 christos symbol_smob *s_smob
426 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
427 1.1 christos const struct symbol *symbol = s_smob->symbol;
428 1.1 christos
429 1.1 christos return scm_from_bool (SYMBOL_IS_ARGUMENT (symbol));
430 1.1 christos }
431 1.1 christos
432 1.1 christos /* (symbol-constant? <gdb:symbol>) -> boolean */
433 1.1 christos
434 1.1 christos static SCM
435 1.1 christos gdbscm_symbol_constant_p (SCM self)
436 1.1 christos {
437 1.1 christos symbol_smob *s_smob
438 1.3 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
439 1.1 christos const struct symbol *symbol = s_smob->symbol;
440 1.3 christos enum address_class theclass;
441 1.1 christos
442 1.3 christos theclass = SYMBOL_CLASS (symbol);
443 1.1 christos
444 1.1 christos return scm_from_bool (theclass == LOC_CONST || theclass == LOC_CONST_BYTES);
445 1.1 christos }
446 1.1 christos
447 1.1 christos /* (symbol-function? <gdb:symbol>) -> boolean */
448 1.1 christos
449 1.1 christos static SCM
450 1.1 christos gdbscm_symbol_function_p (SCM self)
451 1.1 christos {
452 1.1 christos symbol_smob *s_smob
453 1.3 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
454 1.1 christos const struct symbol *symbol = s_smob->symbol;
455 1.3 christos enum address_class theclass;
456 1.1 christos
457 1.3 christos theclass = SYMBOL_CLASS (symbol);
458 1.1 christos
459 1.1 christos return scm_from_bool (theclass == LOC_BLOCK);
460 1.1 christos }
461 1.1 christos
462 1.1 christos /* (symbol-variable? <gdb:symbol>) -> boolean */
463 1.1 christos
464 1.1 christos static SCM
465 1.1 christos gdbscm_symbol_variable_p (SCM self)
466 1.1 christos {
467 1.1 christos symbol_smob *s_smob
468 1.3 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
469 1.1 christos const struct symbol *symbol = s_smob->symbol;
470 1.3 christos enum address_class theclass;
471 1.1 christos
472 1.1 christos theclass = SYMBOL_CLASS (symbol);
473 1.3 christos
474 1.3 christos return scm_from_bool (!SYMBOL_IS_ARGUMENT (symbol)
475 1.3 christos && (theclass == LOC_LOCAL || theclass == LOC_REGISTER
476 1.1 christos || theclass == LOC_STATIC || theclass == LOC_COMPUTED
477 1.1 christos || theclass == LOC_OPTIMIZED_OUT));
478 1.1 christos }
479 1.1 christos
480 1.1 christos /* (symbol-needs-frame? <gdb:symbol>) -> boolean
481 1.1 christos Return #t if the symbol needs a frame for evaluation. */
482 1.1 christos
483 1.1 christos static SCM
484 1.1 christos gdbscm_symbol_needs_frame_p (SCM self)
485 1.1 christos {
486 1.1 christos symbol_smob *s_smob
487 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
488 1.1 christos struct symbol *symbol = s_smob->symbol;
489 1.3 christos int result = 0;
490 1.1 christos
491 1.1 christos TRY
492 1.1 christos {
493 1.3 christos result = symbol_read_needs_frame (symbol);
494 1.3 christos }
495 1.3 christos CATCH (except, RETURN_MASK_ALL)
496 1.3 christos {
497 1.3 christos GDBSCM_HANDLE_GDB_EXCEPTION (except);
498 1.1 christos }
499 1.1 christos END_CATCH
500 1.1 christos
501 1.1 christos return scm_from_bool (result);
502 1.1 christos }
503 1.1 christos
504 1.1 christos /* (symbol-line <gdb:symbol>) -> integer
505 1.1 christos Return the line number at which the symbol was defined. */
506 1.1 christos
507 1.1 christos static SCM
508 1.1 christos gdbscm_symbol_line (SCM self)
509 1.1 christos {
510 1.1 christos symbol_smob *s_smob
511 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
512 1.1 christos const struct symbol *symbol = s_smob->symbol;
513 1.1 christos
514 1.1 christos return scm_from_int (SYMBOL_LINE (symbol));
515 1.1 christos }
516 1.1 christos
517 1.1 christos /* (symbol-value <gdb:symbol> [#:frame <gdb:frame>]) -> <gdb:value>
518 1.1 christos Return the value of the symbol, or an error in various circumstances. */
519 1.1 christos
520 1.1 christos static SCM
521 1.1 christos gdbscm_symbol_value (SCM self, SCM rest)
522 1.1 christos {
523 1.1 christos symbol_smob *s_smob
524 1.1 christos = syscm_get_valid_symbol_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
525 1.1 christos struct symbol *symbol = s_smob->symbol;
526 1.1 christos SCM keywords[] = { frame_keyword, SCM_BOOL_F };
527 1.1 christos int frame_pos = -1;
528 1.1 christos SCM frame_scm = SCM_BOOL_F;
529 1.1 christos frame_smob *f_smob = NULL;
530 1.1 christos struct frame_info *frame_info = NULL;
531 1.1 christos struct value *value = NULL;
532 1.1 christos
533 1.1 christos gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#O",
534 1.1 christos rest, &frame_pos, &frame_scm);
535 1.1 christos if (!gdbscm_is_false (frame_scm))
536 1.1 christos f_smob = frscm_get_frame_smob_arg_unsafe (frame_scm, frame_pos, FUNC_NAME);
537 1.1 christos
538 1.1 christos if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
539 1.1 christos {
540 1.1 christos gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
541 1.1 christos _("cannot get the value of a typedef"));
542 1.3 christos }
543 1.1 christos
544 1.1 christos TRY
545 1.1 christos {
546 1.1 christos if (f_smob != NULL)
547 1.1 christos {
548 1.1 christos frame_info = frscm_frame_smob_to_frame (f_smob);
549 1.1 christos if (frame_info == NULL)
550 1.1 christos error (_("Invalid frame"));
551 1.1 christos }
552 1.1 christos
553 1.1 christos if (symbol_read_needs_frame (symbol) && frame_info == NULL)
554 1.4 christos error (_("Symbol requires a frame to compute its value"));
555 1.4 christos
556 1.4 christos /* TODO: currently, we have no way to recover the block in which SYMBOL
557 1.4 christos was found, so we have no block to pass to read_var_value. This will
558 1.4 christos yield an incorrect value when symbol is not local to FRAME_INFO (this
559 1.1 christos can happen with nested functions). */
560 1.3 christos value = read_var_value (symbol, NULL, frame_info);
561 1.3 christos }
562 1.3 christos CATCH (except, RETURN_MASK_ALL)
563 1.3 christos {
564 1.3 christos GDBSCM_HANDLE_GDB_EXCEPTION (except);
565 1.1 christos }
566 1.1 christos END_CATCH
567 1.1 christos
568 1.1 christos return vlscm_scm_from_value (value);
569 1.1 christos }
570 1.1 christos
571 1.1 christos /* (lookup-symbol name [#:block <gdb:block>] [#:domain domain])
573 1.1 christos -> (<gdb:symbol> field-of-this?)
574 1.1 christos The result is #f if the symbol is not found.
575 1.1 christos See comment in lookup_symbol_in_language for field-of-this?. */
576 1.1 christos
577 1.1 christos static SCM
578 1.1 christos gdbscm_lookup_symbol (SCM name_scm, SCM rest)
579 1.1 christos {
580 1.1 christos char *name;
581 1.1 christos SCM keywords[] = { block_keyword, domain_keyword, SCM_BOOL_F };
582 1.1 christos const struct block *block = NULL;
583 1.1 christos SCM block_scm = SCM_BOOL_F;
584 1.1 christos int domain = VAR_DOMAIN;
585 1.1 christos int block_arg_pos = -1, domain_arg_pos = -1;
586 1.3 christos struct field_of_this_result is_a_field_of_this;
587 1.1 christos struct symbol *symbol = NULL;
588 1.1 christos struct cleanup *cleanups;
589 1.1 christos struct gdb_exception except = exception_none;
590 1.1 christos
591 1.1 christos gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#Oi",
592 1.1 christos name_scm, &name, rest,
593 1.1 christos &block_arg_pos, &block_scm,
594 1.1 christos &domain_arg_pos, &domain);
595 1.1 christos
596 1.1 christos cleanups = make_cleanup (xfree, name);
597 1.1 christos
598 1.1 christos if (block_arg_pos >= 0)
599 1.1 christos {
600 1.1 christos SCM except_scm;
601 1.1 christos
602 1.1 christos block = bkscm_scm_to_block (block_scm, block_arg_pos, FUNC_NAME,
603 1.1 christos &except_scm);
604 1.1 christos if (block == NULL)
605 1.1 christos {
606 1.1 christos do_cleanups (cleanups);
607 1.1 christos gdbscm_throw (except_scm);
608 1.1 christos }
609 1.1 christos }
610 1.1 christos else
611 1.3 christos {
612 1.1 christos struct frame_info *selected_frame;
613 1.1 christos
614 1.1 christos TRY
615 1.1 christos {
616 1.3 christos selected_frame = get_selected_frame (_("no frame selected"));
617 1.3 christos block = get_frame_block (selected_frame, NULL);
618 1.3 christos }
619 1.3 christos CATCH (except, RETURN_MASK_ALL)
620 1.3 christos {
621 1.1 christos GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
622 1.1 christos }
623 1.3 christos END_CATCH
624 1.1 christos }
625 1.4 christos
626 1.4 christos TRY
627 1.1 christos {
628 1.3 christos symbol = lookup_symbol (name, block, (domain_enum) domain,
629 1.3 christos &is_a_field_of_this).symbol;
630 1.3 christos }
631 1.3 christos CATCH (ex, RETURN_MASK_ALL)
632 1.3 christos {
633 1.3 christos except = ex;
634 1.1 christos }
635 1.1 christos END_CATCH
636 1.1 christos
637 1.1 christos do_cleanups (cleanups);
638 1.1 christos GDBSCM_HANDLE_GDB_EXCEPTION (except);
639 1.1 christos
640 1.1 christos if (symbol == NULL)
641 1.1 christos return SCM_BOOL_F;
642 1.1 christos
643 1.1 christos return scm_list_2 (syscm_scm_from_symbol (symbol),
644 1.1 christos scm_from_bool (is_a_field_of_this.type != NULL));
645 1.1 christos }
646 1.1 christos
647 1.1 christos /* (lookup-global-symbol name [#:domain domain]) -> <gdb:symbol>
648 1.1 christos The result is #f if the symbol is not found. */
649 1.1 christos
650 1.1 christos static SCM
651 1.1 christos gdbscm_lookup_global_symbol (SCM name_scm, SCM rest)
652 1.1 christos {
653 1.1 christos char *name;
654 1.1 christos SCM keywords[] = { domain_keyword, SCM_BOOL_F };
655 1.1 christos int domain_arg_pos = -1;
656 1.3 christos int domain = VAR_DOMAIN;
657 1.1 christos struct symbol *symbol = NULL;
658 1.1 christos struct cleanup *cleanups;
659 1.1 christos struct gdb_exception except = exception_none;
660 1.1 christos
661 1.1 christos gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#i",
662 1.1 christos name_scm, &name, rest,
663 1.1 christos &domain_arg_pos, &domain);
664 1.3 christos
665 1.1 christos cleanups = make_cleanup (xfree, name);
666 1.4 christos
667 1.1 christos TRY
668 1.3 christos {
669 1.3 christos symbol = lookup_global_symbol (name, NULL, (domain_enum) domain).symbol;
670 1.3 christos }
671 1.3 christos CATCH (ex, RETURN_MASK_ALL)
672 1.3 christos {
673 1.3 christos except = ex;
674 1.1 christos }
675 1.1 christos END_CATCH
676 1.1 christos
677 1.1 christos do_cleanups (cleanups);
678 1.1 christos GDBSCM_HANDLE_GDB_EXCEPTION (except);
679 1.1 christos
680 1.1 christos if (symbol == NULL)
681 1.1 christos return SCM_BOOL_F;
682 1.1 christos
683 1.1 christos return syscm_scm_from_symbol (symbol);
684 1.1 christos }
685 1.1 christos
686 1.1 christos /* Initialize the Scheme symbol support. */
688 1.1 christos
689 1.1 christos /* Note: The SYMBOL_ prefix on the integer constants here is present for
690 1.1 christos compatibility with the Python support. */
691 1.1 christos
692 1.1 christos static const scheme_integer_constant symbol_integer_constants[] =
693 1.1 christos {
694 1.1 christos #define X(SYM) { "SYMBOL_" #SYM, SYM }
695 1.1 christos X (LOC_UNDEF),
696 1.1 christos X (LOC_CONST),
697 1.1 christos X (LOC_STATIC),
698 1.1 christos X (LOC_REGISTER),
699 1.1 christos X (LOC_ARG),
700 1.1 christos X (LOC_REF_ARG),
701 1.1 christos X (LOC_LOCAL),
702 1.1 christos X (LOC_TYPEDEF),
703 1.1 christos X (LOC_LABEL),
704 1.1 christos X (LOC_BLOCK),
705 1.1 christos X (LOC_CONST_BYTES),
706 1.1 christos X (LOC_UNRESOLVED),
707 1.1 christos X (LOC_OPTIMIZED_OUT),
708 1.1 christos X (LOC_COMPUTED),
709 1.1 christos X (LOC_REGPARM_ADDR),
710 1.1 christos
711 1.1 christos X (UNDEF_DOMAIN),
712 1.1 christos X (VAR_DOMAIN),
713 1.1 christos X (STRUCT_DOMAIN),
714 1.1 christos X (LABEL_DOMAIN),
715 1.1 christos X (VARIABLES_DOMAIN),
716 1.1 christos X (FUNCTIONS_DOMAIN),
717 1.1 christos X (TYPES_DOMAIN),
718 1.1 christos #undef X
719 1.1 christos
720 1.1 christos END_INTEGER_CONSTANTS
721 1.4 christos };
722 1.1 christos
723 1.1 christos static const scheme_function symbol_functions[] =
724 1.1 christos {
725 1.4 christos { "symbol?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_p),
726 1.1 christos "\
727 1.1 christos Return #t if the object is a <gdb:symbol> object." },
728 1.1 christos
729 1.1 christos { "symbol-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_valid_p),
730 1.1 christos "\
731 1.4 christos Return #t if object is a valid <gdb:symbol> object.\n\
732 1.1 christos A valid symbol is a symbol that has not been freed.\n\
733 1.1 christos Symbols are freed when the objfile they come from is freed." },
734 1.1 christos
735 1.4 christos { "symbol-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_type),
736 1.1 christos "\
737 1.1 christos Return the type of symbol." },
738 1.1 christos
739 1.4 christos { "symbol-symtab", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_symtab),
740 1.1 christos "\
741 1.1 christos Return the symbol table (<gdb:symtab>) containing symbol." },
742 1.1 christos
743 1.4 christos { "symbol-line", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_line),
744 1.1 christos "\
745 1.1 christos Return the line number at which the symbol was defined." },
746 1.1 christos
747 1.4 christos { "symbol-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_name),
748 1.4 christos "\
749 1.1 christos Return the name of the symbol as a string." },
750 1.1 christos
751 1.1 christos { "symbol-linkage-name", 1, 0, 0,
752 1.4 christos as_a_scm_t_subr (gdbscm_symbol_linkage_name),
753 1.1 christos "\
754 1.1 christos Return the linkage name of the symbol as a string." },
755 1.1 christos
756 1.1 christos { "symbol-print-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_print_name),
757 1.1 christos "\
758 1.4 christos Return the print name of the symbol as a string.\n\
759 1.1 christos This is either name or linkage-name, depending on whether the user\n\
760 1.1 christos asked GDB to display demangled or mangled names." },
761 1.1 christos
762 1.4 christos { "symbol-addr-class", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_addr_class),
763 1.4 christos "\
764 1.1 christos Return the address class of the symbol." },
765 1.1 christos
766 1.1 christos { "symbol-needs-frame?", 1, 0, 0,
767 1.4 christos as_a_scm_t_subr (gdbscm_symbol_needs_frame_p),
768 1.1 christos "\
769 1.1 christos Return #t if the symbol needs a frame to compute its value." },
770 1.1 christos
771 1.4 christos { "symbol-argument?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_argument_p),
772 1.1 christos "\
773 1.1 christos Return #t if the symbol is a function argument." },
774 1.1 christos
775 1.4 christos { "symbol-constant?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_constant_p),
776 1.1 christos "\
777 1.1 christos Return #t if the symbol is a constant." },
778 1.1 christos
779 1.4 christos { "symbol-function?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_function_p),
780 1.1 christos "\
781 1.1 christos Return #t if the symbol is a function." },
782 1.1 christos
783 1.4 christos { "symbol-variable?", 1, 0, 0, as_a_scm_t_subr (gdbscm_symbol_variable_p),
784 1.1 christos "\
785 1.1 christos Return #t if the symbol is a variable." },
786 1.1 christos
787 1.1 christos { "symbol-value", 1, 0, 1, as_a_scm_t_subr (gdbscm_symbol_value),
788 1.1 christos "\
789 1.4 christos Return the value of the symbol.\n\
790 1.1 christos \n\
791 1.1 christos Arguments: <gdb:symbol> [#:frame frame]" },
792 1.1 christos
793 1.1 christos { "lookup-symbol", 1, 0, 1, as_a_scm_t_subr (gdbscm_lookup_symbol),
794 1.1 christos "\
795 1.1 christos Return (<gdb:symbol> field-of-this?) if found, otherwise #f.\n\
796 1.1 christos \n\
797 1.1 christos Arguments: name [#:block block] [#:domain domain]\n\
798 1.4 christos name: a string containing the name of the symbol to lookup\n\
799 1.4 christos block: a <gdb:block> object\n\
800 1.1 christos domain: a SYMBOL_*_DOMAIN value" },
801 1.1 christos
802 1.1 christos { "lookup-global-symbol", 1, 0, 1,
803 1.1 christos as_a_scm_t_subr (gdbscm_lookup_global_symbol),
804 1.1 christos "\
805 1.1 christos Return <gdb:symbol> if found, otherwise #f.\n\
806 1.1 christos \n\
807 1.1 christos Arguments: name [#:domain domain]\n\
808 1.1 christos name: a string containing the name of the symbol to lookup\n\
809 1.1 christos domain: a SYMBOL_*_DOMAIN value" },
810 1.1 christos
811 1.1 christos END_FUNCTIONS
812 1.1 christos };
813 1.1 christos
814 1.1 christos void
815 1.1 christos gdbscm_initialize_symbols (void)
816 1.1 christos {
817 1.1 christos symbol_smob_tag
818 1.1 christos = gdbscm_make_smob_type (symbol_smob_name, sizeof (symbol_smob));
819 1.1 christos scm_set_smob_free (symbol_smob_tag, syscm_free_symbol_smob);
820 1.1 christos scm_set_smob_print (symbol_smob_tag, syscm_print_symbol_smob);
821 1.1 christos
822 1.1 christos gdbscm_define_integer_constants (symbol_integer_constants, 1);
823 1.1 christos gdbscm_define_functions (symbol_functions, 1);
824 1.1 christos
825 1.1 christos block_keyword = scm_from_latin1_keyword ("block");
826 1.1 christos domain_keyword = scm_from_latin1_keyword ("domain");
827 1.1 christos frame_keyword = scm_from_latin1_keyword ("frame");
828 1.1 christos
829 1.1 christos /* Register an objfile "free" callback so we can properly
830 1.1 christos invalidate symbols when an object file is about to be deleted. */
831 1.1 christos syscm_objfile_data_key
832 1.1 christos = register_objfile_data_with_cleanup (NULL, syscm_del_objfile_symbols);
833 1.1 christos
834 /* Arch-specific symbol data. */
835 syscm_gdbarch_data_key
836 = gdbarch_data_register_post_init (syscm_init_arch_symbols);
837 }
838