1 /* Scheme interface to values.
3 Copyright (C) 2008-2014 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 "gdb_assert.h"
29 #include "symtab.h" /* Needed by language.h. */
33 #include "guile-internal.h"
35 /* The <gdb:value> smob. */
37 typedef struct _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. */
45 struct _value_smob *next;
46 struct _value_smob *prev;
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 "mark" function for <gdb:value>. */
129 vlscm_mark_value_smob (SCM self)
131 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
133 scm_gc_mark (v_smob->address);
134 scm_gc_mark (v_smob->type);
135 scm_gc_mark (v_smob->dynamic_type);
137 return gdbscm_mark_gsmob (&v_smob->base);
140 /* The smob "free" function for <gdb:value>. */
143 vlscm_free_value_smob (SCM self)
145 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
147 vlscm_forget_value_smob (v_smob);
148 value_free (v_smob->value);
153 /* The smob "print" function for <gdb:value>. */
156 vlscm_print_value_smob (SCM self, SCM port, scm_print_state *pstate)
158 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
160 struct value_print_options opts;
161 volatile struct gdb_exception except;
163 if (pstate->writingp)
164 gdbscm_printf (port, "#<%s ", value_smob_name);
166 get_user_print_options (&opts);
169 /* pstate->writingp = zero if invoked by display/~A, and nonzero if
170 invoked by write/~S. What to do here may need to evolve.
171 IWBN if we could pass an argument to format that would we could use
172 instead of writingp. */
173 opts.raw = !!pstate->writingp;
175 TRY_CATCH (except, RETURN_MASK_ALL)
177 struct ui_file *stb = mem_fileopen ();
178 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
180 common_val_print (v_smob->value, stb, 0, &opts, current_language);
181 s = ui_file_xstrdup (stb, NULL);
183 do_cleanups (old_chain);
185 GDBSCM_HANDLE_GDB_EXCEPTION (except);
193 if (pstate->writingp)
194 scm_puts (">", port);
196 scm_remember_upto_here_1 (self);
198 /* Non-zero means success. */
202 /* The smob "equalp" function for <gdb:value>. */
205 vlscm_equal_p_value_smob (SCM v1, SCM v2)
207 const value_smob *v1_smob = (value_smob *) SCM_SMOB_DATA (v1);
208 const value_smob *v2_smob = (value_smob *) SCM_SMOB_DATA (v2);
210 volatile struct gdb_exception except;
212 TRY_CATCH (except, RETURN_MASK_ALL)
214 result = value_equal (v1_smob->value, v2_smob->value);
216 GDBSCM_HANDLE_GDB_EXCEPTION (except);
218 return scm_from_bool (result);
221 /* Low level routine to create a <gdb:value> object. */
224 vlscm_make_value_smob (void)
226 value_smob *v_smob = (value_smob *)
227 scm_gc_malloc (sizeof (value_smob), value_smob_name);
230 /* These must be filled in by the caller. */
231 v_smob->value = NULL;
235 /* These are lazily computed. */
236 v_smob->address = SCM_UNDEFINED;
237 v_smob->type = SCM_UNDEFINED;
238 v_smob->dynamic_type = SCM_UNDEFINED;
240 v_scm = scm_new_smob (value_smob_tag, (scm_t_bits) v_smob);
241 gdbscm_init_gsmob (&v_smob->base);
246 /* Return non-zero if SCM is a <gdb:value> object. */
249 vlscm_is_value (SCM scm)
251 return SCM_SMOB_PREDICATE (value_smob_tag, scm);
254 /* (value? object) -> boolean */
257 gdbscm_value_p (SCM scm)
259 return scm_from_bool (vlscm_is_value (scm));
262 /* Create a new <gdb:value> object that encapsulates VALUE.
263 The value is released from the all_values chain so its lifetime is not
264 bound to the execution of a command. */
267 vlscm_scm_from_value (struct value *value)
269 /* N.B. It's important to not cause any side-effects until we know the
270 conversion worked. */
271 SCM v_scm = vlscm_make_value_smob ();
272 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
274 v_smob->value = value;
275 release_value_or_incref (value);
276 vlscm_remember_scheme_value (v_smob);
281 /* Returns the <gdb:value> object in SELF.
282 Throws an exception if SELF is not a <gdb:value> object. */
285 vlscm_get_value_arg_unsafe (SCM self, int arg_pos, const char *func_name)
287 SCM_ASSERT_TYPE (vlscm_is_value (self), self, arg_pos, func_name,
293 /* Returns a pointer to the value smob of SELF.
294 Throws an exception if SELF is not a <gdb:value> object. */
297 vlscm_get_value_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
299 SCM v_scm = vlscm_get_value_arg_unsafe (self, arg_pos, func_name);
300 value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
305 /* Return the value field of V_SCM, an object of type <gdb:value>.
306 This exists so that we don't have to export the struct's contents. */
309 vlscm_scm_to_value (SCM v_scm)
313 gdb_assert (vlscm_is_value (v_scm));
314 v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
315 return v_smob->value;
320 /* (make-value x [#:type type]) -> <gdb:value> */
323 gdbscm_make_value (SCM x, SCM rest)
325 struct gdbarch *gdbarch = get_current_arch ();
326 const struct language_defn *language = current_language;
327 const SCM keywords[] = { type_keyword, SCM_BOOL_F };
328 int type_arg_pos = -1;
329 SCM type_scm = SCM_UNDEFINED;
330 SCM except_scm, result;
332 struct type *type = NULL;
334 struct cleanup *cleanups;
336 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#O", rest,
337 &type_arg_pos, &type_scm);
339 if (type_arg_pos > 0)
341 t_smob = tyscm_get_type_smob_arg_unsafe (type_scm, type_arg_pos,
343 type = tyscm_type_smob_type (t_smob);
346 cleanups = make_cleanup_value_free_to_mark (value_mark ());
348 value = vlscm_convert_typed_value_from_scheme (FUNC_NAME, SCM_ARG1, x,
349 type_arg_pos, type_scm, type,
354 do_cleanups (cleanups);
355 gdbscm_throw (except_scm);
358 result = vlscm_scm_from_value (value);
360 do_cleanups (cleanups);
362 if (gdbscm_is_exception (result))
363 gdbscm_throw (result);
367 /* (make-lazy-value <gdb:type> address) -> <gdb:value> */
370 gdbscm_make_lazy_value (SCM type_scm, SCM address_scm)
375 struct value *value = NULL;
377 struct cleanup *cleanups;
378 volatile struct gdb_exception except;
380 t_smob = tyscm_get_type_smob_arg_unsafe (type_scm, SCM_ARG1, FUNC_NAME);
381 type = tyscm_type_smob_type (t_smob);
383 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, NULL, "U",
384 address_scm, &address);
386 cleanups = make_cleanup_value_free_to_mark (value_mark ());
388 /* There's no (current) need to wrap this in a TRY_CATCH, but for consistency
389 and future-proofing we do. */
390 TRY_CATCH (except, RETURN_MASK_ALL)
392 value = value_from_contents_and_address (type, NULL, address);
394 GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
396 result = vlscm_scm_from_value (value);
398 do_cleanups (cleanups);
400 if (gdbscm_is_exception (result))
401 gdbscm_throw (result);
405 /* (value-optimized-out? <gdb:value>) -> boolean */
408 gdbscm_value_optimized_out_p (SCM self)
411 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
412 struct value *value = v_smob->value;
414 volatile struct gdb_exception except;
416 TRY_CATCH (except, RETURN_MASK_ALL)
418 opt = value_optimized_out (value);
420 GDBSCM_HANDLE_GDB_EXCEPTION (except);
422 return scm_from_bool (opt);
425 /* (value-address <gdb:value>) -> integer
426 Returns #f if the value doesn't have one. */
429 gdbscm_value_address (SCM self)
432 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
433 struct value *value = v_smob->value;
435 if (SCM_UNBNDP (v_smob->address))
437 struct value *res_val = NULL;
438 struct cleanup *cleanup
439 = make_cleanup_value_free_to_mark (value_mark ());
441 volatile struct gdb_exception except;
443 TRY_CATCH (except, RETURN_MASK_ALL)
445 res_val = value_addr (value);
447 if (except.reason < 0)
448 address = SCM_BOOL_F;
450 address = vlscm_scm_from_value (res_val);
452 do_cleanups (cleanup);
454 if (gdbscm_is_exception (address))
455 gdbscm_throw (address);
457 v_smob->address = address;
460 return v_smob->address;
463 /* (value-dereference <gdb:value>) -> <gdb:value>
464 Given a value of a pointer type, apply the C unary * operator to it. */
467 gdbscm_value_dereference (SCM self)
470 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
471 struct value *value = v_smob->value;
473 struct value *res_val = NULL;
474 struct cleanup *cleanups;
475 volatile struct gdb_exception except;
477 cleanups = make_cleanup_value_free_to_mark (value_mark ());
479 TRY_CATCH (except, RETURN_MASK_ALL)
481 res_val = value_ind (value);
483 GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
485 result = vlscm_scm_from_value (res_val);
487 do_cleanups (cleanups);
489 if (gdbscm_is_exception (result))
490 gdbscm_throw (result);
495 /* (value-referenced-value <gdb:value>) -> <gdb:value>
496 Given a value of a reference type, return the value referenced.
497 The difference between this function and gdbscm_value_dereference is that
498 the latter applies * unary operator to a value, which need not always
499 result in the value referenced.
500 For example, for a value which is a reference to an 'int' pointer ('int *'),
501 gdbscm_value_dereference will result in a value of type 'int' while
502 gdbscm_value_referenced_value will result in a value of type 'int *'. */
505 gdbscm_value_referenced_value (SCM self)
508 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
509 struct value *value = v_smob->value;
511 struct value *res_val = NULL;
512 struct cleanup *cleanups;
513 volatile struct gdb_exception except;
515 cleanups = make_cleanup_value_free_to_mark (value_mark ());
517 TRY_CATCH (except, RETURN_MASK_ALL)
519 switch (TYPE_CODE (check_typedef (value_type (value))))
522 res_val = value_ind (value);
525 res_val = coerce_ref (value);
528 error (_("Trying to get the referenced value from a value which is"
529 " neither a pointer nor a reference"));
532 GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
534 result = vlscm_scm_from_value (res_val);
536 do_cleanups (cleanups);
538 if (gdbscm_is_exception (result))
539 gdbscm_throw (result);
544 /* (value-type <gdb:value>) -> <gdb:type> */
547 gdbscm_value_type (SCM self)
550 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
551 struct value *value = v_smob->value;
553 if (SCM_UNBNDP (v_smob->type))
554 v_smob->type = tyscm_scm_from_type (value_type (value));
559 /* (value-dynamic-type <gdb:value>) -> <gdb:type> */
562 gdbscm_value_dynamic_type (SCM self)
565 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
566 struct value *value = v_smob->value;
567 struct type *type = NULL;
568 volatile struct gdb_exception except;
570 if (! SCM_UNBNDP (v_smob->type))
571 return v_smob->dynamic_type;
573 TRY_CATCH (except, RETURN_MASK_ALL)
575 struct cleanup *cleanup
576 = make_cleanup_value_free_to_mark (value_mark ());
578 type = value_type (value);
579 CHECK_TYPEDEF (type);
581 if (((TYPE_CODE (type) == TYPE_CODE_PTR)
582 || (TYPE_CODE (type) == TYPE_CODE_REF))
583 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
585 struct value *target;
586 int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
588 target = value_ind (value);
589 type = value_rtti_type (target, NULL, NULL, NULL);
594 type = lookup_pointer_type (type);
596 type = lookup_reference_type (type);
599 else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
600 type = value_rtti_type (value, NULL, NULL, NULL);
603 /* Re-use object's static type. */
607 do_cleanups (cleanup);
609 GDBSCM_HANDLE_GDB_EXCEPTION (except);
612 v_smob->dynamic_type = gdbscm_value_type (self);
614 v_smob->dynamic_type = tyscm_scm_from_type (type);
616 return v_smob->dynamic_type;
619 /* A helper function that implements the various cast operators. */
622 vlscm_do_cast (SCM self, SCM type_scm, enum exp_opcode op,
623 const char *func_name)
626 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
627 struct value *value = v_smob->value;
629 = tyscm_get_type_smob_arg_unsafe (type_scm, SCM_ARG2, FUNC_NAME);
630 struct type *type = tyscm_type_smob_type (t_smob);
632 struct value *res_val = NULL;
633 struct cleanup *cleanups;
634 volatile struct gdb_exception except;
636 cleanups = make_cleanup_value_free_to_mark (value_mark ());
638 TRY_CATCH (except, RETURN_MASK_ALL)
640 if (op == UNOP_DYNAMIC_CAST)
641 res_val = value_dynamic_cast (type, value);
642 else if (op == UNOP_REINTERPRET_CAST)
643 res_val = value_reinterpret_cast (type, value);
646 gdb_assert (op == UNOP_CAST);
647 res_val = value_cast (type, value);
650 GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
652 gdb_assert (res_val != NULL);
653 result = vlscm_scm_from_value (res_val);
655 do_cleanups (cleanups);
657 if (gdbscm_is_exception (result))
658 gdbscm_throw (result);
663 /* (value-cast <gdb:value> <gdb:type>) -> <gdb:value> */
666 gdbscm_value_cast (SCM self, SCM new_type)
668 return vlscm_do_cast (self, new_type, UNOP_CAST, FUNC_NAME);
671 /* (value-dynamic-cast <gdb:value> <gdb:type>) -> <gdb:value> */
674 gdbscm_value_dynamic_cast (SCM self, SCM new_type)
676 return vlscm_do_cast (self, new_type, UNOP_DYNAMIC_CAST, FUNC_NAME);
679 /* (value-reinterpret-cast <gdb:value> <gdb:type>) -> <gdb:value> */
682 gdbscm_value_reinterpret_cast (SCM self, SCM new_type)
684 return vlscm_do_cast (self, new_type, UNOP_REINTERPRET_CAST, FUNC_NAME);
687 /* (value-field <gdb:value> string) -> <gdb:value>
688 Given string name of an element inside structure, return its <gdb:value>
692 gdbscm_value_field (SCM self, SCM field_scm)
695 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
696 struct value *value = v_smob->value;
698 struct value *res_val = NULL;
700 struct cleanup *cleanups;
701 volatile struct gdb_exception except;
703 SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
706 cleanups = make_cleanup_value_free_to_mark (value_mark ());
708 field = gdbscm_scm_to_c_string (field_scm);
709 make_cleanup (xfree, field);
711 TRY_CATCH (except, RETURN_MASK_ALL)
713 struct value *tmp = value;
715 res_val = value_struct_elt (&tmp, NULL, field, NULL, NULL);
717 GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
719 gdb_assert (res_val != NULL);
720 result = vlscm_scm_from_value (res_val);
722 do_cleanups (cleanups);
724 if (gdbscm_is_exception (result))
725 gdbscm_throw (result);
730 /* (value-subscript <gdb:value> integer|<gdb:value>) -> <gdb:value>
731 Return the specified value in an array. */
734 gdbscm_value_subscript (SCM self, SCM index_scm)
737 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
738 struct value *value = v_smob->value;
739 struct value *index = NULL;
740 struct value *res_val = NULL;
741 struct type *type = value_type (value);
742 struct gdbarch *gdbarch;
743 SCM result, except_scm;
744 struct cleanup *cleanups;
745 volatile struct gdb_exception except;
747 /* The sequencing here, as everywhere else, is important.
748 We can't have existing cleanups when a Scheme exception is thrown. */
750 SCM_ASSERT (type != NULL, self, SCM_ARG2, FUNC_NAME);
751 gdbarch = get_type_arch (type);
753 cleanups = make_cleanup_value_free_to_mark (value_mark ());
755 index = vlscm_convert_value_from_scheme (FUNC_NAME, SCM_ARG2, index_scm,
757 gdbarch, current_language);
760 do_cleanups (cleanups);
761 gdbscm_throw (except_scm);
764 TRY_CATCH (except, RETURN_MASK_ALL)
766 struct value *tmp = value;
768 /* Assume we are attempting an array access, and let the value code
769 throw an exception if the index has an invalid type.
770 Check the value's type is something that can be accessed via
772 tmp = coerce_ref (tmp);
773 type = check_typedef (value_type (tmp));
774 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
775 && TYPE_CODE (type) != TYPE_CODE_PTR)
776 error (_("Cannot subscript requested type"));
778 res_val = value_subscript (tmp, value_as_long (index));
780 GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
782 gdb_assert (res_val != NULL);
783 result = vlscm_scm_from_value (res_val);
785 do_cleanups (cleanups);
787 if (gdbscm_is_exception (result))
788 gdbscm_throw (result);
793 /* (value-call <gdb:value> arg-list) -> <gdb:value>
794 Perform an inferior function call on the value. */
797 gdbscm_value_call (SCM self, SCM args)
800 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
801 struct value *function = v_smob->value;
802 struct value *mark = value_mark ();
803 struct type *ftype = NULL;
805 struct value **vargs = NULL;
806 SCM result = SCM_BOOL_F;
807 volatile struct gdb_exception except;
809 TRY_CATCH (except, RETURN_MASK_ALL)
811 ftype = check_typedef (value_type (function));
813 GDBSCM_HANDLE_GDB_EXCEPTION (except);
815 SCM_ASSERT_TYPE (TYPE_CODE (ftype) == TYPE_CODE_FUNC, self,
817 _("function (value of TYPE_CODE_FUNC)"));
819 SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (args)), args,
820 SCM_ARG2, FUNC_NAME, _("list"));
822 args_count = scm_ilength (args);
825 struct gdbarch *gdbarch = get_current_arch ();
826 const struct language_defn *language = current_language;
830 vargs = alloca (sizeof (struct value *) * args_count);
831 for (i = 0; i < args_count; i++)
833 SCM arg = scm_car (args);
835 vargs[i] = vlscm_convert_value_from_scheme (FUNC_NAME,
836 GDBSCM_ARG_NONE, arg,
839 if (vargs[i] == NULL)
840 gdbscm_throw (except_scm);
842 args = scm_cdr (args);
844 gdb_assert (gdbscm_is_true (scm_null_p (args)));
847 TRY_CATCH (except, RETURN_MASK_ALL)
849 struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
850 struct value *return_value;
852 return_value = call_function_by_hand (function, args_count, vargs);
853 result = vlscm_scm_from_value (return_value);
854 do_cleanups (cleanup);
856 GDBSCM_HANDLE_GDB_EXCEPTION (except);
858 if (gdbscm_is_exception (result))
859 gdbscm_throw (result);
864 /* (value->bytevector <gdb:value>) -> bytevector */
867 gdbscm_value_to_bytevector (SCM self)
870 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
871 struct value *value = v_smob->value;
874 const gdb_byte *contents = NULL;
876 volatile struct gdb_exception except;
878 type = value_type (value);
880 TRY_CATCH (except, RETURN_MASK_ALL)
882 CHECK_TYPEDEF (type);
883 length = TYPE_LENGTH (type);
884 contents = value_contents (value);
886 GDBSCM_HANDLE_GDB_EXCEPTION (except);
888 bv = scm_c_make_bytevector (length);
889 memcpy (SCM_BYTEVECTOR_CONTENTS (bv), contents, length);
894 /* Helper function to determine if a type is "int-like". */
897 is_intlike (struct type *type, int ptr_ok)
899 return (TYPE_CODE (type) == TYPE_CODE_INT
900 || TYPE_CODE (type) == TYPE_CODE_ENUM
901 || TYPE_CODE (type) == TYPE_CODE_BOOL
902 || TYPE_CODE (type) == TYPE_CODE_CHAR
903 || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
906 /* (value->bool <gdb:value>) -> boolean
907 Throws an error if the value is not integer-like. */
910 gdbscm_value_to_bool (SCM self)
913 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
914 struct value *value = v_smob->value;
917 volatile struct gdb_exception except;
919 type = value_type (value);
921 TRY_CATCH (except, RETURN_MASK_ALL)
923 CHECK_TYPEDEF (type);
925 GDBSCM_HANDLE_GDB_EXCEPTION (except);
927 SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
928 _("integer-like gdb value"));
930 TRY_CATCH (except, RETURN_MASK_ALL)
932 if (TYPE_CODE (type) == TYPE_CODE_PTR)
933 l = value_as_address (value);
935 l = value_as_long (value);
937 GDBSCM_HANDLE_GDB_EXCEPTION (except);
939 return scm_from_bool (l != 0);
942 /* (value->integer <gdb:value>) -> integer
943 Throws an error if the value is not integer-like. */
946 gdbscm_value_to_integer (SCM self)
949 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
950 struct value *value = v_smob->value;
953 volatile struct gdb_exception except;
955 type = value_type (value);
957 TRY_CATCH (except, RETURN_MASK_ALL)
959 CHECK_TYPEDEF (type);
961 GDBSCM_HANDLE_GDB_EXCEPTION (except);
963 SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
964 _("integer-like gdb value"));
966 TRY_CATCH (except, RETURN_MASK_ALL)
968 if (TYPE_CODE (type) == TYPE_CODE_PTR)
969 l = value_as_address (value);
971 l = value_as_long (value);
973 GDBSCM_HANDLE_GDB_EXCEPTION (except);
975 if (TYPE_UNSIGNED (type))
976 return gdbscm_scm_from_ulongest (l);
978 return gdbscm_scm_from_longest (l);
981 /* (value->real <gdb:value>) -> real
982 Throws an error if the value is not a number. */
985 gdbscm_value_to_real (SCM self)
988 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
989 struct value *value = v_smob->value;
992 volatile struct gdb_exception except;
994 type = value_type (value);
996 TRY_CATCH (except, RETURN_MASK_ALL)
998 CHECK_TYPEDEF (type);
1000 GDBSCM_HANDLE_GDB_EXCEPTION (except);
1002 SCM_ASSERT_TYPE (is_intlike (type, 0) || TYPE_CODE (type) == TYPE_CODE_FLT,
1003 self, SCM_ARG1, FUNC_NAME, _("number"));
1005 TRY_CATCH (except, RETURN_MASK_ALL)
1007 d = value_as_double (value);
1009 GDBSCM_HANDLE_GDB_EXCEPTION (except);
1011 /* TODO: Is there a better way to check if the value fits? */
1012 if (d != (double) d)
1013 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
1014 _("number can't be converted to a double"));
1016 return scm_from_double (d);
1019 /* (value->string <gdb:value>
1020 [#:encoding encoding]
1021 [#:errors #f | 'error | 'substitute]
1024 Return Unicode string with value's contents, which must be a string.
1026 If ENCODING is not given, the string is assumed to be encoded in
1027 the target's charset.
1029 ERRORS is one of #f, 'error or 'substitute.
1030 An error setting of #f means use the default, which is
1031 Guile's %default-port-conversion-strategy. If the default is not one
1032 of 'error or 'substitute, 'substitute is used.
1033 An error setting of "error" causes an exception to be thrown if there's
1034 a decoding error. An error setting of "substitute" causes invalid
1035 characters to be replaced with "?".
1037 If LENGTH is provided, only fetch string to the length provided.
1038 LENGTH must be a Scheme integer, it can't be a <gdb:value> integer. */
1041 gdbscm_value_to_string (SCM self, SCM rest)
1044 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1045 struct value *value = v_smob->value;
1046 const SCM keywords[] = {
1047 encoding_keyword, errors_keyword, length_keyword, SCM_BOOL_F
1049 int encoding_arg_pos = -1, errors_arg_pos = -1, length_arg_pos = -1;
1050 char *encoding = NULL;
1051 SCM errors = SCM_BOOL_F;
1053 gdb_byte *buffer = NULL;
1054 const char *la_encoding = NULL;
1055 struct type *char_type = NULL;
1057 struct cleanup *cleanups;
1058 volatile struct gdb_exception except;
1060 /* The sequencing here, as everywhere else, is important.
1061 We can't have existing cleanups when a Scheme exception is thrown. */
1063 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#sOi", rest,
1064 &encoding_arg_pos, &encoding,
1065 &errors_arg_pos, &errors,
1066 &length_arg_pos, &length);
1068 cleanups = make_cleanup (xfree, encoding);
1070 if (errors_arg_pos > 0
1071 && errors != SCM_BOOL_F
1072 && !scm_is_eq (errors, error_symbol)
1073 && !scm_is_eq (errors, substitute_symbol))
1076 = gdbscm_make_out_of_range_error (FUNC_NAME, errors_arg_pos, errors,
1077 _("invalid error kind"));
1079 do_cleanups (cleanups);
1080 gdbscm_throw (excp);
1082 if (errors == SCM_BOOL_F)
1083 errors = scm_port_conversion_strategy (SCM_BOOL_F);
1084 /* We don't assume anything about the result of scm_port_conversion_strategy.
1085 From this point on, if errors is not 'errors, use 'substitute. */
1087 TRY_CATCH (except, RETURN_MASK_ALL)
1089 LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
1091 GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
1093 /* If errors is "error" scm_from_stringn may throw a Scheme exception.
1094 Make sure we don't leak. This is done via scm_dynwind_begin, et.al. */
1095 discard_cleanups (cleanups);
1097 scm_dynwind_begin (0);
1099 gdbscm_dynwind_xfree (encoding);
1100 gdbscm_dynwind_xfree (buffer);
1102 result = scm_from_stringn ((const char *) buffer,
1103 length * TYPE_LENGTH (char_type),
1104 (encoding != NULL && *encoding != '\0'
1107 scm_is_eq (errors, error_symbol)
1108 ? SCM_FAILED_CONVERSION_ERROR
1109 : SCM_FAILED_CONVERSION_QUESTION_MARK);
1116 /* (value->lazy-string <gdb:value> [#:encoding encoding] [#:length length])
1117 -> <gdb:lazy-string>
1118 Return a Scheme object representing a lazy_string_object type.
1119 A lazy string is a pointer to a string with an optional encoding and length.
1120 If ENCODING is not given, the target's charset is used.
1121 If LENGTH is provided then the length parameter is set to LENGTH, otherwise
1122 length will be set to -1 (first null of appropriate with).
1123 LENGTH must be a Scheme integer, it can't be a <gdb:value> integer. */
1126 gdbscm_value_to_lazy_string (SCM self, SCM rest)
1129 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1130 struct value *value = v_smob->value;
1131 const SCM keywords[] = { encoding_keyword, length_keyword, SCM_BOOL_F };
1132 int encoding_arg_pos = -1, length_arg_pos = -1;
1133 char *encoding = NULL;
1135 SCM result = SCM_BOOL_F; /* -Wall */
1136 struct cleanup *cleanups;
1137 volatile struct gdb_exception except;
1139 /* The sequencing here, as everywhere else, is important.
1140 We can't have existing cleanups when a Scheme exception is thrown. */
1142 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#si", rest,
1143 &encoding_arg_pos, &encoding,
1144 &length_arg_pos, &length);
1146 cleanups = make_cleanup (xfree, encoding);
1148 TRY_CATCH (except, RETURN_MASK_ALL)
1150 struct cleanup *inner_cleanup
1151 = make_cleanup_value_free_to_mark (value_mark ());
1153 if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
1154 value = value_ind (value);
1156 result = lsscm_make_lazy_string (value_address (value), length,
1157 encoding, value_type (value));
1159 do_cleanups (inner_cleanup);
1161 do_cleanups (cleanups);
1162 GDBSCM_HANDLE_GDB_EXCEPTION (except);
1164 if (gdbscm_is_exception (result))
1165 gdbscm_throw (result);
1170 /* (value-lazy? <gdb:value>) -> boolean */
1173 gdbscm_value_lazy_p (SCM self)
1176 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1177 struct value *value = v_smob->value;
1179 return scm_from_bool (value_lazy (value));
1182 /* (value-fetch-lazy! <gdb:value>) -> unspecified */
1185 gdbscm_value_fetch_lazy_x (SCM self)
1188 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1189 struct value *value = v_smob->value;
1190 volatile struct gdb_exception except;
1192 TRY_CATCH (except, RETURN_MASK_ALL)
1194 if (value_lazy (value))
1195 value_fetch_lazy (value);
1197 GDBSCM_HANDLE_GDB_EXCEPTION (except);
1199 return SCM_UNSPECIFIED;
1202 /* (value-print <gdb:value>) -> string */
1205 gdbscm_value_print (SCM self)
1208 = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1209 struct value *value = v_smob->value;
1210 struct value_print_options opts;
1213 volatile struct gdb_exception except;
1215 get_user_print_options (&opts);
1218 TRY_CATCH (except, RETURN_MASK_ALL)
1220 struct ui_file *stb = mem_fileopen ();
1221 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
1223 common_val_print (value, stb, 0, &opts, current_language);
1224 s = ui_file_xstrdup (stb, NULL);
1226 do_cleanups (old_chain);
1228 GDBSCM_HANDLE_GDB_EXCEPTION (except);
1230 /* Use SCM_FAILED_CONVERSION_QUESTION_MARK to ensure this doesn't
1231 throw an error if the encoding fails.
1232 IWBN to use scm_take_locale_string here, but we'd have to temporarily
1233 override the default port conversion handler because contrary to
1234 documentation it doesn't necessarily free the input string. */
1235 result = scm_from_stringn (s, strlen (s), host_charset (),
1236 SCM_FAILED_CONVERSION_QUESTION_MARK);
1242 /* (parse-and-eval string) -> <gdb:value>
1243 Parse a string and evaluate the string as an expression. */
1246 gdbscm_parse_and_eval (SCM expr_scm)
1249 struct value *res_val = NULL;
1251 struct cleanup *cleanups;
1252 volatile struct gdb_exception except;
1254 /* The sequencing here, as everywhere else, is important.
1255 We can't have existing cleanups when a Scheme exception is thrown. */
1257 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
1258 expr_scm, &expr_str);
1260 cleanups = make_cleanup_value_free_to_mark (value_mark ());
1261 make_cleanup (xfree, expr_str);
1263 TRY_CATCH (except, RETURN_MASK_ALL)
1265 res_val = parse_and_eval (expr_str);
1267 GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
1269 gdb_assert (res_val != NULL);
1270 result = vlscm_scm_from_value (res_val);
1272 do_cleanups (cleanups);
1274 if (gdbscm_is_exception (result))
1275 gdbscm_throw (result);
1280 /* (history-ref integer) -> <gdb:value>
1281 Return the specified value from GDB's value history. */
1284 gdbscm_history_ref (SCM index)
1287 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
1288 volatile struct gdb_exception except;
1290 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "i", index, &i);
1292 TRY_CATCH (except, RETURN_MASK_ALL)
1294 res_val = access_value_history (i);
1296 GDBSCM_HANDLE_GDB_EXCEPTION (except);
1298 return vlscm_scm_from_value (res_val);
1301 /* Initialize the Scheme value code. */
1303 static const scheme_function value_functions[] =
1305 { "value?", 1, 0, 0, gdbscm_value_p,
1307 Return #t if the object is a <gdb:value> object." },
1309 { "make-value", 1, 0, 1, gdbscm_make_value,
1311 Create a <gdb:value> representing object.\n\
1312 Typically this is used to convert numbers and strings to\n\
1313 <gdb:value> objects.\n\
1315 Arguments: object [#:type <gdb:type>]" },
1317 { "value-optimized-out?", 1, 0, 0, gdbscm_value_optimized_out_p,
1319 Return #t if the value has been optimizd out." },
1321 { "value-address", 1, 0, 0, gdbscm_value_address,
1323 Return the address of the value." },
1325 { "value-type", 1, 0, 0, gdbscm_value_type,
1327 Return the type of the value." },
1329 { "value-dynamic-type", 1, 0, 0, gdbscm_value_dynamic_type,
1331 Return the dynamic type of the value." },
1333 { "value-cast", 2, 0, 0, gdbscm_value_cast,
1335 Cast the value to the supplied type.\n\
1337 Arguments: <gdb:value> <gdb:type>" },
1339 { "value-dynamic-cast", 2, 0, 0, gdbscm_value_dynamic_cast,
1341 Cast the value to the supplied type, as if by the C++\n\
1342 dynamic_cast operator.\n\
1344 Arguments: <gdb:value> <gdb:type>" },
1346 { "value-reinterpret-cast", 2, 0, 0, gdbscm_value_reinterpret_cast,
1348 Cast the value to the supplied type, as if by the C++\n\
1349 reinterpret_cast operator.\n\
1351 Arguments: <gdb:value> <gdb:type>" },
1353 { "value-dereference", 1, 0, 0, gdbscm_value_dereference,
1355 Return the result of applying the C unary * operator to the value." },
1357 { "value-referenced-value", 1, 0, 0, gdbscm_value_referenced_value,
1359 Given a value of a reference type, return the value referenced.\n\
1360 The difference between this function and value-dereference is that\n\
1361 the latter applies * unary operator to a value, which need not always\n\
1362 result in the value referenced.\n\
1363 For example, for a value which is a reference to an 'int' pointer ('int *'),\n\
1364 value-dereference will result in a value of type 'int' while\n\
1365 value-referenced-value will result in a value of type 'int *'." },
1367 { "value-field", 2, 0, 0, gdbscm_value_field,
1369 Return the specified field of the value.\n\
1371 Arguments: <gdb:value> string" },
1373 { "value-subscript", 2, 0, 0, gdbscm_value_subscript,
1375 Return the value of the array at the specified index.\n\
1377 Arguments: <gdb:value> integer" },
1379 { "value-call", 2, 0, 0, gdbscm_value_call,
1381 Perform an inferior function call taking the value as a pointer to the\n\
1382 function to call.\n\
1383 Each element of the argument list must be a <gdb:value> object or an object\n\
1384 that can be converted to one.\n\
1385 The result is the value returned by the function.\n\
1387 Arguments: <gdb:value> arg-list" },
1389 { "value->bool", 1, 0, 0, gdbscm_value_to_bool,
1391 Return the Scheme boolean representing the GDB value.\n\
1392 The value must be \"integer like\". Pointers are ok." },
1394 { "value->integer", 1, 0, 0, gdbscm_value_to_integer,
1396 Return the Scheme integer representing the GDB value.\n\
1397 The value must be \"integer like\". Pointers are ok." },
1399 { "value->real", 1, 0, 0, gdbscm_value_to_real,
1401 Return the Scheme real number representing the GDB value.\n\
1402 The value must be a number." },
1404 { "value->bytevector", 1, 0, 0, gdbscm_value_to_bytevector,
1406 Return a Scheme bytevector with the raw contents of the GDB value.\n\
1407 No transformation, endian or otherwise, is performed." },
1409 { "value->string", 1, 0, 1, gdbscm_value_to_string,
1411 Return the Unicode string of the value's contents.\n\
1412 If ENCODING is not given, the string is assumed to be encoded in\n\
1413 the target's charset.\n\
1414 An error setting \"error\" causes an exception to be thrown if there's\n\
1415 a decoding error. An error setting of \"substitute\" causes invalid\n\
1416 characters to be replaced with \"?\". The default is \"error\".\n\
1417 If LENGTH is provided, only fetch string to the length provided.\n\
1419 Arguments: <gdb:value>\n\
1420 [#:encoding encoding] [#:errors \"error\"|\"substitute\"]\n\
1421 [#:length length]" },
1423 { "value->lazy-string", 1, 0, 1, gdbscm_value_to_lazy_string,
1425 Return a Scheme object representing a lazily fetched Unicode string\n\
1426 of the value's contents.\n\
1427 If ENCODING is not given, the string is assumed to be encoded in\n\
1428 the target's charset.\n\
1429 If LENGTH is provided, only fetch string to the length provided.\n\
1431 Arguments: <gdb:value> [#:encoding encoding] [#:length length]" },
1433 { "value-lazy?", 1, 0, 0, gdbscm_value_lazy_p,
1435 Return #t if the value is lazy (not fetched yet from the inferior).\n\
1436 A lazy value is fetched when needed, or when the value-fetch-lazy! function\n\
1439 { "make-lazy-value", 2, 0, 0, gdbscm_make_lazy_value,
1441 Create a <gdb:value> that will be lazily fetched from the target.\n\
1443 Arguments: <gdb:type> address" },
1445 { "value-fetch-lazy!", 1, 0, 0, gdbscm_value_fetch_lazy_x,
1447 Fetch the value from the inferior, if it was lazy.\n\
1448 The result is \"unspecified\"." },
1450 { "value-print", 1, 0, 0, gdbscm_value_print,
1452 Return the string representation (print form) of the value." },
1454 { "parse-and-eval", 1, 0, 0, gdbscm_parse_and_eval,
1456 Evaluates string in gdb and returns the result as a <gdb:value> object." },
1458 { "history-ref", 1, 0, 0, gdbscm_history_ref,
1460 Return the specified value from GDB's value history." },
1466 gdbscm_initialize_values (void)
1468 value_smob_tag = gdbscm_make_smob_type (value_smob_name,
1469 sizeof (value_smob));
1470 scm_set_smob_mark (value_smob_tag, vlscm_mark_value_smob);
1471 scm_set_smob_free (value_smob_tag, vlscm_free_value_smob);
1472 scm_set_smob_print (value_smob_tag, vlscm_print_value_smob);
1473 scm_set_smob_equalp (value_smob_tag, vlscm_equal_p_value_smob);
1475 gdbscm_define_functions (value_functions, 1);
1477 type_keyword = scm_from_latin1_keyword ("type");
1478 encoding_keyword = scm_from_latin1_keyword ("encoding");
1479 errors_keyword = scm_from_latin1_keyword ("errors");
1480 length_keyword = scm_from_latin1_keyword ("length");
1482 error_symbol = scm_from_latin1_symbol ("error");
1483 escape_symbol = scm_from_latin1_symbol ("escape");
1484 substitute_symbol = scm_from_latin1_symbol ("substitute");