GDB (xrefs)
scm-value.c
Go to the documentation of this file.
1 /* Scheme interface to values.
2 
3  Copyright (C) 2008-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
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.
11 
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.
16 
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/>. */
19 
20 /* See README file in this directory for implementation notes, coding
21  conventions, et.al. */
22 
23 #include "defs.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" /* Needed by language.h. */
30 #include "language.h"
31 #include "valprint.h"
32 #include "value.h"
33 #include "guile-internal.h"
34 
35 /* The <gdb:value> smob. */
36 
37 typedef struct _value_smob
38 {
39  /* This always appears first. */
41 
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;
47 
48  struct value *value;
49 
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;
58 } value_smob;
59 
60 static const char value_smob_name[] = "gdb:value";
61 
62 /* The tag Guile knows the value smob by. */
63 static scm_t_bits value_smob_tag;
64 
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. */
69 
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;
75 
76 /* Possible #:errors values. */
77 static SCM error_symbol;
78 static SCM escape_symbol;
79 static SCM substitute_symbol;
80 
81 /* Administrivia for value smobs. */
82 
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". */
86 
87 void
89  struct objfile *objfile, htab_t copied_types)
90 {
91  value_smob *iter;
92 
93  for (iter = values_in_scheme; iter; iter = iter->next)
94  preserve_one_value (iter->value, objfile, copied_types);
95 }
96 
97 /* Helper to add a value_smob to the global list. */
98 
99 static void
101 {
102  v_smob->next = values_in_scheme;
103  if (v_smob->next)
104  v_smob->next->prev = v_smob;
105  v_smob->prev = NULL;
106  values_in_scheme = v_smob;
107 }
108 
109 /* Helper to remove a value_smob from the global list. */
110 
111 static void
113 {
114  /* Remove SELF from the global list. */
115  if (v_smob->prev)
116  v_smob->prev->next = v_smob->next;
117  else
118  {
119  gdb_assert (values_in_scheme == v_smob);
120  values_in_scheme = v_smob->next;
121  }
122  if (v_smob->next)
123  v_smob->next->prev = v_smob->prev;
124 }
125 
126 /* The smob "free" function for <gdb:value>. */
127 
128 static size_t
130 {
131  value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
132 
133  vlscm_forget_value_smob (v_smob);
134  value_free (v_smob->value);
135 
136  return 0;
137 }
138 
139 /* The smob "print" function for <gdb:value>. */
140 
141 static int
142 vlscm_print_value_smob (SCM self, SCM port, scm_print_state *pstate)
143 {
144  value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
145  struct value_print_options opts;
146 
147  if (pstate->writingp)
148  gdbscm_printf (port, "#<%s ", value_smob_name);
149 
150  get_user_print_options (&opts);
151  opts.deref_ref = 0;
152 
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;
158 
159  TRY
160  {
161  string_file stb;
162 
163  common_val_print (v_smob->value, &stb, 0, &opts, current_language);
164  scm_puts (stb.c_str (), port);
165  }
166  CATCH (except, RETURN_MASK_ALL)
167  {
169  }
170  END_CATCH
171 
172  if (pstate->writingp)
173  scm_puts (">", port);
174 
175  scm_remember_upto_here_1 (self);
176 
177  /* Non-zero means success. */
178  return 1;
179 }
180 
181 /* The smob "equalp" function for <gdb:value>. */
182 
183 static SCM
184 vlscm_equal_p_value_smob (SCM v1, SCM v2)
185 {
186  const value_smob *v1_smob = (value_smob *) SCM_SMOB_DATA (v1);
187  const value_smob *v2_smob = (value_smob *) SCM_SMOB_DATA (v2);
188  int result = 0;
189 
190  TRY
191  {
192  result = value_equal (v1_smob->value, v2_smob->value);
193  }
194  CATCH (except, RETURN_MASK_ALL)
195  {
197  }
198  END_CATCH
199 
200  return scm_from_bool (result);
201 }
202 
203 /* Low level routine to create a <gdb:value> object. */
204 
205 static SCM
207 {
208  value_smob *v_smob = (value_smob *)
209  scm_gc_malloc (sizeof (value_smob), value_smob_name);
210  SCM v_scm;
211 
212  /* These must be filled in by the caller. */
213  v_smob->value = NULL;
214  v_smob->prev = NULL;
215  v_smob->next = NULL;
216 
217  /* These are lazily computed. */
218  v_smob->address = SCM_UNDEFINED;
219  v_smob->type = SCM_UNDEFINED;
220  v_smob->dynamic_type = SCM_UNDEFINED;
221 
222  v_scm = scm_new_smob (value_smob_tag, (scm_t_bits) v_smob);
223  gdbscm_init_gsmob (&v_smob->base);
224 
225  return v_scm;
226 }
227 
228 /* Return non-zero if SCM is a <gdb:value> object. */
229 
230 int
231 vlscm_is_value (SCM scm)
232 {
233  return SCM_SMOB_PREDICATE (value_smob_tag, scm);
234 }
235 
236 /* (value? object) -> boolean */
237 
238 static SCM
239 gdbscm_value_p (SCM scm)
240 {
241  return scm_from_bool (vlscm_is_value (scm));
242 }
243 
244 /* Create a new <gdb:value> object that encapsulates VALUE.
245  The value is released from the all_values chain so its lifetime is not
246  bound to the execution of a command. */
247 
248 SCM
250 {
251  /* N.B. It's important to not cause any side-effects until we know the
252  conversion worked. */
253  SCM v_scm = vlscm_make_value_smob ();
254  value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
255 
256  v_smob->value = value;
259 
260  return v_scm;
261 }
262 
263 /* Returns the <gdb:value> object in SELF.
264  Throws an exception if SELF is not a <gdb:value> object. */
265 
266 static SCM
267 vlscm_get_value_arg_unsafe (SCM self, int arg_pos, const char *func_name)
268 {
269  SCM_ASSERT_TYPE (vlscm_is_value (self), self, arg_pos, func_name,
271 
272  return self;
273 }
274 
275 /* Returns a pointer to the value smob of SELF.
276  Throws an exception if SELF is not a <gdb:value> object. */
277 
278 static value_smob *
279 vlscm_get_value_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
280 {
281  SCM v_scm = vlscm_get_value_arg_unsafe (self, arg_pos, func_name);
282  value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
283 
284  return v_smob;
285 }
286 
287 /* Return the value field of V_SCM, an object of type <gdb:value>.
288  This exists so that we don't have to export the struct's contents. */
289 
290 struct value *
292 {
293  value_smob *v_smob;
294 
295  gdb_assert (vlscm_is_value (v_scm));
296  v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
297  return v_smob->value;
298 }
299 
300 /* Value methods. */
301 
302 /* (make-value x [#:type type]) -> <gdb:value> */
303 
304 static SCM
305 gdbscm_make_value (SCM x, SCM rest)
306 {
307  struct gdbarch *gdbarch = get_current_arch ();
308  const struct language_defn *language = current_language;
309  const SCM keywords[] = { type_keyword, SCM_BOOL_F };
310  int type_arg_pos = -1;
311  SCM type_scm = SCM_UNDEFINED;
312  SCM except_scm, result;
313  type_smob *t_smob;
314  struct type *type = NULL;
315  struct value *value;
316  struct cleanup *cleanups;
317 
318  gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#O", rest,
319  &type_arg_pos, &type_scm);
320 
321  if (type_arg_pos > 0)
322  {
323  t_smob = tyscm_get_type_smob_arg_unsafe (type_scm, type_arg_pos,
324  FUNC_NAME);
325  type = tyscm_type_smob_type (t_smob);
326  }
327 
329 
331  type_arg_pos, type_scm, type,
332  &except_scm,
333  gdbarch, language);
334  if (value == NULL)
335  {
336  do_cleanups (cleanups);
337  gdbscm_throw (except_scm);
338  }
339 
340  result = vlscm_scm_from_value (value);
341 
342  do_cleanups (cleanups);
343 
344  if (gdbscm_is_exception (result))
345  gdbscm_throw (result);
346  return result;
347 }
348 
349 /* (make-lazy-value <gdb:type> address) -> <gdb:value> */
350 
351 static SCM
352 gdbscm_make_lazy_value (SCM type_scm, SCM address_scm)
353 {
354  type_smob *t_smob;
355  struct type *type;
356  ULONGEST address;
357  struct value *value = NULL;
358  SCM result;
359  struct cleanup *cleanups;
360 
361  t_smob = tyscm_get_type_smob_arg_unsafe (type_scm, SCM_ARG1, FUNC_NAME);
362  type = tyscm_type_smob_type (t_smob);
363 
364  gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, NULL, "U",
365  address_scm, &address);
366 
368 
369  /* There's no (current) need to wrap this in a TRY_CATCH, but for consistency
370  and future-proofing we do. */
371  TRY
372  {
373  value = value_from_contents_and_address (type, NULL, address);
374  }
375  CATCH (except, RETURN_MASK_ALL)
376  {
378  }
379  END_CATCH
380 
381  result = vlscm_scm_from_value (value);
382 
383  do_cleanups (cleanups);
384 
385  if (gdbscm_is_exception (result))
386  gdbscm_throw (result);
387  return result;
388 }
389 
390 /* (value-optimized-out? <gdb:value>) -> boolean */
391 
392 static SCM
394 {
395  value_smob *v_smob
396  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
397  struct value *value = v_smob->value;
398  int opt = 0;
399 
400  TRY
401  {
402  opt = value_optimized_out (value);
403  }
404  CATCH (except, RETURN_MASK_ALL)
405  {
407  }
408  END_CATCH
409 
410  return scm_from_bool (opt);
411 }
412 
413 /* (value-address <gdb:value>) -> integer
414  Returns #f if the value doesn't have one. */
415 
416 static SCM
418 {
419  value_smob *v_smob
420  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
421  struct value *value = v_smob->value;
422 
423  if (SCM_UNBNDP (v_smob->address))
424  {
425  struct cleanup *cleanup
427  SCM address = SCM_BOOL_F;
428 
429  TRY
430  {
431  address = vlscm_scm_from_value (value_addr (value));
432  }
433  CATCH (except, RETURN_MASK_ALL)
434  {
435  }
436  END_CATCH
437 
439 
440  if (gdbscm_is_exception (address))
441  gdbscm_throw (address);
442 
443  v_smob->address = address;
444  }
445 
446  return v_smob->address;
447 }
448 
449 /* (value-dereference <gdb:value>) -> <gdb:value>
450  Given a value of a pointer type, apply the C unary * operator to it. */
451 
452 static SCM
454 {
455  value_smob *v_smob
456  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
457  struct value *value = v_smob->value;
458  SCM result;
459  struct value *res_val = NULL;
460  struct cleanup *cleanups;
461 
463 
464  TRY
465  {
466  res_val = value_ind (value);
467  }
468  CATCH (except, RETURN_MASK_ALL)
469  {
471  }
472  END_CATCH
473 
474  result = vlscm_scm_from_value (res_val);
475 
476  do_cleanups (cleanups);
477 
478  if (gdbscm_is_exception (result))
479  gdbscm_throw (result);
480 
481  return result;
482 }
483 
484 /* (value-referenced-value <gdb:value>) -> <gdb:value>
485  Given a value of a reference type, return the value referenced.
486  The difference between this function and gdbscm_value_dereference is that
487  the latter applies * unary operator to a value, which need not always
488  result in the value referenced.
489  For example, for a value which is a reference to an 'int' pointer ('int *'),
490  gdbscm_value_dereference will result in a value of type 'int' while
491  gdbscm_value_referenced_value will result in a value of type 'int *'. */
492 
493 static SCM
495 {
496  value_smob *v_smob
497  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
498  struct value *value = v_smob->value;
499  SCM result;
500  struct value *res_val = NULL;
501  struct cleanup *cleanups;
502 
504 
505  TRY
506  {
507  switch (TYPE_CODE (check_typedef (value_type (value))))
508  {
509  case TYPE_CODE_PTR:
510  res_val = value_ind (value);
511  break;
512  case TYPE_CODE_REF:
513  res_val = coerce_ref (value);
514  break;
515  default:
516  error (_("Trying to get the referenced value from a value which is"
517  " neither a pointer nor a reference"));
518  }
519  }
520  CATCH (except, RETURN_MASK_ALL)
521  {
523  }
524  END_CATCH
525 
526  result = vlscm_scm_from_value (res_val);
527 
528  do_cleanups (cleanups);
529 
530  if (gdbscm_is_exception (result))
531  gdbscm_throw (result);
532 
533  return result;
534 }
535 
536 /* (value-type <gdb:value>) -> <gdb:type> */
537 
538 static SCM
540 {
541  value_smob *v_smob
542  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
543  struct value *value = v_smob->value;
544 
545  if (SCM_UNBNDP (v_smob->type))
547 
548  return v_smob->type;
549 }
550 
551 /* (value-dynamic-type <gdb:value>) -> <gdb:type> */
552 
553 static SCM
555 {
556  value_smob *v_smob
557  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
558  struct value *value = v_smob->value;
559  struct type *type = NULL;
560 
561  if (! SCM_UNBNDP (v_smob->dynamic_type))
562  return v_smob->dynamic_type;
563 
564  TRY
565  {
566  struct cleanup *cleanup
568 
569  type = value_type (value);
570  type = check_typedef (type);
571 
572  if (((TYPE_CODE (type) == TYPE_CODE_PTR)
573  || (TYPE_CODE (type) == TYPE_CODE_REF))
575  {
576  struct value *target;
577  int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
578 
579  if (was_pointer)
580  target = value_ind (value);
581  else
582  target = coerce_ref (value);
583  type = value_rtti_type (target, NULL, NULL, NULL);
584 
585  if (type)
586  {
587  if (was_pointer)
589  else
591  }
592  }
593  else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
594  type = value_rtti_type (value, NULL, NULL, NULL);
595  else
596  {
597  /* Re-use object's static type. */
598  type = NULL;
599  }
600 
602  }
603  CATCH (except, RETURN_MASK_ALL)
604  {
606  }
607  END_CATCH
608 
609  if (type == NULL)
610  v_smob->dynamic_type = gdbscm_value_type (self);
611  else
613 
614  return v_smob->dynamic_type;
615 }
616 
617 /* A helper function that implements the various cast operators. */
618 
619 static SCM
620 vlscm_do_cast (SCM self, SCM type_scm, enum exp_opcode op,
621  const char *func_name)
622 {
623  value_smob *v_smob
624  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
625  struct value *value = v_smob->value;
626  type_smob *t_smob
627  = tyscm_get_type_smob_arg_unsafe (type_scm, SCM_ARG2, FUNC_NAME);
628  struct type *type = tyscm_type_smob_type (t_smob);
629  SCM result;
630  struct value *res_val = NULL;
631  struct cleanup *cleanups;
632 
634 
635  TRY
636  {
637  if (op == UNOP_DYNAMIC_CAST)
638  res_val = value_dynamic_cast (type, value);
639  else if (op == UNOP_REINTERPRET_CAST)
640  res_val = value_reinterpret_cast (type, value);
641  else
642  {
643  gdb_assert (op == UNOP_CAST);
644  res_val = value_cast (type, value);
645  }
646  }
647  CATCH (except, RETURN_MASK_ALL)
648  {
650  }
651  END_CATCH
652 
653  gdb_assert (res_val != NULL);
654  result = vlscm_scm_from_value (res_val);
655 
656  do_cleanups (cleanups);
657 
658  if (gdbscm_is_exception (result))
659  gdbscm_throw (result);
660 
661  return result;
662 }
663 
664 /* (value-cast <gdb:value> <gdb:type>) -> <gdb:value> */
665 
666 static SCM
668 {
669  return vlscm_do_cast (self, new_type, UNOP_CAST, FUNC_NAME);
670 }
671 
672 /* (value-dynamic-cast <gdb:value> <gdb:type>) -> <gdb:value> */
673 
674 static SCM
676 {
677  return vlscm_do_cast (self, new_type, UNOP_DYNAMIC_CAST, FUNC_NAME);
678 }
679 
680 /* (value-reinterpret-cast <gdb:value> <gdb:type>) -> <gdb:value> */
681 
682 static SCM
684 {
685  return vlscm_do_cast (self, new_type, UNOP_REINTERPRET_CAST, FUNC_NAME);
686 }
687 
688 /* (value-field <gdb:value> string) -> <gdb:value>
689  Given string name of an element inside structure, return its <gdb:value>
690  object. */
691 
692 static SCM
693 gdbscm_value_field (SCM self, SCM field_scm)
694 {
695  value_smob *v_smob
696  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
697  struct value *value = v_smob->value;
698  char *field = NULL;
699  struct value *res_val = NULL;
700  SCM result;
701  struct cleanup *cleanups;
702 
703  SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
704  _("string"));
705 
707 
708  field = gdbscm_scm_to_c_string (field_scm);
710 
711  TRY
712  {
713  struct value *tmp = value;
714 
715  res_val = value_struct_elt (&tmp, NULL, field, NULL,
716  "struct/class/union");
717  }
718  CATCH (except, RETURN_MASK_ALL)
719  {
721  }
722  END_CATCH
723 
724  gdb_assert (res_val != NULL);
725  result = vlscm_scm_from_value (res_val);
726 
727  do_cleanups (cleanups);
728 
729  if (gdbscm_is_exception (result))
730  gdbscm_throw (result);
731 
732  return result;
733 }
734 
735 /* (value-subscript <gdb:value> integer|<gdb:value>) -> <gdb:value>
736  Return the specified value in an array. */
737 
738 static SCM
739 gdbscm_value_subscript (SCM self, SCM index_scm)
740 {
741  value_smob *v_smob
742  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
743  struct value *value = v_smob->value;
744  struct value *index = NULL;
745  struct value *res_val = NULL;
746  struct type *type = value_type (value);
747  struct gdbarch *gdbarch;
748  SCM result, except_scm;
749  struct cleanup *cleanups;
750 
751  /* The sequencing here, as everywhere else, is important.
752  We can't have existing cleanups when a Scheme exception is thrown. */
753 
754  SCM_ASSERT (type != NULL, self, SCM_ARG2, FUNC_NAME);
756 
758 
759  index = vlscm_convert_value_from_scheme (FUNC_NAME, SCM_ARG2, index_scm,
760  &except_scm,
762  if (index == NULL)
763  {
764  do_cleanups (cleanups);
765  gdbscm_throw (except_scm);
766  }
767 
768  TRY
769  {
770  struct value *tmp = value;
771 
772  /* Assume we are attempting an array access, and let the value code
773  throw an exception if the index has an invalid type.
774  Check the value's type is something that can be accessed via
775  a subscript. */
776  tmp = coerce_ref (tmp);
777  type = check_typedef (value_type (tmp));
779  && TYPE_CODE (type) != TYPE_CODE_PTR)
780  error (_("Cannot subscript requested type"));
781 
782  res_val = value_subscript (tmp, value_as_long (index));
783  }
784  CATCH (except, RETURN_MASK_ALL)
785  {
787  }
788  END_CATCH
789 
790  gdb_assert (res_val != NULL);
791  result = vlscm_scm_from_value (res_val);
792 
793  do_cleanups (cleanups);
794 
795  if (gdbscm_is_exception (result))
796  gdbscm_throw (result);
797 
798  return result;
799 }
800 
801 /* (value-call <gdb:value> arg-list) -> <gdb:value>
802  Perform an inferior function call on the value. */
803 
804 static SCM
805 gdbscm_value_call (SCM self, SCM args)
806 {
807  value_smob *v_smob
808  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
809  struct value *function = v_smob->value;
810  struct value *mark = value_mark ();
811  struct type *ftype = NULL;
812  long args_count;
813  struct value **vargs = NULL;
814  SCM result = SCM_BOOL_F;
815 
816  TRY
817  {
818  ftype = check_typedef (value_type (function));
819  }
820  CATCH (except, RETURN_MASK_ALL)
821  {
823  }
824  END_CATCH
825 
826  SCM_ASSERT_TYPE (TYPE_CODE (ftype) == TYPE_CODE_FUNC, self,
827  SCM_ARG1, FUNC_NAME,
828  _("function (value of TYPE_CODE_FUNC)"));
829 
830  SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (args)), args,
831  SCM_ARG2, FUNC_NAME, _("list"));
832 
833  args_count = scm_ilength (args);
834  if (args_count > 0)
835  {
836  struct gdbarch *gdbarch = get_current_arch ();
837  const struct language_defn *language = current_language;
838  SCM except_scm;
839  long i;
840 
841  vargs = XALLOCAVEC (struct value *, args_count);
842  for (i = 0; i < args_count; i++)
843  {
844  SCM arg = scm_car (args);
845 
847  GDBSCM_ARG_NONE, arg,
848  &except_scm,
849  gdbarch, language);
850  if (vargs[i] == NULL)
851  gdbscm_throw (except_scm);
852 
853  args = scm_cdr (args);
854  }
855  gdb_assert (gdbscm_is_true (scm_null_p (args)));
856  }
857 
858  TRY
859  {
861  struct value *return_value;
862 
863  return_value = call_function_by_hand (function, NULL, args_count, vargs);
864  result = vlscm_scm_from_value (return_value);
866  }
867  CATCH (except, RETURN_MASK_ALL)
868  {
870  }
871  END_CATCH
872 
873  if (gdbscm_is_exception (result))
874  gdbscm_throw (result);
875 
876  return result;
877 }
878 
879 /* (value->bytevector <gdb:value>) -> bytevector */
880 
881 static SCM
883 {
884  value_smob *v_smob
885  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
886  struct value *value = v_smob->value;
887  struct type *type;
888  size_t length = 0;
889  const gdb_byte *contents = NULL;
890  SCM bv;
891 
892  type = value_type (value);
893 
894  TRY
895  {
896  type = check_typedef (type);
897  length = TYPE_LENGTH (type);
898  contents = value_contents (value);
899  }
900  CATCH (except, RETURN_MASK_ALL)
901  {
903  }
904  END_CATCH
905 
906  bv = scm_c_make_bytevector (length);
907  memcpy (SCM_BYTEVECTOR_CONTENTS (bv), contents, length);
908 
909  return bv;
910 }
911 
912 /* Helper function to determine if a type is "int-like". */
913 
914 static int
915 is_intlike (struct type *type, int ptr_ok)
916 {
917  return (TYPE_CODE (type) == TYPE_CODE_INT
921  || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
922 }
923 
924 /* (value->bool <gdb:value>) -> boolean
925  Throws an error if the value is not integer-like. */
926 
927 static SCM
929 {
930  value_smob *v_smob
931  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
932  struct value *value = v_smob->value;
933  struct type *type;
934  LONGEST l = 0;
935 
936  type = value_type (value);
937 
938  TRY
939  {
940  type = check_typedef (type);
941  }
942  CATCH (except, RETURN_MASK_ALL)
943  {
945  }
946  END_CATCH
947 
948  SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
949  _("integer-like gdb value"));
950 
951  TRY
952  {
953  if (TYPE_CODE (type) == TYPE_CODE_PTR)
954  l = value_as_address (value);
955  else
956  l = value_as_long (value);
957  }
958  CATCH (except, RETURN_MASK_ALL)
959  {
961  }
962  END_CATCH
963 
964  return scm_from_bool (l != 0);
965 }
966 
967 /* (value->integer <gdb:value>) -> integer
968  Throws an error if the value is not integer-like. */
969 
970 static SCM
972 {
973  value_smob *v_smob
974  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
975  struct value *value = v_smob->value;
976  struct type *type;
977  LONGEST l = 0;
978 
979  type = value_type (value);
980 
981  TRY
982  {
983  type = check_typedef (type);
984  }
985  CATCH (except, RETURN_MASK_ALL)
986  {
988  }
989  END_CATCH
990 
991  SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
992  _("integer-like gdb value"));
993 
994  TRY
995  {
996  if (TYPE_CODE (type) == TYPE_CODE_PTR)
997  l = value_as_address (value);
998  else
999  l = value_as_long (value);
1000  }
1001  CATCH (except, RETURN_MASK_ALL)
1002  {
1003  GDBSCM_HANDLE_GDB_EXCEPTION (except);
1004  }
1005  END_CATCH
1006 
1007  if (TYPE_UNSIGNED (type))
1008  return gdbscm_scm_from_ulongest (l);
1009  else
1010  return gdbscm_scm_from_longest (l);
1011 }
1012 
1013 /* (value->real <gdb:value>) -> real
1014  Throws an error if the value is not a number. */
1015 
1016 static SCM
1018 {
1019  value_smob *v_smob
1020  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1021  struct value *value = v_smob->value;
1022  struct type *type;
1023  double d = 0;
1024  struct value *check = nullptr;
1025 
1026  type = value_type (value);
1027 
1028  TRY
1029  {
1030  type = check_typedef (type);
1031  }
1032  CATCH (except, RETURN_MASK_ALL)
1033  {
1034  GDBSCM_HANDLE_GDB_EXCEPTION (except);
1035  }
1036  END_CATCH
1037 
1038  SCM_ASSERT_TYPE (is_intlike (type, 0) || TYPE_CODE (type) == TYPE_CODE_FLT,
1039  self, SCM_ARG1, FUNC_NAME, _("number"));
1040 
1041  TRY
1042  {
1043  if (is_floating_value (value))
1044  {
1048  }
1049  else if (TYPE_UNSIGNED (type))
1050  {
1051  d = (ULONGEST) value_as_long (value);
1053  }
1054  else
1055  {
1056  d = value_as_long (value);
1058  }
1059  }
1060  CATCH (except, RETURN_MASK_ALL)
1061  {
1062  GDBSCM_HANDLE_GDB_EXCEPTION (except);
1063  }
1064  END_CATCH
1065 
1066  /* TODO: Is there a better way to check if the value fits? */
1067  if (!value_equal (value, check))
1068  gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
1069  _("number can't be converted to a double"));
1070 
1071  return scm_from_double (d);
1072 }
1073 
1074 /* (value->string <gdb:value>
1075  [#:encoding encoding]
1076  [#:errors #f | 'error | 'substitute]
1077  [#:length length])
1078  -> string
1079  Return Unicode string with value's contents, which must be a string.
1080 
1081  If ENCODING is not given, the string is assumed to be encoded in
1082  the target's charset.
1083 
1084  ERRORS is one of #f, 'error or 'substitute.
1085  An error setting of #f means use the default, which is Guile's
1086  %default-port-conversion-strategy when using Guile >= 2.0.6, or 'error if
1087  using an earlier version of Guile. Earlier versions do not properly
1088  support obtaining the default port conversion strategy.
1089  If the default is not one of 'error or 'substitute, 'substitute is used.
1090  An error setting of "error" causes an exception to be thrown if there's
1091  a decoding error. An error setting of "substitute" causes invalid
1092  characters to be replaced with "?".
1093 
1094  If LENGTH is provided, only fetch string to the length provided.
1095  LENGTH must be a Scheme integer, it can't be a <gdb:value> integer. */
1096 
1097 static SCM
1098 gdbscm_value_to_string (SCM self, SCM rest)
1099 {
1100  value_smob *v_smob
1101  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1102  struct value *value = v_smob->value;
1103  const SCM keywords[] = {
1105  };
1106  int encoding_arg_pos = -1, errors_arg_pos = -1, length_arg_pos = -1;
1107  char *encoding = NULL;
1108  SCM errors = SCM_BOOL_F;
1109  int length = -1;
1110  gdb_byte *buffer = NULL;
1111  const char *la_encoding = NULL;
1112  struct type *char_type = NULL;
1113  SCM result;
1114  struct cleanup *cleanups;
1115 
1116  /* The sequencing here, as everywhere else, is important.
1117  We can't have existing cleanups when a Scheme exception is thrown. */
1118 
1119  gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#sOi", rest,
1120  &encoding_arg_pos, &encoding,
1121  &errors_arg_pos, &errors,
1122  &length_arg_pos, &length);
1123 
1124  cleanups = make_cleanup (xfree, encoding);
1125 
1126  if (errors_arg_pos > 0
1127  && errors != SCM_BOOL_F
1128  && !scm_is_eq (errors, error_symbol)
1129  && !scm_is_eq (errors, substitute_symbol))
1130  {
1131  SCM excp
1132  = gdbscm_make_out_of_range_error (FUNC_NAME, errors_arg_pos, errors,
1133  _("invalid error kind"));
1134 
1135  do_cleanups (cleanups);
1136  gdbscm_throw (excp);
1137  }
1138  if (errors == SCM_BOOL_F)
1139  {
1140  /* N.B. scm_port_conversion_strategy in Guile versions prior to 2.0.6
1141  will throw a Scheme error when passed #f. */
1142  if (gdbscm_guile_version_is_at_least (2, 0, 6))
1143  errors = scm_port_conversion_strategy (SCM_BOOL_F);
1144  else
1145  errors = error_symbol;
1146  }
1147  /* We don't assume anything about the result of scm_port_conversion_strategy.
1148  From this point on, if errors is not 'errors, use 'substitute. */
1149 
1150  TRY
1151  {
1152  LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
1153  }
1154  CATCH (except, RETURN_MASK_ALL)
1155  {
1156  GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
1157  }
1158  END_CATCH
1159 
1160  /* If errors is "error" scm_from_stringn may throw a Scheme exception.
1161  Make sure we don't leak. This is done via scm_dynwind_begin, et.al. */
1162  discard_cleanups (cleanups);
1163 
1164  scm_dynwind_begin ((scm_t_dynwind_flags) 0);
1165 
1166  gdbscm_dynwind_xfree (encoding);
1168 
1169  result = scm_from_stringn ((const char *) buffer,
1170  length * TYPE_LENGTH (char_type),
1171  (encoding != NULL && *encoding != '\0'
1172  ? encoding
1173  : la_encoding),
1174  scm_is_eq (errors, error_symbol)
1175  ? SCM_FAILED_CONVERSION_ERROR
1176  : SCM_FAILED_CONVERSION_QUESTION_MARK);
1177 
1178  scm_dynwind_end ();
1179 
1180  return result;
1181 }
1182 
1183 /* (value->lazy-string <gdb:value> [#:encoding encoding] [#:length length])
1184  -> <gdb:lazy-string>
1185  Return a Scheme object representing a lazy_string_object type.
1186  A lazy string is a pointer to a string with an optional encoding and length.
1187  If ENCODING is not given, the target's charset is used.
1188  If LENGTH is provided then the length parameter is set to LENGTH.
1189  Otherwise if the value is an array of known length then the array's length
1190  is used. Otherwise the length will be set to -1 (meaning first null of
1191  appropriate with).
1192  LENGTH must be a Scheme integer, it can't be a <gdb:value> integer. */
1193 
1194 static SCM
1195 gdbscm_value_to_lazy_string (SCM self, SCM rest)
1196 {
1197  value_smob *v_smob
1198  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1199  struct value *value = v_smob->value;
1200  const SCM keywords[] = { encoding_keyword, length_keyword, SCM_BOOL_F };
1201  int encoding_arg_pos = -1, length_arg_pos = -1;
1202  char *encoding = NULL;
1203  int length = -1;
1204  SCM result = SCM_BOOL_F; /* -Wall */
1205  struct cleanup *cleanups;
1206  struct gdb_exception except = exception_none;
1207 
1208  /* The sequencing here, as everywhere else, is important.
1209  We can't have existing cleanups when a Scheme exception is thrown. */
1210 
1211  gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#si", rest,
1212  &encoding_arg_pos, &encoding,
1213  &length_arg_pos, &length);
1214 
1215  if (length < -1)
1216  {
1217  gdbscm_out_of_range_error (FUNC_NAME, length_arg_pos,
1218  scm_from_int (length),
1219  _("invalid length"));
1220  }
1221 
1222  cleanups = make_cleanup (xfree, encoding);
1223 
1224  TRY
1225  {
1226  struct cleanup *inner_cleanup
1228  struct type *type, *realtype;
1229  CORE_ADDR addr;
1230 
1231  type = value_type (value);
1232  realtype = check_typedef (type);
1233 
1234  switch (TYPE_CODE (realtype))
1235  {
1236  case TYPE_CODE_ARRAY:
1237  {
1238  LONGEST array_length = -1;
1239  LONGEST low_bound, high_bound;
1240 
1241  /* PR 20786: There's no way to specify an array of length zero.
1242  Record a length of [0,-1] which is how Ada does it. Anything
1243  we do is broken, but this one possible solution. */
1244  if (get_array_bounds (realtype, &low_bound, &high_bound))
1245  array_length = high_bound - low_bound + 1;
1246  if (length == -1)
1247  length = array_length;
1248  else if (array_length == -1)
1249  {
1251  0, length - 1);
1252  }
1253  else if (length != array_length)
1254  {
1255  /* We need to create a new array type with the
1256  specified length. */
1257  if (length > array_length)
1258  error (_("length is larger than array size"));
1260  low_bound,
1261  low_bound + length - 1);
1262  }
1263  addr = value_address (value);
1264  break;
1265  }
1266  case TYPE_CODE_PTR:
1267  /* If a length is specified we defer creating an array of the
1268  specified width until we need to. */
1269  addr = value_as_address (value);
1270  break;
1271  default:
1272  /* Should flag an error here. PR 20769. */
1273  addr = value_address (value);
1274  break;
1275  }
1276 
1277  result = lsscm_make_lazy_string (addr, length, encoding, type);
1278 
1279  do_cleanups (inner_cleanup);
1280  }
1281  CATCH (ex, RETURN_MASK_ALL)
1282  {
1283  except = ex;
1284  }
1285  END_CATCH
1286 
1287  do_cleanups (cleanups);
1288  GDBSCM_HANDLE_GDB_EXCEPTION (except);
1289 
1290  if (gdbscm_is_exception (result))
1291  gdbscm_throw (result);
1292 
1293  return result;
1294 }
1295 
1296 /* (value-lazy? <gdb:value>) -> boolean */
1297 
1298 static SCM
1300 {
1301  value_smob *v_smob
1302  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1303  struct value *value = v_smob->value;
1304 
1305  return scm_from_bool (value_lazy (value));
1306 }
1307 
1308 /* (value-fetch-lazy! <gdb:value>) -> unspecified */
1309 
1310 static SCM
1312 {
1313  value_smob *v_smob
1314  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1315  struct value *value = v_smob->value;
1316 
1317  TRY
1318  {
1319  if (value_lazy (value))
1321  }
1322  CATCH (except, RETURN_MASK_ALL)
1323  {
1324  GDBSCM_HANDLE_GDB_EXCEPTION (except);
1325  }
1326  END_CATCH
1327 
1328  return SCM_UNSPECIFIED;
1329 }
1330 
1331 /* (value-print <gdb:value>) -> string */
1332 
1333 static SCM
1335 {
1336  value_smob *v_smob
1337  = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1338  struct value *value = v_smob->value;
1339  struct value_print_options opts;
1340 
1341  get_user_print_options (&opts);
1342  opts.deref_ref = 0;
1343 
1344  string_file stb;
1345 
1346  TRY
1347  {
1348  common_val_print (value, &stb, 0, &opts, current_language);
1349  }
1350  CATCH (except, RETURN_MASK_ALL)
1351  {
1352  GDBSCM_HANDLE_GDB_EXCEPTION (except);
1353  }
1354  END_CATCH
1355 
1356  /* Use SCM_FAILED_CONVERSION_QUESTION_MARK to ensure this doesn't
1357  throw an error if the encoding fails.
1358  IWBN to use scm_take_locale_string here, but we'd have to temporarily
1359  override the default port conversion handler because contrary to
1360  documentation it doesn't necessarily free the input string. */
1361  return scm_from_stringn (stb.c_str (), stb.size (), host_charset (),
1362  SCM_FAILED_CONVERSION_QUESTION_MARK);
1363 }
1364 
1365 /* (parse-and-eval string) -> <gdb:value>
1366  Parse a string and evaluate the string as an expression. */
1367 
1368 static SCM
1370 {
1371  char *expr_str;
1372  struct value *res_val = NULL;
1373  SCM result;
1374  struct cleanup *cleanups;
1375 
1376  /* The sequencing here, as everywhere else, is important.
1377  We can't have existing cleanups when a Scheme exception is thrown. */
1378 
1379  gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
1380  expr_scm, &expr_str);
1381 
1383  make_cleanup (xfree, expr_str);
1384 
1385  TRY
1386  {
1387  res_val = parse_and_eval (expr_str);
1388  }
1389  CATCH (except, RETURN_MASK_ALL)
1390  {
1391  GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
1392  }
1393  END_CATCH
1394 
1395  gdb_assert (res_val != NULL);
1396  result = vlscm_scm_from_value (res_val);
1397 
1398  do_cleanups (cleanups);
1399 
1400  if (gdbscm_is_exception (result))
1401  gdbscm_throw (result);
1402 
1403  return result;
1404 }
1405 
1406 /* (history-ref integer) -> <gdb:value>
1407  Return the specified value from GDB's value history. */
1408 
1409 static SCM
1411 {
1412  int i;
1413  struct value *res_val = NULL; /* Initialize to appease gcc warning. */
1414 
1415  gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "i", index, &i);
1416 
1417  TRY
1418  {
1419  res_val = access_value_history (i);
1420  }
1421  CATCH (except, RETURN_MASK_ALL)
1422  {
1423  GDBSCM_HANDLE_GDB_EXCEPTION (except);
1424  }
1425  END_CATCH
1426 
1427  return vlscm_scm_from_value (res_val);
1428 }
1429 
1430 /* (history-append! <gdb:value>) -> index
1431  Append VALUE to GDB's value history. Return its index in the history. */
1432 
1433 static SCM
1435 {
1436  int res_index = -1;
1437  struct value *v;
1438  value_smob *v_smob;
1439 
1440  v_smob = vlscm_get_value_smob_arg_unsafe (value, SCM_ARG1, FUNC_NAME);
1441  v = v_smob->value;
1442 
1443  TRY
1444  {
1445  res_index = record_latest_value (v);
1446  }
1447  CATCH (except, RETURN_MASK_ALL)
1448  {
1449  GDBSCM_HANDLE_GDB_EXCEPTION (except);
1450  }
1451  END_CATCH
1452 
1453  return scm_from_int (res_index);
1454 }
1455 
1456 /* Initialize the Scheme value code. */
1457 
1459 {
1460  { "value?", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_p),
1461  "\
1462 Return #t if the object is a <gdb:value> object." },
1463 
1464  { "make-value", 1, 0, 1, as_a_scm_t_subr (gdbscm_make_value),
1465  "\
1466 Create a <gdb:value> representing object.\n\
1467 Typically this is used to convert numbers and strings to\n\
1468 <gdb:value> objects.\n\
1469 \n\
1470  Arguments: object [#:type <gdb:type>]" },
1471 
1472  { "value-optimized-out?", 1, 0, 0,
1474  "\
1475 Return #t if the value has been optimizd out." },
1476 
1477  { "value-address", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_address),
1478  "\
1479 Return the address of the value." },
1480 
1481  { "value-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_type),
1482  "\
1483 Return the type of the value." },
1484 
1485  { "value-dynamic-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_dynamic_type),
1486  "\
1487 Return the dynamic type of the value." },
1488 
1489  { "value-cast", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_cast),
1490  "\
1491 Cast the value to the supplied type.\n\
1492 \n\
1493  Arguments: <gdb:value> <gdb:type>" },
1494 
1495  { "value-dynamic-cast", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_dynamic_cast),
1496  "\
1497 Cast the value to the supplied type, as if by the C++\n\
1498 dynamic_cast operator.\n\
1499 \n\
1500  Arguments: <gdb:value> <gdb:type>" },
1501 
1502  { "value-reinterpret-cast", 2, 0, 0,
1504  "\
1505 Cast the value to the supplied type, as if by the C++\n\
1506 reinterpret_cast operator.\n\
1507 \n\
1508  Arguments: <gdb:value> <gdb:type>" },
1509 
1510  { "value-dereference", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_dereference),
1511  "\
1512 Return the result of applying the C unary * operator to the value." },
1513 
1514  { "value-referenced-value", 1, 0, 0,
1516  "\
1517 Given a value of a reference type, return the value referenced.\n\
1518 The difference between this function and value-dereference is that\n\
1519 the latter applies * unary operator to a value, which need not always\n\
1520 result in the value referenced.\n\
1521 For example, for a value which is a reference to an 'int' pointer ('int *'),\n\
1522 value-dereference will result in a value of type 'int' while\n\
1523 value-referenced-value will result in a value of type 'int *'." },
1524 
1525  { "value-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_field),
1526  "\
1527 Return the specified field of the value.\n\
1528 \n\
1529  Arguments: <gdb:value> string" },
1530 
1531  { "value-subscript", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_subscript),
1532  "\
1533 Return the value of the array at the specified index.\n\
1534 \n\
1535  Arguments: <gdb:value> integer" },
1536 
1537  { "value-call", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_call),
1538  "\
1539 Perform an inferior function call taking the value as a pointer to the\n\
1540 function to call.\n\
1541 Each element of the argument list must be a <gdb:value> object or an object\n\
1542 that can be converted to one.\n\
1543 The result is the value returned by the function.\n\
1544 \n\
1545  Arguments: <gdb:value> arg-list" },
1546 
1547  { "value->bool", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_bool),
1548  "\
1549 Return the Scheme boolean representing the GDB value.\n\
1550 The value must be \"integer like\". Pointers are ok." },
1551 
1552  { "value->integer", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_integer),
1553  "\
1554 Return the Scheme integer representing the GDB value.\n\
1555 The value must be \"integer like\". Pointers are ok." },
1556 
1557  { "value->real", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_real),
1558  "\
1559 Return the Scheme real number representing the GDB value.\n\
1560 The value must be a number." },
1561 
1562  { "value->bytevector", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_bytevector),
1563  "\
1564 Return a Scheme bytevector with the raw contents of the GDB value.\n\
1565 No transformation, endian or otherwise, is performed." },
1566 
1567  { "value->string", 1, 0, 1, as_a_scm_t_subr (gdbscm_value_to_string),
1568  "\
1569 Return the Unicode string of the value's contents.\n\
1570 If ENCODING is not given, the string is assumed to be encoded in\n\
1571 the target's charset.\n\
1572 An error setting \"error\" causes an exception to be thrown if there's\n\
1573 a decoding error. An error setting of \"substitute\" causes invalid\n\
1574 characters to be replaced with \"?\". The default is \"error\".\n\
1575 If LENGTH is provided, only fetch string to the length provided.\n\
1576 \n\
1577  Arguments: <gdb:value>\n\
1578  [#:encoding encoding] [#:errors \"error\"|\"substitute\"]\n\
1579  [#:length length]" },
1580 
1581  { "value->lazy-string", 1, 0, 1,
1583  "\
1584 Return a Scheme object representing a lazily fetched Unicode string\n\
1585 of the value's contents.\n\
1586 If ENCODING is not given, the string is assumed to be encoded in\n\
1587 the target's charset.\n\
1588 If LENGTH is provided, only fetch string to the length provided.\n\
1589 \n\
1590  Arguments: <gdb:value> [#:encoding encoding] [#:length length]" },
1591 
1592  { "value-lazy?", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_lazy_p),
1593  "\
1594 Return #t if the value is lazy (not fetched yet from the inferior).\n\
1595 A lazy value is fetched when needed, or when the value-fetch-lazy! function\n\
1596 is called." },
1597 
1598  { "make-lazy-value", 2, 0, 0, as_a_scm_t_subr (gdbscm_make_lazy_value),
1599  "\
1600 Create a <gdb:value> that will be lazily fetched from the target.\n\
1601 \n\
1602  Arguments: <gdb:type> address" },
1603 
1604  { "value-fetch-lazy!", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_fetch_lazy_x),
1605  "\
1606 Fetch the value from the inferior, if it was lazy.\n\
1607 The result is \"unspecified\"." },
1608 
1609  { "value-print", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_print),
1610  "\
1611 Return the string representation (print form) of the value." },
1612 
1613  { "parse-and-eval", 1, 0, 0, as_a_scm_t_subr (gdbscm_parse_and_eval),
1614  "\
1615 Evaluates string in gdb and returns the result as a <gdb:value> object." },
1616 
1617  { "history-ref", 1, 0, 0, as_a_scm_t_subr (gdbscm_history_ref),
1618  "\
1619 Return the specified value from GDB's value history." },
1620 
1621  { "history-append!", 1, 0, 0, as_a_scm_t_subr (gdbscm_history_append_x),
1622  "\
1623 Append the specified value onto GDB's value history." },
1624 
1626 };
1627 
1628 void
1630 {
1632  sizeof (value_smob));
1633  scm_set_smob_free (value_smob_tag, vlscm_free_value_smob);
1634  scm_set_smob_print (value_smob_tag, vlscm_print_value_smob);
1635  scm_set_smob_equalp (value_smob_tag, vlscm_equal_p_value_smob);
1636 
1638 
1639  type_keyword = scm_from_latin1_keyword ("type");
1640  encoding_keyword = scm_from_latin1_keyword ("encoding");
1641  errors_keyword = scm_from_latin1_keyword ("errors");
1642  length_keyword = scm_from_latin1_keyword ("length");
1643 
1644  error_symbol = scm_from_latin1_symbol ("error");
1645  escape_symbol = scm_from_latin1_symbol ("escape");
1646  substitute_symbol = scm_from_latin1_symbol ("substitute");
1647 }
unsigned int length
Definition: gdbtypes.h:803
struct type * value_rtti_type(struct value *v, int *full, LONGEST *top, int *using_enc)
Definition: cp-abi.c:108
struct value * value_mark(void)
Definition: value.c:1589
static scm_t_subr as_a_scm_t_subr(SCM(*func)(void))
static void vlscm_remember_scheme_value(value_smob *v_smob)
Definition: scm-value.c:100
struct type * lookup_array_range_type(struct type *element_type, LONGEST low_bound, LONGEST high_bound)
Definition: gdbtypes.c:1232
static SCM scm_new_smob(scm_t_bits tc, scm_t_bits data)
struct value * value_addr(struct value *arg1)
Definition: valops.c:1462
static value_smob * values_in_scheme
Definition: scm-value.c:68
static SCM gdbscm_value_cast(SCM self, SCM new_type)
Definition: scm-value.c:667
struct value * value_subscript(struct value *array, LONGEST index)
Definition: valarith.c:142
void gdbscm_define_functions(const scheme_function *, int is_public)
Definition: scm-utils.c:44
static SCM vlscm_make_value_smob(void)
Definition: scm-value.c:206
struct value * value_from_contents_and_address(struct type *type, const gdb_byte *valaddr, CORE_ADDR address)
Definition: value.c:3599
bfd_vma CORE_ADDR
Definition: common-types.h:41
static SCM errors_keyword
Definition: scm-value.c:73
SCM gdbscm_scm_from_ulongest(ULONGEST l)
Definition: scm-utils.c:552
void xfree(void *)
struct _value_smob value_smob
struct value * value_reinterpret_cast(struct type *type, struct value *arg)
Definition: valops.c:577
static SCM gdbscm_value_type(SCM self)
Definition: scm-value.c:539
LONGEST value_as_long(struct value *val)
Definition: value.c:2749
SCM tyscm_scm_from_type(struct type *type)
Definition: scm-type.c:289
SCM dynamic_type
Definition: scm-value.c:57
struct value * vlscm_convert_value_from_scheme(const char *func_name, int obj_arg_pos, SCM obj, SCM *except_scmp, struct gdbarch *gdbarch, const struct language_defn *language)
Definition: scm-math.c:903
struct type * tyscm_type_smob_type(type_smob *t_smob)
Definition: scm-type.c:97
static SCM vlscm_get_value_arg_unsafe(SCM self, int arg_pos, const char *func_name)
Definition: scm-value.c:267
char * gdbscm_scm_to_c_string(SCM string)
Definition: scm-string.c:55
static scm_t_bits value_smob_tag
Definition: scm-value.c:63
static SCM gdbscm_value_call(SCM self, SCM args)
Definition: scm-value.c:805
void common_val_print(struct value *val, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language)
Definition: valprint.c:1136
static SCM substitute_symbol
Definition: scm-value.c:79
#define FUNC_NAME
const struct gdb_exception exception_none
int value_lazy(const struct value *value)
Definition: value.c:1383
struct value * coerce_ref(struct value *arg)
Definition: value.c:3755
void value_free(struct value *val)
Definition: value.c:1608
SCM gdbscm_scm_from_longest(LONGEST l)
Definition: scm-utils.c:533
struct value * value_ind(struct value *arg1)
Definition: valops.c:1542
SCM gdbscm_make_out_of_range_error(const char *subr, int arg_pos, SCM bad_value, const char *error)
scm_t_bits gdbscm_make_smob_type(const char *name, size_t size)
Definition: scm-gsmob.c:102
struct _value_smob * next
Definition: scm-value.c:45
double target_float_to_host_double(const gdb_byte *addr, const struct type *type)
#define _(String)
Definition: gdb_locale.h:35
static SCM length_keyword
Definition: scm-value.c:74
#define END_CATCH
int gdbscm_is_exception(SCM scm)
struct value * allocate_value(struct type *type)
Definition: value.c:1036
#define gdbscm_is_true(scm)
static struct type * new_type(char *)
Definition: mdebugread.c:4852
#define GDBSCM_ARG_NONE
static SCM gdbscm_value_to_lazy_string(SCM self, SCM rest)
Definition: scm-value.c:1195
struct _value_smob * prev
Definition: scm-value.c:46
SCM lsscm_make_lazy_string(CORE_ADDR address, int length, const char *encoding, struct type *type)
static int is_intlike(struct type *type, int ptr_ok)
Definition: scm-value.c:915
#define TRY
int gdbscm_guile_version_is_at_least(int major, int minor, int micro)
Definition: scm-utils.c:631
void gdbscm_dynwind_xfree(void *ptr)
Definition: scm-utils.c:571
static SCM gdbscm_value_subscript(SCM self, SCM index_scm)
Definition: scm-value.c:739
static SCM gdbscm_value_to_bool(SCM self)
Definition: scm-value.c:928
struct value * value_struct_elt(struct value **argp, struct value **args, const char *name, int *static_memfuncp, const char *err)
Definition: valops.c:2133
int vlscm_is_value(SCM scm)
Definition: scm-value.c:231
void preserve_one_value(struct value *value, struct objfile *objfile, htab_t copied_types)
Definition: value.c:2590
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:2421
void gdbscm_init_gsmob(gdb_smob *base)
Definition: scm-gsmob.c:113
const gdb_byte * value_contents(struct value *value)
Definition: value.c:1407
#define CATCH(EXCEPTION, MASK)
static SCM gdbscm_value_address(SCM self)
Definition: scm-value.c:417
SCM address
Definition: scm-value.c:55
static SCM gdbscm_make_lazy_value(SCM type_scm, SCM address_scm)
Definition: scm-value.c:352
static struct parser_state * pstate
Definition: ada-exp.c:98
static SCM gdbscm_value_to_string(SCM self, SCM rest)
Definition: scm-value.c:1098
SCM vlscm_scm_from_value(struct value *value)
Definition: scm-value.c:249
static SCM gdbscm_parse_and_eval(SCM expr_scm)
Definition: scm-value.c:1369
struct cleanup * make_cleanup_value_free_to_mark(struct value *mark)
Definition: utils.c:185
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:116
static SCM gdbscm_value_to_bytevector(SCM self)
Definition: scm-value.c:882
Definition: gdbtypes.h:749
static SCM vlscm_equal_p_value_smob(SCM v1, SCM v2)
Definition: scm-value.c:184
void gdbscm_throw(SCM exception) ATTRIBUTE_NORETURN
struct gdbarch * get_type_arch(const struct type *type)
Definition: gdbtypes.c:234
struct gdbarch * get_current_arch(void)
Definition: arch-utils.c:798
static SCM gdbscm_value_print(SCM self)
Definition: scm-value.c:1334
static const char * type
Definition: language.c:113
struct value * value_from_longest(struct type *type, LONGEST num)
Definition: value.c:3534
static SCM gdbscm_value_dereference(SCM self)
Definition: scm-value.c:453
struct value * value_cast(struct type *type, struct value *arg2)
Definition: valops.c:351
size_t size() const
Definition: ui-file.h:138
static SCM gdbscm_value_lazy_p(SCM self)
Definition: scm-value.c:1299
static SCM escape_symbol
Definition: scm-value.c:78
static const scheme_function value_functions[]
Definition: scm-value.c:1458
struct value * value_dynamic_cast(struct type *type, struct value *arg)
Definition: valops.c:724
static value_smob * vlscm_get_value_smob_arg_unsafe(SCM self, int arg_pos, const char *func_name)
Definition: scm-value.c:279
int value_equal(struct value *arg1, struct value *arg2)
Definition: valarith.c:1474
void gdbscm_printf(SCM port, const char *format,...) ATTRIBUTE_PRINTF(2
static SCM encoding_keyword
Definition: scm-value.c:72
static SCM gdbscm_value_p(SCM scm)
Definition: scm-value.c:239
static SCM gdbscm_value_reinterpret_cast(SCM self, SCM new_type)
Definition: scm-value.c:683
#define TYPE_UNSIGNED(t)
Definition: gdbtypes.h:205
#define gdb_assert(expr)
Definition: gdb_assert.h:32
static SCM gdbscm_history_append_x(SCM value)
Definition: scm-value.c:1434
static SCM gdbscm_value_to_real(SCM self)
Definition: scm-value.c:1017
Definition: value.c:169
struct value * vlscm_scm_to_value(SCM v_scm)
Definition: scm-value.c:291
static SCM gdbscm_value_referenced_value(SCM self)
Definition: scm-value.c:494
static SCM gdbscm_value_to_integer(SCM self)
Definition: scm-value.c:971
void release_value_or_incref(struct value *val)
Definition: value.c:1723
bfd_byte gdb_byte
Definition: common-types.h:38
#define END_FUNCTIONS
void discard_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:212
void target_float_from_host_double(gdb_byte *addr, const struct type *type, double val)
const struct language_defn * current_language
Definition: language.c:81
int value_optimized_out(struct value *value)
Definition: value.c:1424
#define TYPE_TARGET_TYPE(thistype)
Definition: gdbtypes.h:1226
static SCM gdbscm_value_dynamic_type(SCM self)
Definition: scm-value.c:554
void value_fetch_lazy(struct value *val)
Definition: value.c:3860
static SCM gdbscm_value_fetch_lazy_x(SCM self)
Definition: scm-value.c:1311
static void check(BOOL ok, const char *file, int line)
Definition: windows-nat.c:323
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
const char * c_str() const
Definition: ui-file.h:137
static SCM gdbscm_value_optimized_out_p(SCM self)
Definition: scm-value.c:393
gdb_smob base
Definition: scm-value.c:40
void gdbscm_out_of_range_error(const char *subr, int arg_pos, SCM bad_value, const char *error) ATTRIBUTE_NORETURN
void get_user_print_options(struct value_print_options *opts)
Definition: valprint.c:120
Definition: buffer.h:23
static SCM error_symbol
Definition: scm-value.c:77
struct value * value
Definition: scm-value.c:48
type_smob * tyscm_get_type_smob_arg_unsafe(SCM type_scm, int arg_pos, const char *func_name)
Definition: scm-type.c:328
exp_opcode
Definition: expression.h:42
#define GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS(exception, cleanups)
void gdbscm_parse_function_args(const char *function_name, int beginning_arg_pos, const SCM *keywords, const char *format,...)
Definition: scm-utils.c:377
static SCM gdbscm_history_ref(SCM index)
Definition: scm-value.c:1410
static SCM vlscm_do_cast(SCM self, SCM type_scm, enum exp_opcode op, const char *func_name)
Definition: scm-value.c:620
int record_latest_value(struct value *val)
Definition: value.c:1884
struct value * parse_and_eval(const char *exp)
Definition: eval.c:119
unsigned long long ULONGEST
Definition: common-types.h:53
struct value * call_function_by_hand(struct value *function, type *default_return_type, int nargs, struct value **args)
Definition: infcall.c:690
#define GDBSCM_HANDLE_GDB_EXCEPTION(exception)
language
Definition: defs.h:203
struct type * value_type(const struct value *value)
Definition: value.c:1095
static size_t vlscm_free_value_smob(SCM self)
Definition: scm-value.c:129
static int vlscm_print_value_smob(SCM self, SCM port, scm_print_state *pstate)
Definition: scm-value.c:142
struct value * access_value_history(int num)
Definition: value.c:1927
struct value * value_from_ulongest(struct type *type, ULONGEST num)
Definition: value.c:3546
CORE_ADDR value_as_address(struct value *val)
Definition: value.c:2762
const char * host_charset(void)
Definition: charset.c:417
gdb_byte * value_contents_raw(struct value *value)
Definition: value.c:1158
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1235
static SCM gdbscm_value_field(SCM self, SCM field_scm)
Definition: scm-value.c:693
static SCM type_keyword
Definition: scm-value.c:71
void gdbscm_preserve_values(const struct extension_language_defn *extlang, struct objfile *objfile, htab_t copied_types)
Definition: scm-value.c:88
static void vlscm_forget_value_smob(value_smob *v_smob)
Definition: scm-value.c:112
static SCM gdbscm_value_dynamic_cast(SCM self, SCM new_type)
Definition: scm-value.c:675
static const char value_smob_name[]
Definition: scm-value.c:60
CORE_ADDR value_address(const struct value *value)
Definition: value.c:1529
int get_array_bounds(struct type *type, LONGEST *low_bound, LONGEST *high_bound)
Definition: gdbtypes.c:1056
void gdbscm_initialize_values(void)
Definition: scm-value.c:1629
struct type * lookup_lvalue_reference_type(struct type *type)
Definition: gdbtypes.c:470
void error(const char *fmt,...)
Definition: errors.c:38
struct type * lookup_pointer_type(struct type *type)
Definition: gdbtypes.c:381
static SCM gdbscm_make_value(SCM x, SCM rest)
Definition: scm-value.c:305
long long LONGEST
Definition: common-types.h:52
bool is_floating_value(struct value *val)
Definition: value.c:2942
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:174
#define LA_GET_STRING(value, buffer, length, chartype, encoding)
Definition: language.h:534
struct value * vlscm_convert_typed_value_from_scheme(const char *func_name, int obj_arg_pos, SCM obj, int type_arg_pos, SCM type_scm, struct type *type, SCM *except_scmp, struct gdbarch *gdbarch, const struct language_defn *language)
Definition: scm-math.c:757