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