1 /* Scheme interface to stack frames.
3 Copyright (C) 2008-2024 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 /* This is called when an inferior is about to be freed.
78 Invalidate the frame as further actions on the frame could result
79 in bad data. All access to the frame should be gated by
80 frscm_get_frame_smob_arg_unsafe which will raise an exception on
84 /* Helper function for frscm_del_inferior_frames to mark the frame
88 frscm_mark_frame_invalid (void **slot
, void *info
)
90 frame_smob
*f_smob
= (frame_smob
*) *slot
;
92 f_smob
->inferior
= NULL
;
96 void operator() (htab_t htab
)
98 gdb_assert (htab
!= nullptr);
99 htab_traverse_noresize (htab
, frscm_mark_frame_invalid
, NULL
);
104 static const registry
<inferior
>::key
<htab
, frscm_deleter
>
105 frscm_inferior_data_key
;
107 /* Administrivia for frame smobs. */
109 /* Helper function to hash a frame_smob. */
112 frscm_hash_frame_smob (const void *p
)
114 const frame_smob
*f_smob
= (const frame_smob
*) p
;
115 const struct frame_id
*fid
= &f_smob
->frame_id
;
116 hashval_t hash
= htab_hash_pointer (f_smob
->inferior
);
118 if (fid
->stack_status
== FID_STACK_VALID
)
119 hash
= iterative_hash (&fid
->stack_addr
, sizeof (fid
->stack_addr
), hash
);
120 if (fid
->code_addr_p
)
121 hash
= iterative_hash (&fid
->code_addr
, sizeof (fid
->code_addr
), hash
);
122 if (fid
->special_addr_p
)
123 hash
= iterative_hash (&fid
->special_addr
, sizeof (fid
->special_addr
),
129 /* Helper function to compute equality of frame_smobs. */
132 frscm_eq_frame_smob (const void *ap
, const void *bp
)
134 const frame_smob
*a
= (const frame_smob
*) ap
;
135 const frame_smob
*b
= (const frame_smob
*) bp
;
137 return (a
->frame_id
== b
->frame_id
138 && a
->inferior
== b
->inferior
139 && a
->inferior
!= NULL
);
142 /* Return the frame -> SCM mapping table.
143 It is created if necessary. */
146 frscm_inferior_frame_map (struct inferior
*inferior
)
148 htab_t htab
= frscm_inferior_data_key
.get (inferior
);
152 htab
= gdbscm_create_eqable_gsmob_ptr_map (frscm_hash_frame_smob
,
153 frscm_eq_frame_smob
);
154 frscm_inferior_data_key
.set (inferior
, htab
);
160 /* The smob "free" function for <gdb:frame>. */
163 frscm_free_frame_smob (SCM self
)
165 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (self
);
167 if (f_smob
->inferior
!= NULL
)
169 htab_t htab
= frscm_inferior_frame_map (f_smob
->inferior
);
171 gdbscm_clear_eqable_gsmob_ptr_slot (htab
, &f_smob
->base
);
174 /* Not necessary, done to catch bugs. */
175 f_smob
->inferior
= NULL
;
180 /* The smob "print" function for <gdb:frame>. */
183 frscm_print_frame_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
185 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (self
);
187 gdbscm_printf (port
, "#<%s %s>",
189 f_smob
->frame_id
.to_string ().c_str ());
190 scm_remember_upto_here_1 (self
);
192 /* Non-zero means success. */
196 /* Low level routine to create a <gdb:frame> object. */
199 frscm_make_frame_smob (void)
201 frame_smob
*f_smob
= (frame_smob
*)
202 scm_gc_malloc (sizeof (frame_smob
), frame_smob_name
);
205 f_smob
->frame_id
= null_frame_id
;
206 f_smob
->gdbarch
= NULL
;
207 f_smob
->inferior
= NULL
;
208 f_smob
->frame_id_is_next
= 0;
209 f_scm
= scm_new_smob (frame_smob_tag
, (scm_t_bits
) f_smob
);
210 gdbscm_init_eqable_gsmob (&f_smob
->base
, f_scm
);
215 /* Return non-zero if SCM is a <gdb:frame> object. */
218 frscm_is_frame (SCM scm
)
220 return SCM_SMOB_PREDICATE (frame_smob_tag
, scm
);
223 /* (frame? object) -> boolean */
226 gdbscm_frame_p (SCM scm
)
228 return scm_from_bool (frscm_is_frame (scm
));
231 /* Create a new <gdb:frame> object that encapsulates FRAME.
232 Returns a <gdb:exception> object if there is an error. */
235 frscm_scm_from_frame (struct frame_info
*frame
, struct inferior
*inferior
)
237 frame_smob
*f_smob
, f_smob_for_lookup
;
240 eqable_gdb_smob
**slot
;
241 struct frame_id frame_id
= null_frame_id
;
242 struct gdbarch
*gdbarch
= NULL
;
243 int frame_id_is_next
= 0;
245 /* If we've already created a gsmob for this frame, return it.
246 This makes frames eq?-able. */
247 htab
= frscm_inferior_frame_map (inferior
);
248 f_smob_for_lookup
.frame_id
= get_frame_id (frame_info_ptr (frame
));
249 f_smob_for_lookup
.inferior
= inferior
;
250 slot
= gdbscm_find_eqable_gsmob_ptr_slot (htab
, &f_smob_for_lookup
.base
);
252 return (*slot
)->containing_scm
;
256 frame_info_ptr
frame_ptr (frame
);
258 /* Try to get the previous frame, to determine if this is the last frame
259 in a corrupt stack. If so, we need to store the frame_id of the next
260 frame and not of this one (which is possibly invalid). */
261 if (get_prev_frame (frame_ptr
) == NULL
262 && get_frame_unwind_stop_reason (frame_ptr
) != UNWIND_NO_REASON
263 && get_next_frame (frame_ptr
) != NULL
)
265 frame_id
= get_frame_id (get_next_frame (frame_ptr
));
266 frame_id_is_next
= 1;
270 frame_id
= get_frame_id (frame_ptr
);
271 frame_id_is_next
= 0;
273 gdbarch
= get_frame_arch (frame_ptr
);
275 catch (const gdb_exception
&except
)
277 return gdbscm_scm_from_gdb_exception (unpack (except
));
280 f_scm
= frscm_make_frame_smob ();
281 f_smob
= (frame_smob
*) SCM_SMOB_DATA (f_scm
);
282 f_smob
->frame_id
= frame_id
;
283 f_smob
->gdbarch
= gdbarch
;
284 f_smob
->inferior
= inferior
;
285 f_smob
->frame_id_is_next
= frame_id_is_next
;
287 gdbscm_fill_eqable_gsmob_ptr_slot (slot
, &f_smob
->base
);
292 /* Create a new <gdb:frame> object that encapsulates FRAME.
293 A Scheme exception is thrown if there is an error. */
296 frscm_scm_from_frame_unsafe (struct frame_info
*frame
,
297 struct inferior
*inferior
)
299 SCM f_scm
= frscm_scm_from_frame (frame
, inferior
);
301 if (gdbscm_is_exception (f_scm
))
302 gdbscm_throw (f_scm
);
307 /* Returns the <gdb:frame> object in SELF.
308 Throws an exception if SELF is not a <gdb:frame> object. */
311 frscm_get_frame_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
313 SCM_ASSERT_TYPE (frscm_is_frame (self
), self
, arg_pos
, func_name
,
319 /* There is no gdbscm_scm_to_frame function because translating
320 a frame SCM object to a struct frame_info * can throw a GDB error.
321 Thus code working with frames has to handle both Scheme errors (e.g., the
322 object is not a frame) and GDB errors (e.g., the frame lookup failed).
324 To help keep things clear we split what would be gdbscm_scm_to_frame
327 frscm_get_frame_smob_arg_unsafe
328 - throws a Scheme error if object is not a frame,
329 or if the inferior is gone or is no longer current
331 frscm_frame_smob_to_frame
332 - may throw a gdb error if the conversion fails
333 - it's not clear when it will and won't throw a GDB error,
334 but for robustness' sake we assume that whenever we call out to GDB
335 a GDB error may get thrown (and thus the call must be wrapped in a
338 /* Returns the frame_smob for the object wrapped by FRAME_SCM.
339 A Scheme error is thrown if FRAME_SCM is not a frame. */
342 frscm_get_frame_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
344 SCM f_scm
= frscm_get_frame_arg_unsafe (self
, arg_pos
, func_name
);
345 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (f_scm
);
347 if (f_smob
->inferior
== NULL
)
349 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
352 if (f_smob
->inferior
!= current_inferior ())
353 scm_misc_error (func_name
, _("inferior has changed"), SCM_EOL
);
358 /* Returns the frame_info object wrapped by F_SMOB.
359 If the frame doesn't exist anymore (the frame id doesn't
360 correspond to any frame in the inferior), returns NULL.
361 This function calls GDB routines, so don't assume a GDB error will
364 struct frame_info_ptr
365 frscm_frame_smob_to_frame (frame_smob
*f_smob
)
367 frame_info_ptr frame
= frame_find_by_id (f_smob
->frame_id
);
371 if (f_smob
->frame_id_is_next
)
372 frame
= get_prev_frame (frame
);
380 /* (frame-valid? <gdb:frame>) -> bool
381 Returns #t if the frame corresponding to the frame_id of this
382 object still exists in the inferior. */
385 gdbscm_frame_valid_p (SCM self
)
390 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
392 gdbscm_gdb_exception exc
{};
395 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
396 result
= frame
!= nullptr;
398 catch (const gdb_exception
&except
)
400 exc
= unpack (except
);
403 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
404 return scm_from_bool (result
);
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
;
420 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
422 gdbscm_gdb_exception exc
{};
425 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
429 name
= find_frame_funname (frame
, &lang
, NULL
);
432 catch (const gdb_exception
&except
)
434 exc
= unpack (except
);
437 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
440 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
445 result
= gdbscm_scm_from_c_string (name
.get ());
452 /* (frame-type <gdb:frame>) -> integer
453 Returns the frame type, namely one of the gdb:*_FRAME constants. */
456 gdbscm_frame_type (SCM self
)
459 enum frame_type type
= NORMAL_FRAME
;
462 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
464 gdbscm_gdb_exception exc
{};
467 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
471 type
= get_frame_type (frame
);
474 catch (const gdb_exception
&except
)
476 exc
= unpack (except
);
479 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
482 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
486 return scm_from_int (type
);
489 /* (frame-arch <gdb:frame>) -> <gdb:architecture>
490 Returns the frame's architecture as a gdb:architecture object. */
493 gdbscm_frame_arch (SCM self
)
498 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
500 gdbscm_gdb_exception exc
{};
503 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
504 found
= frame
!= nullptr;
506 catch (const gdb_exception
&except
)
508 exc
= unpack (except
);
511 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
514 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
518 return arscm_scm_from_arch (f_smob
->gdbarch
);
521 /* (frame-unwind-stop-reason <gdb:frame>) -> integer
522 Returns one of the gdb:FRAME_UNWIND_* constants. */
525 gdbscm_frame_unwind_stop_reason (SCM self
)
529 enum unwind_stop_reason stop_reason
= UNWIND_NO_REASON
;
531 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
533 gdbscm_gdb_exception exc
{};
536 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
537 if (frame
!= nullptr)
540 stop_reason
= get_frame_unwind_stop_reason (frame
);
543 catch (const gdb_exception
&except
)
545 exc
= unpack (except
);
548 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
551 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
555 return scm_from_int (stop_reason
);
558 /* (frame-pc <gdb:frame>) -> integer
559 Returns the frame's resume address. */
562 gdbscm_frame_pc (SCM self
)
568 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
570 gdbscm_gdb_exception exc
{};
573 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
576 pc
= get_frame_pc (frame
);
580 catch (const gdb_exception
&except
)
582 exc
= unpack (except
);
585 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
588 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
592 return gdbscm_scm_from_ulongest (pc
);
595 /* (frame-block <gdb:frame>) -> <gdb:block>
596 Returns the frame's code block, or #f if one cannot be found. */
599 gdbscm_frame_block (SCM self
)
602 const struct block
*block
= NULL
, *fn_block
;
605 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
607 gdbscm_gdb_exception exc
{};
610 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
614 block
= get_frame_block (frame
, NULL
);
617 catch (const gdb_exception
&except
)
619 exc
= unpack (except
);
622 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
625 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
629 for (fn_block
= block
;
630 fn_block
!= NULL
&& fn_block
->function () == NULL
;
631 fn_block
= fn_block
->superblock ())
634 if (block
== NULL
|| fn_block
== NULL
|| fn_block
->function () == NULL
)
636 scm_misc_error (FUNC_NAME
, _("cannot find block for frame"),
642 return bkscm_scm_from_block
643 (block
, fn_block
->function ()->objfile ());
649 /* (frame-function <gdb:frame>) -> <gdb:symbol>
650 Returns the symbol for the function corresponding to this frame,
651 or #f if there isn't one. */
654 gdbscm_frame_function (SCM self
)
657 struct symbol
*sym
= NULL
;
660 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
662 gdbscm_gdb_exception exc
{};
665 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
669 sym
= find_pc_function (get_frame_address_in_block (frame
));
672 catch (const gdb_exception
&except
)
674 exc
= unpack (except
);
677 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
680 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
685 return syscm_scm_from_symbol (sym
);
690 /* (frame-older <gdb:frame>) -> <gdb:frame>
691 Returns the frame immediately older (outer) to this frame,
692 or #f if there isn't one. */
695 gdbscm_frame_older (SCM self
)
698 struct frame_info
*prev
= NULL
;
701 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
703 gdbscm_gdb_exception exc
{};
706 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
710 prev
= get_prev_frame (frame
).get ();
713 catch (const gdb_exception
&except
)
715 exc
= unpack (except
);
718 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
721 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
726 return frscm_scm_from_frame_unsafe (prev
, f_smob
->inferior
);
731 /* (frame-newer <gdb:frame>) -> <gdb:frame>
732 Returns the frame immediately newer (inner) to this frame,
733 or #f if there isn't one. */
736 gdbscm_frame_newer (SCM self
)
739 struct frame_info
*next
= NULL
;
742 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
744 gdbscm_gdb_exception exc
{};
747 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
751 next
= get_next_frame (frame
).get ();
754 catch (const gdb_exception
&except
)
756 exc
= unpack (except
);
759 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
762 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
767 return frscm_scm_from_frame_unsafe (next
, f_smob
->inferior
);
772 /* (frame-sal <gdb:frame>) -> <gdb:sal>
773 Returns the frame's symtab and line. */
776 gdbscm_frame_sal (SCM self
)
779 struct symtab_and_line sal
;
782 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
784 gdbscm_gdb_exception exc
{};
787 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
791 sal
= find_frame_sal (frame
);
794 catch (const gdb_exception
&except
)
796 exc
= unpack (except
);
799 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
802 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
806 return stscm_scm_from_sal (sal
);
809 /* (frame-read-register <gdb:frame> string) -> <gdb:value>
810 The register argument must be a string. */
813 gdbscm_frame_read_register (SCM self
, SCM register_scm
)
816 struct value
*value
= NULL
;
820 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
821 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG2
, NULL
, "s",
822 register_scm
, ®ister_str
);
824 gdbscm_gdb_exception except
{};
830 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
834 regnum
= user_reg_map_name_to_regnum (get_frame_arch (frame
),
836 strlen (register_str
));
838 value
= value_of_register (regnum
,
839 get_next_frame_sentinel_okay (frame
));
842 catch (const gdb_exception
&ex
)
844 except
= unpack (ex
);
847 xfree (register_str
);
848 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
852 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
858 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, register_scm
,
859 _("unknown register"));
862 return vlscm_scm_from_value (value
);
865 /* (frame-read-var <gdb:frame> <gdb:symbol>) -> <gdb:value>
866 (frame-read-var <gdb:frame> string [#:block <gdb:block>]) -> <gdb:value>
867 If the optional block argument is provided start the search from that block,
868 otherwise search from the frame's current block (determined by examining
869 the resume address of the frame). The variable argument must be a string
870 or an instance of a <gdb:symbol>. The block argument must be an instance of
874 gdbscm_frame_read_var (SCM self
, SCM symbol_scm
, SCM rest
)
876 SCM keywords
[] = { block_keyword
, SCM_BOOL_F
};
878 int block_arg_pos
= -1;
879 SCM block_scm
= SCM_UNDEFINED
;
880 struct frame_info
*frame
= NULL
;
881 struct symbol
*var
= NULL
;
882 const struct block
*block
= NULL
;
883 struct value
*value
= NULL
;
885 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
887 gdbscm_gdb_exception exc
{};
890 frame
= frscm_frame_smob_to_frame (f_smob
).get ();
892 catch (const gdb_exception
&except
)
894 exc
= unpack (except
);
897 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
900 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
904 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG3
, keywords
, "#O",
905 rest
, &block_arg_pos
, &block_scm
);
907 if (syscm_is_symbol (symbol_scm
))
909 var
= syscm_get_valid_symbol_arg_unsafe (symbol_scm
, SCM_ARG2
,
911 SCM_ASSERT (SCM_UNBNDP (block_scm
), block_scm
, SCM_ARG3
, FUNC_NAME
);
913 else if (scm_is_string (symbol_scm
))
915 gdbscm_gdb_exception except
{};
917 if (! SCM_UNBNDP (block_scm
))
921 gdb_assert (block_arg_pos
> 0);
922 block
= bkscm_scm_to_block (block_scm
, block_arg_pos
, FUNC_NAME
,
925 gdbscm_throw (except_scm
);
929 gdb::unique_xmalloc_ptr
<char> var_name
930 (gdbscm_scm_to_c_string (symbol_scm
));
931 /* N.B. Between here and the end of the scope, don't do anything
932 to cause a Scheme exception. */
936 struct block_symbol lookup_sym
;
939 block
= get_frame_block (frame_info_ptr (frame
), NULL
);
940 lookup_sym
= lookup_symbol (var_name
.get (), block
, VAR_DOMAIN
,
942 var
= lookup_sym
.symbol
;
943 block
= lookup_sym
.block
;
945 catch (const gdb_exception
&ex
)
947 except
= unpack (ex
);
951 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
954 gdbscm_out_of_range_error (FUNC_NAME
, 0, symbol_scm
,
955 _("variable not found"));
959 /* Use SCM_ASSERT_TYPE for more consistent error messages. */
960 SCM_ASSERT_TYPE (0, symbol_scm
, SCM_ARG1
, FUNC_NAME
,
961 _("gdb:symbol or string"));
966 value
= read_var_value (var
, block
, frame_info_ptr (frame
));
968 catch (const gdb_exception
&except
)
970 exc
= unpack (except
);
973 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
974 return vlscm_scm_from_value (value
);
977 /* (frame-select <gdb:frame>) -> unspecified
978 Select this frame. */
981 gdbscm_frame_select (SCM self
)
986 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
988 gdbscm_gdb_exception exc
{};
991 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
995 select_frame (frame
);
998 catch (const gdb_exception
&except
)
1000 exc
= unpack (except
);
1003 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1006 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
1010 return SCM_UNSPECIFIED
;
1013 /* (newest-frame) -> <gdb:frame>
1014 Returns the newest frame. */
1017 gdbscm_newest_frame (void)
1019 struct frame_info
*frame
= NULL
;
1021 gdbscm_gdb_exception exc
{};
1024 frame
= get_current_frame ().get ();
1026 catch (const gdb_exception
&except
)
1028 exc
= unpack (except
);
1031 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1032 return frscm_scm_from_frame_unsafe (frame
, current_inferior ());
1035 /* (selected-frame) -> <gdb:frame>
1036 Returns the selected frame. */
1039 gdbscm_selected_frame (void)
1041 struct frame_info
*frame
= NULL
;
1043 gdbscm_gdb_exception exc
{};
1046 frame
= get_selected_frame (_("No frame is currently selected")).get ();
1048 catch (const gdb_exception
&except
)
1050 exc
= unpack (except
);
1053 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1054 return frscm_scm_from_frame_unsafe (frame
, current_inferior ());
1057 /* (unwind-stop-reason-string integer) -> string
1058 Return a string explaining the unwind stop reason. */
1061 gdbscm_unwind_stop_reason_string (SCM reason_scm
)
1066 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, NULL
, "i",
1067 reason_scm
, &reason
);
1069 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
1070 scm_out_of_range (FUNC_NAME
, reason_scm
);
1072 str
= unwind_stop_reason_to_string ((enum unwind_stop_reason
) reason
);
1073 return gdbscm_scm_from_c_string (str
);
1076 /* Initialize the Scheme frame support. */
1078 static const scheme_integer_constant frame_integer_constants
[] =
1080 #define ENTRY(X) { #X, X }
1082 ENTRY (NORMAL_FRAME
),
1083 ENTRY (DUMMY_FRAME
),
1084 ENTRY (INLINE_FRAME
),
1085 ENTRY (TAILCALL_FRAME
),
1086 ENTRY (SIGTRAMP_FRAME
),
1088 ENTRY (SENTINEL_FRAME
),
1092 #define SET(name, description) \
1093 { "FRAME_" #name, name },
1094 #include "unwind_stop_reasons.def"
1097 END_INTEGER_CONSTANTS
1100 static const scheme_function frame_functions
[] =
1102 { "frame?", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_p
),
1104 Return #t if the object is a <gdb:frame> object." },
1106 { "frame-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_valid_p
),
1108 Return #t if the object is a valid <gdb:frame> object.\n\
1109 Frames become invalid when the inferior returns to its caller." },
1111 { "frame-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_name
),
1113 Return the name of the function corresponding to this frame,\n\
1114 or #f if there is no function." },
1116 { "frame-arch", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_arch
),
1118 Return the frame's architecture as a <gdb:arch> object." },
1120 { "frame-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_type
),
1122 Return the frame type, namely one of the gdb:*_FRAME constants." },
1124 { "frame-unwind-stop-reason", 1, 0, 0,
1125 as_a_scm_t_subr (gdbscm_frame_unwind_stop_reason
),
1127 Return one of the gdb:FRAME_UNWIND_* constants explaining why\n\
1128 it's not possible to find frames older than this." },
1130 { "frame-pc", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_pc
),
1132 Return the frame's resume address." },
1134 { "frame-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_block
),
1136 Return the frame's code block, or #f if one cannot be found." },
1138 { "frame-function", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_function
),
1140 Return the <gdb:symbol> for the function corresponding to this frame,\n\
1141 or #f if there isn't one." },
1143 { "frame-older", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_older
),
1145 Return the frame immediately older (outer) to this frame,\n\
1146 or #f if there isn't one." },
1148 { "frame-newer", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_newer
),
1150 Return the frame immediately newer (inner) to this frame,\n\
1151 or #f if there isn't one." },
1153 { "frame-sal", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_sal
),
1155 Return the frame's symtab-and-line <gdb:sal> object." },
1157 { "frame-read-var", 2, 0, 1, as_a_scm_t_subr (gdbscm_frame_read_var
),
1159 Return the value of the symbol in the frame.\n\
1161 Arguments: <gdb:frame> <gdb:symbol>\n\
1162 Or: <gdb:frame> string [#:block <gdb:block>]" },
1164 { "frame-read-register", 2, 0, 0,
1165 as_a_scm_t_subr (gdbscm_frame_read_register
),
1167 Return the value of the register in the frame.\n\
1169 Arguments: <gdb:frame> string" },
1171 { "frame-select", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_select
),
1173 Select this frame." },
1175 { "newest-frame", 0, 0, 0, as_a_scm_t_subr (gdbscm_newest_frame
),
1177 Return the newest frame." },
1179 { "selected-frame", 0, 0, 0, as_a_scm_t_subr (gdbscm_selected_frame
),
1181 Return the selected frame." },
1183 { "unwind-stop-reason-string", 1, 0, 0,
1184 as_a_scm_t_subr (gdbscm_unwind_stop_reason_string
),
1186 Return a string explaining the unwind stop reason.\n\
1188 Arguments: integer (the result of frame-unwind-stop-reason)" },
1194 gdbscm_initialize_frames (void)
1197 = gdbscm_make_smob_type (frame_smob_name
, sizeof (frame_smob
));
1198 scm_set_smob_free (frame_smob_tag
, frscm_free_frame_smob
);
1199 scm_set_smob_print (frame_smob_tag
, frscm_print_frame_smob
);
1201 gdbscm_define_integer_constants (frame_integer_constants
, 1);
1202 gdbscm_define_functions (frame_functions
, 1);
1204 block_keyword
= scm_from_latin1_keyword ("block");