1 /* Scheme interface to stack frames.
3 Copyright (C) 2008-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
31 #include "user-regs.h"
33 #include "guile-internal.h"
35 /* The <gdb:frame> smob. */
39 /* This always appears first. */
42 struct frame_id frame_id
;
43 struct gdbarch
*gdbarch
;
45 /* Frames are tracked by inferior.
46 We need some place to put the eq?-able hash table, and this feels as
47 good a place as any. Frames in one inferior shouldn't be considered
48 equal to frames in a different inferior. The frame becomes invalid if
49 this becomes NULL (the inferior has been deleted from gdb).
50 It's easier to relax restrictions than impose them after the fact.
51 N.B. It is an outstanding question whether a frame survives reruns of
52 the inferior. Intuitively the answer is "No", but currently a frame
53 also survives, e.g., multiple invocations of the same function from
54 the same point. Even different threads can have the same frame, e.g.,
55 if a thread dies and a new thread gets the same stack. */
56 struct inferior
*inferior
;
58 /* Marks that the FRAME_ID member actually holds the ID of the frame next
59 to this, and not this frame's ID itself. This is a hack to permit Scheme
60 frame objects which represent invalid frames (i.e., the last frame_info
61 in a corrupt stack). The problem arises from the fact that this code
62 relies on FRAME_ID to uniquely identify a frame, which is not always true
63 for the last "frame" in a corrupt stack (it can have a null ID, or the
64 same ID as the previous frame). Whenever get_prev_frame returns NULL, we
65 record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1. */
69 static const char frame_smob_name
[] = "gdb:frame";
71 /* The tag Guile knows the frame smob by. */
72 static scm_t_bits frame_smob_tag
;
74 /* Keywords used in argument passing. */
75 static SCM block_keyword
;
77 static const struct inferior_data
*frscm_inferior_data_key
;
79 /* Administrivia for frame smobs. */
81 /* Helper function to hash a frame_smob. */
84 frscm_hash_frame_smob (const void *p
)
86 const frame_smob
*f_smob
= (const frame_smob
*) p
;
87 const struct frame_id
*fid
= &f_smob
->frame_id
;
88 hashval_t hash
= htab_hash_pointer (f_smob
->inferior
);
90 if (fid
->stack_status
== FID_STACK_VALID
)
91 hash
= iterative_hash (&fid
->stack_addr
, sizeof (fid
->stack_addr
), hash
);
93 hash
= iterative_hash (&fid
->code_addr
, sizeof (fid
->code_addr
), hash
);
94 if (fid
->special_addr_p
)
95 hash
= iterative_hash (&fid
->special_addr
, sizeof (fid
->special_addr
),
101 /* Helper function to compute equality of frame_smobs. */
104 frscm_eq_frame_smob (const void *ap
, const void *bp
)
106 const frame_smob
*a
= (const frame_smob
*) ap
;
107 const frame_smob
*b
= (const frame_smob
*) bp
;
109 return (frame_id_eq (a
->frame_id
, b
->frame_id
)
110 && a
->inferior
== b
->inferior
111 && a
->inferior
!= NULL
);
114 /* Return the frame -> SCM mapping table.
115 It is created if necessary. */
118 frscm_inferior_frame_map (struct inferior
*inferior
)
120 htab_t htab
= (htab_t
) inferior_data (inferior
, frscm_inferior_data_key
);
124 htab
= gdbscm_create_eqable_gsmob_ptr_map (frscm_hash_frame_smob
,
125 frscm_eq_frame_smob
);
126 set_inferior_data (inferior
, frscm_inferior_data_key
, htab
);
132 /* The smob "free" function for <gdb:frame>. */
135 frscm_free_frame_smob (SCM self
)
137 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (self
);
139 if (f_smob
->inferior
!= NULL
)
141 htab_t htab
= frscm_inferior_frame_map (f_smob
->inferior
);
143 gdbscm_clear_eqable_gsmob_ptr_slot (htab
, &f_smob
->base
);
146 /* Not necessary, done to catch bugs. */
147 f_smob
->inferior
= NULL
;
152 /* The smob "print" function for <gdb:frame>. */
155 frscm_print_frame_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
157 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (self
);
159 gdbscm_printf (port
, "#<%s %s>",
161 f_smob
->frame_id
.to_string ().c_str ());
162 scm_remember_upto_here_1 (self
);
164 /* Non-zero means success. */
168 /* Low level routine to create a <gdb:frame> object. */
171 frscm_make_frame_smob (void)
173 frame_smob
*f_smob
= (frame_smob
*)
174 scm_gc_malloc (sizeof (frame_smob
), frame_smob_name
);
177 f_smob
->frame_id
= null_frame_id
;
178 f_smob
->gdbarch
= NULL
;
179 f_smob
->inferior
= NULL
;
180 f_smob
->frame_id_is_next
= 0;
181 f_scm
= scm_new_smob (frame_smob_tag
, (scm_t_bits
) f_smob
);
182 gdbscm_init_eqable_gsmob (&f_smob
->base
, f_scm
);
187 /* Return non-zero if SCM is a <gdb:frame> object. */
190 frscm_is_frame (SCM scm
)
192 return SCM_SMOB_PREDICATE (frame_smob_tag
, scm
);
195 /* (frame? object) -> boolean */
198 gdbscm_frame_p (SCM scm
)
200 return scm_from_bool (frscm_is_frame (scm
));
203 /* Create a new <gdb:frame> object that encapsulates FRAME.
204 Returns a <gdb:exception> object if there is an error. */
207 frscm_scm_from_frame (struct frame_info
*frame
, struct inferior
*inferior
)
209 frame_smob
*f_smob
, f_smob_for_lookup
;
212 eqable_gdb_smob
**slot
;
213 struct frame_id frame_id
= null_frame_id
;
214 struct gdbarch
*gdbarch
= NULL
;
215 int frame_id_is_next
= 0;
217 /* If we've already created a gsmob for this frame, return it.
218 This makes frames eq?-able. */
219 htab
= frscm_inferior_frame_map (inferior
);
220 f_smob_for_lookup
.frame_id
= get_frame_id (frame
);
221 f_smob_for_lookup
.inferior
= inferior
;
222 slot
= gdbscm_find_eqable_gsmob_ptr_slot (htab
, &f_smob_for_lookup
.base
);
224 return (*slot
)->containing_scm
;
228 /* Try to get the previous frame, to determine if this is the last frame
229 in a corrupt stack. If so, we need to store the frame_id of the next
230 frame and not of this one (which is possibly invalid). */
231 if (get_prev_frame (frame
) == NULL
232 && get_frame_unwind_stop_reason (frame
) != UNWIND_NO_REASON
233 && get_next_frame (frame
) != NULL
)
235 frame_id
= get_frame_id (get_next_frame (frame
));
236 frame_id_is_next
= 1;
240 frame_id
= get_frame_id (frame
);
241 frame_id_is_next
= 0;
243 gdbarch
= get_frame_arch (frame
);
245 catch (const gdb_exception
&except
)
247 return gdbscm_scm_from_gdb_exception (unpack (except
));
250 f_scm
= frscm_make_frame_smob ();
251 f_smob
= (frame_smob
*) SCM_SMOB_DATA (f_scm
);
252 f_smob
->frame_id
= frame_id
;
253 f_smob
->gdbarch
= gdbarch
;
254 f_smob
->inferior
= inferior
;
255 f_smob
->frame_id_is_next
= frame_id_is_next
;
257 gdbscm_fill_eqable_gsmob_ptr_slot (slot
, &f_smob
->base
);
262 /* Create a new <gdb:frame> object that encapsulates FRAME.
263 A Scheme exception is thrown if there is an error. */
266 frscm_scm_from_frame_unsafe (struct frame_info
*frame
,
267 struct inferior
*inferior
)
269 SCM f_scm
= frscm_scm_from_frame (frame
, inferior
);
271 if (gdbscm_is_exception (f_scm
))
272 gdbscm_throw (f_scm
);
277 /* Returns the <gdb:frame> object in SELF.
278 Throws an exception if SELF is not a <gdb:frame> object. */
281 frscm_get_frame_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
283 SCM_ASSERT_TYPE (frscm_is_frame (self
), self
, arg_pos
, func_name
,
289 /* There is no gdbscm_scm_to_frame function because translating
290 a frame SCM object to a struct frame_info * can throw a GDB error.
291 Thus code working with frames has to handle both Scheme errors (e.g., the
292 object is not a frame) and GDB errors (e.g., the frame lookup failed).
294 To help keep things clear we split what would be gdbscm_scm_to_frame
297 frscm_get_frame_smob_arg_unsafe
298 - throws a Scheme error if object is not a frame,
299 or if the inferior is gone or is no longer current
301 frscm_frame_smob_to_frame
302 - may throw a gdb error if the conversion fails
303 - it's not clear when it will and won't throw a GDB error,
304 but for robustness' sake we assume that whenever we call out to GDB
305 a GDB error may get thrown (and thus the call must be wrapped in a
308 /* Returns the frame_smob for the object wrapped by FRAME_SCM.
309 A Scheme error is thrown if FRAME_SCM is not a frame. */
312 frscm_get_frame_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
314 SCM f_scm
= frscm_get_frame_arg_unsafe (self
, arg_pos
, func_name
);
315 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (f_scm
);
317 if (f_smob
->inferior
== NULL
)
319 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
322 if (f_smob
->inferior
!= current_inferior ())
323 scm_misc_error (func_name
, _("inferior has changed"), SCM_EOL
);
328 /* Returns the frame_info object wrapped by F_SMOB.
329 If the frame doesn't exist anymore (the frame id doesn't
330 correspond to any frame in the inferior), returns NULL.
331 This function calls GDB routines, so don't assume a GDB error will
335 frscm_frame_smob_to_frame (frame_smob
*f_smob
)
337 struct frame_info
*frame
;
339 frame
= frame_find_by_id (f_smob
->frame_id
);
343 if (f_smob
->frame_id_is_next
)
344 frame
= get_prev_frame (frame
);
349 /* Helper function for frscm_del_inferior_frames to mark the frame
353 frscm_mark_frame_invalid (void **slot
, void *info
)
355 frame_smob
*f_smob
= (frame_smob
*) *slot
;
357 f_smob
->inferior
= NULL
;
361 /* This function is called when an inferior is about to be freed.
362 Invalidate the frame as further actions on the frame could result
363 in bad data. All access to the frame should be gated by
364 frscm_get_frame_smob_arg_unsafe which will raise an exception on
368 frscm_del_inferior_frames (struct inferior
*inferior
, void *datum
)
370 htab_t htab
= (htab_t
) datum
;
374 htab_traverse_noresize (htab
, frscm_mark_frame_invalid
, NULL
);
381 /* (frame-valid? <gdb:frame>) -> bool
382 Returns #t if the frame corresponding to the frame_id of this
383 object still exists in the inferior. */
386 gdbscm_frame_valid_p (SCM self
)
389 struct frame_info
*frame
= NULL
;
391 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
393 gdbscm_gdb_exception exc
{};
396 frame
= frscm_frame_smob_to_frame (f_smob
);
398 catch (const gdb_exception
&except
)
400 exc
= unpack (except
);
403 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
404 return scm_from_bool (frame
!= NULL
);
407 /* (frame-name <gdb:frame>) -> string
408 Returns the name of the function corresponding to this frame,
409 or #f if there is no function. */
412 gdbscm_frame_name (SCM self
)
415 gdb::unique_xmalloc_ptr
<char> name
;
416 enum language lang
= language_minimal
;
417 struct frame_info
*frame
= NULL
;
420 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
422 gdbscm_gdb_exception exc
{};
425 frame
= frscm_frame_smob_to_frame (f_smob
);
427 name
= find_frame_funname (frame
, &lang
, NULL
);
429 catch (const gdb_exception
&except
)
431 exc
= unpack (except
);
434 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
437 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
442 result
= gdbscm_scm_from_c_string (name
.get ());
449 /* (frame-type <gdb:frame>) -> integer
450 Returns the frame type, namely one of the gdb:*_FRAME constants. */
453 gdbscm_frame_type (SCM self
)
456 enum frame_type type
= NORMAL_FRAME
;
457 struct frame_info
*frame
= NULL
;
459 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
461 gdbscm_gdb_exception exc
{};
464 frame
= frscm_frame_smob_to_frame (f_smob
);
466 type
= get_frame_type (frame
);
468 catch (const gdb_exception
&except
)
470 exc
= unpack (except
);
473 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
476 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
480 return scm_from_int (type
);
483 /* (frame-arch <gdb:frame>) -> <gdb:architecture>
484 Returns the frame's architecture as a gdb:architecture object. */
487 gdbscm_frame_arch (SCM self
)
490 struct frame_info
*frame
= NULL
;
492 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
494 gdbscm_gdb_exception exc
{};
497 frame
= frscm_frame_smob_to_frame (f_smob
);
499 catch (const gdb_exception
&except
)
501 exc
= unpack (except
);
504 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
507 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
511 return arscm_scm_from_arch (f_smob
->gdbarch
);
514 /* (frame-unwind-stop-reason <gdb:frame>) -> integer
515 Returns one of the gdb:FRAME_UNWIND_* constants. */
518 gdbscm_frame_unwind_stop_reason (SCM self
)
521 struct frame_info
*frame
= NULL
;
522 enum unwind_stop_reason stop_reason
;
524 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
526 gdbscm_gdb_exception exc
{};
529 frame
= frscm_frame_smob_to_frame (f_smob
);
531 catch (const gdb_exception
&except
)
533 exc
= unpack (except
);
536 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
539 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
543 stop_reason
= get_frame_unwind_stop_reason (frame
);
545 return scm_from_int (stop_reason
);
548 /* (frame-pc <gdb:frame>) -> integer
549 Returns the frame's resume address. */
552 gdbscm_frame_pc (SCM self
)
556 struct frame_info
*frame
= NULL
;
558 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
560 gdbscm_gdb_exception exc
{};
563 frame
= frscm_frame_smob_to_frame (f_smob
);
565 pc
= get_frame_pc (frame
);
567 catch (const gdb_exception
&except
)
569 exc
= unpack (except
);
572 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
575 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
579 return gdbscm_scm_from_ulongest (pc
);
582 /* (frame-block <gdb:frame>) -> <gdb:block>
583 Returns the frame's code block, or #f if one cannot be found. */
586 gdbscm_frame_block (SCM self
)
589 const struct block
*block
= NULL
, *fn_block
;
590 struct frame_info
*frame
= NULL
;
592 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
594 gdbscm_gdb_exception exc
{};
597 frame
= frscm_frame_smob_to_frame (f_smob
);
599 block
= get_frame_block (frame
, NULL
);
601 catch (const gdb_exception
&except
)
603 exc
= unpack (except
);
606 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
609 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
613 for (fn_block
= block
;
614 fn_block
!= NULL
&& fn_block
->function () == NULL
;
615 fn_block
= fn_block
->superblock ())
618 if (block
== NULL
|| fn_block
== NULL
|| fn_block
->function () == NULL
)
620 scm_misc_error (FUNC_NAME
, _("cannot find block for frame"),
626 return bkscm_scm_from_block
627 (block
, fn_block
->function ()->objfile ());
633 /* (frame-function <gdb:frame>) -> <gdb:symbol>
634 Returns the symbol for the function corresponding to this frame,
635 or #f if there isn't one. */
638 gdbscm_frame_function (SCM self
)
641 struct symbol
*sym
= NULL
;
642 struct frame_info
*frame
= NULL
;
644 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
646 gdbscm_gdb_exception exc
{};
649 frame
= frscm_frame_smob_to_frame (f_smob
);
651 sym
= find_pc_function (get_frame_address_in_block (frame
));
653 catch (const gdb_exception
&except
)
655 exc
= unpack (except
);
658 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
661 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
666 return syscm_scm_from_symbol (sym
);
671 /* (frame-older <gdb:frame>) -> <gdb:frame>
672 Returns the frame immediately older (outer) to this frame,
673 or #f if there isn't one. */
676 gdbscm_frame_older (SCM self
)
679 struct frame_info
*prev
= NULL
;
680 struct frame_info
*frame
= NULL
;
682 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
684 gdbscm_gdb_exception exc
{};
687 frame
= frscm_frame_smob_to_frame (f_smob
);
689 prev
= get_prev_frame (frame
);
691 catch (const gdb_exception
&except
)
693 exc
= unpack (except
);
696 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
699 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
704 return frscm_scm_from_frame_unsafe (prev
, f_smob
->inferior
);
709 /* (frame-newer <gdb:frame>) -> <gdb:frame>
710 Returns the frame immediately newer (inner) to this frame,
711 or #f if there isn't one. */
714 gdbscm_frame_newer (SCM self
)
717 struct frame_info
*next
= NULL
;
718 struct frame_info
*frame
= NULL
;
720 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
722 gdbscm_gdb_exception exc
{};
725 frame
= frscm_frame_smob_to_frame (f_smob
);
727 next
= get_next_frame (frame
);
729 catch (const gdb_exception
&except
)
731 exc
= unpack (except
);
734 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
737 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
742 return frscm_scm_from_frame_unsafe (next
, f_smob
->inferior
);
747 /* (frame-sal <gdb:frame>) -> <gdb:sal>
748 Returns the frame's symtab and line. */
751 gdbscm_frame_sal (SCM self
)
754 struct symtab_and_line sal
;
755 struct frame_info
*frame
= NULL
;
757 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
759 gdbscm_gdb_exception exc
{};
762 frame
= frscm_frame_smob_to_frame (f_smob
);
764 sal
= find_frame_sal (frame
);
766 catch (const gdb_exception
&except
)
768 exc
= unpack (except
);
771 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
774 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
778 return stscm_scm_from_sal (sal
);
781 /* (frame-read-register <gdb:frame> string) -> <gdb:value>
782 The register argument must be a string. */
785 gdbscm_frame_read_register (SCM self
, SCM register_scm
)
788 struct value
*value
= NULL
;
789 struct frame_info
*frame
= NULL
;
792 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
793 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG2
, NULL
, "s",
794 register_scm
, ®ister_str
);
796 gdbscm_gdb_exception except
{};
802 frame
= frscm_frame_smob_to_frame (f_smob
);
805 regnum
= user_reg_map_name_to_regnum (get_frame_arch (frame
),
807 strlen (register_str
));
809 value
= value_of_register (regnum
, frame
);
812 catch (const gdb_exception
&ex
)
814 except
= unpack (ex
);
817 xfree (register_str
);
818 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
822 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
828 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, register_scm
,
829 _("unknown register"));
832 return vlscm_scm_from_value (value
);
835 /* (frame-read-var <gdb:frame> <gdb:symbol>) -> <gdb:value>
836 (frame-read-var <gdb:frame> string [#:block <gdb:block>]) -> <gdb:value>
837 If the optional block argument is provided start the search from that block,
838 otherwise search from the frame's current block (determined by examining
839 the resume address of the frame). The variable argument must be a string
840 or an instance of a <gdb:symbol>. The block argument must be an instance of
844 gdbscm_frame_read_var (SCM self
, SCM symbol_scm
, SCM rest
)
846 SCM keywords
[] = { block_keyword
, SCM_BOOL_F
};
848 int block_arg_pos
= -1;
849 SCM block_scm
= SCM_UNDEFINED
;
850 struct frame_info
*frame
= NULL
;
851 struct symbol
*var
= NULL
;
852 const struct block
*block
= NULL
;
853 struct value
*value
= NULL
;
855 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
857 gdbscm_gdb_exception exc
{};
860 frame
= frscm_frame_smob_to_frame (f_smob
);
862 catch (const gdb_exception
&except
)
864 exc
= unpack (except
);
867 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
870 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
874 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG3
, keywords
, "#O",
875 rest
, &block_arg_pos
, &block_scm
);
877 if (syscm_is_symbol (symbol_scm
))
879 var
= syscm_get_valid_symbol_arg_unsafe (symbol_scm
, SCM_ARG2
,
881 SCM_ASSERT (SCM_UNBNDP (block_scm
), block_scm
, SCM_ARG3
, FUNC_NAME
);
883 else if (scm_is_string (symbol_scm
))
885 gdbscm_gdb_exception except
{};
887 if (! SCM_UNBNDP (block_scm
))
891 gdb_assert (block_arg_pos
> 0);
892 block
= bkscm_scm_to_block (block_scm
, block_arg_pos
, FUNC_NAME
,
895 gdbscm_throw (except_scm
);
899 gdb::unique_xmalloc_ptr
<char> var_name
900 (gdbscm_scm_to_c_string (symbol_scm
));
901 /* N.B. Between here and the end of the scope, don't do anything
902 to cause a Scheme exception. */
906 struct block_symbol lookup_sym
;
909 block
= get_frame_block (frame
, NULL
);
910 lookup_sym
= lookup_symbol (var_name
.get (), block
, VAR_DOMAIN
,
912 var
= lookup_sym
.symbol
;
913 block
= lookup_sym
.block
;
915 catch (const gdb_exception
&ex
)
917 except
= unpack (ex
);
921 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
924 gdbscm_out_of_range_error (FUNC_NAME
, 0, symbol_scm
,
925 _("variable not found"));
929 /* Use SCM_ASSERT_TYPE for more consistent error messages. */
930 SCM_ASSERT_TYPE (0, symbol_scm
, SCM_ARG1
, FUNC_NAME
,
931 _("gdb:symbol or string"));
936 value
= read_var_value (var
, block
, frame
);
938 catch (const gdb_exception
&except
)
940 exc
= unpack (except
);
943 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
944 return vlscm_scm_from_value (value
);
947 /* (frame-select <gdb:frame>) -> unspecified
948 Select this frame. */
951 gdbscm_frame_select (SCM self
)
954 struct frame_info
*frame
= NULL
;
956 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
958 gdbscm_gdb_exception exc
{};
961 frame
= frscm_frame_smob_to_frame (f_smob
);
963 select_frame (frame
);
965 catch (const gdb_exception
&except
)
967 exc
= unpack (except
);
970 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
973 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
977 return SCM_UNSPECIFIED
;
980 /* (newest-frame) -> <gdb:frame>
981 Returns the newest frame. */
984 gdbscm_newest_frame (void)
986 struct frame_info
*frame
= NULL
;
988 gdbscm_gdb_exception exc
{};
991 frame
= get_current_frame ();
993 catch (const gdb_exception
&except
)
995 exc
= unpack (except
);
998 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
999 return frscm_scm_from_frame_unsafe (frame
, current_inferior ());
1002 /* (selected-frame) -> <gdb:frame>
1003 Returns the selected frame. */
1006 gdbscm_selected_frame (void)
1008 struct frame_info
*frame
= NULL
;
1010 gdbscm_gdb_exception exc
{};
1013 frame
= get_selected_frame (_("No frame is currently selected"));
1015 catch (const gdb_exception
&except
)
1017 exc
= unpack (except
);
1020 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1021 return frscm_scm_from_frame_unsafe (frame
, current_inferior ());
1024 /* (unwind-stop-reason-string integer) -> string
1025 Return a string explaining the unwind stop reason. */
1028 gdbscm_unwind_stop_reason_string (SCM reason_scm
)
1033 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, NULL
, "i",
1034 reason_scm
, &reason
);
1036 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
1037 scm_out_of_range (FUNC_NAME
, reason_scm
);
1039 str
= unwind_stop_reason_to_string ((enum unwind_stop_reason
) reason
);
1040 return gdbscm_scm_from_c_string (str
);
1043 /* Initialize the Scheme frame support. */
1045 static const scheme_integer_constant frame_integer_constants
[] =
1047 #define ENTRY(X) { #X, X }
1049 ENTRY (NORMAL_FRAME
),
1050 ENTRY (DUMMY_FRAME
),
1051 ENTRY (INLINE_FRAME
),
1052 ENTRY (TAILCALL_FRAME
),
1053 ENTRY (SIGTRAMP_FRAME
),
1055 ENTRY (SENTINEL_FRAME
),
1059 #define SET(name, description) \
1060 { "FRAME_" #name, name },
1061 #include "unwind_stop_reasons.def"
1064 END_INTEGER_CONSTANTS
1067 static const scheme_function frame_functions
[] =
1069 { "frame?", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_p
),
1071 Return #t if the object is a <gdb:frame> object." },
1073 { "frame-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_valid_p
),
1075 Return #t if the object is a valid <gdb:frame> object.\n\
1076 Frames become invalid when the inferior returns to its caller." },
1078 { "frame-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_name
),
1080 Return the name of the function corresponding to this frame,\n\
1081 or #f if there is no function." },
1083 { "frame-arch", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_arch
),
1085 Return the frame's architecture as a <gdb:arch> object." },
1087 { "frame-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_type
),
1089 Return the frame type, namely one of the gdb:*_FRAME constants." },
1091 { "frame-unwind-stop-reason", 1, 0, 0,
1092 as_a_scm_t_subr (gdbscm_frame_unwind_stop_reason
),
1094 Return one of the gdb:FRAME_UNWIND_* constants explaining why\n\
1095 it's not possible to find frames older than this." },
1097 { "frame-pc", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_pc
),
1099 Return the frame's resume address." },
1101 { "frame-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_block
),
1103 Return the frame's code block, or #f if one cannot be found." },
1105 { "frame-function", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_function
),
1107 Return the <gdb:symbol> for the function corresponding to this frame,\n\
1108 or #f if there isn't one." },
1110 { "frame-older", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_older
),
1112 Return the frame immediately older (outer) to this frame,\n\
1113 or #f if there isn't one." },
1115 { "frame-newer", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_newer
),
1117 Return the frame immediately newer (inner) to this frame,\n\
1118 or #f if there isn't one." },
1120 { "frame-sal", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_sal
),
1122 Return the frame's symtab-and-line <gdb:sal> object." },
1124 { "frame-read-var", 2, 0, 1, as_a_scm_t_subr (gdbscm_frame_read_var
),
1126 Return the value of the symbol in the frame.\n\
1128 Arguments: <gdb:frame> <gdb:symbol>\n\
1129 Or: <gdb:frame> string [#:block <gdb:block>]" },
1131 { "frame-read-register", 2, 0, 0,
1132 as_a_scm_t_subr (gdbscm_frame_read_register
),
1134 Return the value of the register in the frame.\n\
1136 Arguments: <gdb:frame> string" },
1138 { "frame-select", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_select
),
1140 Select this frame." },
1142 { "newest-frame", 0, 0, 0, as_a_scm_t_subr (gdbscm_newest_frame
),
1144 Return the newest frame." },
1146 { "selected-frame", 0, 0, 0, as_a_scm_t_subr (gdbscm_selected_frame
),
1148 Return the selected frame." },
1150 { "unwind-stop-reason-string", 1, 0, 0,
1151 as_a_scm_t_subr (gdbscm_unwind_stop_reason_string
),
1153 Return a string explaining the unwind stop reason.\n\
1155 Arguments: integer (the result of frame-unwind-stop-reason)" },
1161 gdbscm_initialize_frames (void)
1164 = gdbscm_make_smob_type (frame_smob_name
, sizeof (frame_smob
));
1165 scm_set_smob_free (frame_smob_tag
, frscm_free_frame_smob
);
1166 scm_set_smob_print (frame_smob_tag
, frscm_print_frame_smob
);
1168 gdbscm_define_integer_constants (frame_integer_constants
, 1);
1169 gdbscm_define_functions (frame_functions
, 1);
1171 block_keyword
= scm_from_latin1_keyword ("block");
1174 void _initialize_scm_frame ();
1176 _initialize_scm_frame ()
1178 /* Register an inferior "free" callback so we can properly
1179 invalidate frames when an inferior file is about to be deleted. */
1180 frscm_inferior_data_key
1181 = register_inferior_data_with_cleanup (NULL
, frscm_del_inferior_frames
);