More updated translations
[binutils-gdb.git] / gdb / guile / scm-value.c
blob0f4a6a46da0e82471971a6721580f600512abb46
1 /* Scheme interface to values.
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. */
23 #include "top.h"
24 #include "arch-utils.h"
25 #include "charset.h"
26 #include "cp-abi.h"
27 #include "target-float.h"
28 #include "infcall.h"
29 #include "symtab.h"
30 #include "language.h"
31 #include "valprint.h"
32 #include "value.h"
33 #include "guile-internal.h"
35 /* The <gdb:value> smob. */
37 struct value_smob
39 /* This always appears first. */
40 gdb_smob base;
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. */
45 value_smob *next;
46 value_smob *prev;
48 struct value *value;
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. */
55 SCM address;
56 SCM type;
57 SCM dynamic_type;
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
84 each.
85 This is the extension_language_ops.preserve_values "method". */
87 void
88 gdbscm_preserve_values (const struct extension_language_defn *extlang,
89 struct objfile *objfile,
90 copied_types_hash_t &copied_types)
92 value_smob *iter;
94 for (iter = values_in_scheme; iter; iter = iter->next)
95 iter->value->preserve (objfile, copied_types);
98 /* Helper to add a value_smob to the global list. */
100 static void
101 vlscm_remember_scheme_value (value_smob *v_smob)
103 v_smob->next = values_in_scheme;
104 if (v_smob->next)
105 v_smob->next->prev = v_smob;
106 v_smob->prev = NULL;
107 values_in_scheme = v_smob;
110 /* Helper to remove a value_smob from the global list. */
112 static void
113 vlscm_forget_value_smob (value_smob *v_smob)
115 /* Remove SELF from the global list. */
116 if (v_smob->prev)
117 v_smob->prev->next = v_smob->next;
118 else
120 gdb_assert (values_in_scheme == v_smob);
121 values_in_scheme = v_smob->next;
123 if (v_smob->next)
124 v_smob->next->prev = v_smob->prev;
127 /* The smob "free" function for <gdb:value>. */
129 static size_t
130 vlscm_free_value_smob (SCM self)
132 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
134 vlscm_forget_value_smob (v_smob);
135 v_smob->value->decref ();
137 return 0;
140 /* The smob "print" function for <gdb:value>. */
142 static int
143 vlscm_print_value_smob (SCM self, SCM port, scm_print_state *pstate)
145 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
146 struct value_print_options opts;
148 if (pstate->writingp)
149 gdbscm_printf (port, "#<%s ", value_smob_name);
151 get_user_print_options (&opts);
152 opts.deref_ref = false;
154 /* pstate->writingp = zero if invoked by display/~A, and nonzero if
155 invoked by write/~S. What to do here may need to evolve.
156 IWBN if we could pass an argument to format that would we could use
157 instead of writingp. */
158 opts.raw = !!pstate->writingp;
160 gdbscm_gdb_exception exc {};
163 string_file stb;
165 common_val_print (v_smob->value, &stb, 0, &opts, current_language);
166 scm_puts (stb.c_str (), port);
168 catch (const gdb_exception &except)
170 exc = unpack (except);
173 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
174 if (pstate->writingp)
175 scm_puts (">", port);
177 scm_remember_upto_here_1 (self);
179 /* Non-zero means success. */
180 return 1;
183 /* The smob "equalp" function for <gdb:value>. */
185 static SCM
186 vlscm_equal_p_value_smob (SCM v1, SCM v2)
188 const value_smob *v1_smob = (value_smob *) SCM_SMOB_DATA (v1);
189 const value_smob *v2_smob = (value_smob *) SCM_SMOB_DATA (v2);
190 int result = 0;
192 gdbscm_gdb_exception exc {};
195 result = value_equal (v1_smob->value, v2_smob->value);
197 catch (const gdb_exception &except)
199 exc = unpack (except);
202 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
203 return scm_from_bool (result);
206 /* Low level routine to create a <gdb:value> object. */
208 static SCM
209 vlscm_make_value_smob (void)
211 value_smob *v_smob = (value_smob *)
212 scm_gc_malloc (sizeof (value_smob), value_smob_name);
213 SCM v_scm;
215 /* These must be filled in by the caller. */
216 v_smob->value = NULL;
217 v_smob->prev = NULL;
218 v_smob->next = NULL;
220 /* These are lazily computed. */
221 v_smob->address = SCM_UNDEFINED;
222 v_smob->type = SCM_UNDEFINED;
223 v_smob->dynamic_type = SCM_UNDEFINED;
225 v_scm = scm_new_smob (value_smob_tag, (scm_t_bits) v_smob);
226 gdbscm_init_gsmob (&v_smob->base);
228 return v_scm;
231 /* Return non-zero if SCM is a <gdb:value> object. */
234 vlscm_is_value (SCM scm)
236 return SCM_SMOB_PREDICATE (value_smob_tag, scm);
239 /* (value? object) -> boolean */
241 static SCM
242 gdbscm_value_p (SCM scm)
244 return scm_from_bool (vlscm_is_value (scm));
247 /* Create a new <gdb:value> object that encapsulates VALUE.
248 The value is released from the all_values chain so its lifetime is not
249 bound to the execution of a command. */
252 vlscm_scm_from_value (struct value *value)
254 /* N.B. It's important to not cause any side-effects until we know the
255 conversion worked. */
256 SCM v_scm = vlscm_make_value_smob ();
257 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
259 v_smob->value = release_value (value).release ();
260 vlscm_remember_scheme_value (v_smob);
262 return v_scm;
265 /* Create a new <gdb:value> object that encapsulates VALUE.
266 The value is not released from the all_values chain. */
269 vlscm_scm_from_value_no_release (struct value *value)
271 /* N.B. It's important to not cause any side-effects until we know the
272 conversion worked. */
273 SCM v_scm = vlscm_make_value_smob ();
274 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
276 value->incref ();
277 v_smob->value = value;
278 vlscm_remember_scheme_value (v_smob);
280 return v_scm;
283 /* Returns the <gdb:value> object in SELF.
284 Throws an exception if SELF is not a <gdb:value> object. */
286 static SCM
287 vlscm_get_value_arg_unsafe (SCM self, int arg_pos, const char *func_name)
289 SCM_ASSERT_TYPE (vlscm_is_value (self), self, arg_pos, func_name,
290 value_smob_name);
292 return self;
295 /* Returns a pointer to the value smob of SELF.
296 Throws an exception if SELF is not a <gdb:value> object. */
298 static value_smob *
299 vlscm_get_value_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
301 SCM v_scm = vlscm_get_value_arg_unsafe (self, arg_pos, func_name);
302 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
304 return v_smob;
307 /* Return the value field of V_SCM, an object of type <gdb:value>.
308 This exists so that we don't have to export the struct's contents. */
310 struct value *
311 vlscm_scm_to_value (SCM v_scm)
313 value_smob *v_smob;
315 gdb_assert (vlscm_is_value (v_scm));
316 v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
317 return v_smob->value;
320 /* Value methods. */
322 /* (make-value x [#:type type]) -> <gdb:value> */
324 static SCM
325 gdbscm_make_value (SCM x, SCM rest)
327 const SCM keywords[] = { type_keyword, SCM_BOOL_F };
329 int type_arg_pos = -1;
330 SCM type_scm = SCM_UNDEFINED;
331 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#O", rest,
332 &type_arg_pos, &type_scm);
334 struct type *type = NULL;
335 if (type_arg_pos > 0)
337 type_smob *t_smob = tyscm_get_type_smob_arg_unsafe (type_scm,
338 type_arg_pos,
339 FUNC_NAME);
340 type = tyscm_type_smob_type (t_smob);
343 return gdbscm_wrap ([=]
345 scoped_value_mark free_values;
347 SCM except_scm;
348 struct value *value
349 = vlscm_convert_typed_value_from_scheme (FUNC_NAME, SCM_ARG1, x,
350 type_arg_pos, type_scm, type,
351 &except_scm,
352 get_current_arch (),
353 current_language);
354 if (value == NULL)
355 return except_scm;
357 return vlscm_scm_from_value (value);
361 /* (make-lazy-value <gdb:type> address) -> <gdb:value> */
363 static SCM
364 gdbscm_make_lazy_value (SCM type_scm, SCM address_scm)
366 type_smob *t_smob = tyscm_get_type_smob_arg_unsafe (type_scm,
367 SCM_ARG1, FUNC_NAME);
368 struct type *type = tyscm_type_smob_type (t_smob);
370 ULONGEST address;
371 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, NULL, "U",
372 address_scm, &address);
374 return gdbscm_wrap ([=]
376 scoped_value_mark free_values;
378 struct value *value = value_from_contents_and_address (type, NULL,
379 address);
380 return vlscm_scm_from_value (value);
384 /* (value-optimized-out? <gdb:value>) -> boolean */
386 static SCM
387 gdbscm_value_optimized_out_p (SCM self)
389 value_smob *v_smob
390 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
392 return gdbscm_wrap ([=]
394 return scm_from_bool (v_smob->value->optimized_out ());
398 /* (value-address <gdb:value>) -> integer
399 Returns #f if the value doesn't have one. */
401 static SCM
402 gdbscm_value_address (SCM self)
404 value_smob *v_smob
405 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
406 struct value *value = v_smob->value;
408 return gdbscm_wrap ([=]
410 if (SCM_UNBNDP (v_smob->address))
412 scoped_value_mark free_values;
414 SCM address = SCM_BOOL_F;
418 address = vlscm_scm_from_value (value_addr (value));
420 catch (const gdb_exception_forced_quit &except)
422 quit_force (NULL, 0);
424 catch (const gdb_exception &except)
428 if (gdbscm_is_exception (address))
429 return address;
431 v_smob->address = address;
434 return v_smob->address;
438 /* (value-dereference <gdb:value>) -> <gdb:value>
439 Given a value of a pointer type, apply the C unary * operator to it. */
441 static SCM
442 gdbscm_value_dereference (SCM self)
444 value_smob *v_smob
445 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
447 return gdbscm_wrap ([=]
449 scoped_value_mark free_values;
451 struct value *res_val = value_ind (v_smob->value);
452 return vlscm_scm_from_value (res_val);
456 /* (value-referenced-value <gdb:value>) -> <gdb:value>
457 Given a value of a reference type, return the value referenced.
458 The difference between this function and gdbscm_value_dereference is that
459 the latter applies * unary operator to a value, which need not always
460 result in the value referenced.
461 For example, for a value which is a reference to an 'int' pointer ('int *'),
462 gdbscm_value_dereference will result in a value of type 'int' while
463 gdbscm_value_referenced_value will result in a value of type 'int *'. */
465 static SCM
466 gdbscm_value_referenced_value (SCM self)
468 value_smob *v_smob
469 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
470 struct value *value = v_smob->value;
472 return gdbscm_wrap ([=]
474 scoped_value_mark free_values;
476 struct value *res_val;
478 switch (check_typedef (value->type ())->code ())
480 case TYPE_CODE_PTR:
481 res_val = value_ind (value);
482 break;
483 case TYPE_CODE_REF:
484 case TYPE_CODE_RVALUE_REF:
485 res_val = coerce_ref (value);
486 break;
487 default:
488 error (_("Trying to get the referenced value from a value which is"
489 " neither a pointer nor a reference"));
492 return vlscm_scm_from_value (res_val);
496 static SCM
497 gdbscm_reference_value (SCM self, enum type_code refcode)
499 value_smob *v_smob
500 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
501 struct value *value = v_smob->value;
503 return gdbscm_wrap ([=]
505 scoped_value_mark free_values;
507 struct value *res_val = value_ref (value, refcode);
508 return vlscm_scm_from_value (res_val);
512 /* (value-reference-value <gdb:value>) -> <gdb:value> */
514 static SCM
515 gdbscm_value_reference_value (SCM self)
517 return gdbscm_reference_value (self, TYPE_CODE_REF);
520 /* (value-rvalue-reference-value <gdb:value>) -> <gdb:value> */
522 static SCM
523 gdbscm_value_rvalue_reference_value (SCM self)
525 return gdbscm_reference_value (self, TYPE_CODE_RVALUE_REF);
528 /* (value-const-value <gdb:value>) -> <gdb:value> */
530 static SCM
531 gdbscm_value_const_value (SCM self)
533 value_smob *v_smob
534 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
535 struct value *value = v_smob->value;
537 return gdbscm_wrap ([=]
539 scoped_value_mark free_values;
541 struct value *res_val = make_cv_value (1, 0, value);
542 return vlscm_scm_from_value (res_val);
546 /* (value-type <gdb:value>) -> <gdb:type> */
548 static SCM
549 gdbscm_value_type (SCM self)
551 value_smob *v_smob
552 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
553 struct value *value = v_smob->value;
555 if (SCM_UNBNDP (v_smob->type))
556 v_smob->type = tyscm_scm_from_type (value->type ());
558 return v_smob->type;
561 /* (value-dynamic-type <gdb:value>) -> <gdb:type> */
563 static SCM
564 gdbscm_value_dynamic_type (SCM self)
566 value_smob *v_smob
567 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
568 struct value *value = v_smob->value;
569 struct type *type = NULL;
571 if (! SCM_UNBNDP (v_smob->dynamic_type))
572 return v_smob->dynamic_type;
574 gdbscm_gdb_exception exc {};
577 scoped_value_mark free_values;
579 type = value->type ();
580 type = check_typedef (type);
582 if (((type->code () == TYPE_CODE_PTR)
583 || (type->code () == TYPE_CODE_REF))
584 && (type->target_type ()->code () == TYPE_CODE_STRUCT))
586 struct value *target;
587 int was_pointer = type->code () == TYPE_CODE_PTR;
589 if (was_pointer)
590 target = value_ind (value);
591 else
592 target = coerce_ref (value);
593 type = value_rtti_type (target, NULL, NULL, NULL);
595 if (type)
597 if (was_pointer)
598 type = lookup_pointer_type (type);
599 else
600 type = lookup_lvalue_reference_type (type);
603 else if (type->code () == TYPE_CODE_STRUCT)
604 type = value_rtti_type (value, NULL, NULL, NULL);
605 else
607 /* Re-use object's static type. */
608 type = NULL;
611 catch (const gdb_exception &except)
613 exc = unpack (except);
616 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
617 if (type == NULL)
618 v_smob->dynamic_type = gdbscm_value_type (self);
619 else
620 v_smob->dynamic_type = tyscm_scm_from_type (type);
622 return v_smob->dynamic_type;
625 /* A helper function that implements the various cast operators. */
627 static SCM
628 vlscm_do_cast (SCM self, SCM type_scm, enum exp_opcode op,
629 const char *func_name)
631 value_smob *v_smob
632 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
633 struct value *value = v_smob->value;
634 type_smob *t_smob
635 = tyscm_get_type_smob_arg_unsafe (type_scm, SCM_ARG2, FUNC_NAME);
636 struct type *type = tyscm_type_smob_type (t_smob);
638 return gdbscm_wrap ([=]
640 scoped_value_mark free_values;
642 struct value *res_val;
643 if (op == UNOP_DYNAMIC_CAST)
644 res_val = value_dynamic_cast (type, value);
645 else if (op == UNOP_REINTERPRET_CAST)
646 res_val = value_reinterpret_cast (type, value);
647 else
649 gdb_assert (op == UNOP_CAST);
650 res_val = value_cast (type, value);
653 return vlscm_scm_from_value (res_val);
657 /* (value-cast <gdb:value> <gdb:type>) -> <gdb:value> */
659 static SCM
660 gdbscm_value_cast (SCM self, SCM new_type)
662 return vlscm_do_cast (self, new_type, UNOP_CAST, FUNC_NAME);
665 /* (value-dynamic-cast <gdb:value> <gdb:type>) -> <gdb:value> */
667 static SCM
668 gdbscm_value_dynamic_cast (SCM self, SCM new_type)
670 return vlscm_do_cast (self, new_type, UNOP_DYNAMIC_CAST, FUNC_NAME);
673 /* (value-reinterpret-cast <gdb:value> <gdb:type>) -> <gdb:value> */
675 static SCM
676 gdbscm_value_reinterpret_cast (SCM self, SCM new_type)
678 return vlscm_do_cast (self, new_type, UNOP_REINTERPRET_CAST, FUNC_NAME);
681 /* (value-field <gdb:value> string) -> <gdb:value>
682 Given string name of an element inside structure, return its <gdb:value>
683 object. */
685 static SCM
686 gdbscm_value_field (SCM self, SCM field_scm)
688 value_smob *v_smob
689 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
691 SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
692 _("string"));
694 return gdbscm_wrap ([=]
696 scoped_value_mark free_values;
698 gdb::unique_xmalloc_ptr<char> field = gdbscm_scm_to_c_string (field_scm);
700 struct value *tmp = v_smob->value;
702 struct value *res_val = value_struct_elt (&tmp, {}, field.get (), NULL,
703 "struct/class/union");
705 return vlscm_scm_from_value (res_val);
709 /* (value-subscript <gdb:value> integer|<gdb:value>) -> <gdb:value>
710 Return the specified value in an array. */
712 static SCM
713 gdbscm_value_subscript (SCM self, SCM index_scm)
715 value_smob *v_smob
716 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
717 struct value *value = v_smob->value;
718 struct type *type = value->type ();
720 SCM_ASSERT (type != NULL, self, SCM_ARG2, FUNC_NAME);
722 return gdbscm_wrap ([=]
724 scoped_value_mark free_values;
726 SCM except_scm;
727 struct value *index
728 = vlscm_convert_value_from_scheme (FUNC_NAME, SCM_ARG2, index_scm,
729 &except_scm,
730 type->arch (),
731 current_language);
732 if (index == NULL)
733 return except_scm;
735 /* Assume we are attempting an array access, and let the value code
736 throw an exception if the index has an invalid type.
737 Check the value's type is something that can be accessed via
738 a subscript. */
739 struct value *tmp = coerce_ref (value);
740 struct type *tmp_type = check_typedef (tmp->type ());
741 if (tmp_type->code () != TYPE_CODE_ARRAY
742 && tmp_type->code () != TYPE_CODE_PTR)
743 error (_("Cannot subscript requested type"));
745 struct value *res_val = value_subscript (tmp, value_as_long (index));
746 return vlscm_scm_from_value (res_val);
750 /* (value-call <gdb:value> arg-list) -> <gdb:value>
751 Perform an inferior function call on the value. */
753 static SCM
754 gdbscm_value_call (SCM self, SCM args)
756 value_smob *v_smob
757 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
758 struct value *function = v_smob->value;
759 struct type *ftype = NULL;
760 long args_count;
761 struct value **vargs = NULL;
763 gdbscm_gdb_exception exc {};
766 ftype = check_typedef (function->type ());
768 catch (const gdb_exception &except)
770 exc = unpack (except);
773 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
774 SCM_ASSERT_TYPE (ftype->code () == TYPE_CODE_FUNC, self,
775 SCM_ARG1, FUNC_NAME,
776 _("function (value of TYPE_CODE_FUNC)"));
778 SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (args)), args,
779 SCM_ARG2, FUNC_NAME, _("list"));
781 args_count = scm_ilength (args);
782 if (args_count > 0)
784 struct gdbarch *gdbarch = get_current_arch ();
785 const struct language_defn *language = current_language;
786 SCM except_scm;
787 long i;
789 vargs = XALLOCAVEC (struct value *, args_count);
790 for (i = 0; i < args_count; i++)
792 SCM arg = scm_car (args);
794 vargs[i] = vlscm_convert_value_from_scheme (FUNC_NAME,
795 GDBSCM_ARG_NONE, arg,
796 &except_scm,
797 gdbarch, language);
798 if (vargs[i] == NULL)
799 gdbscm_throw (except_scm);
801 args = scm_cdr (args);
803 gdb_assert (gdbscm_is_true (scm_null_p (args)));
806 return gdbscm_wrap ([=]
808 scoped_value_mark free_values;
810 auto av = gdb::make_array_view (vargs, args_count);
811 value *return_value = call_function_by_hand (function, NULL, av);
812 return vlscm_scm_from_value (return_value);
816 /* (value->bytevector <gdb:value>) -> bytevector */
818 static SCM
819 gdbscm_value_to_bytevector (SCM self)
821 value_smob *v_smob
822 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
823 struct value *value = v_smob->value;
824 struct type *type;
825 size_t length = 0;
826 const gdb_byte *contents = NULL;
827 SCM bv;
829 type = value->type ();
831 gdbscm_gdb_exception exc {};
834 type = check_typedef (type);
835 length = type->length ();
836 contents = value->contents ().data ();
838 catch (const gdb_exception &except)
840 exc = unpack (except);
843 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
844 bv = scm_c_make_bytevector (length);
845 memcpy (SCM_BYTEVECTOR_CONTENTS (bv), contents, length);
847 return bv;
850 /* Helper function to determine if a type is "int-like". */
852 static int
853 is_intlike (struct type *type, int ptr_ok)
855 return (type->code () == TYPE_CODE_INT
856 || type->code () == TYPE_CODE_ENUM
857 || type->code () == TYPE_CODE_BOOL
858 || type->code () == TYPE_CODE_CHAR
859 || (ptr_ok && type->code () == TYPE_CODE_PTR));
862 /* (value->bool <gdb:value>) -> boolean
863 Throws an error if the value is not integer-like. */
865 static SCM
866 gdbscm_value_to_bool (SCM self)
868 value_smob *v_smob
869 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
870 struct value *value = v_smob->value;
871 struct type *type;
872 LONGEST l = 0;
874 type = value->type ();
876 gdbscm_gdb_exception exc {};
879 type = check_typedef (type);
881 catch (const gdb_exception &except)
883 exc = unpack (except);
886 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
887 SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
888 _("integer-like gdb value"));
892 if (type->code () == TYPE_CODE_PTR)
893 l = value_as_address (value);
894 else
895 l = value_as_long (value);
897 catch (const gdb_exception &except)
899 exc = unpack (except);
902 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
903 return scm_from_bool (l != 0);
906 /* (value->integer <gdb:value>) -> integer
907 Throws an error if the value is not integer-like. */
909 static SCM
910 gdbscm_value_to_integer (SCM self)
912 value_smob *v_smob
913 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
914 struct value *value = v_smob->value;
915 struct type *type;
916 LONGEST l = 0;
918 type = value->type ();
920 gdbscm_gdb_exception exc {};
923 type = check_typedef (type);
925 catch (const gdb_exception &except)
927 exc = unpack (except);
930 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
931 SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
932 _("integer-like gdb value"));
936 if (type->code () == TYPE_CODE_PTR)
937 l = value_as_address (value);
938 else
939 l = value_as_long (value);
941 catch (const gdb_exception &except)
943 exc = unpack (except);
946 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
947 if (type->is_unsigned ())
948 return gdbscm_scm_from_ulongest (l);
949 else
950 return gdbscm_scm_from_longest (l);
953 /* (value->real <gdb:value>) -> real
954 Throws an error if the value is not a number. */
956 static SCM
957 gdbscm_value_to_real (SCM self)
959 value_smob *v_smob
960 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
961 struct value *value = v_smob->value;
962 struct type *type;
963 double d = 0;
964 struct value *check = nullptr;
966 type = value->type ();
968 gdbscm_gdb_exception exc {};
971 type = check_typedef (type);
973 catch (const gdb_exception &except)
975 exc = unpack (except);
978 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
979 SCM_ASSERT_TYPE (is_intlike (type, 0) || type->code () == TYPE_CODE_FLT,
980 self, SCM_ARG1, FUNC_NAME, _("number"));
984 if (is_floating_value (value))
986 d = target_float_to_host_double (value->contents ().data (),
987 type);
988 check = value_from_host_double (type, d);
990 else if (type->is_unsigned ())
992 d = (ULONGEST) value_as_long (value);
993 check = value_from_ulongest (type, (ULONGEST) d);
995 else
997 d = value_as_long (value);
998 check = value_from_longest (type, (LONGEST) d);
1001 catch (const gdb_exception &except)
1003 exc = unpack (except);
1006 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
1007 /* TODO: Is there a better way to check if the value fits? */
1008 if (!value_equal (value, check))
1009 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
1010 _("number can't be converted to a double"));
1012 return scm_from_double (d);
1015 /* (value->string <gdb:value>
1016 [#:encoding encoding]
1017 [#:errors #f | 'error | 'substitute]
1018 [#:length length])
1019 -> string
1020 Return Unicode string with value's contents, which must be a string.
1022 If ENCODING is not given, the string is assumed to be encoded in
1023 the target's charset.
1025 ERRORS is one of #f, 'error or 'substitute.
1026 An error setting of #f means use the default, which is Guile's
1027 %default-port-conversion-strategy when using Guile >= 2.0.6, or 'error if
1028 using an earlier version of Guile. Earlier versions do not properly
1029 support obtaining the default port conversion strategy.
1030 If the default is not one of 'error or 'substitute, 'substitute is used.
1031 An error setting of "error" causes an exception to be thrown if there's
1032 a decoding error. An error setting of "substitute" causes invalid
1033 characters to be replaced with "?".
1035 If LENGTH is provided, only fetch string to the length provided.
1036 LENGTH must be a Scheme integer, it can't be a <gdb:value> integer. */
1038 static SCM
1039 gdbscm_value_to_string (SCM self, SCM rest)
1041 value_smob *v_smob
1042 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1043 struct value *value = v_smob->value;
1044 const SCM keywords[] = {
1045 encoding_keyword, errors_keyword, length_keyword, SCM_BOOL_F
1047 int encoding_arg_pos = -1, errors_arg_pos = -1, length_arg_pos = -1;
1048 char *encoding = NULL;
1049 SCM errors = SCM_BOOL_F;
1050 /* Avoid an uninitialized warning from gcc. */
1051 gdb_byte *buffer_contents = nullptr;
1052 int length = -1;
1053 const char *la_encoding = NULL;
1054 struct type *char_type = NULL;
1055 SCM result;
1057 /* The sequencing here, as everywhere else, is important.
1058 We can't have existing cleanups when a Scheme exception is thrown. */
1060 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#sOi", rest,
1061 &encoding_arg_pos, &encoding,
1062 &errors_arg_pos, &errors,
1063 &length_arg_pos, &length);
1065 if (errors_arg_pos > 0
1066 && errors != SCM_BOOL_F
1067 && !scm_is_eq (errors, error_symbol)
1068 && !scm_is_eq (errors, substitute_symbol))
1070 SCM excp
1071 = gdbscm_make_out_of_range_error (FUNC_NAME, errors_arg_pos, errors,
1072 _("invalid error kind"));
1074 xfree (encoding);
1075 gdbscm_throw (excp);
1077 if (errors == SCM_BOOL_F)
1079 /* N.B. scm_port_conversion_strategy in Guile versions prior to 2.0.6
1080 will throw a Scheme error when passed #f. */
1081 if (gdbscm_guile_version_is_at_least (2, 0, 6))
1082 errors = scm_port_conversion_strategy (SCM_BOOL_F);
1083 else
1084 errors = error_symbol;
1086 /* We don't assume anything about the result of scm_port_conversion_strategy.
1087 From this point on, if errors is not 'errors, use 'substitute. */
1089 gdbscm_gdb_exception exc {};
1092 gdb::unique_xmalloc_ptr<gdb_byte> buffer;
1093 c_get_string (value, &buffer, &length, &char_type, &la_encoding);
1094 buffer_contents = buffer.release ();
1096 catch (const gdb_exception &except)
1098 xfree (encoding);
1099 exc = unpack (except);
1101 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
1103 /* If errors is "error", scm_from_stringn may throw a Scheme exception.
1104 Make sure we don't leak. This is done via scm_dynwind_begin, et.al. */
1106 scm_dynwind_begin ((scm_t_dynwind_flags) 0);
1108 gdbscm_dynwind_xfree (encoding);
1109 gdbscm_dynwind_xfree (buffer_contents);
1111 result = scm_from_stringn ((const char *) buffer_contents,
1112 length * char_type->length (),
1113 (encoding != NULL && *encoding != '\0'
1114 ? encoding
1115 : la_encoding),
1116 scm_is_eq (errors, error_symbol)
1117 ? SCM_FAILED_CONVERSION_ERROR
1118 : SCM_FAILED_CONVERSION_QUESTION_MARK);
1120 scm_dynwind_end ();
1122 return result;
1125 /* (value->lazy-string <gdb:value> [#:encoding encoding] [#:length length])
1126 -> <gdb:lazy-string>
1127 Return a Scheme object representing a lazy_string_object type.
1128 A lazy string is a pointer to a string with an optional encoding and length.
1129 If ENCODING is not given, the target's charset is used.
1130 If LENGTH is provided then the length parameter is set to LENGTH.
1131 Otherwise if the value is an array of known length then the array's length
1132 is used. Otherwise the length will be set to -1 (meaning first null of
1133 appropriate with).
1134 LENGTH must be a Scheme integer, it can't be a <gdb:value> integer. */
1136 static SCM
1137 gdbscm_value_to_lazy_string (SCM self, SCM rest)
1139 value_smob *v_smob
1140 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1141 struct value *value = v_smob->value;
1142 const SCM keywords[] = { encoding_keyword, length_keyword, SCM_BOOL_F };
1143 int encoding_arg_pos = -1, length_arg_pos = -1;
1144 char *encoding = NULL;
1145 int length = -1;
1146 SCM result = SCM_BOOL_F; /* -Wall */
1147 gdbscm_gdb_exception except {};
1149 /* The sequencing here, as everywhere else, is important.
1150 We can't have existing cleanups when a Scheme exception is thrown. */
1152 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#si", rest,
1153 &encoding_arg_pos, &encoding,
1154 &length_arg_pos, &length);
1156 if (length < -1)
1158 gdbscm_out_of_range_error (FUNC_NAME, length_arg_pos,
1159 scm_from_int (length),
1160 _("invalid length"));
1165 scoped_value_mark free_values;
1167 struct type *type, *realtype;
1168 CORE_ADDR addr;
1170 type = value->type ();
1171 realtype = check_typedef (type);
1173 switch (realtype->code ())
1175 case TYPE_CODE_ARRAY:
1177 LONGEST array_length = -1;
1178 LONGEST low_bound, high_bound;
1180 /* PR 20786: There's no way to specify an array of length zero.
1181 Record a length of [0,-1] which is how Ada does it. Anything
1182 we do is broken, but this one possible solution. */
1183 if (get_array_bounds (realtype, &low_bound, &high_bound))
1184 array_length = high_bound - low_bound + 1;
1185 if (length == -1)
1186 length = array_length;
1187 else if (array_length == -1)
1189 type = lookup_array_range_type (realtype->target_type (),
1190 0, length - 1);
1192 else if (length != array_length)
1194 /* We need to create a new array type with the
1195 specified length. */
1196 if (length > array_length)
1197 error (_("length is larger than array size"));
1198 type = lookup_array_range_type (type->target_type (),
1199 low_bound,
1200 low_bound + length - 1);
1202 addr = value->address ();
1203 break;
1205 case TYPE_CODE_PTR:
1206 /* If a length is specified we defer creating an array of the
1207 specified width until we need to. */
1208 addr = value_as_address (value);
1209 break;
1210 default:
1211 /* Should flag an error here. PR 20769. */
1212 addr = value->address ();
1213 break;
1216 result = lsscm_make_lazy_string (addr, length, encoding, type);
1218 catch (const gdb_exception &ex)
1220 except = unpack (ex);
1223 xfree (encoding);
1224 GDBSCM_HANDLE_GDB_EXCEPTION (except);
1226 if (gdbscm_is_exception (result))
1227 gdbscm_throw (result);
1229 return result;
1232 /* (value-lazy? <gdb:value>) -> boolean */
1234 static SCM
1235 gdbscm_value_lazy_p (SCM self)
1237 value_smob *v_smob
1238 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1239 struct value *value = v_smob->value;
1241 return scm_from_bool (value->lazy ());
1244 /* (value-fetch-lazy! <gdb:value>) -> unspecified */
1246 static SCM
1247 gdbscm_value_fetch_lazy_x (SCM self)
1249 value_smob *v_smob
1250 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1251 struct value *value = v_smob->value;
1253 return gdbscm_wrap ([=]
1255 if (value->lazy ())
1256 value->fetch_lazy ();
1257 return SCM_UNSPECIFIED;
1261 /* (value-print <gdb:value>) -> string */
1263 static SCM
1264 gdbscm_value_print (SCM self)
1266 value_smob *v_smob
1267 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1268 struct value *value = v_smob->value;
1269 struct value_print_options opts;
1271 get_user_print_options (&opts);
1272 opts.deref_ref = false;
1274 string_file stb;
1276 gdbscm_gdb_exception exc {};
1279 common_val_print (value, &stb, 0, &opts, current_language);
1281 catch (const gdb_exception &except)
1283 exc = unpack (except);
1286 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
1287 /* Use SCM_FAILED_CONVERSION_QUESTION_MARK to ensure this doesn't
1288 throw an error if the encoding fails.
1289 IWBN to use scm_take_locale_string here, but we'd have to temporarily
1290 override the default port conversion handler because contrary to
1291 documentation it doesn't necessarily free the input string. */
1292 return scm_from_stringn (stb.c_str (), stb.size (), host_charset (),
1293 SCM_FAILED_CONVERSION_QUESTION_MARK);
1296 /* (parse-and-eval string) -> <gdb:value>
1297 Parse a string and evaluate the string as an expression. */
1299 static SCM
1300 gdbscm_parse_and_eval (SCM expr_scm)
1302 char *expr_str;
1303 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
1304 expr_scm, &expr_str);
1306 return gdbscm_wrap ([=]
1308 scoped_value_mark free_values;
1309 return vlscm_scm_from_value (parse_and_eval (expr_str));
1313 /* (history-ref integer) -> <gdb:value>
1314 Return the specified value from GDB's value history. */
1316 static SCM
1317 gdbscm_history_ref (SCM index)
1319 int i;
1320 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "i", index, &i);
1322 return gdbscm_wrap ([=]
1324 return vlscm_scm_from_value (access_value_history (i));
1328 /* (history-append! <gdb:value>) -> index
1329 Append VALUE to GDB's value history. Return its index in the history. */
1331 static SCM
1332 gdbscm_history_append_x (SCM value)
1334 value_smob *v_smob
1335 = vlscm_get_value_smob_arg_unsafe (value, SCM_ARG1, FUNC_NAME);
1336 return gdbscm_wrap ([=]
1338 return scm_from_int (v_smob->value->record_latest ());
1342 /* Initialize the Scheme value code. */
1344 static const scheme_function value_functions[] =
1346 { "value?", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_p),
1348 Return #t if the object is a <gdb:value> object." },
1350 { "make-value", 1, 0, 1, as_a_scm_t_subr (gdbscm_make_value),
1352 Create a <gdb:value> representing object.\n\
1353 Typically this is used to convert numbers and strings to\n\
1354 <gdb:value> objects.\n\
1356 Arguments: object [#:type <gdb:type>]" },
1358 { "value-optimized-out?", 1, 0, 0,
1359 as_a_scm_t_subr (gdbscm_value_optimized_out_p),
1361 Return #t if the value has been optimized out." },
1363 { "value-address", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_address),
1365 Return the address of the value." },
1367 { "value-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_type),
1369 Return the type of the value." },
1371 { "value-dynamic-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_dynamic_type),
1373 Return the dynamic type of the value." },
1375 { "value-cast", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_cast),
1377 Cast the value to the supplied type.\n\
1379 Arguments: <gdb:value> <gdb:type>" },
1381 { "value-dynamic-cast", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_dynamic_cast),
1383 Cast the value to the supplied type, as if by the C++\n\
1384 dynamic_cast operator.\n\
1386 Arguments: <gdb:value> <gdb:type>" },
1388 { "value-reinterpret-cast", 2, 0, 0,
1389 as_a_scm_t_subr (gdbscm_value_reinterpret_cast),
1391 Cast the value to the supplied type, as if by the C++\n\
1392 reinterpret_cast operator.\n\
1394 Arguments: <gdb:value> <gdb:type>" },
1396 { "value-dereference", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_dereference),
1398 Return the result of applying the C unary * operator to the value." },
1400 { "value-referenced-value", 1, 0, 0,
1401 as_a_scm_t_subr (gdbscm_value_referenced_value),
1403 Given a value of a reference type, return the value referenced.\n\
1404 The difference between this function and value-dereference is that\n\
1405 the latter applies * unary operator to a value, which need not always\n\
1406 result in the value referenced.\n\
1407 For example, for a value which is a reference to an 'int' pointer ('int *'),\n\
1408 value-dereference will result in a value of type 'int' while\n\
1409 value-referenced-value will result in a value of type 'int *'." },
1411 { "value-reference-value", 1, 0, 0,
1412 as_a_scm_t_subr (gdbscm_value_reference_value),
1414 Return a <gdb:value> object which is a reference to the given value." },
1416 { "value-rvalue-reference-value", 1, 0, 0,
1417 as_a_scm_t_subr (gdbscm_value_rvalue_reference_value),
1419 Return a <gdb:value> object which is an rvalue reference to the given value." },
1421 { "value-const-value", 1, 0, 0,
1422 as_a_scm_t_subr (gdbscm_value_const_value),
1424 Return a <gdb:value> object which is a 'const' version of the given value." },
1426 { "value-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_field),
1428 Return the specified field of the value.\n\
1430 Arguments: <gdb:value> string" },
1432 { "value-subscript", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_subscript),
1434 Return the value of the array at the specified index.\n\
1436 Arguments: <gdb:value> integer" },
1438 { "value-call", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_call),
1440 Perform an inferior function call taking the value as a pointer to the\n\
1441 function to call.\n\
1442 Each element of the argument list must be a <gdb:value> object or an object\n\
1443 that can be converted to one.\n\
1444 The result is the value returned by the function.\n\
1446 Arguments: <gdb:value> arg-list" },
1448 { "value->bool", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_bool),
1450 Return the Scheme boolean representing the GDB value.\n\
1451 The value must be \"integer like\". Pointers are ok." },
1453 { "value->integer", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_integer),
1455 Return the Scheme integer representing the GDB value.\n\
1456 The value must be \"integer like\". Pointers are ok." },
1458 { "value->real", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_real),
1460 Return the Scheme real number representing the GDB value.\n\
1461 The value must be a number." },
1463 { "value->bytevector", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_bytevector),
1465 Return a Scheme bytevector with the raw contents of the GDB value.\n\
1466 No transformation, endian or otherwise, is performed." },
1468 { "value->string", 1, 0, 1, as_a_scm_t_subr (gdbscm_value_to_string),
1470 Return the Unicode string of the value's contents.\n\
1471 If ENCODING is not given, the string is assumed to be encoded in\n\
1472 the target's charset.\n\
1473 An error setting \"error\" causes an exception to be thrown if there's\n\
1474 a decoding error. An error setting of \"substitute\" causes invalid\n\
1475 characters to be replaced with \"?\". The default is \"error\".\n\
1476 If LENGTH is provided, only fetch string to the length provided.\n\
1478 Arguments: <gdb:value>\n\
1479 [#:encoding encoding] [#:errors \"error\"|\"substitute\"]\n\
1480 [#:length length]" },
1482 { "value->lazy-string", 1, 0, 1,
1483 as_a_scm_t_subr (gdbscm_value_to_lazy_string),
1485 Return a Scheme object representing a lazily fetched Unicode string\n\
1486 of the value's contents.\n\
1487 If ENCODING is not given, the string is assumed to be encoded in\n\
1488 the target's charset.\n\
1489 If LENGTH is provided, only fetch string to the length provided.\n\
1491 Arguments: <gdb:value> [#:encoding encoding] [#:length length]" },
1493 { "value-lazy?", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_lazy_p),
1495 Return #t if the value is lazy (not fetched yet from the inferior).\n\
1496 A lazy value is fetched when needed, or when the value-fetch-lazy! function\n\
1497 is called." },
1499 { "make-lazy-value", 2, 0, 0, as_a_scm_t_subr (gdbscm_make_lazy_value),
1501 Create a <gdb:value> that will be lazily fetched from the target.\n\
1503 Arguments: <gdb:type> address" },
1505 { "value-fetch-lazy!", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_fetch_lazy_x),
1507 Fetch the value from the inferior, if it was lazy.\n\
1508 The result is \"unspecified\"." },
1510 { "value-print", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_print),
1512 Return the string representation (print form) of the value." },
1514 { "parse-and-eval", 1, 0, 0, as_a_scm_t_subr (gdbscm_parse_and_eval),
1516 Evaluates string in gdb and returns the result as a <gdb:value> object." },
1518 { "history-ref", 1, 0, 0, as_a_scm_t_subr (gdbscm_history_ref),
1520 Return the specified value from GDB's value history." },
1522 { "history-append!", 1, 0, 0, as_a_scm_t_subr (gdbscm_history_append_x),
1524 Append the specified value onto GDB's value history." },
1526 END_FUNCTIONS
1529 void
1530 gdbscm_initialize_values (void)
1532 value_smob_tag = gdbscm_make_smob_type (value_smob_name,
1533 sizeof (value_smob));
1534 scm_set_smob_free (value_smob_tag, vlscm_free_value_smob);
1535 scm_set_smob_print (value_smob_tag, vlscm_print_value_smob);
1536 scm_set_smob_equalp (value_smob_tag, vlscm_equal_p_value_smob);
1538 gdbscm_define_functions (value_functions, 1);
1540 type_keyword = scm_from_latin1_keyword ("type");
1541 encoding_keyword = scm_from_latin1_keyword ("encoding");
1542 errors_keyword = scm_from_latin1_keyword ("errors");
1543 length_keyword = scm_from_latin1_keyword ("length");
1545 error_symbol = scm_from_latin1_symbol ("error");
1546 escape_symbol = scm_from_latin1_symbol ("escape");
1547 substitute_symbol = scm_from_latin1_symbol ("substitute");