Avoid undefined behavior in Guile exception handling
[external/binutils.git] / gdb / guile / scm-value.c
1 /* Scheme interface to values.
2
3    Copyright (C) 2008-2019 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.  */
40   gdb_smob base;
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;
57   SCM dynamic_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.  */
68 static value_smob *values_in_scheme;
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 \f
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
88 gdbscm_preserve_values (const struct extension_language_defn *extlang,
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
100 vlscm_remember_scheme_value (value_smob *v_smob)
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
112 vlscm_forget_value_smob (value_smob *v_smob)
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
129 vlscm_free_value_smob (SCM self)
130 {
131   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
132
133   vlscm_forget_value_smob (v_smob);
134   value_decref (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   gdbscm_gdb_exception exc {};
160   try
161     {
162       string_file stb;
163
164       common_val_print (v_smob->value, &stb, 0, &opts, current_language);
165       scm_puts (stb.c_str (), port);
166     }
167   catch (const gdb_exception &except)
168     {
169       exc = unpack (except);
170     }
171
172   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
173   if (pstate->writingp)
174     scm_puts (">", port);
175
176   scm_remember_upto_here_1 (self);
177
178   /* Non-zero means success.  */
179   return 1;
180 }
181
182 /* The smob "equalp" function for <gdb:value>.  */
183
184 static SCM
185 vlscm_equal_p_value_smob (SCM v1, SCM v2)
186 {
187   const value_smob *v1_smob = (value_smob *) SCM_SMOB_DATA (v1);
188   const value_smob *v2_smob = (value_smob *) SCM_SMOB_DATA (v2);
189   int result = 0;
190
191   gdbscm_gdb_exception exc {};
192   try
193     {
194       result = value_equal (v1_smob->value, v2_smob->value);
195     }
196   catch (const gdb_exception &except)
197     {
198       exc = unpack (except);
199     }
200
201   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
202   return scm_from_bool (result);
203 }
204
205 /* Low level routine to create a <gdb:value> object.  */
206
207 static SCM
208 vlscm_make_value_smob (void)
209 {
210   value_smob *v_smob = (value_smob *)
211     scm_gc_malloc (sizeof (value_smob), value_smob_name);
212   SCM v_scm;
213
214   /* These must be filled in by the caller.  */
215   v_smob->value = NULL;
216   v_smob->prev = NULL;
217   v_smob->next = NULL;
218
219   /* These are lazily computed.  */
220   v_smob->address = SCM_UNDEFINED;
221   v_smob->type = SCM_UNDEFINED;
222   v_smob->dynamic_type = SCM_UNDEFINED;
223
224   v_scm = scm_new_smob (value_smob_tag, (scm_t_bits) v_smob);
225   gdbscm_init_gsmob (&v_smob->base);
226
227   return v_scm;
228 }
229
230 /* Return non-zero if SCM is a <gdb:value> object.  */
231
232 int
233 vlscm_is_value (SCM scm)
234 {
235   return SCM_SMOB_PREDICATE (value_smob_tag, scm);
236 }
237
238 /* (value? object) -> boolean */
239
240 static SCM
241 gdbscm_value_p (SCM scm)
242 {
243   return scm_from_bool (vlscm_is_value (scm));
244 }
245
246 /* Create a new <gdb:value> object that encapsulates VALUE.
247    The value is released from the all_values chain so its lifetime is not
248    bound to the execution of a command.  */
249
250 SCM
251 vlscm_scm_from_value (struct value *value)
252 {
253   /* N.B. It's important to not cause any side-effects until we know the
254      conversion worked.  */
255   SCM v_scm = vlscm_make_value_smob ();
256   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
257
258   v_smob->value = release_value (value).release ();
259   vlscm_remember_scheme_value (v_smob);
260
261   return v_scm;
262 }
263
264 /* Returns the <gdb:value> object in SELF.
265    Throws an exception if SELF is not a <gdb:value> object.  */
266
267 static SCM
268 vlscm_get_value_arg_unsafe (SCM self, int arg_pos, const char *func_name)
269 {
270   SCM_ASSERT_TYPE (vlscm_is_value (self), self, arg_pos, func_name,
271                    value_smob_name);
272
273   return self;
274 }
275
276 /* Returns a pointer to the value smob of SELF.
277    Throws an exception if SELF is not a <gdb:value> object.  */
278
279 static value_smob *
280 vlscm_get_value_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
281 {
282   SCM v_scm = vlscm_get_value_arg_unsafe (self, arg_pos, func_name);
283   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
284
285   return v_smob;
286 }
287
288 /* Return the value field of V_SCM, an object of type <gdb:value>.
289    This exists so that we don't have to export the struct's contents.  */
290
291 struct value *
292 vlscm_scm_to_value (SCM v_scm)
293 {
294   value_smob *v_smob;
295
296   gdb_assert (vlscm_is_value (v_scm));
297   v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
298   return v_smob->value;
299 }
300 \f
301 /* Value methods.  */
302
303 /* (make-value x [#:type type]) -> <gdb:value> */
304
305 static SCM
306 gdbscm_make_value (SCM x, SCM rest)
307 {
308   const SCM keywords[] = { type_keyword, SCM_BOOL_F };
309
310   int type_arg_pos = -1;
311   SCM type_scm = SCM_UNDEFINED;
312   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#O", rest,
313                               &type_arg_pos, &type_scm);
314
315   struct type *type = NULL;
316   if (type_arg_pos > 0)
317     {
318       type_smob *t_smob = tyscm_get_type_smob_arg_unsafe (type_scm,
319                                                           type_arg_pos,
320                                                           FUNC_NAME);
321       type = tyscm_type_smob_type (t_smob);
322     }
323
324   return gdbscm_wrap ([=]
325     {
326       scoped_value_mark free_values;
327
328       SCM except_scm;
329       struct value *value
330         = vlscm_convert_typed_value_from_scheme (FUNC_NAME, SCM_ARG1, x,
331                                                  type_arg_pos, type_scm, type,
332                                                  &except_scm,
333                                                  get_current_arch (),
334                                                  current_language);
335       if (value == NULL)
336         return except_scm;
337
338       return vlscm_scm_from_value (value);
339     });
340 }
341
342 /* (make-lazy-value <gdb:type> address) -> <gdb:value> */
343
344 static SCM
345 gdbscm_make_lazy_value (SCM type_scm, SCM address_scm)
346 {
347   type_smob *t_smob = tyscm_get_type_smob_arg_unsafe (type_scm,
348                                                       SCM_ARG1, FUNC_NAME);
349   struct type *type = tyscm_type_smob_type (t_smob);
350
351   ULONGEST address;
352   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, NULL, "U",
353                               address_scm, &address);
354
355   return gdbscm_wrap ([=]
356     {
357       scoped_value_mark free_values;
358
359       struct value *value = value_from_contents_and_address (type, NULL,
360                                                              address);
361       return vlscm_scm_from_value (value);
362     });
363 }
364
365 /* (value-optimized-out? <gdb:value>) -> boolean */
366
367 static SCM
368 gdbscm_value_optimized_out_p (SCM self)
369 {
370   value_smob *v_smob
371     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
372
373   return gdbscm_wrap ([=]
374     {
375       return scm_from_bool (value_optimized_out (v_smob->value));
376     });
377 }
378
379 /* (value-address <gdb:value>) -> integer
380    Returns #f if the value doesn't have one.  */
381
382 static SCM
383 gdbscm_value_address (SCM self)
384 {
385   value_smob *v_smob
386     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
387   struct value *value = v_smob->value;
388
389   return gdbscm_wrap ([=]
390     {
391       if (SCM_UNBNDP (v_smob->address))
392         {
393           scoped_value_mark free_values;
394
395           SCM address = SCM_BOOL_F;
396
397           try
398             {
399               address = vlscm_scm_from_value (value_addr (value));
400             }
401           catch (const gdb_exception &except)
402             {
403             }
404
405           if (gdbscm_is_exception (address))
406             return address;
407
408           v_smob->address = address;
409         }
410
411       return v_smob->address;
412     });
413 }
414
415 /* (value-dereference <gdb:value>) -> <gdb:value>
416    Given a value of a pointer type, apply the C unary * operator to it.  */
417
418 static SCM
419 gdbscm_value_dereference (SCM self)
420 {
421   value_smob *v_smob
422     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
423
424   return gdbscm_wrap ([=]
425     {
426       scoped_value_mark free_values;
427
428       struct value *res_val = value_ind (v_smob->value);
429       return vlscm_scm_from_value (res_val);
430     });
431 }
432
433 /* (value-referenced-value <gdb:value>) -> <gdb:value>
434    Given a value of a reference type, return the value referenced.
435    The difference between this function and gdbscm_value_dereference is that
436    the latter applies * unary operator to a value, which need not always
437    result in the value referenced.
438    For example, for a value which is a reference to an 'int' pointer ('int *'),
439    gdbscm_value_dereference will result in a value of type 'int' while
440    gdbscm_value_referenced_value will result in a value of type 'int *'.  */
441
442 static SCM
443 gdbscm_value_referenced_value (SCM self)
444 {
445   value_smob *v_smob
446     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
447   struct value *value = v_smob->value;
448
449   return gdbscm_wrap ([=]
450     {
451       scoped_value_mark free_values;
452
453       struct value *res_val;
454
455       switch (TYPE_CODE (check_typedef (value_type (value))))
456         {
457         case TYPE_CODE_PTR:
458           res_val = value_ind (value);
459           break;
460         case TYPE_CODE_REF:
461           res_val = coerce_ref (value);
462           break;
463         default:
464           error (_("Trying to get the referenced value from a value which is"
465                    " neither a pointer nor a reference"));
466         }
467
468       return vlscm_scm_from_value (res_val);
469     });
470 }
471
472 /* (value-type <gdb:value>) -> <gdb:type> */
473
474 static SCM
475 gdbscm_value_type (SCM self)
476 {
477   value_smob *v_smob
478     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
479   struct value *value = v_smob->value;
480
481   if (SCM_UNBNDP (v_smob->type))
482     v_smob->type = tyscm_scm_from_type (value_type (value));
483
484   return v_smob->type;
485 }
486
487 /* (value-dynamic-type <gdb:value>) -> <gdb:type> */
488
489 static SCM
490 gdbscm_value_dynamic_type (SCM self)
491 {
492   value_smob *v_smob
493     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
494   struct value *value = v_smob->value;
495   struct type *type = NULL;
496
497   if (! SCM_UNBNDP (v_smob->dynamic_type))
498     return v_smob->dynamic_type;
499
500   gdbscm_gdb_exception exc {};
501   try
502     {
503       scoped_value_mark free_values;
504
505       type = value_type (value);
506       type = check_typedef (type);
507
508       if (((TYPE_CODE (type) == TYPE_CODE_PTR)
509            || (TYPE_CODE (type) == TYPE_CODE_REF))
510           && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
511         {
512           struct value *target;
513           int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
514
515           if (was_pointer)
516             target = value_ind (value);
517           else
518             target = coerce_ref (value);
519           type = value_rtti_type (target, NULL, NULL, NULL);
520
521           if (type)
522             {
523               if (was_pointer)
524                 type = lookup_pointer_type (type);
525               else
526                 type = lookup_lvalue_reference_type (type);
527             }
528         }
529       else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
530         type = value_rtti_type (value, NULL, NULL, NULL);
531       else
532         {
533           /* Re-use object's static type.  */
534           type = NULL;
535         }
536     }
537   catch (const gdb_exception &except)
538     {
539       exc = unpack (except);
540     }
541
542   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
543   if (type == NULL)
544     v_smob->dynamic_type = gdbscm_value_type (self);
545   else
546     v_smob->dynamic_type = tyscm_scm_from_type (type);
547
548   return v_smob->dynamic_type;
549 }
550
551 /* A helper function that implements the various cast operators.  */
552
553 static SCM
554 vlscm_do_cast (SCM self, SCM type_scm, enum exp_opcode op,
555                const char *func_name)
556 {
557   value_smob *v_smob
558     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
559   struct value *value = v_smob->value;
560   type_smob *t_smob
561     = tyscm_get_type_smob_arg_unsafe (type_scm, SCM_ARG2, FUNC_NAME);
562   struct type *type = tyscm_type_smob_type (t_smob);
563
564   return gdbscm_wrap ([=]
565     {
566       scoped_value_mark free_values;
567
568       struct value *res_val;
569       if (op == UNOP_DYNAMIC_CAST)
570         res_val = value_dynamic_cast (type, value);
571       else if (op == UNOP_REINTERPRET_CAST)
572         res_val = value_reinterpret_cast (type, value);
573       else
574         {
575           gdb_assert (op == UNOP_CAST);
576           res_val = value_cast (type, value);
577         }
578
579       return vlscm_scm_from_value (res_val);
580     });
581 }
582
583 /* (value-cast <gdb:value> <gdb:type>) -> <gdb:value> */
584
585 static SCM
586 gdbscm_value_cast (SCM self, SCM new_type)
587 {
588   return vlscm_do_cast (self, new_type, UNOP_CAST, FUNC_NAME);
589 }
590
591 /* (value-dynamic-cast <gdb:value> <gdb:type>) -> <gdb:value> */
592
593 static SCM
594 gdbscm_value_dynamic_cast (SCM self, SCM new_type)
595 {
596   return vlscm_do_cast (self, new_type, UNOP_DYNAMIC_CAST, FUNC_NAME);
597 }
598
599 /* (value-reinterpret-cast <gdb:value> <gdb:type>) -> <gdb:value> */
600
601 static SCM
602 gdbscm_value_reinterpret_cast (SCM self, SCM new_type)
603 {
604   return vlscm_do_cast (self, new_type, UNOP_REINTERPRET_CAST, FUNC_NAME);
605 }
606
607 /* (value-field <gdb:value> string) -> <gdb:value>
608    Given string name of an element inside structure, return its <gdb:value>
609    object.  */
610
611 static SCM
612 gdbscm_value_field (SCM self, SCM field_scm)
613 {
614   value_smob *v_smob
615     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
616
617   SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
618                    _("string"));
619
620   return gdbscm_wrap ([=]
621     {
622       scoped_value_mark free_values;
623
624       gdb::unique_xmalloc_ptr<char> field = gdbscm_scm_to_c_string (field_scm);
625
626       struct value *tmp = v_smob->value;
627
628       struct value *res_val = value_struct_elt (&tmp, NULL, field.get (), NULL,
629                                                 "struct/class/union");
630
631       return vlscm_scm_from_value (res_val);
632     });
633 }
634
635 /* (value-subscript <gdb:value> integer|<gdb:value>) -> <gdb:value>
636    Return the specified value in an array.  */
637
638 static SCM
639 gdbscm_value_subscript (SCM self, SCM index_scm)
640 {
641   value_smob *v_smob
642     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
643   struct value *value = v_smob->value;
644   struct type *type = value_type (value);
645
646   SCM_ASSERT (type != NULL, self, SCM_ARG2, FUNC_NAME);
647
648   return gdbscm_wrap ([=]
649     {
650       scoped_value_mark free_values;
651
652       SCM except_scm;
653       struct value *index
654         = vlscm_convert_value_from_scheme (FUNC_NAME, SCM_ARG2, index_scm,
655                                            &except_scm,
656                                            get_type_arch (type),
657                                            current_language);
658       if (index == NULL)
659         return except_scm;
660
661       /* Assume we are attempting an array access, and let the value code
662          throw an exception if the index has an invalid type.
663          Check the value's type is something that can be accessed via
664          a subscript.  */
665       struct value *tmp = coerce_ref (value);
666       struct type *tmp_type = check_typedef (value_type (tmp));
667       if (TYPE_CODE (tmp_type) != TYPE_CODE_ARRAY
668           && TYPE_CODE (tmp_type) != TYPE_CODE_PTR)
669         error (_("Cannot subscript requested type"));
670
671       struct value *res_val = value_subscript (tmp, value_as_long (index));
672       return vlscm_scm_from_value (res_val);
673     });
674 }
675
676 /* (value-call <gdb:value> arg-list) -> <gdb:value>
677    Perform an inferior function call on the value.  */
678
679 static SCM
680 gdbscm_value_call (SCM self, SCM args)
681 {
682   value_smob *v_smob
683     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
684   struct value *function = v_smob->value;
685   struct type *ftype = NULL;
686   long args_count;
687   struct value **vargs = NULL;
688
689   gdbscm_gdb_exception exc {};
690   try
691     {
692       ftype = check_typedef (value_type (function));
693     }
694   catch (const gdb_exception &except)
695     {
696       exc = unpack (except);
697     }
698
699   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
700   SCM_ASSERT_TYPE (TYPE_CODE (ftype) == TYPE_CODE_FUNC, self,
701                    SCM_ARG1, FUNC_NAME,
702                    _("function (value of TYPE_CODE_FUNC)"));
703
704   SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (args)), args,
705                    SCM_ARG2, FUNC_NAME, _("list"));
706
707   args_count = scm_ilength (args);
708   if (args_count > 0)
709     {
710       struct gdbarch *gdbarch = get_current_arch ();
711       const struct language_defn *language = current_language;
712       SCM except_scm;
713       long i;
714
715       vargs = XALLOCAVEC (struct value *, args_count);
716       for (i = 0; i < args_count; i++)
717         {
718           SCM arg = scm_car (args);
719
720           vargs[i] = vlscm_convert_value_from_scheme (FUNC_NAME,
721                                                       GDBSCM_ARG_NONE, arg,
722                                                       &except_scm,
723                                                       gdbarch, language);
724           if (vargs[i] == NULL)
725             gdbscm_throw (except_scm);
726
727           args = scm_cdr (args);
728         }
729       gdb_assert (gdbscm_is_true (scm_null_p (args)));
730     }
731
732   return gdbscm_wrap ([=]
733     {
734       scoped_value_mark free_values;
735
736       auto av = gdb::make_array_view (vargs, args_count);
737       value *return_value = call_function_by_hand (function, NULL, av);
738       return vlscm_scm_from_value (return_value);
739     });
740 }
741
742 /* (value->bytevector <gdb:value>) -> bytevector */
743
744 static SCM
745 gdbscm_value_to_bytevector (SCM self)
746 {
747   value_smob *v_smob
748     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
749   struct value *value = v_smob->value;
750   struct type *type;
751   size_t length = 0;
752   const gdb_byte *contents = NULL;
753   SCM bv;
754
755   type = value_type (value);
756
757   gdbscm_gdb_exception exc {};
758   try
759     {
760       type = check_typedef (type);
761       length = TYPE_LENGTH (type);
762       contents = value_contents (value);
763     }
764   catch (const gdb_exception &except)
765     {
766       exc = unpack (except);
767     }
768
769   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
770   bv = scm_c_make_bytevector (length);
771   memcpy (SCM_BYTEVECTOR_CONTENTS (bv), contents, length);
772
773   return bv;
774 }
775
776 /* Helper function to determine if a type is "int-like".  */
777
778 static int
779 is_intlike (struct type *type, int ptr_ok)
780 {
781   return (TYPE_CODE (type) == TYPE_CODE_INT
782           || TYPE_CODE (type) == TYPE_CODE_ENUM
783           || TYPE_CODE (type) == TYPE_CODE_BOOL
784           || TYPE_CODE (type) == TYPE_CODE_CHAR
785           || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
786 }
787
788 /* (value->bool <gdb:value>) -> boolean
789    Throws an error if the value is not integer-like.  */
790
791 static SCM
792 gdbscm_value_to_bool (SCM self)
793 {
794   value_smob *v_smob
795     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
796   struct value *value = v_smob->value;
797   struct type *type;
798   LONGEST l = 0;
799
800   type = value_type (value);
801
802   gdbscm_gdb_exception exc {};
803   try
804     {
805       type = check_typedef (type);
806     }
807   catch (const gdb_exception &except)
808     {
809       exc = unpack (except);
810     }
811
812   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
813   SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
814                    _("integer-like gdb value"));
815
816   try
817     {
818       if (TYPE_CODE (type) == TYPE_CODE_PTR)
819         l = value_as_address (value);
820       else
821         l = value_as_long (value);
822     }
823   catch (const gdb_exception &except)
824     {
825       exc = unpack (except);
826     }
827
828   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
829   return scm_from_bool (l != 0);
830 }
831
832 /* (value->integer <gdb:value>) -> integer
833    Throws an error if the value is not integer-like.  */
834
835 static SCM
836 gdbscm_value_to_integer (SCM self)
837 {
838   value_smob *v_smob
839     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
840   struct value *value = v_smob->value;
841   struct type *type;
842   LONGEST l = 0;
843
844   type = value_type (value);
845
846   gdbscm_gdb_exception exc {};
847   try
848     {
849       type = check_typedef (type);
850     }
851   catch (const gdb_exception &except)
852     {
853       exc = unpack (except);
854     }
855
856   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
857   SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
858                    _("integer-like gdb value"));
859
860   try
861     {
862       if (TYPE_CODE (type) == TYPE_CODE_PTR)
863         l = value_as_address (value);
864       else
865         l = value_as_long (value);
866     }
867   catch (const gdb_exception &except)
868     {
869       exc = unpack (except);
870     }
871
872   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
873   if (TYPE_UNSIGNED (type))
874     return gdbscm_scm_from_ulongest (l);
875   else
876     return gdbscm_scm_from_longest (l);
877 }
878
879 /* (value->real <gdb:value>) -> real
880    Throws an error if the value is not a number.  */
881
882 static SCM
883 gdbscm_value_to_real (SCM self)
884 {
885   value_smob *v_smob
886     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
887   struct value *value = v_smob->value;
888   struct type *type;
889   double d = 0;
890   struct value *check = nullptr;
891
892   type = value_type (value);
893
894   gdbscm_gdb_exception exc {};
895   try
896     {
897       type = check_typedef (type);
898     }
899   catch (const gdb_exception &except)
900     {
901       exc = unpack (except);
902     }
903
904   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
905   SCM_ASSERT_TYPE (is_intlike (type, 0) || TYPE_CODE (type) == TYPE_CODE_FLT,
906                    self, SCM_ARG1, FUNC_NAME, _("number"));
907
908   try
909     {
910       if (is_floating_value (value))
911         {
912           d = target_float_to_host_double (value_contents (value), type);
913           check = value_from_host_double (type, d);
914         }
915       else if (TYPE_UNSIGNED (type))
916         {
917           d = (ULONGEST) value_as_long (value);
918           check = value_from_ulongest (type, (ULONGEST) d);
919         }
920       else
921         {
922           d = value_as_long (value);
923           check = value_from_longest (type, (LONGEST) d);
924         }
925     }
926   catch (const gdb_exception &except)
927     {
928       exc = unpack (except);
929     }
930
931   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
932   /* TODO: Is there a better way to check if the value fits?  */
933   if (!value_equal (value, check))
934     gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
935                                _("number can't be converted to a double"));
936
937   return scm_from_double (d);
938 }
939
940 /* (value->string <gdb:value>
941        [#:encoding encoding]
942        [#:errors #f | 'error | 'substitute]
943        [#:length length])
944      -> string
945    Return Unicode string with value's contents, which must be a string.
946
947    If ENCODING is not given, the string is assumed to be encoded in
948    the target's charset.
949
950    ERRORS is one of #f, 'error or 'substitute.
951    An error setting of #f means use the default, which is Guile's
952    %default-port-conversion-strategy when using Guile >= 2.0.6, or 'error if
953    using an earlier version of Guile.  Earlier versions do not properly
954    support obtaining the default port conversion strategy.
955    If the default is not one of 'error or 'substitute, 'substitute is used.
956    An error setting of "error" causes an exception to be thrown if there's
957    a decoding error.  An error setting of "substitute" causes invalid
958    characters to be replaced with "?".
959
960    If LENGTH is provided, only fetch string to the length provided.
961    LENGTH must be a Scheme integer, it can't be a <gdb:value> integer.  */
962
963 static SCM
964 gdbscm_value_to_string (SCM self, SCM rest)
965 {
966   value_smob *v_smob
967     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
968   struct value *value = v_smob->value;
969   const SCM keywords[] = {
970     encoding_keyword, errors_keyword, length_keyword, SCM_BOOL_F
971   };
972   int encoding_arg_pos = -1, errors_arg_pos = -1, length_arg_pos = -1;
973   char *encoding = NULL;
974   SCM errors = SCM_BOOL_F;
975   /* Avoid an uninitialized warning from gcc.  */
976   gdb_byte *buffer_contents = nullptr;
977   int length = -1;
978   const char *la_encoding = NULL;
979   struct type *char_type = NULL;
980   SCM result;
981
982   /* The sequencing here, as everywhere else, is important.
983      We can't have existing cleanups when a Scheme exception is thrown.  */
984
985   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#sOi", rest,
986                               &encoding_arg_pos, &encoding,
987                               &errors_arg_pos, &errors,
988                               &length_arg_pos, &length);
989
990   if (errors_arg_pos > 0
991       && errors != SCM_BOOL_F
992       && !scm_is_eq (errors, error_symbol)
993       && !scm_is_eq (errors, substitute_symbol))
994     {
995       SCM excp
996         = gdbscm_make_out_of_range_error (FUNC_NAME, errors_arg_pos, errors,
997                                           _("invalid error kind"));
998
999       xfree (encoding);
1000       gdbscm_throw (excp);
1001     }
1002   if (errors == SCM_BOOL_F)
1003     {
1004       /* N.B. scm_port_conversion_strategy in Guile versions prior to 2.0.6
1005          will throw a Scheme error when passed #f.  */
1006       if (gdbscm_guile_version_is_at_least (2, 0, 6))
1007         errors = scm_port_conversion_strategy (SCM_BOOL_F);
1008       else
1009         errors = error_symbol;
1010     }
1011   /* We don't assume anything about the result of scm_port_conversion_strategy.
1012      From this point on, if errors is not 'errors, use 'substitute.  */
1013
1014   gdbscm_gdb_exception exc {};
1015   try
1016     {
1017       gdb::unique_xmalloc_ptr<gdb_byte> buffer;
1018       LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
1019       buffer_contents = buffer.release ();
1020     }
1021   catch (const gdb_exception &except)
1022     {
1023       xfree (encoding);
1024       exc = unpack (except);
1025     }
1026   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
1027
1028   /* If errors is "error", scm_from_stringn may throw a Scheme exception.
1029      Make sure we don't leak.  This is done via scm_dynwind_begin, et.al.  */
1030
1031   scm_dynwind_begin ((scm_t_dynwind_flags) 0);
1032
1033   gdbscm_dynwind_xfree (encoding);
1034   gdbscm_dynwind_xfree (buffer_contents);
1035
1036   result = scm_from_stringn ((const char *) buffer_contents,
1037                              length * TYPE_LENGTH (char_type),
1038                              (encoding != NULL && *encoding != '\0'
1039                               ? encoding
1040                               : la_encoding),
1041                              scm_is_eq (errors, error_symbol)
1042                              ? SCM_FAILED_CONVERSION_ERROR
1043                              : SCM_FAILED_CONVERSION_QUESTION_MARK);
1044
1045   scm_dynwind_end ();
1046
1047   return result;
1048 }
1049
1050 /* (value->lazy-string <gdb:value> [#:encoding encoding] [#:length length])
1051      -> <gdb:lazy-string>
1052    Return a Scheme object representing a lazy_string_object type.
1053    A lazy string is a pointer to a string with an optional encoding and length.
1054    If ENCODING is not given, the target's charset is used.
1055    If LENGTH is provided then the length parameter is set to LENGTH.
1056    Otherwise if the value is an array of known length then the array's length
1057    is used.  Otherwise the length will be set to -1 (meaning first null of
1058    appropriate with).
1059    LENGTH must be a Scheme integer, it can't be a <gdb:value> integer.  */
1060
1061 static SCM
1062 gdbscm_value_to_lazy_string (SCM self, SCM rest)
1063 {
1064   value_smob *v_smob
1065     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1066   struct value *value = v_smob->value;
1067   const SCM keywords[] = { encoding_keyword, length_keyword, SCM_BOOL_F };
1068   int encoding_arg_pos = -1, length_arg_pos = -1;
1069   char *encoding = NULL;
1070   int length = -1;
1071   SCM result = SCM_BOOL_F; /* -Wall */
1072   gdbscm_gdb_exception except {};
1073
1074   /* The sequencing here, as everywhere else, is important.
1075      We can't have existing cleanups when a Scheme exception is thrown.  */
1076
1077   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#si", rest,
1078                               &encoding_arg_pos, &encoding,
1079                               &length_arg_pos, &length);
1080
1081   if (length < -1)
1082     {
1083       gdbscm_out_of_range_error (FUNC_NAME, length_arg_pos,
1084                                  scm_from_int (length),
1085                                  _("invalid length"));
1086     }
1087
1088   try
1089     {
1090       scoped_value_mark free_values;
1091
1092       struct type *type, *realtype;
1093       CORE_ADDR addr;
1094
1095       type = value_type (value);
1096       realtype = check_typedef (type);
1097
1098       switch (TYPE_CODE (realtype))
1099         {
1100         case TYPE_CODE_ARRAY:
1101           {
1102             LONGEST array_length = -1;
1103             LONGEST low_bound, high_bound;
1104
1105             /* PR 20786: There's no way to specify an array of length zero.
1106                Record a length of [0,-1] which is how Ada does it.  Anything
1107                we do is broken, but this one possible solution.  */
1108             if (get_array_bounds (realtype, &low_bound, &high_bound))
1109               array_length = high_bound - low_bound + 1;
1110             if (length == -1)
1111               length = array_length;
1112             else if (array_length == -1)
1113               {
1114                 type = lookup_array_range_type (TYPE_TARGET_TYPE (realtype),
1115                                                 0, length - 1);
1116               }
1117             else if (length != array_length)
1118               {
1119                 /* We need to create a new array type with the
1120                    specified length.  */
1121                 if (length > array_length)
1122                   error (_("length is larger than array size"));
1123                 type = lookup_array_range_type (TYPE_TARGET_TYPE (type),
1124                                                 low_bound,
1125                                                 low_bound + length - 1);
1126               }
1127             addr = value_address (value);
1128             break;
1129           }
1130         case TYPE_CODE_PTR:
1131           /* If a length is specified we defer creating an array of the
1132              specified width until we need to.  */
1133           addr = value_as_address (value);
1134           break;
1135         default:
1136           /* Should flag an error here.  PR 20769.  */
1137           addr = value_address (value);
1138           break;
1139         }
1140
1141       result = lsscm_make_lazy_string (addr, length, encoding, type);
1142     }
1143   catch (const gdb_exception &ex)
1144     {
1145       except = unpack (ex);
1146     }
1147
1148   xfree (encoding);
1149   GDBSCM_HANDLE_GDB_EXCEPTION (except);
1150
1151   if (gdbscm_is_exception (result))
1152     gdbscm_throw (result);
1153
1154   return result;
1155 }
1156
1157 /* (value-lazy? <gdb:value>) -> boolean */
1158
1159 static SCM
1160 gdbscm_value_lazy_p (SCM self)
1161 {
1162   value_smob *v_smob
1163     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1164   struct value *value = v_smob->value;
1165
1166   return scm_from_bool (value_lazy (value));
1167 }
1168
1169 /* (value-fetch-lazy! <gdb:value>) -> unspecified */
1170
1171 static SCM
1172 gdbscm_value_fetch_lazy_x (SCM self)
1173 {
1174   value_smob *v_smob
1175     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1176   struct value *value = v_smob->value;
1177
1178   return gdbscm_wrap ([=]
1179     {
1180       if (value_lazy (value))
1181         value_fetch_lazy (value);
1182       return SCM_UNSPECIFIED;
1183     });
1184 }
1185
1186 /* (value-print <gdb:value>) -> string */
1187
1188 static SCM
1189 gdbscm_value_print (SCM self)
1190 {
1191   value_smob *v_smob
1192     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1193   struct value *value = v_smob->value;
1194   struct value_print_options opts;
1195
1196   get_user_print_options (&opts);
1197   opts.deref_ref = 0;
1198
1199   string_file stb;
1200
1201   gdbscm_gdb_exception exc {};
1202   try
1203     {
1204       common_val_print (value, &stb, 0, &opts, current_language);
1205     }
1206   catch (const gdb_exception &except)
1207     {
1208       exc = unpack (except);
1209     }
1210
1211   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
1212   /* Use SCM_FAILED_CONVERSION_QUESTION_MARK to ensure this doesn't
1213      throw an error if the encoding fails.
1214      IWBN to use scm_take_locale_string here, but we'd have to temporarily
1215      override the default port conversion handler because contrary to
1216      documentation it doesn't necessarily free the input string.  */
1217   return scm_from_stringn (stb.c_str (), stb.size (), host_charset (),
1218                            SCM_FAILED_CONVERSION_QUESTION_MARK);
1219 }
1220 \f
1221 /* (parse-and-eval string) -> <gdb:value>
1222    Parse a string and evaluate the string as an expression.  */
1223
1224 static SCM
1225 gdbscm_parse_and_eval (SCM expr_scm)
1226 {
1227   char *expr_str;
1228   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
1229                               expr_scm, &expr_str);
1230
1231   return gdbscm_wrap ([=]
1232     {
1233       scoped_value_mark free_values;
1234       return vlscm_scm_from_value (parse_and_eval (expr_str));
1235     });
1236 }
1237
1238 /* (history-ref integer) -> <gdb:value>
1239    Return the specified value from GDB's value history.  */
1240
1241 static SCM
1242 gdbscm_history_ref (SCM index)
1243 {
1244   int i;
1245   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "i", index, &i);
1246
1247   return gdbscm_wrap ([=]
1248     {
1249       return vlscm_scm_from_value (access_value_history (i));
1250     });
1251 }
1252
1253 /* (history-append! <gdb:value>) -> index
1254    Append VALUE to GDB's value history.  Return its index in the history.  */
1255
1256 static SCM
1257 gdbscm_history_append_x (SCM value)
1258 {
1259   value_smob *v_smob
1260     = vlscm_get_value_smob_arg_unsafe (value, SCM_ARG1, FUNC_NAME);
1261   return gdbscm_wrap ([=]
1262     {
1263       return scm_from_int (record_latest_value (v_smob->value));
1264     });
1265 }
1266 \f
1267 /* Initialize the Scheme value code.  */
1268
1269 static const scheme_function value_functions[] =
1270 {
1271   { "value?", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_p),
1272     "\
1273 Return #t if the object is a <gdb:value> object." },
1274
1275   { "make-value", 1, 0, 1, as_a_scm_t_subr (gdbscm_make_value),
1276     "\
1277 Create a <gdb:value> representing object.\n\
1278 Typically this is used to convert numbers and strings to\n\
1279 <gdb:value> objects.\n\
1280 \n\
1281   Arguments: object [#:type <gdb:type>]" },
1282
1283   { "value-optimized-out?", 1, 0, 0,
1284     as_a_scm_t_subr (gdbscm_value_optimized_out_p),
1285     "\
1286 Return #t if the value has been optimizd out." },
1287
1288   { "value-address", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_address),
1289     "\
1290 Return the address of the value." },
1291
1292   { "value-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_type),
1293     "\
1294 Return the type of the value." },
1295
1296   { "value-dynamic-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_dynamic_type),
1297     "\
1298 Return the dynamic type of the value." },
1299
1300   { "value-cast", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_cast),
1301     "\
1302 Cast the value to the supplied type.\n\
1303 \n\
1304   Arguments: <gdb:value> <gdb:type>" },
1305
1306   { "value-dynamic-cast", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_dynamic_cast),
1307     "\
1308 Cast the value to the supplied type, as if by the C++\n\
1309 dynamic_cast operator.\n\
1310 \n\
1311   Arguments: <gdb:value> <gdb:type>" },
1312
1313   { "value-reinterpret-cast", 2, 0, 0,
1314     as_a_scm_t_subr (gdbscm_value_reinterpret_cast),
1315     "\
1316 Cast the value to the supplied type, as if by the C++\n\
1317 reinterpret_cast operator.\n\
1318 \n\
1319   Arguments: <gdb:value> <gdb:type>" },
1320
1321   { "value-dereference", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_dereference),
1322     "\
1323 Return the result of applying the C unary * operator to the value." },
1324
1325   { "value-referenced-value", 1, 0, 0,
1326     as_a_scm_t_subr (gdbscm_value_referenced_value),
1327     "\
1328 Given a value of a reference type, return the value referenced.\n\
1329 The difference between this function and value-dereference is that\n\
1330 the latter applies * unary operator to a value, which need not always\n\
1331 result in the value referenced.\n\
1332 For example, for a value which is a reference to an 'int' pointer ('int *'),\n\
1333 value-dereference will result in a value of type 'int' while\n\
1334 value-referenced-value will result in a value of type 'int *'." },
1335
1336   { "value-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_field),
1337     "\
1338 Return the specified field of the value.\n\
1339 \n\
1340   Arguments: <gdb:value> string" },
1341
1342   { "value-subscript", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_subscript),
1343     "\
1344 Return the value of the array at the specified index.\n\
1345 \n\
1346   Arguments: <gdb:value> integer" },
1347
1348   { "value-call", 2, 0, 0, as_a_scm_t_subr (gdbscm_value_call),
1349     "\
1350 Perform an inferior function call taking the value as a pointer to the\n\
1351 function to call.\n\
1352 Each element of the argument list must be a <gdb:value> object or an object\n\
1353 that can be converted to one.\n\
1354 The result is the value returned by the function.\n\
1355 \n\
1356   Arguments: <gdb:value> arg-list" },
1357
1358   { "value->bool", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_bool),
1359     "\
1360 Return the Scheme boolean representing the GDB value.\n\
1361 The value must be \"integer like\".  Pointers are ok." },
1362
1363   { "value->integer", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_integer),
1364     "\
1365 Return the Scheme integer representing the GDB value.\n\
1366 The value must be \"integer like\".  Pointers are ok." },
1367
1368   { "value->real", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_real),
1369     "\
1370 Return the Scheme real number representing the GDB value.\n\
1371 The value must be a number." },
1372
1373   { "value->bytevector", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_to_bytevector),
1374     "\
1375 Return a Scheme bytevector with the raw contents of the GDB value.\n\
1376 No transformation, endian or otherwise, is performed." },
1377
1378   { "value->string", 1, 0, 1, as_a_scm_t_subr (gdbscm_value_to_string),
1379     "\
1380 Return the Unicode string of the value's contents.\n\
1381 If ENCODING is not given, the string is assumed to be encoded in\n\
1382 the target's charset.\n\
1383 An error setting \"error\" causes an exception to be thrown if there's\n\
1384 a decoding error.  An error setting of \"substitute\" causes invalid\n\
1385 characters to be replaced with \"?\".  The default is \"error\".\n\
1386 If LENGTH is provided, only fetch string to the length provided.\n\
1387 \n\
1388   Arguments: <gdb:value>\n\
1389              [#:encoding encoding] [#:errors \"error\"|\"substitute\"]\n\
1390              [#:length length]" },
1391
1392   { "value->lazy-string", 1, 0, 1,
1393     as_a_scm_t_subr (gdbscm_value_to_lazy_string),
1394     "\
1395 Return a Scheme object representing a lazily fetched Unicode string\n\
1396 of the value's contents.\n\
1397 If ENCODING is not given, the string is assumed to be encoded in\n\
1398 the target's charset.\n\
1399 If LENGTH is provided, only fetch string to the length provided.\n\
1400 \n\
1401   Arguments: <gdb:value> [#:encoding encoding] [#:length length]" },
1402
1403   { "value-lazy?", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_lazy_p),
1404     "\
1405 Return #t if the value is lazy (not fetched yet from the inferior).\n\
1406 A lazy value is fetched when needed, or when the value-fetch-lazy! function\n\
1407 is called." },
1408
1409   { "make-lazy-value", 2, 0, 0, as_a_scm_t_subr (gdbscm_make_lazy_value),
1410     "\
1411 Create a <gdb:value> that will be lazily fetched from the target.\n\
1412 \n\
1413   Arguments: <gdb:type> address" },
1414
1415   { "value-fetch-lazy!", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_fetch_lazy_x),
1416     "\
1417 Fetch the value from the inferior, if it was lazy.\n\
1418 The result is \"unspecified\"." },
1419
1420   { "value-print", 1, 0, 0, as_a_scm_t_subr (gdbscm_value_print),
1421     "\
1422 Return the string representation (print form) of the value." },
1423
1424   { "parse-and-eval", 1, 0, 0, as_a_scm_t_subr (gdbscm_parse_and_eval),
1425     "\
1426 Evaluates string in gdb and returns the result as a <gdb:value> object." },
1427
1428   { "history-ref", 1, 0, 0, as_a_scm_t_subr (gdbscm_history_ref),
1429     "\
1430 Return the specified value from GDB's value history." },
1431
1432   { "history-append!", 1, 0, 0, as_a_scm_t_subr (gdbscm_history_append_x),
1433     "\
1434 Append the specified value onto GDB's value history." },
1435
1436   END_FUNCTIONS
1437 };
1438
1439 void
1440 gdbscm_initialize_values (void)
1441 {
1442   value_smob_tag = gdbscm_make_smob_type (value_smob_name,
1443                                           sizeof (value_smob));
1444   scm_set_smob_free (value_smob_tag, vlscm_free_value_smob);
1445   scm_set_smob_print (value_smob_tag, vlscm_print_value_smob);
1446   scm_set_smob_equalp (value_smob_tag, vlscm_equal_p_value_smob);
1447
1448   gdbscm_define_functions (value_functions, 1);
1449
1450   type_keyword = scm_from_latin1_keyword ("type");
1451   encoding_keyword = scm_from_latin1_keyword ("encoding");
1452   errors_keyword = scm_from_latin1_keyword ("errors");
1453   length_keyword = scm_from_latin1_keyword ("length");
1454
1455   error_symbol = scm_from_latin1_symbol ("error");
1456   escape_symbol = scm_from_latin1_symbol ("escape");
1457   substitute_symbol = scm_from_latin1_symbol ("substitute");
1458 }