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