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. */
30 #include "user-regs.h"
32 #include "guile-internal.h"
34 /* The <gdb:frame> smob. */
38 /* This always appears first. */
41 struct frame_id frame_id
;
42 struct gdbarch
*gdbarch
;
44 /* Frames are tracked by inferior.
45 We need some place to put the eq?-able hash table, and this feels as
46 good a place as any. Frames in one inferior shouldn't be considered
47 equal to frames in a different inferior. The frame becomes invalid if
48 this becomes NULL (the inferior has been deleted from gdb).
49 It's easier to relax restrictions than impose them after the fact.
50 N.B. It is an outstanding question whether a frame survives reruns of
51 the inferior. Intuitively the answer is "No", but currently a frame
52 also survives, e.g., multiple invocations of the same function from
53 the same point. Even different threads can have the same frame, e.g.,
54 if a thread dies and a new thread gets the same stack. */
55 struct inferior
*inferior
;
57 /* Marks that the FRAME_ID member actually holds the ID of the frame next
58 to this, and not this frame's ID itself. This is a hack to permit Scheme
59 frame objects which represent invalid frames (i.e., the last frame_info
60 in a corrupt stack). The problem arises from the fact that this code
61 relies on FRAME_ID to uniquely identify a frame, which is not always true
62 for the last "frame" in a corrupt stack (it can have a null ID, or the
63 same ID as the previous frame). Whenever get_prev_frame returns NULL, we
64 record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1. */
68 static const char frame_smob_name
[] = "gdb:frame";
70 /* The tag Guile knows the frame smob by. */
71 static scm_t_bits frame_smob_tag
;
73 /* Keywords used in argument passing. */
74 static SCM block_keyword
;
76 /* This is called when an inferior is about to be freed.
77 Invalidate the frame as further actions on the frame could result
78 in bad data. All access to the frame should be gated by
79 frscm_get_frame_smob_arg_unsafe which will raise an exception on
83 /* Helper function for frscm_del_inferior_frames to mark the frame
87 frscm_mark_frame_invalid (void **slot
, void *info
)
89 frame_smob
*f_smob
= (frame_smob
*) *slot
;
91 f_smob
->inferior
= NULL
;
95 void operator() (htab_t htab
)
97 gdb_assert (htab
!= nullptr);
98 htab_traverse_noresize (htab
, frscm_mark_frame_invalid
, NULL
);
103 static const registry
<inferior
>::key
<htab
, frscm_deleter
>
104 frscm_inferior_data_key
;
106 /* Administrivia for frame smobs. */
108 /* Helper function to hash a frame_smob. */
111 frscm_hash_frame_smob (const void *p
)
113 const frame_smob
*f_smob
= (const frame_smob
*) p
;
114 const struct frame_id
*fid
= &f_smob
->frame_id
;
115 hashval_t hash
= htab_hash_pointer (f_smob
->inferior
);
117 if (fid
->stack_status
== FID_STACK_VALID
)
118 hash
= iterative_hash (&fid
->stack_addr
, sizeof (fid
->stack_addr
), hash
);
119 if (fid
->code_addr_p
)
120 hash
= iterative_hash (&fid
->code_addr
, sizeof (fid
->code_addr
), hash
);
121 if (fid
->special_addr_p
)
122 hash
= iterative_hash (&fid
->special_addr
, sizeof (fid
->special_addr
),
128 /* Helper function to compute equality of frame_smobs. */
131 frscm_eq_frame_smob (const void *ap
, const void *bp
)
133 const frame_smob
*a
= (const frame_smob
*) ap
;
134 const frame_smob
*b
= (const frame_smob
*) bp
;
136 return (a
->frame_id
== b
->frame_id
137 && a
->inferior
== b
->inferior
138 && a
->inferior
!= NULL
);
141 /* Return the frame -> SCM mapping table.
142 It is created if necessary. */
145 frscm_inferior_frame_map (struct inferior
*inferior
)
147 htab_t htab
= frscm_inferior_data_key
.get (inferior
);
151 htab
= gdbscm_create_eqable_gsmob_ptr_map (frscm_hash_frame_smob
,
152 frscm_eq_frame_smob
);
153 frscm_inferior_data_key
.set (inferior
, htab
);
159 /* The smob "free" function for <gdb:frame>. */
162 frscm_free_frame_smob (SCM self
)
164 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (self
);
166 if (f_smob
->inferior
!= NULL
)
168 htab_t htab
= frscm_inferior_frame_map (f_smob
->inferior
);
170 gdbscm_clear_eqable_gsmob_ptr_slot (htab
, &f_smob
->base
);
173 /* Not necessary, done to catch bugs. */
174 f_smob
->inferior
= NULL
;
179 /* The smob "print" function for <gdb:frame>. */
182 frscm_print_frame_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
184 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (self
);
186 gdbscm_printf (port
, "#<%s %s>",
188 f_smob
->frame_id
.to_string ().c_str ());
189 scm_remember_upto_here_1 (self
);
191 /* Non-zero means success. */
195 /* Low level routine to create a <gdb:frame> object. */
198 frscm_make_frame_smob (void)
200 frame_smob
*f_smob
= (frame_smob
*)
201 scm_gc_malloc (sizeof (frame_smob
), frame_smob_name
);
204 f_smob
->frame_id
= null_frame_id
;
205 f_smob
->gdbarch
= NULL
;
206 f_smob
->inferior
= NULL
;
207 f_smob
->frame_id_is_next
= 0;
208 f_scm
= scm_new_smob (frame_smob_tag
, (scm_t_bits
) f_smob
);
209 gdbscm_init_eqable_gsmob (&f_smob
->base
, f_scm
);
214 /* Return non-zero if SCM is a <gdb:frame> object. */
217 frscm_is_frame (SCM scm
)
219 return SCM_SMOB_PREDICATE (frame_smob_tag
, scm
);
222 /* (frame? object) -> boolean */
225 gdbscm_frame_p (SCM scm
)
227 return scm_from_bool (frscm_is_frame (scm
));
230 /* Create a new <gdb:frame> object that encapsulates FRAME.
231 Returns a <gdb:exception> object if there is an error. */
234 frscm_scm_from_frame (struct frame_info
*frame
, struct inferior
*inferior
)
236 frame_smob
*f_smob
, f_smob_for_lookup
;
239 eqable_gdb_smob
**slot
;
240 struct frame_id frame_id
= null_frame_id
;
241 struct gdbarch
*gdbarch
= NULL
;
242 int frame_id_is_next
= 0;
244 /* If we've already created a gsmob for this frame, return it.
245 This makes frames eq?-able. */
246 htab
= frscm_inferior_frame_map (inferior
);
247 f_smob_for_lookup
.frame_id
= get_frame_id (frame_info_ptr (frame
));
248 f_smob_for_lookup
.inferior
= inferior
;
249 slot
= gdbscm_find_eqable_gsmob_ptr_slot (htab
, &f_smob_for_lookup
.base
);
251 return (*slot
)->containing_scm
;
255 frame_info_ptr
frame_ptr (frame
);
257 /* Try to get the previous frame, to determine if this is the last frame
258 in a corrupt stack. If so, we need to store the frame_id of the next
259 frame and not of this one (which is possibly invalid). */
260 if (get_prev_frame (frame_ptr
) == NULL
261 && get_frame_unwind_stop_reason (frame_ptr
) != UNWIND_NO_REASON
262 && get_next_frame (frame_ptr
) != NULL
)
264 frame_id
= get_frame_id (get_next_frame (frame_ptr
));
265 frame_id_is_next
= 1;
269 frame_id
= get_frame_id (frame_ptr
);
270 frame_id_is_next
= 0;
272 gdbarch
= get_frame_arch (frame_ptr
);
274 catch (const gdb_exception
&except
)
276 return gdbscm_scm_from_gdb_exception (unpack (except
));
279 f_scm
= frscm_make_frame_smob ();
280 f_smob
= (frame_smob
*) SCM_SMOB_DATA (f_scm
);
281 f_smob
->frame_id
= frame_id
;
282 f_smob
->gdbarch
= gdbarch
;
283 f_smob
->inferior
= inferior
;
284 f_smob
->frame_id_is_next
= frame_id_is_next
;
286 gdbscm_fill_eqable_gsmob_ptr_slot (slot
, &f_smob
->base
);
291 /* Create a new <gdb:frame> object that encapsulates FRAME.
292 A Scheme exception is thrown if there is an error. */
295 frscm_scm_from_frame_unsafe (struct frame_info
*frame
,
296 struct inferior
*inferior
)
298 SCM f_scm
= frscm_scm_from_frame (frame
, inferior
);
300 if (gdbscm_is_exception (f_scm
))
301 gdbscm_throw (f_scm
);
306 /* Returns the <gdb:frame> object in SELF.
307 Throws an exception if SELF is not a <gdb:frame> object. */
310 frscm_get_frame_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
312 SCM_ASSERT_TYPE (frscm_is_frame (self
), self
, arg_pos
, func_name
,
318 /* There is no gdbscm_scm_to_frame function because translating
319 a frame SCM object to a struct frame_info * can throw a GDB error.
320 Thus code working with frames has to handle both Scheme errors (e.g., the
321 object is not a frame) and GDB errors (e.g., the frame lookup failed).
323 To help keep things clear we split what would be gdbscm_scm_to_frame
326 frscm_get_frame_smob_arg_unsafe
327 - throws a Scheme error if object is not a frame,
328 or if the inferior is gone or is no longer current
330 frscm_frame_smob_to_frame
331 - may throw a gdb error if the conversion fails
332 - it's not clear when it will and won't throw a GDB error,
333 but for robustness' sake we assume that whenever we call out to GDB
334 a GDB error may get thrown (and thus the call must be wrapped in a
337 /* Returns the frame_smob for the object wrapped by FRAME_SCM.
338 A Scheme error is thrown if FRAME_SCM is not a frame. */
341 frscm_get_frame_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
343 SCM f_scm
= frscm_get_frame_arg_unsafe (self
, arg_pos
, func_name
);
344 frame_smob
*f_smob
= (frame_smob
*) SCM_SMOB_DATA (f_scm
);
346 if (f_smob
->inferior
== NULL
)
348 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
351 if (f_smob
->inferior
!= current_inferior ())
352 scm_misc_error (func_name
, _("inferior has changed"), SCM_EOL
);
357 /* Returns the frame_info object wrapped by F_SMOB.
358 If the frame doesn't exist anymore (the frame id doesn't
359 correspond to any frame in the inferior), returns NULL.
360 This function calls GDB routines, so don't assume a GDB error will
363 struct frame_info_ptr
364 frscm_frame_smob_to_frame (frame_smob
*f_smob
)
366 frame_info_ptr frame
= frame_find_by_id (f_smob
->frame_id
);
370 if (f_smob
->frame_id_is_next
)
371 frame
= get_prev_frame (frame
);
379 /* (frame-valid? <gdb:frame>) -> bool
380 Returns #t if the frame corresponding to the frame_id of this
381 object still exists in the inferior. */
384 gdbscm_frame_valid_p (SCM self
)
389 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
391 gdbscm_gdb_exception exc
{};
394 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
395 result
= frame
!= nullptr;
397 catch (const gdb_exception
&except
)
399 exc
= unpack (except
);
402 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
403 return scm_from_bool (result
);
406 /* (frame-name <gdb:frame>) -> string
407 Returns the name of the function corresponding to this frame,
408 or #f if there is no function. */
411 gdbscm_frame_name (SCM self
)
414 gdb::unique_xmalloc_ptr
<char> name
;
415 enum language lang
= language_minimal
;
419 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
421 gdbscm_gdb_exception exc
{};
424 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
428 name
= find_frame_funname (frame
, &lang
, NULL
);
431 catch (const gdb_exception
&except
)
433 exc
= unpack (except
);
436 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
439 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
444 result
= gdbscm_scm_from_c_string (name
.get ());
451 /* (frame-type <gdb:frame>) -> integer
452 Returns the frame type, namely one of the gdb:*_FRAME constants. */
455 gdbscm_frame_type (SCM self
)
458 enum frame_type type
= NORMAL_FRAME
;
461 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
463 gdbscm_gdb_exception exc
{};
466 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
470 type
= get_frame_type (frame
);
473 catch (const gdb_exception
&except
)
475 exc
= unpack (except
);
478 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
481 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
485 return scm_from_int (type
);
488 /* (frame-arch <gdb:frame>) -> <gdb:architecture>
489 Returns the frame's architecture as a gdb:architecture object. */
492 gdbscm_frame_arch (SCM self
)
497 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
499 gdbscm_gdb_exception exc
{};
502 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
503 found
= frame
!= nullptr;
505 catch (const gdb_exception
&except
)
507 exc
= unpack (except
);
510 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
513 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
517 return arscm_scm_from_arch (f_smob
->gdbarch
);
520 /* (frame-unwind-stop-reason <gdb:frame>) -> integer
521 Returns one of the gdb:FRAME_UNWIND_* constants. */
524 gdbscm_frame_unwind_stop_reason (SCM self
)
528 enum unwind_stop_reason stop_reason
= UNWIND_NO_REASON
;
530 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
532 gdbscm_gdb_exception exc
{};
535 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
536 if (frame
!= nullptr)
539 stop_reason
= get_frame_unwind_stop_reason (frame
);
542 catch (const gdb_exception
&except
)
544 exc
= unpack (except
);
547 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
550 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
554 return scm_from_int (stop_reason
);
557 /* (frame-pc <gdb:frame>) -> integer
558 Returns the frame's resume address. */
561 gdbscm_frame_pc (SCM self
)
567 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
569 gdbscm_gdb_exception exc
{};
572 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
575 pc
= get_frame_pc (frame
);
579 catch (const gdb_exception
&except
)
581 exc
= unpack (except
);
584 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
587 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
591 return gdbscm_scm_from_ulongest (pc
);
594 /* (frame-block <gdb:frame>) -> <gdb:block>
595 Returns the frame's code block, or #f if one cannot be found. */
598 gdbscm_frame_block (SCM self
)
601 const struct block
*block
= NULL
, *fn_block
;
604 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
606 gdbscm_gdb_exception exc
{};
609 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
613 block
= get_frame_block (frame
, NULL
);
616 catch (const gdb_exception
&except
)
618 exc
= unpack (except
);
621 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
624 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
628 for (fn_block
= block
;
629 fn_block
!= NULL
&& fn_block
->function () == NULL
;
630 fn_block
= fn_block
->superblock ())
633 if (block
== NULL
|| fn_block
== NULL
|| fn_block
->function () == NULL
)
635 scm_misc_error (FUNC_NAME
, _("cannot find block for frame"),
641 return bkscm_scm_from_block
642 (block
, fn_block
->function ()->objfile ());
648 /* (frame-function <gdb:frame>) -> <gdb:symbol>
649 Returns the symbol for the function corresponding to this frame,
650 or #f if there isn't one. */
653 gdbscm_frame_function (SCM self
)
656 struct symbol
*sym
= NULL
;
659 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
661 gdbscm_gdb_exception exc
{};
664 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
668 sym
= find_pc_function (get_frame_address_in_block (frame
));
671 catch (const gdb_exception
&except
)
673 exc
= unpack (except
);
676 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
679 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
684 return syscm_scm_from_symbol (sym
);
689 /* (frame-older <gdb:frame>) -> <gdb:frame>
690 Returns the frame immediately older (outer) to this frame,
691 or #f if there isn't one. */
694 gdbscm_frame_older (SCM self
)
697 struct frame_info
*prev
= NULL
;
700 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
702 gdbscm_gdb_exception exc
{};
705 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
709 prev
= get_prev_frame (frame
).get ();
712 catch (const gdb_exception
&except
)
714 exc
= unpack (except
);
717 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
720 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
725 return frscm_scm_from_frame_unsafe (prev
, f_smob
->inferior
);
730 /* (frame-newer <gdb:frame>) -> <gdb:frame>
731 Returns the frame immediately newer (inner) to this frame,
732 or #f if there isn't one. */
735 gdbscm_frame_newer (SCM self
)
738 struct frame_info
*next
= NULL
;
741 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
743 gdbscm_gdb_exception exc
{};
746 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
750 next
= get_next_frame (frame
).get ();
753 catch (const gdb_exception
&except
)
755 exc
= unpack (except
);
758 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
761 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
766 return frscm_scm_from_frame_unsafe (next
, f_smob
->inferior
);
771 /* (frame-sal <gdb:frame>) -> <gdb:sal>
772 Returns the frame's symtab and line. */
775 gdbscm_frame_sal (SCM self
)
778 struct symtab_and_line sal
;
781 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
783 gdbscm_gdb_exception exc
{};
786 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
790 sal
= find_frame_sal (frame
);
793 catch (const gdb_exception
&except
)
795 exc
= unpack (except
);
798 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
801 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
805 return stscm_scm_from_sal (sal
);
808 /* (frame-read-register <gdb:frame> string) -> <gdb:value>
809 The register argument must be a string. */
812 gdbscm_frame_read_register (SCM self
, SCM register_scm
)
815 struct value
*value
= NULL
;
819 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
820 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG2
, NULL
, "s",
821 register_scm
, ®ister_str
);
823 gdbscm_gdb_exception except
{};
829 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
833 regnum
= user_reg_map_name_to_regnum (get_frame_arch (frame
),
835 strlen (register_str
));
837 value
= value_of_register (regnum
,
838 get_next_frame_sentinel_okay (frame
));
841 catch (const gdb_exception
&ex
)
843 except
= unpack (ex
);
846 xfree (register_str
);
847 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
851 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
857 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, register_scm
,
858 _("unknown register"));
861 return vlscm_scm_from_value (value
);
864 /* (frame-read-var <gdb:frame> <gdb:symbol>) -> <gdb:value>
865 (frame-read-var <gdb:frame> string [#:block <gdb:block>]) -> <gdb:value>
866 If the optional block argument is provided start the search from that block,
867 otherwise search from the frame's current block (determined by examining
868 the resume address of the frame). The variable argument must be a string
869 or an instance of a <gdb:symbol>. The block argument must be an instance of
873 gdbscm_frame_read_var (SCM self
, SCM symbol_scm
, SCM rest
)
875 SCM keywords
[] = { block_keyword
, SCM_BOOL_F
};
877 int block_arg_pos
= -1;
878 SCM block_scm
= SCM_UNDEFINED
;
879 struct frame_info
*frame
= NULL
;
880 struct symbol
*var
= NULL
;
881 const struct block
*block
= NULL
;
882 struct value
*value
= NULL
;
884 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
886 gdbscm_gdb_exception exc
{};
889 frame
= frscm_frame_smob_to_frame (f_smob
).get ();
891 catch (const gdb_exception
&except
)
893 exc
= unpack (except
);
896 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
899 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
903 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG3
, keywords
, "#O",
904 rest
, &block_arg_pos
, &block_scm
);
906 if (syscm_is_symbol (symbol_scm
))
908 var
= syscm_get_valid_symbol_arg_unsafe (symbol_scm
, SCM_ARG2
,
910 SCM_ASSERT (SCM_UNBNDP (block_scm
), block_scm
, SCM_ARG3
, FUNC_NAME
);
912 else if (scm_is_string (symbol_scm
))
914 gdbscm_gdb_exception except
{};
916 if (! SCM_UNBNDP (block_scm
))
920 gdb_assert (block_arg_pos
> 0);
921 block
= bkscm_scm_to_block (block_scm
, block_arg_pos
, FUNC_NAME
,
924 gdbscm_throw (except_scm
);
928 gdb::unique_xmalloc_ptr
<char> var_name
929 (gdbscm_scm_to_c_string (symbol_scm
));
930 /* N.B. Between here and the end of the scope, don't do anything
931 to cause a Scheme exception. */
935 struct block_symbol lookup_sym
;
938 block
= get_frame_block (frame_info_ptr (frame
), NULL
);
939 lookup_sym
= lookup_symbol (var_name
.get (), block
, SEARCH_VFT
,
941 var
= lookup_sym
.symbol
;
942 block
= lookup_sym
.block
;
944 catch (const gdb_exception
&ex
)
946 except
= unpack (ex
);
950 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
953 gdbscm_out_of_range_error (FUNC_NAME
, 0, symbol_scm
,
954 _("variable not found"));
958 /* Use SCM_ASSERT_TYPE for more consistent error messages. */
959 SCM_ASSERT_TYPE (0, symbol_scm
, SCM_ARG1
, FUNC_NAME
,
960 _("gdb:symbol or string"));
965 value
= read_var_value (var
, block
, frame_info_ptr (frame
));
967 catch (const gdb_exception
&except
)
969 exc
= unpack (except
);
972 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
973 return vlscm_scm_from_value (value
);
976 /* (frame-select <gdb:frame>) -> unspecified
977 Select this frame. */
980 gdbscm_frame_select (SCM self
)
985 f_smob
= frscm_get_frame_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
987 gdbscm_gdb_exception exc
{};
990 frame_info_ptr frame
= frscm_frame_smob_to_frame (f_smob
);
994 select_frame (frame
);
997 catch (const gdb_exception
&except
)
999 exc
= unpack (except
);
1002 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1005 gdbscm_invalid_object_error (FUNC_NAME
, SCM_ARG1
, self
,
1009 return SCM_UNSPECIFIED
;
1012 /* (newest-frame) -> <gdb:frame>
1013 Returns the newest frame. */
1016 gdbscm_newest_frame (void)
1018 struct frame_info
*frame
= NULL
;
1020 gdbscm_gdb_exception exc
{};
1023 frame
= get_current_frame ().get ();
1025 catch (const gdb_exception
&except
)
1027 exc
= unpack (except
);
1030 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1031 return frscm_scm_from_frame_unsafe (frame
, current_inferior ());
1034 /* (selected-frame) -> <gdb:frame>
1035 Returns the selected frame. */
1038 gdbscm_selected_frame (void)
1040 struct frame_info
*frame
= NULL
;
1042 gdbscm_gdb_exception exc
{};
1045 frame
= get_selected_frame (_("No frame is currently selected")).get ();
1047 catch (const gdb_exception
&except
)
1049 exc
= unpack (except
);
1052 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1053 return frscm_scm_from_frame_unsafe (frame
, current_inferior ());
1056 /* (unwind-stop-reason-string integer) -> string
1057 Return a string explaining the unwind stop reason. */
1060 gdbscm_unwind_stop_reason_string (SCM reason_scm
)
1065 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, NULL
, "i",
1066 reason_scm
, &reason
);
1068 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
1069 scm_out_of_range (FUNC_NAME
, reason_scm
);
1071 str
= unwind_stop_reason_to_string ((enum unwind_stop_reason
) reason
);
1072 return gdbscm_scm_from_c_string (str
);
1075 /* Initialize the Scheme frame support. */
1077 static const scheme_integer_constant frame_integer_constants
[] =
1079 #define ENTRY(X) { #X, X }
1081 ENTRY (NORMAL_FRAME
),
1082 ENTRY (DUMMY_FRAME
),
1083 ENTRY (INLINE_FRAME
),
1084 ENTRY (TAILCALL_FRAME
),
1085 ENTRY (SIGTRAMP_FRAME
),
1087 ENTRY (SENTINEL_FRAME
),
1091 #define SET(name, description) \
1092 { "FRAME_" #name, name },
1093 #include "unwind_stop_reasons.def"
1096 END_INTEGER_CONSTANTS
1099 static const scheme_function frame_functions
[] =
1101 { "frame?", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_p
),
1103 Return #t if the object is a <gdb:frame> object." },
1105 { "frame-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_valid_p
),
1107 Return #t if the object is a valid <gdb:frame> object.\n\
1108 Frames become invalid when the inferior returns to its caller." },
1110 { "frame-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_name
),
1112 Return the name of the function corresponding to this frame,\n\
1113 or #f if there is no function." },
1115 { "frame-arch", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_arch
),
1117 Return the frame's architecture as a <gdb:arch> object." },
1119 { "frame-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_type
),
1121 Return the frame type, namely one of the gdb:*_FRAME constants." },
1123 { "frame-unwind-stop-reason", 1, 0, 0,
1124 as_a_scm_t_subr (gdbscm_frame_unwind_stop_reason
),
1126 Return one of the gdb:FRAME_UNWIND_* constants explaining why\n\
1127 it's not possible to find frames older than this." },
1129 { "frame-pc", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_pc
),
1131 Return the frame's resume address." },
1133 { "frame-block", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_block
),
1135 Return the frame's code block, or #f if one cannot be found." },
1137 { "frame-function", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_function
),
1139 Return the <gdb:symbol> for the function corresponding to this frame,\n\
1140 or #f if there isn't one." },
1142 { "frame-older", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_older
),
1144 Return the frame immediately older (outer) to this frame,\n\
1145 or #f if there isn't one." },
1147 { "frame-newer", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_newer
),
1149 Return the frame immediately newer (inner) to this frame,\n\
1150 or #f if there isn't one." },
1152 { "frame-sal", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_sal
),
1154 Return the frame's symtab-and-line <gdb:sal> object." },
1156 { "frame-read-var", 2, 0, 1, as_a_scm_t_subr (gdbscm_frame_read_var
),
1158 Return the value of the symbol in the frame.\n\
1160 Arguments: <gdb:frame> <gdb:symbol>\n\
1161 Or: <gdb:frame> string [#:block <gdb:block>]" },
1163 { "frame-read-register", 2, 0, 0,
1164 as_a_scm_t_subr (gdbscm_frame_read_register
),
1166 Return the value of the register in the frame.\n\
1168 Arguments: <gdb:frame> string" },
1170 { "frame-select", 1, 0, 0, as_a_scm_t_subr (gdbscm_frame_select
),
1172 Select this frame." },
1174 { "newest-frame", 0, 0, 0, as_a_scm_t_subr (gdbscm_newest_frame
),
1176 Return the newest frame." },
1178 { "selected-frame", 0, 0, 0, as_a_scm_t_subr (gdbscm_selected_frame
),
1180 Return the selected frame." },
1182 { "unwind-stop-reason-string", 1, 0, 0,
1183 as_a_scm_t_subr (gdbscm_unwind_stop_reason_string
),
1185 Return a string explaining the unwind stop reason.\n\
1187 Arguments: integer (the result of frame-unwind-stop-reason)" },
1193 gdbscm_initialize_frames (void)
1196 = gdbscm_make_smob_type (frame_smob_name
, sizeof (frame_smob
));
1197 scm_set_smob_free (frame_smob_tag
, frscm_free_frame_smob
);
1198 scm_set_smob_print (frame_smob_tag
, frscm_print_frame_smob
);
1200 gdbscm_define_integer_constants (frame_integer_constants
, 1);
1201 gdbscm_define_functions (frame_functions
, 1);
1203 block_keyword
= scm_from_latin1_keyword ("block");