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