1 /* Scheme interface to values.
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. */
24 #include "arch-utils.h"
27 #include "target-float.h"
29 #include "symtab.h" /* Needed by language.h. */
33 #include "guile-internal.h"
35 /* The <gdb:value> smob. */
39 /* This always appears first. */
42 /* Doubly linked list of values in values_in_scheme.
43 IWBN to use a chained_gdb_smob instead, which is doable, it just requires
44 a bit more casting than normal. */
50 /* These are cached here to avoid making multiple copies of them.
51 Plus computing the dynamic_type can be a bit expensive.
52 We use #f to indicate that the value doesn't exist (e.g. value doesn't
53 have an address), so we need another value to indicate that we haven't
54 computed the value yet. For this we use SCM_UNDEFINED. */
60 static const char value_smob_name
[] = "gdb:value";
62 /* The tag Guile knows the value smob by. */
63 static scm_t_bits value_smob_tag
;
65 /* List of all values which are currently exposed to Scheme. It is
66 maintained so that when an objfile is discarded, preserve_values
67 can copy the values' types if needed. */
68 static value_smob
*values_in_scheme
;
70 /* Keywords used by Scheme procedures in this file. */
71 static SCM type_keyword
;
72 static SCM encoding_keyword
;
73 static SCM errors_keyword
;
74 static SCM length_keyword
;
76 /* Possible #:errors values. */
77 static SCM error_symbol
;
78 static SCM escape_symbol
;
79 static SCM substitute_symbol
;
81 /* Administrivia for value smobs. */
83 /* Iterate over all the <gdb:value> objects, calling preserve_one_value on
85 This is the extension_language_ops.preserve_values "method". */
88 gdbscm_preserve_values (const struct extension_language_defn
*extlang
,
89 struct objfile
*objfile
, htab_t copied_types
)
93 for (iter
= values_in_scheme
; iter
; iter
= iter
->next
)
94 preserve_one_value (iter
->value
, objfile
, copied_types
);
97 /* Helper to add a value_smob to the global list. */
100 vlscm_remember_scheme_value (value_smob
*v_smob
)
102 v_smob
->next
= values_in_scheme
;
104 v_smob
->next
->prev
= v_smob
;
106 values_in_scheme
= v_smob
;
109 /* Helper to remove a value_smob from the global list. */
112 vlscm_forget_value_smob (value_smob
*v_smob
)
114 /* Remove SELF from the global list. */
116 v_smob
->prev
->next
= v_smob
->next
;
119 gdb_assert (values_in_scheme
== v_smob
);
120 values_in_scheme
= v_smob
->next
;
123 v_smob
->next
->prev
= v_smob
->prev
;
126 /* The smob "free" function for <gdb:value>. */
129 vlscm_free_value_smob (SCM self
)
131 value_smob
*v_smob
= (value_smob
*) SCM_SMOB_DATA (self
);
133 vlscm_forget_value_smob (v_smob
);
134 value_decref (v_smob
->value
);
139 /* The smob "print" function for <gdb:value>. */
142 vlscm_print_value_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
144 value_smob
*v_smob
= (value_smob
*) SCM_SMOB_DATA (self
);
145 struct value_print_options opts
;
147 if (pstate
->writingp
)
148 gdbscm_printf (port
, "#<%s ", value_smob_name
);
150 get_user_print_options (&opts
);
153 /* pstate->writingp = zero if invoked by display/~A, and nonzero if
154 invoked by write/~S. What to do here may need to evolve.
155 IWBN if we could pass an argument to format that would we could use
156 instead of writingp. */
157 opts
.raw
= !!pstate
->writingp
;
159 gdbscm_gdb_exception exc
{};
164 common_val_print (v_smob
->value
, &stb
, 0, &opts
, current_language
);
165 scm_puts (stb
.c_str (), port
);
167 catch (const gdb_exception
&except
)
169 exc
= unpack (except
);
172 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
173 if (pstate
->writingp
)
174 scm_puts (">", port
);
176 scm_remember_upto_here_1 (self
);
178 /* Non-zero means success. */
182 /* The smob "equalp" function for <gdb:value>. */
185 vlscm_equal_p_value_smob (SCM v1
, SCM v2
)
187 const value_smob
*v1_smob
= (value_smob
*) SCM_SMOB_DATA (v1
);
188 const value_smob
*v2_smob
= (value_smob
*) SCM_SMOB_DATA (v2
);
191 gdbscm_gdb_exception exc
{};
194 result
= value_equal (v1_smob
->value
, v2_smob
->value
);
196 catch (const gdb_exception
&except
)
198 exc
= unpack (except
);
201 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
202 return scm_from_bool (result
);
205 /* Low level routine to create a <gdb:value> object. */
208 vlscm_make_value_smob (void)
210 value_smob
*v_smob
= (value_smob
*)
211 scm_gc_malloc (sizeof (value_smob
), value_smob_name
);
214 /* These must be filled in by the caller. */
215 v_smob
->value
= NULL
;
219 /* These are lazily computed. */
220 v_smob
->address
= SCM_UNDEFINED
;
221 v_smob
->type
= SCM_UNDEFINED
;
222 v_smob
->dynamic_type
= SCM_UNDEFINED
;
224 v_scm
= scm_new_smob (value_smob_tag
, (scm_t_bits
) v_smob
);
225 gdbscm_init_gsmob (&v_smob
->base
);
230 /* Return non-zero if SCM is a <gdb:value> object. */
233 vlscm_is_value (SCM scm
)
235 return SCM_SMOB_PREDICATE (value_smob_tag
, scm
);
238 /* (value? object) -> boolean */
241 gdbscm_value_p (SCM scm
)
243 return scm_from_bool (vlscm_is_value (scm
));
246 /* Create a new <gdb:value> object that encapsulates VALUE.
247 The value is released from the all_values chain so its lifetime is not
248 bound to the execution of a command. */
251 vlscm_scm_from_value (struct value
*value
)
253 /* N.B. It's important to not cause any side-effects until we know the
254 conversion worked. */
255 SCM v_scm
= vlscm_make_value_smob ();
256 value_smob
*v_smob
= (value_smob
*) SCM_SMOB_DATA (v_scm
);
258 v_smob
->value
= release_value (value
).release ();
259 vlscm_remember_scheme_value (v_smob
);
264 /* Create a new <gdb:value> object that encapsulates VALUE.
265 The value is not released from the all_values chain. */
268 vlscm_scm_from_value_no_release (struct value
*value
)
270 /* N.B. It's important to not cause any side-effects until we know the
271 conversion worked. */
272 SCM v_scm
= vlscm_make_value_smob ();
273 value_smob
*v_smob
= (value_smob
*) SCM_SMOB_DATA (v_scm
);
275 value_incref (value
);
276 v_smob
->value
= value
;
277 vlscm_remember_scheme_value (v_smob
);
282 /* Returns the <gdb:value> object in SELF.
283 Throws an exception if SELF is not a <gdb:value> object. */
286 vlscm_get_value_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
288 SCM_ASSERT_TYPE (vlscm_is_value (self
), self
, arg_pos
, func_name
,
294 /* Returns a pointer to the value smob of SELF.
295 Throws an exception if SELF is not a <gdb:value> object. */
298 vlscm_get_value_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
300 SCM v_scm
= vlscm_get_value_arg_unsafe (self
, arg_pos
, func_name
);
301 value_smob
*v_smob
= (value_smob
*) SCM_SMOB_DATA (v_scm
);
306 /* Return the value field of V_SCM, an object of type <gdb:value>.
307 This exists so that we don't have to export the struct's contents. */
310 vlscm_scm_to_value (SCM v_scm
)
314 gdb_assert (vlscm_is_value (v_scm
));
315 v_smob
= (value_smob
*) SCM_SMOB_DATA (v_scm
);
316 return v_smob
->value
;
321 /* (make-value x [#:type type]) -> <gdb:value> */
324 gdbscm_make_value (SCM x
, SCM rest
)
326 const SCM keywords
[] = { type_keyword
, SCM_BOOL_F
};
328 int type_arg_pos
= -1;
329 SCM type_scm
= SCM_UNDEFINED
;
330 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG2
, keywords
, "#O", rest
,
331 &type_arg_pos
, &type_scm
);
333 struct type
*type
= NULL
;
334 if (type_arg_pos
> 0)
336 type_smob
*t_smob
= tyscm_get_type_smob_arg_unsafe (type_scm
,
339 type
= tyscm_type_smob_type (t_smob
);
342 return gdbscm_wrap ([=]
344 scoped_value_mark free_values
;
348 = vlscm_convert_typed_value_from_scheme (FUNC_NAME
, SCM_ARG1
, x
,
349 type_arg_pos
, type_scm
, type
,
356 return vlscm_scm_from_value (value
);
360 /* (make-lazy-value <gdb:type> address) -> <gdb:value> */
363 gdbscm_make_lazy_value (SCM type_scm
, SCM address_scm
)
365 type_smob
*t_smob
= tyscm_get_type_smob_arg_unsafe (type_scm
,
366 SCM_ARG1
, FUNC_NAME
);
367 struct type
*type
= tyscm_type_smob_type (t_smob
);
370 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG2
, NULL
, "U",
371 address_scm
, &address
);
373 return gdbscm_wrap ([=]
375 scoped_value_mark free_values
;
377 struct value
*value
= value_from_contents_and_address (type
, NULL
,
379 return vlscm_scm_from_value (value
);
383 /* (value-optimized-out? <gdb:value>) -> boolean */
386 gdbscm_value_optimized_out_p (SCM self
)
389 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
391 return gdbscm_wrap ([=]
393 return scm_from_bool (value_optimized_out (v_smob
->value
));
397 /* (value-address <gdb:value>) -> integer
398 Returns #f if the value doesn't have one. */
401 gdbscm_value_address (SCM self
)
404 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
405 struct value
*value
= v_smob
->value
;
407 return gdbscm_wrap ([=]
409 if (SCM_UNBNDP (v_smob
->address
))
411 scoped_value_mark free_values
;
413 SCM address
= SCM_BOOL_F
;
417 address
= vlscm_scm_from_value (value_addr (value
));
419 catch (const gdb_exception
&except
)
423 if (gdbscm_is_exception (address
))
426 v_smob
->address
= address
;
429 return v_smob
->address
;
433 /* (value-dereference <gdb:value>) -> <gdb:value>
434 Given a value of a pointer type, apply the C unary * operator to it. */
437 gdbscm_value_dereference (SCM self
)
440 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
442 return gdbscm_wrap ([=]
444 scoped_value_mark free_values
;
446 struct value
*res_val
= value_ind (v_smob
->value
);
447 return vlscm_scm_from_value (res_val
);
451 /* (value-referenced-value <gdb:value>) -> <gdb:value>
452 Given a value of a reference type, return the value referenced.
453 The difference between this function and gdbscm_value_dereference is that
454 the latter applies * unary operator to a value, which need not always
455 result in the value referenced.
456 For example, for a value which is a reference to an 'int' pointer ('int *'),
457 gdbscm_value_dereference will result in a value of type 'int' while
458 gdbscm_value_referenced_value will result in a value of type 'int *'. */
461 gdbscm_value_referenced_value (SCM self
)
464 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
465 struct value
*value
= v_smob
->value
;
467 return gdbscm_wrap ([=]
469 scoped_value_mark free_values
;
471 struct value
*res_val
;
473 switch (check_typedef (value_type (value
))->code ())
476 res_val
= value_ind (value
);
479 case TYPE_CODE_RVALUE_REF
:
480 res_val
= coerce_ref (value
);
483 error (_("Trying to get the referenced value from a value which is"
484 " neither a pointer nor a reference"));
487 return vlscm_scm_from_value (res_val
);
492 gdbscm_reference_value (SCM self
, enum type_code refcode
)
495 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
496 struct value
*value
= v_smob
->value
;
498 return gdbscm_wrap ([=]
500 scoped_value_mark free_values
;
502 struct value
*res_val
= value_ref (value
, refcode
);
503 return vlscm_scm_from_value (res_val
);
507 /* (value-reference-value <gdb:value>) -> <gdb:value> */
510 gdbscm_value_reference_value (SCM self
)
512 return gdbscm_reference_value (self
, TYPE_CODE_REF
);
515 /* (value-rvalue-reference-value <gdb:value>) -> <gdb:value> */
518 gdbscm_value_rvalue_reference_value (SCM self
)
520 return gdbscm_reference_value (self
, TYPE_CODE_RVALUE_REF
);
523 /* (value-const-value <gdb:value>) -> <gdb:value> */
526 gdbscm_value_const_value (SCM self
)
529 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
530 struct value
*value
= v_smob
->value
;
532 return gdbscm_wrap ([=]
534 scoped_value_mark free_values
;
536 struct value
*res_val
= make_cv_value (1, 0, value
);
537 return vlscm_scm_from_value (res_val
);
541 /* (value-type <gdb:value>) -> <gdb:type> */
544 gdbscm_value_type (SCM self
)
547 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
548 struct value
*value
= v_smob
->value
;
550 if (SCM_UNBNDP (v_smob
->type
))
551 v_smob
->type
= tyscm_scm_from_type (value_type (value
));
556 /* (value-dynamic-type <gdb:value>) -> <gdb:type> */
559 gdbscm_value_dynamic_type (SCM self
)
562 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
563 struct value
*value
= v_smob
->value
;
564 struct type
*type
= NULL
;
566 if (! SCM_UNBNDP (v_smob
->dynamic_type
))
567 return v_smob
->dynamic_type
;
569 gdbscm_gdb_exception exc
{};
572 scoped_value_mark free_values
;
574 type
= value_type (value
);
575 type
= check_typedef (type
);
577 if (((type
->code () == TYPE_CODE_PTR
)
578 || (type
->code () == TYPE_CODE_REF
))
579 && (type
->target_type ()->code () == TYPE_CODE_STRUCT
))
581 struct value
*target
;
582 int was_pointer
= type
->code () == TYPE_CODE_PTR
;
585 target
= value_ind (value
);
587 target
= coerce_ref (value
);
588 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
593 type
= lookup_pointer_type (type
);
595 type
= lookup_lvalue_reference_type (type
);
598 else if (type
->code () == TYPE_CODE_STRUCT
)
599 type
= value_rtti_type (value
, NULL
, NULL
, NULL
);
602 /* Re-use object's static type. */
606 catch (const gdb_exception
&except
)
608 exc
= unpack (except
);
611 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
613 v_smob
->dynamic_type
= gdbscm_value_type (self
);
615 v_smob
->dynamic_type
= tyscm_scm_from_type (type
);
617 return v_smob
->dynamic_type
;
620 /* A helper function that implements the various cast operators. */
623 vlscm_do_cast (SCM self
, SCM type_scm
, enum exp_opcode op
,
624 const char *func_name
)
627 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
628 struct value
*value
= v_smob
->value
;
630 = tyscm_get_type_smob_arg_unsafe (type_scm
, SCM_ARG2
, FUNC_NAME
);
631 struct type
*type
= tyscm_type_smob_type (t_smob
);
633 return gdbscm_wrap ([=]
635 scoped_value_mark free_values
;
637 struct value
*res_val
;
638 if (op
== UNOP_DYNAMIC_CAST
)
639 res_val
= value_dynamic_cast (type
, value
);
640 else if (op
== UNOP_REINTERPRET_CAST
)
641 res_val
= value_reinterpret_cast (type
, value
);
644 gdb_assert (op
== UNOP_CAST
);
645 res_val
= value_cast (type
, value
);
648 return vlscm_scm_from_value (res_val
);
652 /* (value-cast <gdb:value> <gdb:type>) -> <gdb:value> */
655 gdbscm_value_cast (SCM self
, SCM new_type
)
657 return vlscm_do_cast (self
, new_type
, UNOP_CAST
, FUNC_NAME
);
660 /* (value-dynamic-cast <gdb:value> <gdb:type>) -> <gdb:value> */
663 gdbscm_value_dynamic_cast (SCM self
, SCM new_type
)
665 return vlscm_do_cast (self
, new_type
, UNOP_DYNAMIC_CAST
, FUNC_NAME
);
668 /* (value-reinterpret-cast <gdb:value> <gdb:type>) -> <gdb:value> */
671 gdbscm_value_reinterpret_cast (SCM self
, SCM new_type
)
673 return vlscm_do_cast (self
, new_type
, UNOP_REINTERPRET_CAST
, FUNC_NAME
);
676 /* (value-field <gdb:value> string) -> <gdb:value>
677 Given string name of an element inside structure, return its <gdb:value>
681 gdbscm_value_field (SCM self
, SCM field_scm
)
684 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
686 SCM_ASSERT_TYPE (scm_is_string (field_scm
), field_scm
, SCM_ARG2
, FUNC_NAME
,
689 return gdbscm_wrap ([=]
691 scoped_value_mark free_values
;
693 gdb::unique_xmalloc_ptr
<char> field
= gdbscm_scm_to_c_string (field_scm
);
695 struct value
*tmp
= v_smob
->value
;
697 struct value
*res_val
= value_struct_elt (&tmp
, {}, field
.get (), NULL
,
698 "struct/class/union");
700 return vlscm_scm_from_value (res_val
);
704 /* (value-subscript <gdb:value> integer|<gdb:value>) -> <gdb:value>
705 Return the specified value in an array. */
708 gdbscm_value_subscript (SCM self
, SCM index_scm
)
711 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
712 struct value
*value
= v_smob
->value
;
713 struct type
*type
= value_type (value
);
715 SCM_ASSERT (type
!= NULL
, self
, SCM_ARG2
, FUNC_NAME
);
717 return gdbscm_wrap ([=]
719 scoped_value_mark free_values
;
723 = vlscm_convert_value_from_scheme (FUNC_NAME
, SCM_ARG2
, index_scm
,
730 /* Assume we are attempting an array access, and let the value code
731 throw an exception if the index has an invalid type.
732 Check the value's type is something that can be accessed via
734 struct value
*tmp
= coerce_ref (value
);
735 struct type
*tmp_type
= check_typedef (value_type (tmp
));
736 if (tmp_type
->code () != TYPE_CODE_ARRAY
737 && tmp_type
->code () != TYPE_CODE_PTR
)
738 error (_("Cannot subscript requested type"));
740 struct value
*res_val
= value_subscript (tmp
, value_as_long (index
));
741 return vlscm_scm_from_value (res_val
);
745 /* (value-call <gdb:value> arg-list) -> <gdb:value>
746 Perform an inferior function call on the value. */
749 gdbscm_value_call (SCM self
, SCM args
)
752 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
753 struct value
*function
= v_smob
->value
;
754 struct type
*ftype
= NULL
;
756 struct value
**vargs
= NULL
;
758 gdbscm_gdb_exception exc
{};
761 ftype
= check_typedef (value_type (function
));
763 catch (const gdb_exception
&except
)
765 exc
= unpack (except
);
768 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
769 SCM_ASSERT_TYPE (ftype
->code () == TYPE_CODE_FUNC
, self
,
771 _("function (value of TYPE_CODE_FUNC)"));
773 SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (args
)), args
,
774 SCM_ARG2
, FUNC_NAME
, _("list"));
776 args_count
= scm_ilength (args
);
779 struct gdbarch
*gdbarch
= get_current_arch ();
780 const struct language_defn
*language
= current_language
;
784 vargs
= XALLOCAVEC (struct value
*, args_count
);
785 for (i
= 0; i
< args_count
; i
++)
787 SCM arg
= scm_car (args
);
789 vargs
[i
] = vlscm_convert_value_from_scheme (FUNC_NAME
,
790 GDBSCM_ARG_NONE
, arg
,
793 if (vargs
[i
] == NULL
)
794 gdbscm_throw (except_scm
);
796 args
= scm_cdr (args
);
798 gdb_assert (gdbscm_is_true (scm_null_p (args
)));
801 return gdbscm_wrap ([=]
803 scoped_value_mark free_values
;
805 auto av
= gdb::make_array_view (vargs
, args_count
);
806 value
*return_value
= call_function_by_hand (function
, NULL
, av
);
807 return vlscm_scm_from_value (return_value
);
811 /* (value->bytevector <gdb:value>) -> bytevector */
814 gdbscm_value_to_bytevector (SCM self
)
817 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
818 struct value
*value
= v_smob
->value
;
821 const gdb_byte
*contents
= NULL
;
824 type
= value_type (value
);
826 gdbscm_gdb_exception exc
{};
829 type
= check_typedef (type
);
830 length
= type
->length ();
831 contents
= value_contents (value
).data ();
833 catch (const gdb_exception
&except
)
835 exc
= unpack (except
);
838 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
839 bv
= scm_c_make_bytevector (length
);
840 memcpy (SCM_BYTEVECTOR_CONTENTS (bv
), contents
, length
);
845 /* Helper function to determine if a type is "int-like". */
848 is_intlike (struct type
*type
, int ptr_ok
)
850 return (type
->code () == TYPE_CODE_INT
851 || type
->code () == TYPE_CODE_ENUM
852 || type
->code () == TYPE_CODE_BOOL
853 || type
->code () == TYPE_CODE_CHAR
854 || (ptr_ok
&& type
->code () == TYPE_CODE_PTR
));
857 /* (value->bool <gdb:value>) -> boolean
858 Throws an error if the value is not integer-like. */
861 gdbscm_value_to_bool (SCM self
)
864 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
865 struct value
*value
= v_smob
->value
;
869 type
= value_type (value
);
871 gdbscm_gdb_exception exc
{};
874 type
= check_typedef (type
);
876 catch (const gdb_exception
&except
)
878 exc
= unpack (except
);
881 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
882 SCM_ASSERT_TYPE (is_intlike (type
, 1), self
, SCM_ARG1
, FUNC_NAME
,
883 _("integer-like gdb value"));
887 if (type
->code () == TYPE_CODE_PTR
)
888 l
= value_as_address (value
);
890 l
= value_as_long (value
);
892 catch (const gdb_exception
&except
)
894 exc
= unpack (except
);
897 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
898 return scm_from_bool (l
!= 0);
901 /* (value->integer <gdb:value>) -> integer
902 Throws an error if the value is not integer-like. */
905 gdbscm_value_to_integer (SCM self
)
908 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
909 struct value
*value
= v_smob
->value
;
913 type
= value_type (value
);
915 gdbscm_gdb_exception exc
{};
918 type
= check_typedef (type
);
920 catch (const gdb_exception
&except
)
922 exc
= unpack (except
);
925 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
926 SCM_ASSERT_TYPE (is_intlike (type
, 1), self
, SCM_ARG1
, FUNC_NAME
,
927 _("integer-like gdb value"));
931 if (type
->code () == TYPE_CODE_PTR
)
932 l
= value_as_address (value
);
934 l
= value_as_long (value
);
936 catch (const gdb_exception
&except
)
938 exc
= unpack (except
);
941 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
942 if (type
->is_unsigned ())
943 return gdbscm_scm_from_ulongest (l
);
945 return gdbscm_scm_from_longest (l
);
948 /* (value->real <gdb:value>) -> real
949 Throws an error if the value is not a number. */
952 gdbscm_value_to_real (SCM self
)
955 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
956 struct value
*value
= v_smob
->value
;
959 struct value
*check
= nullptr;
961 type
= value_type (value
);
963 gdbscm_gdb_exception exc
{};
966 type
= check_typedef (type
);
968 catch (const gdb_exception
&except
)
970 exc
= unpack (except
);
973 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
974 SCM_ASSERT_TYPE (is_intlike (type
, 0) || type
->code () == TYPE_CODE_FLT
,
975 self
, SCM_ARG1
, FUNC_NAME
, _("number"));
979 if (is_floating_value (value
))
981 d
= target_float_to_host_double (value_contents (value
).data (),
983 check
= value_from_host_double (type
, d
);
985 else if (type
->is_unsigned ())
987 d
= (ULONGEST
) value_as_long (value
);
988 check
= value_from_ulongest (type
, (ULONGEST
) d
);
992 d
= value_as_long (value
);
993 check
= value_from_longest (type
, (LONGEST
) d
);
996 catch (const gdb_exception
&except
)
998 exc
= unpack (except
);
1001 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1002 /* TODO: Is there a better way to check if the value fits? */
1003 if (!value_equal (value
, check
))
1004 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, self
,
1005 _("number can't be converted to a double"));
1007 return scm_from_double (d
);
1010 /* (value->string <gdb:value>
1011 [#:encoding encoding]
1012 [#:errors #f | 'error | 'substitute]
1015 Return Unicode string with value's contents, which must be a string.
1017 If ENCODING is not given, the string is assumed to be encoded in
1018 the target's charset.
1020 ERRORS is one of #f, 'error or 'substitute.
1021 An error setting of #f means use the default, which is Guile's
1022 %default-port-conversion-strategy when using Guile >= 2.0.6, or 'error if
1023 using an earlier version of Guile. Earlier versions do not properly
1024 support obtaining the default port conversion strategy.
1025 If the default is not one of 'error or 'substitute, 'substitute is used.
1026 An error setting of "error" causes an exception to be thrown if there's
1027 a decoding error. An error setting of "substitute" causes invalid
1028 characters to be replaced with "?".
1030 If LENGTH is provided, only fetch string to the length provided.
1031 LENGTH must be a Scheme integer, it can't be a <gdb:value> integer. */
1034 gdbscm_value_to_string (SCM self
, SCM rest
)
1037 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1038 struct value
*value
= v_smob
->value
;
1039 const SCM keywords
[] = {
1040 encoding_keyword
, errors_keyword
, length_keyword
, SCM_BOOL_F
1042 int encoding_arg_pos
= -1, errors_arg_pos
= -1, length_arg_pos
= -1;
1043 char *encoding
= NULL
;
1044 SCM errors
= SCM_BOOL_F
;
1045 /* Avoid an uninitialized warning from gcc. */
1046 gdb_byte
*buffer_contents
= nullptr;
1048 const char *la_encoding
= NULL
;
1049 struct type
*char_type
= NULL
;
1052 /* The sequencing here, as everywhere else, is important.
1053 We can't have existing cleanups when a Scheme exception is thrown. */
1055 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG2
, keywords
, "#sOi", rest
,
1056 &encoding_arg_pos
, &encoding
,
1057 &errors_arg_pos
, &errors
,
1058 &length_arg_pos
, &length
);
1060 if (errors_arg_pos
> 0
1061 && errors
!= SCM_BOOL_F
1062 && !scm_is_eq (errors
, error_symbol
)
1063 && !scm_is_eq (errors
, substitute_symbol
))
1066 = gdbscm_make_out_of_range_error (FUNC_NAME
, errors_arg_pos
, errors
,
1067 _("invalid error kind"));
1070 gdbscm_throw (excp
);
1072 if (errors
== SCM_BOOL_F
)
1074 /* N.B. scm_port_conversion_strategy in Guile versions prior to 2.0.6
1075 will throw a Scheme error when passed #f. */
1076 if (gdbscm_guile_version_is_at_least (2, 0, 6))
1077 errors
= scm_port_conversion_strategy (SCM_BOOL_F
);
1079 errors
= error_symbol
;
1081 /* We don't assume anything about the result of scm_port_conversion_strategy.
1082 From this point on, if errors is not 'errors, use 'substitute. */
1084 gdbscm_gdb_exception exc
{};
1087 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
;
1088 c_get_string (value
, &buffer
, &length
, &char_type
, &la_encoding
);
1089 buffer_contents
= buffer
.release ();
1091 catch (const gdb_exception
&except
)
1094 exc
= unpack (except
);
1096 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1098 /* If errors is "error", scm_from_stringn may throw a Scheme exception.
1099 Make sure we don't leak. This is done via scm_dynwind_begin, et.al. */
1101 scm_dynwind_begin ((scm_t_dynwind_flags
) 0);
1103 gdbscm_dynwind_xfree (encoding
);
1104 gdbscm_dynwind_xfree (buffer_contents
);
1106 result
= scm_from_stringn ((const char *) buffer_contents
,
1107 length
* char_type
->length (),
1108 (encoding
!= NULL
&& *encoding
!= '\0'
1111 scm_is_eq (errors
, error_symbol
)
1112 ? SCM_FAILED_CONVERSION_ERROR
1113 : SCM_FAILED_CONVERSION_QUESTION_MARK
);
1120 /* (value->lazy-string <gdb:value> [#:encoding encoding] [#:length length])
1121 -> <gdb:lazy-string>
1122 Return a Scheme object representing a lazy_string_object type.
1123 A lazy string is a pointer to a string with an optional encoding and length.
1124 If ENCODING is not given, the target's charset is used.
1125 If LENGTH is provided then the length parameter is set to LENGTH.
1126 Otherwise if the value is an array of known length then the array's length
1127 is used. Otherwise the length will be set to -1 (meaning first null of
1129 LENGTH must be a Scheme integer, it can't be a <gdb:value> integer. */
1132 gdbscm_value_to_lazy_string (SCM self
, SCM rest
)
1135 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1136 struct value
*value
= v_smob
->value
;
1137 const SCM keywords
[] = { encoding_keyword
, length_keyword
, SCM_BOOL_F
};
1138 int encoding_arg_pos
= -1, length_arg_pos
= -1;
1139 char *encoding
= NULL
;
1141 SCM result
= SCM_BOOL_F
; /* -Wall */
1142 gdbscm_gdb_exception except
{};
1144 /* The sequencing here, as everywhere else, is important.
1145 We can't have existing cleanups when a Scheme exception is thrown. */
1147 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG2
, keywords
, "#si", rest
,
1148 &encoding_arg_pos
, &encoding
,
1149 &length_arg_pos
, &length
);
1153 gdbscm_out_of_range_error (FUNC_NAME
, length_arg_pos
,
1154 scm_from_int (length
),
1155 _("invalid length"));
1160 scoped_value_mark free_values
;
1162 struct type
*type
, *realtype
;
1165 type
= value_type (value
);
1166 realtype
= check_typedef (type
);
1168 switch (realtype
->code ())
1170 case TYPE_CODE_ARRAY
:
1172 LONGEST array_length
= -1;
1173 LONGEST low_bound
, high_bound
;
1175 /* PR 20786: There's no way to specify an array of length zero.
1176 Record a length of [0,-1] which is how Ada does it. Anything
1177 we do is broken, but this one possible solution. */
1178 if (get_array_bounds (realtype
, &low_bound
, &high_bound
))
1179 array_length
= high_bound
- low_bound
+ 1;
1181 length
= array_length
;
1182 else if (array_length
== -1)
1184 type
= lookup_array_range_type (realtype
->target_type (),
1187 else if (length
!= array_length
)
1189 /* We need to create a new array type with the
1190 specified length. */
1191 if (length
> array_length
)
1192 error (_("length is larger than array size"));
1193 type
= lookup_array_range_type (type
->target_type (),
1195 low_bound
+ length
- 1);
1197 addr
= value_address (value
);
1201 /* If a length is specified we defer creating an array of the
1202 specified width until we need to. */
1203 addr
= value_as_address (value
);
1206 /* Should flag an error here. PR 20769. */
1207 addr
= value_address (value
);
1211 result
= lsscm_make_lazy_string (addr
, length
, encoding
, type
);
1213 catch (const gdb_exception
&ex
)
1215 except
= unpack (ex
);
1219 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
1221 if (gdbscm_is_exception (result
))
1222 gdbscm_throw (result
);
1227 /* (value-lazy? <gdb:value>) -> boolean */
1230 gdbscm_value_lazy_p (SCM self
)
1233 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1234 struct value
*value
= v_smob
->value
;
1236 return scm_from_bool (value_lazy (value
));
1239 /* (value-fetch-lazy! <gdb:value>) -> unspecified */
1242 gdbscm_value_fetch_lazy_x (SCM self
)
1245 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1246 struct value
*value
= v_smob
->value
;
1248 return gdbscm_wrap ([=]
1250 if (value_lazy (value
))
1251 value_fetch_lazy (value
);
1252 return SCM_UNSPECIFIED
;
1256 /* (value-print <gdb:value>) -> string */
1259 gdbscm_value_print (SCM self
)
1262 = vlscm_get_value_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1263 struct value
*value
= v_smob
->value
;
1264 struct value_print_options opts
;
1266 get_user_print_options (&opts
);
1271 gdbscm_gdb_exception exc
{};
1274 common_val_print (value
, &stb
, 0, &opts
, current_language
);
1276 catch (const gdb_exception
&except
)
1278 exc
= unpack (except
);
1281 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
1282 /* Use SCM_FAILED_CONVERSION_QUESTION_MARK to ensure this doesn't
1283 throw an error if the encoding fails.
1284 IWBN to use scm_take_locale_string here, but we'd have to temporarily
1285 override the default port conversion handler because contrary to
1286 documentation it doesn't necessarily free the input string. */
1287 return scm_from_stringn (stb
.c_str (), stb
.size (), host_charset (),
1288 SCM_FAILED_CONVERSION_QUESTION_MARK
);
1291 /* (parse-and-eval string) -> <gdb:value>
1292 Parse a string and evaluate the string as an expression. */
1295 gdbscm_parse_and_eval (SCM expr_scm
)
1298 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, NULL
, "s",
1299 expr_scm
, &expr_str
);
1301 return gdbscm_wrap ([=]
1303 scoped_value_mark free_values
;
1304 return vlscm_scm_from_value (parse_and_eval (expr_str
));
1308 /* (history-ref integer) -> <gdb:value>
1309 Return the specified value from GDB's value history. */
1312 gdbscm_history_ref (SCM index
)
1315 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, NULL
, "i", index
, &i
);
1317 return gdbscm_wrap ([=]
1319 return vlscm_scm_from_value (access_value_history (i
));
1323 /* (history-append! <gdb:value>) -> index
1324 Append VALUE to GDB's value history. Return its index in the history. */
1327 gdbscm_history_append_x (SCM value
)
1330 = vlscm_get_value_smob_arg_unsafe (value
, SCM_ARG1
, FUNC_NAME
);
1331 return gdbscm_wrap ([=]
1333 return scm_from_int (record_latest_value (v_smob
->value
));
1337 /* Initialize the Scheme value code. */
1339 static const scheme_function value_functions
[] =
1341 { "value?", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_p
),
1343 Return #t if the object is a <gdb:value> object." },
1345 { "make-value", 1, 0, 1, as_a_scm_t_subr (gdbscm_make_value
),
1347 Create a <gdb:value> representing object.\n\
1348 Typically this is used to convert numbers and strings to\n\
1349 <gdb:value> objects.\n\
1351 Arguments: object [#:type <gdb:type>]" },
1353 { "value-optimized-out?", 1, 0, 0,
1354 as_a_scm_t_subr (gdbscm_value_optimized_out_p
),
1356 Return #t if the value has been optimizd out." },
1358 { "value-address", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_address
),
1360 Return the address of the value." },
1362 { "value-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_type
),
1364 Return the type of the value." },
1366 { "value-dynamic-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_dynamic_type
),
1368 Return the dynamic type of the value." },
1370 { "value-cast", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_cast
),
1372 Cast the value to the supplied type.\n\
1374 Arguments: <gdb:value> <gdb:type>" },
1376 { "value-dynamic-cast", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_dynamic_cast
),
1378 Cast the value to the supplied type, as if by the C++\n\
1379 dynamic_cast operator.\n\
1381 Arguments: <gdb:value> <gdb:type>" },
1383 { "value-reinterpret-cast", 2, 0, 0,
1384 as_a_scm_t_subr (gdbscm_value_reinterpret_cast
),
1386 Cast the value to the supplied type, as if by the C++\n\
1387 reinterpret_cast operator.\n\
1389 Arguments: <gdb:value> <gdb:type>" },
1391 { "value-dereference", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_dereference
),
1393 Return the result of applying the C unary * operator to the value." },
1395 { "value-referenced-value", 1, 0, 0,
1396 as_a_scm_t_subr (gdbscm_value_referenced_value
),
1398 Given a value of a reference type, return the value referenced.\n\
1399 The difference between this function and value-dereference is that\n\
1400 the latter applies * unary operator to a value, which need not always\n\
1401 result in the value referenced.\n\
1402 For example, for a value which is a reference to an 'int' pointer ('int *'),\n\
1403 value-dereference will result in a value of type 'int' while\n\
1404 value-referenced-value will result in a value of type 'int *'." },
1406 { "value-reference-value", 1, 0, 0,
1407 as_a_scm_t_subr (gdbscm_value_reference_value
),
1409 Return a <gdb:value> object which is a reference to the given value." },
1411 { "value-rvalue-reference-value", 1, 0, 0,
1412 as_a_scm_t_subr (gdbscm_value_rvalue_reference_value
),
1414 Return a <gdb:value> object which is an rvalue reference to the given value." },
1416 { "value-const-value", 1, 0, 0,
1417 as_a_scm_t_subr (gdbscm_value_const_value
),
1419 Return a <gdb:value> object which is a 'const' version of the given value." },
1421 { "value-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_field
),
1423 Return the specified field of the value.\n\
1425 Arguments: <gdb:value> string" },
1427 { "value-subscript", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_subscript
),
1429 Return the value of the array at the specified index.\n\
1431 Arguments: <gdb:value> integer" },
1433 { "value-call", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_call
),
1435 Perform an inferior function call taking the value as a pointer to the\n\
1436 function to call.\n\
1437 Each element of the argument list must be a <gdb:value> object or an object\n\
1438 that can be converted to one.\n\
1439 The result is the value returned by the function.\n\
1441 Arguments: <gdb:value> arg-list" },
1443 { "value->bool", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_bool
),
1445 Return the Scheme boolean representing the GDB value.\n\
1446 The value must be \"integer like\". Pointers are ok." },
1448 { "value->integer", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_integer
),
1450 Return the Scheme integer representing the GDB value.\n\
1451 The value must be \"integer like\". Pointers are ok." },
1453 { "value->real", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_real
),
1455 Return the Scheme real number representing the GDB value.\n\
1456 The value must be a number." },
1458 { "value->bytevector", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_bytevector
),
1460 Return a Scheme bytevector with the raw contents of the GDB value.\n\
1461 No transformation, endian or otherwise, is performed." },
1463 { "value->string", 1, 0, 1, as_a_scm_t_subr (gdbscm_value_to_string
),
1465 Return the Unicode string of the value's contents.\n\
1466 If ENCODING is not given, the string is assumed to be encoded in\n\
1467 the target's charset.\n\
1468 An error setting \"error\" causes an exception to be thrown if there's\n\
1469 a decoding error. An error setting of \"substitute\" causes invalid\n\
1470 characters to be replaced with \"?\". The default is \"error\".\n\
1471 If LENGTH is provided, only fetch string to the length provided.\n\
1473 Arguments: <gdb:value>\n\
1474 [#:encoding encoding] [#:errors \"error\"|\"substitute\"]\n\
1475 [#:length length]" },
1477 { "value->lazy-string", 1, 0, 1,
1478 as_a_scm_t_subr (gdbscm_value_to_lazy_string
),
1480 Return a Scheme object representing a lazily fetched Unicode string\n\
1481 of the value's contents.\n\
1482 If ENCODING is not given, the string is assumed to be encoded in\n\
1483 the target's charset.\n\
1484 If LENGTH is provided, only fetch string to the length provided.\n\
1486 Arguments: <gdb:value> [#:encoding encoding] [#:length length]" },
1488 { "value-lazy?", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_lazy_p
),
1490 Return #t if the value is lazy (not fetched yet from the inferior).\n\
1491 A lazy value is fetched when needed, or when the value-fetch-lazy! function\n\
1494 { "make-lazy-value", 2, 0, 0, as_a_scm_t_subr (gdbscm_make_lazy_value
),
1496 Create a <gdb:value> that will be lazily fetched from the target.\n\
1498 Arguments: <gdb:type> address" },
1500 { "value-fetch-lazy!", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_fetch_lazy_x
),
1502 Fetch the value from the inferior, if it was lazy.\n\
1503 The result is \"unspecified\"." },
1505 { "value-print", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_print
),
1507 Return the string representation (print form) of the value." },
1509 { "parse-and-eval", 1, 0, 0, as_a_scm_t_subr (gdbscm_parse_and_eval
),
1511 Evaluates string in gdb and returns the result as a <gdb:value> object." },
1513 { "history-ref", 1, 0, 0, as_a_scm_t_subr (gdbscm_history_ref
),
1515 Return the specified value from GDB's value history." },
1517 { "history-append!", 1, 0, 0, as_a_scm_t_subr (gdbscm_history_append_x
),
1519 Append the specified value onto GDB's value history." },
1525 gdbscm_initialize_values (void)
1527 value_smob_tag
= gdbscm_make_smob_type (value_smob_name
,
1528 sizeof (value_smob
));
1529 scm_set_smob_free (value_smob_tag
, vlscm_free_value_smob
);
1530 scm_set_smob_print (value_smob_tag
, vlscm_print_value_smob
);
1531 scm_set_smob_equalp (value_smob_tag
, vlscm_equal_p_value_smob
);
1533 gdbscm_define_functions (value_functions
, 1);
1535 type_keyword
= scm_from_latin1_keyword ("type");
1536 encoding_keyword
= scm_from_latin1_keyword ("encoding");
1537 errors_keyword
= scm_from_latin1_keyword ("errors");
1538 length_keyword
= scm_from_latin1_keyword ("length");
1540 error_symbol
= scm_from_latin1_symbol ("error");
1541 escape_symbol
= scm_from_latin1_symbol ("escape");
1542 substitute_symbol
= scm_from_latin1_symbol ("substitute");