]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Low level packing and unpacking of values for GDB, the GNU Debugger. |
1bac305b | 2 | |
213516ef | 3 | Copyright (C) 1986-2023 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
e17c207e | 21 | #include "arch-utils.h" |
c906108c SS |
22 | #include "symtab.h" |
23 | #include "gdbtypes.h" | |
24 | #include "value.h" | |
25 | #include "gdbcore.h" | |
c906108c SS |
26 | #include "command.h" |
27 | #include "gdbcmd.h" | |
28 | #include "target.h" | |
29 | #include "language.h" | |
c906108c | 30 | #include "demangle.h" |
36160dc4 | 31 | #include "regcache.h" |
fe898f56 | 32 | #include "block.h" |
70100014 | 33 | #include "target-float.h" |
bccdca4a | 34 | #include "objfiles.h" |
79a45b7d | 35 | #include "valprint.h" |
bc3b79fd | 36 | #include "cli/cli-decode.h" |
6dddc817 | 37 | #include "extension.h" |
3bd0f5ef | 38 | #include <ctype.h> |
0914bcdb | 39 | #include "tracepoint.h" |
be335936 | 40 | #include "cp-abi.h" |
a58e2656 | 41 | #include "user-regs.h" |
325fac50 | 42 | #include <algorithm> |
b0cf0a5b CDB |
43 | #include <iterator> |
44 | #include <utility> | |
45 | #include <vector> | |
eb3ff9a5 | 46 | #include "completer.h" |
268a13a5 TT |
47 | #include "gdbsupport/selftest.h" |
48 | #include "gdbsupport/array-view.h" | |
7f6aba03 | 49 | #include "cli/cli-style.h" |
413403fc | 50 | #include "expop.h" |
328d42d8 | 51 | #include "inferior.h" |
bc20e562 | 52 | #include "varobj.h" |
0914bcdb | 53 | |
bc3b79fd TJB |
54 | /* Definition of a user function. */ |
55 | struct internal_function | |
56 | { | |
57 | /* The name of the function. It is a bit odd to have this in the | |
58 | function itself -- the user might use a differently-named | |
59 | convenience variable to hold the function. */ | |
60 | char *name; | |
61 | ||
62 | /* The handler. */ | |
63 | internal_function_fn handler; | |
64 | ||
65 | /* User data for the handler. */ | |
66 | void *cookie; | |
67 | }; | |
68 | ||
4e07d55f PA |
69 | /* Returns true if the ranges defined by [offset1, offset1+len1) and |
70 | [offset2, offset2+len2) overlap. */ | |
71 | ||
72 | static int | |
4f82620c MR |
73 | ranges_overlap (LONGEST offset1, ULONGEST len1, |
74 | LONGEST offset2, ULONGEST len2) | |
4e07d55f | 75 | { |
4f82620c | 76 | LONGEST h, l; |
4e07d55f | 77 | |
325fac50 PA |
78 | l = std::max (offset1, offset2); |
79 | h = std::min (offset1 + len1, offset2 + len2); | |
4e07d55f PA |
80 | return (l < h); |
81 | } | |
82 | ||
4e07d55f PA |
83 | /* Returns true if RANGES contains any range that overlaps [OFFSET, |
84 | OFFSET+LENGTH). */ | |
85 | ||
86 | static int | |
0c7e6dd8 | 87 | ranges_contain (const std::vector<range> &ranges, LONGEST offset, |
4f82620c | 88 | ULONGEST length) |
4e07d55f | 89 | { |
0c7e6dd8 | 90 | range what; |
4e07d55f PA |
91 | |
92 | what.offset = offset; | |
93 | what.length = length; | |
94 | ||
95 | /* We keep ranges sorted by offset and coalesce overlapping and | |
96 | contiguous ranges, so to check if a range list contains a given | |
97 | range, we can do a binary search for the position the given range | |
98 | would be inserted if we only considered the starting OFFSET of | |
99 | ranges. We call that position I. Since we also have LENGTH to | |
100 | care for (this is a range afterall), we need to check if the | |
101 | _previous_ range overlaps the I range. E.g., | |
102 | ||
dda83cd7 SM |
103 | R |
104 | |---| | |
4e07d55f PA |
105 | |---| |---| |------| ... |--| |
106 | 0 1 2 N | |
107 | ||
108 | I=1 | |
109 | ||
110 | In the case above, the binary search would return `I=1', meaning, | |
111 | this OFFSET should be inserted at position 1, and the current | |
112 | position 1 should be pushed further (and before 2). But, `0' | |
113 | overlaps with R. | |
114 | ||
115 | Then we need to check if the I range overlaps the I range itself. | |
116 | E.g., | |
117 | ||
dda83cd7 SM |
118 | R |
119 | |---| | |
4e07d55f PA |
120 | |---| |---| |-------| ... |--| |
121 | 0 1 2 N | |
122 | ||
123 | I=1 | |
124 | */ | |
125 | ||
4e07d55f | 126 | |
0c7e6dd8 TT |
127 | auto i = std::lower_bound (ranges.begin (), ranges.end (), what); |
128 | ||
129 | if (i > ranges.begin ()) | |
4e07d55f | 130 | { |
0c7e6dd8 | 131 | const struct range &bef = *(i - 1); |
4e07d55f | 132 | |
0c7e6dd8 | 133 | if (ranges_overlap (bef.offset, bef.length, offset, length)) |
4e07d55f PA |
134 | return 1; |
135 | } | |
136 | ||
0c7e6dd8 | 137 | if (i < ranges.end ()) |
4e07d55f | 138 | { |
0c7e6dd8 | 139 | const struct range &r = *i; |
4e07d55f | 140 | |
0c7e6dd8 | 141 | if (ranges_overlap (r.offset, r.length, offset, length)) |
4e07d55f PA |
142 | return 1; |
143 | } | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
bc3b79fd TJB |
148 | static struct cmd_list_element *functionlist; |
149 | ||
e714001c TT |
150 | value::~value () |
151 | { | |
152 | if (VALUE_LVAL (this) == lval_computed) | |
153 | { | |
154 | const struct lval_funcs *funcs = m_location.computed.funcs; | |
155 | ||
156 | if (funcs->free_closure) | |
157 | funcs->free_closure (this); | |
158 | } | |
159 | else if (VALUE_LVAL (this) == lval_xcallable) | |
160 | delete m_location.xm_worker; | |
161 | } | |
162 | ||
e512cdbd SM |
163 | /* See value.h. */ |
164 | ||
165 | struct gdbarch * | |
f9ee742c | 166 | value::arch () const |
e512cdbd | 167 | { |
f9ee742c | 168 | return type ()->arch (); |
e512cdbd SM |
169 | } |
170 | ||
4e07d55f | 171 | int |
4f82620c MR |
172 | value_bits_available (const struct value *value, |
173 | LONGEST offset, ULONGEST length) | |
4e07d55f | 174 | { |
382d927f | 175 | gdb_assert (!value->m_lazy); |
4e07d55f | 176 | |
aaab5fce MR |
177 | /* Don't pretend we have anything available there in the history beyond |
178 | the boundaries of the value recorded. It's not like inferior memory | |
179 | where there is actual stuff underneath. */ | |
463b870d | 180 | ULONGEST val_len = TARGET_CHAR_BIT * value->enclosing_type ()->length (); |
382d927f | 181 | return !((value->m_in_history |
aaab5fce | 182 | && (offset < 0 || offset + length > val_len)) |
382d927f | 183 | || ranges_contain (value->m_unavailable, offset, length)); |
4e07d55f PA |
184 | } |
185 | ||
bdf22206 | 186 | int |
6b850546 | 187 | value_bytes_available (const struct value *value, |
4f82620c | 188 | LONGEST offset, ULONGEST length) |
bdf22206 | 189 | { |
4f82620c MR |
190 | ULONGEST sign = (1ULL << (sizeof (ULONGEST) * 8 - 1)) / TARGET_CHAR_BIT; |
191 | ULONGEST mask = (sign << 1) - 1; | |
192 | ||
193 | if (offset != ((offset & mask) ^ sign) - sign | |
194 | || length != ((length & mask) ^ sign) - sign | |
195 | || (length > 0 && (~offset & (offset + length - 1) & sign) != 0)) | |
196 | error (_("Integer overflow in data location calculation")); | |
197 | ||
bdf22206 AB |
198 | return value_bits_available (value, |
199 | offset * TARGET_CHAR_BIT, | |
200 | length * TARGET_CHAR_BIT); | |
201 | } | |
202 | ||
9a0dc9e3 PA |
203 | int |
204 | value_bits_any_optimized_out (const struct value *value, int bit_offset, int bit_length) | |
205 | { | |
382d927f | 206 | gdb_assert (!value->m_lazy); |
9a0dc9e3 | 207 | |
382d927f | 208 | return ranges_contain (value->m_optimized_out, bit_offset, bit_length); |
9a0dc9e3 PA |
209 | } |
210 | ||
ec0a52e1 PA |
211 | int |
212 | value_entirely_available (struct value *value) | |
213 | { | |
214 | /* We can only tell whether the whole value is available when we try | |
215 | to read it. */ | |
382d927f | 216 | if (value->m_lazy) |
ec0a52e1 PA |
217 | value_fetch_lazy (value); |
218 | ||
382d927f | 219 | if (value->m_unavailable.empty ()) |
ec0a52e1 PA |
220 | return 1; |
221 | return 0; | |
222 | } | |
223 | ||
9a0dc9e3 PA |
224 | /* Returns true if VALUE is entirely covered by RANGES. If the value |
225 | is lazy, it'll be read now. Note that RANGE is a pointer to | |
226 | pointer because reading the value might change *RANGE. */ | |
227 | ||
228 | static int | |
229 | value_entirely_covered_by_range_vector (struct value *value, | |
0c7e6dd8 | 230 | const std::vector<range> &ranges) |
6211c335 | 231 | { |
9a0dc9e3 PA |
232 | /* We can only tell whether the whole value is optimized out / |
233 | unavailable when we try to read it. */ | |
382d927f | 234 | if (value->m_lazy) |
6211c335 YQ |
235 | value_fetch_lazy (value); |
236 | ||
0c7e6dd8 | 237 | if (ranges.size () == 1) |
6211c335 | 238 | { |
0c7e6dd8 | 239 | const struct range &t = ranges[0]; |
6211c335 | 240 | |
0c7e6dd8 TT |
241 | if (t.offset == 0 |
242 | && t.length == (TARGET_CHAR_BIT | |
463b870d | 243 | * value->enclosing_type ()->length ())) |
6211c335 YQ |
244 | return 1; |
245 | } | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
9a0dc9e3 PA |
250 | int |
251 | value_entirely_unavailable (struct value *value) | |
252 | { | |
382d927f | 253 | return value_entirely_covered_by_range_vector (value, value->m_unavailable); |
9a0dc9e3 PA |
254 | } |
255 | ||
256 | int | |
257 | value_entirely_optimized_out (struct value *value) | |
258 | { | |
382d927f | 259 | return value_entirely_covered_by_range_vector (value, value->m_optimized_out); |
9a0dc9e3 PA |
260 | } |
261 | ||
262 | /* Insert into the vector pointed to by VECTORP the bit range starting of | |
263 | OFFSET bits, and extending for the next LENGTH bits. */ | |
264 | ||
265 | static void | |
0c7e6dd8 | 266 | insert_into_bit_range_vector (std::vector<range> *vectorp, |
4f82620c | 267 | LONGEST offset, ULONGEST length) |
4e07d55f | 268 | { |
0c7e6dd8 | 269 | range newr; |
4e07d55f PA |
270 | |
271 | /* Insert the range sorted. If there's overlap or the new range | |
272 | would be contiguous with an existing range, merge. */ | |
273 | ||
274 | newr.offset = offset; | |
275 | newr.length = length; | |
276 | ||
277 | /* Do a binary search for the position the given range would be | |
278 | inserted if we only considered the starting OFFSET of ranges. | |
279 | Call that position I. Since we also have LENGTH to care for | |
280 | (this is a range afterall), we need to check if the _previous_ | |
281 | range overlaps the I range. E.g., calling R the new range: | |
282 | ||
283 | #1 - overlaps with previous | |
284 | ||
285 | R | |
286 | |-...-| | |
287 | |---| |---| |------| ... |--| | |
288 | 0 1 2 N | |
289 | ||
290 | I=1 | |
291 | ||
292 | In the case #1 above, the binary search would return `I=1', | |
293 | meaning, this OFFSET should be inserted at position 1, and the | |
294 | current position 1 should be pushed further (and become 2). But, | |
295 | note that `0' overlaps with R, so we want to merge them. | |
296 | ||
297 | A similar consideration needs to be taken if the new range would | |
298 | be contiguous with the previous range: | |
299 | ||
300 | #2 - contiguous with previous | |
301 | ||
302 | R | |
303 | |-...-| | |
304 | |--| |---| |------| ... |--| | |
305 | 0 1 2 N | |
306 | ||
307 | I=1 | |
308 | ||
309 | If there's no overlap with the previous range, as in: | |
310 | ||
311 | #3 - not overlapping and not contiguous | |
312 | ||
313 | R | |
314 | |-...-| | |
315 | |--| |---| |------| ... |--| | |
316 | 0 1 2 N | |
317 | ||
318 | I=1 | |
319 | ||
320 | or if I is 0: | |
321 | ||
322 | #4 - R is the range with lowest offset | |
323 | ||
324 | R | |
325 | |-...-| | |
dda83cd7 SM |
326 | |--| |---| |------| ... |--| |
327 | 0 1 2 N | |
4e07d55f PA |
328 | |
329 | I=0 | |
330 | ||
331 | ... we just push the new range to I. | |
332 | ||
333 | All the 4 cases above need to consider that the new range may | |
334 | also overlap several of the ranges that follow, or that R may be | |
335 | contiguous with the following range, and merge. E.g., | |
336 | ||
337 | #5 - overlapping following ranges | |
338 | ||
339 | R | |
340 | |------------------------| | |
dda83cd7 SM |
341 | |--| |---| |------| ... |--| |
342 | 0 1 2 N | |
4e07d55f PA |
343 | |
344 | I=0 | |
345 | ||
346 | or: | |
347 | ||
348 | R | |
349 | |-------| | |
350 | |--| |---| |------| ... |--| | |
351 | 0 1 2 N | |
352 | ||
353 | I=1 | |
354 | ||
355 | */ | |
356 | ||
0c7e6dd8 TT |
357 | auto i = std::lower_bound (vectorp->begin (), vectorp->end (), newr); |
358 | if (i > vectorp->begin ()) | |
4e07d55f | 359 | { |
0c7e6dd8 | 360 | struct range &bef = *(i - 1); |
4e07d55f | 361 | |
0c7e6dd8 | 362 | if (ranges_overlap (bef.offset, bef.length, offset, length)) |
4e07d55f PA |
363 | { |
364 | /* #1 */ | |
4f82620c MR |
365 | LONGEST l = std::min (bef.offset, offset); |
366 | LONGEST h = std::max (bef.offset + bef.length, offset + length); | |
4e07d55f | 367 | |
0c7e6dd8 TT |
368 | bef.offset = l; |
369 | bef.length = h - l; | |
4e07d55f PA |
370 | i--; |
371 | } | |
0c7e6dd8 | 372 | else if (offset == bef.offset + bef.length) |
4e07d55f PA |
373 | { |
374 | /* #2 */ | |
0c7e6dd8 | 375 | bef.length += length; |
4e07d55f PA |
376 | i--; |
377 | } | |
378 | else | |
379 | { | |
380 | /* #3 */ | |
0c7e6dd8 | 381 | i = vectorp->insert (i, newr); |
4e07d55f PA |
382 | } |
383 | } | |
384 | else | |
385 | { | |
386 | /* #4 */ | |
0c7e6dd8 | 387 | i = vectorp->insert (i, newr); |
4e07d55f PA |
388 | } |
389 | ||
390 | /* Check whether the ranges following the one we've just added or | |
391 | touched can be folded in (#5 above). */ | |
0c7e6dd8 | 392 | if (i != vectorp->end () && i + 1 < vectorp->end ()) |
4e07d55f | 393 | { |
4e07d55f | 394 | int removed = 0; |
0c7e6dd8 | 395 | auto next = i + 1; |
4e07d55f PA |
396 | |
397 | /* Get the range we just touched. */ | |
0c7e6dd8 | 398 | struct range &t = *i; |
4e07d55f PA |
399 | removed = 0; |
400 | ||
401 | i = next; | |
0c7e6dd8 TT |
402 | for (; i < vectorp->end (); i++) |
403 | { | |
404 | struct range &r = *i; | |
405 | if (r.offset <= t.offset + t.length) | |
406 | { | |
4f82620c | 407 | LONGEST l, h; |
0c7e6dd8 TT |
408 | |
409 | l = std::min (t.offset, r.offset); | |
410 | h = std::max (t.offset + t.length, r.offset + r.length); | |
411 | ||
412 | t.offset = l; | |
413 | t.length = h - l; | |
414 | ||
415 | removed++; | |
416 | } | |
417 | else | |
418 | { | |
419 | /* If we couldn't merge this one, we won't be able to | |
420 | merge following ones either, since the ranges are | |
421 | always sorted by OFFSET. */ | |
422 | break; | |
423 | } | |
424 | } | |
4e07d55f PA |
425 | |
426 | if (removed != 0) | |
0c7e6dd8 | 427 | vectorp->erase (next, next + removed); |
4e07d55f PA |
428 | } |
429 | } | |
430 | ||
9a0dc9e3 | 431 | void |
6b850546 | 432 | mark_value_bits_unavailable (struct value *value, |
4f82620c | 433 | LONGEST offset, ULONGEST length) |
9a0dc9e3 | 434 | { |
382d927f | 435 | insert_into_bit_range_vector (&value->m_unavailable, offset, length); |
9a0dc9e3 PA |
436 | } |
437 | ||
bdf22206 | 438 | void |
6b850546 | 439 | mark_value_bytes_unavailable (struct value *value, |
4f82620c | 440 | LONGEST offset, ULONGEST length) |
bdf22206 AB |
441 | { |
442 | mark_value_bits_unavailable (value, | |
443 | offset * TARGET_CHAR_BIT, | |
444 | length * TARGET_CHAR_BIT); | |
445 | } | |
446 | ||
c8c1c22f PA |
447 | /* Find the first range in RANGES that overlaps the range defined by |
448 | OFFSET and LENGTH, starting at element POS in the RANGES vector, | |
449 | Returns the index into RANGES where such overlapping range was | |
450 | found, or -1 if none was found. */ | |
451 | ||
452 | static int | |
0c7e6dd8 | 453 | find_first_range_overlap (const std::vector<range> *ranges, int pos, |
6b850546 | 454 | LONGEST offset, LONGEST length) |
c8c1c22f | 455 | { |
c8c1c22f PA |
456 | int i; |
457 | ||
0c7e6dd8 TT |
458 | for (i = pos; i < ranges->size (); i++) |
459 | { | |
460 | const range &r = (*ranges)[i]; | |
461 | if (ranges_overlap (r.offset, r.length, offset, length)) | |
462 | return i; | |
463 | } | |
c8c1c22f PA |
464 | |
465 | return -1; | |
466 | } | |
467 | ||
bdf22206 AB |
468 | /* Compare LENGTH_BITS of memory at PTR1 + OFFSET1_BITS with the memory at |
469 | PTR2 + OFFSET2_BITS. Return 0 if the memory is the same, otherwise | |
470 | return non-zero. | |
471 | ||
472 | It must always be the case that: | |
473 | OFFSET1_BITS % TARGET_CHAR_BIT == OFFSET2_BITS % TARGET_CHAR_BIT | |
474 | ||
475 | It is assumed that memory can be accessed from: | |
476 | PTR + (OFFSET_BITS / TARGET_CHAR_BIT) | |
477 | to: | |
478 | PTR + ((OFFSET_BITS + LENGTH_BITS + TARGET_CHAR_BIT - 1) | |
dda83cd7 | 479 | / TARGET_CHAR_BIT) */ |
bdf22206 AB |
480 | static int |
481 | memcmp_with_bit_offsets (const gdb_byte *ptr1, size_t offset1_bits, | |
482 | const gdb_byte *ptr2, size_t offset2_bits, | |
483 | size_t length_bits) | |
484 | { | |
485 | gdb_assert (offset1_bits % TARGET_CHAR_BIT | |
486 | == offset2_bits % TARGET_CHAR_BIT); | |
487 | ||
488 | if (offset1_bits % TARGET_CHAR_BIT != 0) | |
489 | { | |
490 | size_t bits; | |
491 | gdb_byte mask, b1, b2; | |
492 | ||
493 | /* The offset from the base pointers PTR1 and PTR2 is not a complete | |
494 | number of bytes. A number of bits up to either the next exact | |
495 | byte boundary, or LENGTH_BITS (which ever is sooner) will be | |
496 | compared. */ | |
497 | bits = TARGET_CHAR_BIT - offset1_bits % TARGET_CHAR_BIT; | |
498 | gdb_assert (bits < sizeof (mask) * TARGET_CHAR_BIT); | |
499 | mask = (1 << bits) - 1; | |
500 | ||
501 | if (length_bits < bits) | |
502 | { | |
503 | mask &= ~(gdb_byte) ((1 << (bits - length_bits)) - 1); | |
504 | bits = length_bits; | |
505 | } | |
506 | ||
507 | /* Now load the two bytes and mask off the bits we care about. */ | |
508 | b1 = *(ptr1 + offset1_bits / TARGET_CHAR_BIT) & mask; | |
509 | b2 = *(ptr2 + offset2_bits / TARGET_CHAR_BIT) & mask; | |
510 | ||
511 | if (b1 != b2) | |
512 | return 1; | |
513 | ||
514 | /* Now update the length and offsets to take account of the bits | |
515 | we've just compared. */ | |
516 | length_bits -= bits; | |
517 | offset1_bits += bits; | |
518 | offset2_bits += bits; | |
519 | } | |
520 | ||
521 | if (length_bits % TARGET_CHAR_BIT != 0) | |
522 | { | |
523 | size_t bits; | |
524 | size_t o1, o2; | |
525 | gdb_byte mask, b1, b2; | |
526 | ||
527 | /* The length is not an exact number of bytes. After the previous | |
528 | IF.. block then the offsets are byte aligned, or the | |
529 | length is zero (in which case this code is not reached). Compare | |
530 | a number of bits at the end of the region, starting from an exact | |
531 | byte boundary. */ | |
532 | bits = length_bits % TARGET_CHAR_BIT; | |
533 | o1 = offset1_bits + length_bits - bits; | |
534 | o2 = offset2_bits + length_bits - bits; | |
535 | ||
536 | gdb_assert (bits < sizeof (mask) * TARGET_CHAR_BIT); | |
537 | mask = ((1 << bits) - 1) << (TARGET_CHAR_BIT - bits); | |
538 | ||
539 | gdb_assert (o1 % TARGET_CHAR_BIT == 0); | |
540 | gdb_assert (o2 % TARGET_CHAR_BIT == 0); | |
541 | ||
542 | b1 = *(ptr1 + o1 / TARGET_CHAR_BIT) & mask; | |
543 | b2 = *(ptr2 + o2 / TARGET_CHAR_BIT) & mask; | |
544 | ||
545 | if (b1 != b2) | |
546 | return 1; | |
547 | ||
548 | length_bits -= bits; | |
549 | } | |
550 | ||
551 | if (length_bits > 0) | |
552 | { | |
553 | /* We've now taken care of any stray "bits" at the start, or end of | |
554 | the region to compare, the remainder can be covered with a simple | |
555 | memcmp. */ | |
556 | gdb_assert (offset1_bits % TARGET_CHAR_BIT == 0); | |
557 | gdb_assert (offset2_bits % TARGET_CHAR_BIT == 0); | |
558 | gdb_assert (length_bits % TARGET_CHAR_BIT == 0); | |
559 | ||
560 | return memcmp (ptr1 + offset1_bits / TARGET_CHAR_BIT, | |
561 | ptr2 + offset2_bits / TARGET_CHAR_BIT, | |
562 | length_bits / TARGET_CHAR_BIT); | |
563 | } | |
564 | ||
565 | /* Length is zero, regions match. */ | |
566 | return 0; | |
567 | } | |
568 | ||
9a0dc9e3 PA |
569 | /* Helper struct for find_first_range_overlap_and_match and |
570 | value_contents_bits_eq. Keep track of which slot of a given ranges | |
571 | vector have we last looked at. */ | |
bdf22206 | 572 | |
9a0dc9e3 PA |
573 | struct ranges_and_idx |
574 | { | |
575 | /* The ranges. */ | |
0c7e6dd8 | 576 | const std::vector<range> *ranges; |
9a0dc9e3 PA |
577 | |
578 | /* The range we've last found in RANGES. Given ranges are sorted, | |
579 | we can start the next lookup here. */ | |
580 | int idx; | |
581 | }; | |
582 | ||
583 | /* Helper function for value_contents_bits_eq. Compare LENGTH bits of | |
584 | RP1's ranges starting at OFFSET1 bits with LENGTH bits of RP2's | |
585 | ranges starting at OFFSET2 bits. Return true if the ranges match | |
586 | and fill in *L and *H with the overlapping window relative to | |
587 | (both) OFFSET1 or OFFSET2. */ | |
bdf22206 AB |
588 | |
589 | static int | |
9a0dc9e3 PA |
590 | find_first_range_overlap_and_match (struct ranges_and_idx *rp1, |
591 | struct ranges_and_idx *rp2, | |
6b850546 | 592 | LONGEST offset1, LONGEST offset2, |
4f82620c | 593 | ULONGEST length, ULONGEST *l, ULONGEST *h) |
c8c1c22f | 594 | { |
9a0dc9e3 PA |
595 | rp1->idx = find_first_range_overlap (rp1->ranges, rp1->idx, |
596 | offset1, length); | |
597 | rp2->idx = find_first_range_overlap (rp2->ranges, rp2->idx, | |
598 | offset2, length); | |
c8c1c22f | 599 | |
9a0dc9e3 PA |
600 | if (rp1->idx == -1 && rp2->idx == -1) |
601 | { | |
602 | *l = length; | |
603 | *h = length; | |
604 | return 1; | |
605 | } | |
606 | else if (rp1->idx == -1 || rp2->idx == -1) | |
607 | return 0; | |
608 | else | |
c8c1c22f | 609 | { |
0c7e6dd8 | 610 | const range *r1, *r2; |
c8c1c22f PA |
611 | ULONGEST l1, h1; |
612 | ULONGEST l2, h2; | |
613 | ||
0c7e6dd8 TT |
614 | r1 = &(*rp1->ranges)[rp1->idx]; |
615 | r2 = &(*rp2->ranges)[rp2->idx]; | |
c8c1c22f PA |
616 | |
617 | /* Get the unavailable windows intersected by the incoming | |
618 | ranges. The first and last ranges that overlap the argument | |
619 | range may be wider than said incoming arguments ranges. */ | |
325fac50 PA |
620 | l1 = std::max (offset1, r1->offset); |
621 | h1 = std::min (offset1 + length, r1->offset + r1->length); | |
c8c1c22f | 622 | |
325fac50 PA |
623 | l2 = std::max (offset2, r2->offset); |
624 | h2 = std::min (offset2 + length, offset2 + r2->length); | |
c8c1c22f PA |
625 | |
626 | /* Make them relative to the respective start offsets, so we can | |
627 | compare them for equality. */ | |
628 | l1 -= offset1; | |
629 | h1 -= offset1; | |
630 | ||
631 | l2 -= offset2; | |
632 | h2 -= offset2; | |
633 | ||
9a0dc9e3 | 634 | /* Different ranges, no match. */ |
c8c1c22f PA |
635 | if (l1 != l2 || h1 != h2) |
636 | return 0; | |
637 | ||
9a0dc9e3 PA |
638 | *h = h1; |
639 | *l = l1; | |
640 | return 1; | |
641 | } | |
642 | } | |
643 | ||
644 | /* Helper function for value_contents_eq. The only difference is that | |
645 | this function is bit rather than byte based. | |
646 | ||
647 | Compare LENGTH bits of VAL1's contents starting at OFFSET1 bits | |
648 | with LENGTH bits of VAL2's contents starting at OFFSET2 bits. | |
649 | Return true if the available bits match. */ | |
650 | ||
98ead37e | 651 | static bool |
9a0dc9e3 PA |
652 | value_contents_bits_eq (const struct value *val1, int offset1, |
653 | const struct value *val2, int offset2, | |
654 | int length) | |
655 | { | |
656 | /* Each array element corresponds to a ranges source (unavailable, | |
657 | optimized out). '1' is for VAL1, '2' for VAL2. */ | |
658 | struct ranges_and_idx rp1[2], rp2[2]; | |
659 | ||
660 | /* See function description in value.h. */ | |
382d927f | 661 | gdb_assert (!val1->m_lazy && !val2->m_lazy); |
9a0dc9e3 PA |
662 | |
663 | /* We shouldn't be trying to compare past the end of the values. */ | |
664 | gdb_assert (offset1 + length | |
382d927f | 665 | <= val1->m_enclosing_type->length () * TARGET_CHAR_BIT); |
9a0dc9e3 | 666 | gdb_assert (offset2 + length |
382d927f | 667 | <= val2->m_enclosing_type->length () * TARGET_CHAR_BIT); |
9a0dc9e3 PA |
668 | |
669 | memset (&rp1, 0, sizeof (rp1)); | |
670 | memset (&rp2, 0, sizeof (rp2)); | |
382d927f TT |
671 | rp1[0].ranges = &val1->m_unavailable; |
672 | rp2[0].ranges = &val2->m_unavailable; | |
673 | rp1[1].ranges = &val1->m_optimized_out; | |
674 | rp2[1].ranges = &val2->m_optimized_out; | |
9a0dc9e3 PA |
675 | |
676 | while (length > 0) | |
677 | { | |
000339af | 678 | ULONGEST l = 0, h = 0; /* init for gcc -Wall */ |
9a0dc9e3 PA |
679 | int i; |
680 | ||
681 | for (i = 0; i < 2; i++) | |
682 | { | |
683 | ULONGEST l_tmp, h_tmp; | |
684 | ||
685 | /* The contents only match equal if the invalid/unavailable | |
686 | contents ranges match as well. */ | |
687 | if (!find_first_range_overlap_and_match (&rp1[i], &rp2[i], | |
688 | offset1, offset2, length, | |
689 | &l_tmp, &h_tmp)) | |
98ead37e | 690 | return false; |
9a0dc9e3 PA |
691 | |
692 | /* We're interested in the lowest/first range found. */ | |
693 | if (i == 0 || l_tmp < l) | |
694 | { | |
695 | l = l_tmp; | |
696 | h = h_tmp; | |
697 | } | |
698 | } | |
699 | ||
700 | /* Compare the available/valid contents. */ | |
382d927f TT |
701 | if (memcmp_with_bit_offsets (val1->m_contents.get (), offset1, |
702 | val2->m_contents.get (), offset2, l) != 0) | |
98ead37e | 703 | return false; |
c8c1c22f | 704 | |
9a0dc9e3 PA |
705 | length -= h; |
706 | offset1 += h; | |
707 | offset2 += h; | |
c8c1c22f PA |
708 | } |
709 | ||
98ead37e | 710 | return true; |
c8c1c22f PA |
711 | } |
712 | ||
98ead37e | 713 | bool |
6b850546 DT |
714 | value_contents_eq (const struct value *val1, LONGEST offset1, |
715 | const struct value *val2, LONGEST offset2, | |
716 | LONGEST length) | |
bdf22206 | 717 | { |
9a0dc9e3 PA |
718 | return value_contents_bits_eq (val1, offset1 * TARGET_CHAR_BIT, |
719 | val2, offset2 * TARGET_CHAR_BIT, | |
720 | length * TARGET_CHAR_BIT); | |
bdf22206 AB |
721 | } |
722 | ||
e379f652 TT |
723 | /* See value.h. */ |
724 | ||
725 | bool | |
726 | value_contents_eq (const struct value *val1, const struct value *val2) | |
727 | { | |
463b870d TT |
728 | ULONGEST len1 = check_typedef (val1->enclosing_type ())->length (); |
729 | ULONGEST len2 = check_typedef (val2->enclosing_type ())->length (); | |
e379f652 TT |
730 | if (len1 != len2) |
731 | return false; | |
732 | return value_contents_eq (val1, 0, val2, 0, len1); | |
733 | } | |
c906108c | 734 | |
4d0266a0 TT |
735 | /* The value-history records all the values printed by print commands |
736 | during this session. */ | |
c906108c | 737 | |
4d0266a0 | 738 | static std::vector<value_ref_ptr> value_history; |
bc3b79fd | 739 | |
c906108c SS |
740 | \f |
741 | /* List of all value objects currently allocated | |
742 | (except for those released by calls to release_value) | |
743 | This is so they can be freed after each command. */ | |
744 | ||
062d818d | 745 | static std::vector<value_ref_ptr> all_values; |
c906108c | 746 | |
3e3d7139 JG |
747 | /* Allocate a lazy value for type TYPE. Its actual content is |
748 | "lazily" allocated too: the content field of the return value is | |
749 | NULL; it will be allocated when it is fetched from the target. */ | |
c906108c | 750 | |
f23631e4 | 751 | struct value * |
3e3d7139 | 752 | allocate_value_lazy (struct type *type) |
c906108c | 753 | { |
f23631e4 | 754 | struct value *val; |
c54eabfa JK |
755 | |
756 | /* Call check_typedef on our type to make sure that, if TYPE | |
757 | is a TYPE_CODE_TYPEDEF, its length is set to the length | |
758 | of the target type instead of zero. However, we do not | |
759 | replace the typedef type by the target type, because we want | |
760 | to keep the typedef in order to be able to set the VAL's type | |
761 | description correctly. */ | |
762 | check_typedef (type); | |
c906108c | 763 | |
466ce3ae | 764 | val = new struct value (type); |
828d3400 DJ |
765 | |
766 | /* Values start out on the all_values chain. */ | |
062d818d | 767 | all_values.emplace_back (val); |
828d3400 | 768 | |
c906108c SS |
769 | return val; |
770 | } | |
771 | ||
5fdf6324 AB |
772 | /* The maximum size, in bytes, that GDB will try to allocate for a value. |
773 | The initial value of 64k was not selected for any specific reason, it is | |
774 | just a reasonable starting point. */ | |
775 | ||
776 | static int max_value_size = 65536; /* 64k bytes */ | |
777 | ||
778 | /* It is critical that the MAX_VALUE_SIZE is at least as big as the size of | |
779 | LONGEST, otherwise GDB will not be able to parse integer values from the | |
780 | CLI; for example if the MAX_VALUE_SIZE could be set to 1 then GDB would | |
781 | be unable to parse "set max-value-size 2". | |
782 | ||
783 | As we want a consistent GDB experience across hosts with different sizes | |
784 | of LONGEST, this arbitrary minimum value was selected, so long as this | |
785 | is bigger than LONGEST on all GDB supported hosts we're fine. */ | |
786 | ||
787 | #define MIN_VALUE_FOR_MAX_VALUE_SIZE 16 | |
788 | gdb_static_assert (sizeof (LONGEST) <= MIN_VALUE_FOR_MAX_VALUE_SIZE); | |
789 | ||
790 | /* Implement the "set max-value-size" command. */ | |
791 | ||
792 | static void | |
eb4c3f4a | 793 | set_max_value_size (const char *args, int from_tty, |
5fdf6324 AB |
794 | struct cmd_list_element *c) |
795 | { | |
796 | gdb_assert (max_value_size == -1 || max_value_size >= 0); | |
797 | ||
798 | if (max_value_size > -1 && max_value_size < MIN_VALUE_FOR_MAX_VALUE_SIZE) | |
799 | { | |
800 | max_value_size = MIN_VALUE_FOR_MAX_VALUE_SIZE; | |
801 | error (_("max-value-size set too low, increasing to %d bytes"), | |
802 | max_value_size); | |
803 | } | |
804 | } | |
805 | ||
806 | /* Implement the "show max-value-size" command. */ | |
807 | ||
808 | static void | |
809 | show_max_value_size (struct ui_file *file, int from_tty, | |
810 | struct cmd_list_element *c, const char *value) | |
811 | { | |
812 | if (max_value_size == -1) | |
6cb06a8c | 813 | gdb_printf (file, _("Maximum value size is unlimited.\n")); |
5fdf6324 | 814 | else |
6cb06a8c TT |
815 | gdb_printf (file, _("Maximum value size is %d bytes.\n"), |
816 | max_value_size); | |
5fdf6324 AB |
817 | } |
818 | ||
819 | /* Called before we attempt to allocate or reallocate a buffer for the | |
820 | contents of a value. TYPE is the type of the value for which we are | |
821 | allocating the buffer. If the buffer is too large (based on the user | |
822 | controllable setting) then throw an error. If this function returns | |
823 | then we should attempt to allocate the buffer. */ | |
824 | ||
825 | static void | |
826 | check_type_length_before_alloc (const struct type *type) | |
827 | { | |
df86565b | 828 | ULONGEST length = type->length (); |
5fdf6324 AB |
829 | |
830 | if (max_value_size > -1 && length > max_value_size) | |
831 | { | |
7d93a1e0 | 832 | if (type->name () != NULL) |
6d8a0a5e LM |
833 | error (_("value of type `%s' requires %s bytes, which is more " |
834 | "than max-value-size"), type->name (), pulongest (length)); | |
5fdf6324 | 835 | else |
6d8a0a5e LM |
836 | error (_("value requires %s bytes, which is more than " |
837 | "max-value-size"), pulongest (length)); | |
5fdf6324 AB |
838 | } |
839 | } | |
840 | ||
a0c07915 AB |
841 | /* When this has a value, it is used to limit the number of array elements |
842 | of an array that are loaded into memory when an array value is made | |
843 | non-lazy. */ | |
844 | static gdb::optional<int> array_length_limiting_element_count; | |
845 | ||
846 | /* See value.h. */ | |
847 | scoped_array_length_limiting::scoped_array_length_limiting (int elements) | |
848 | { | |
849 | m_old_value = array_length_limiting_element_count; | |
850 | array_length_limiting_element_count.emplace (elements); | |
851 | } | |
852 | ||
853 | /* See value.h. */ | |
854 | scoped_array_length_limiting::~scoped_array_length_limiting () | |
855 | { | |
856 | array_length_limiting_element_count = m_old_value; | |
857 | } | |
858 | ||
859 | /* Find the inner element type for ARRAY_TYPE. */ | |
860 | ||
861 | static struct type * | |
862 | find_array_element_type (struct type *array_type) | |
863 | { | |
864 | array_type = check_typedef (array_type); | |
865 | gdb_assert (array_type->code () == TYPE_CODE_ARRAY); | |
866 | ||
867 | if (current_language->la_language == language_fortran) | |
868 | while (array_type->code () == TYPE_CODE_ARRAY) | |
869 | { | |
870 | array_type = array_type->target_type (); | |
871 | array_type = check_typedef (array_type); | |
872 | } | |
873 | else | |
874 | { | |
875 | array_type = array_type->target_type (); | |
876 | array_type = check_typedef (array_type); | |
877 | } | |
878 | ||
879 | return array_type; | |
880 | } | |
881 | ||
882 | /* Return the limited length of ARRAY_TYPE, which must be of | |
883 | TYPE_CODE_ARRAY. This function can only be called when the global | |
884 | ARRAY_LENGTH_LIMITING_ELEMENT_COUNT has a value. | |
885 | ||
886 | The limited length of an array is the smallest of either (1) the total | |
887 | size of the array type, or (2) the array target type multiplies by the | |
888 | array_length_limiting_element_count. */ | |
889 | ||
890 | static ULONGEST | |
891 | calculate_limited_array_length (struct type *array_type) | |
892 | { | |
893 | gdb_assert (array_length_limiting_element_count.has_value ()); | |
894 | ||
895 | array_type = check_typedef (array_type); | |
896 | gdb_assert (array_type->code () == TYPE_CODE_ARRAY); | |
897 | ||
898 | struct type *elm_type = find_array_element_type (array_type); | |
899 | ULONGEST len = (elm_type->length () | |
900 | * (*array_length_limiting_element_count)); | |
901 | len = std::min (len, array_type->length ()); | |
902 | ||
903 | return len; | |
904 | } | |
905 | ||
906 | /* Try to limit ourselves to only fetching the limited number of | |
907 | elements. However, if this limited number of elements still | |
908 | puts us over max_value_size, then we still refuse it and | |
909 | return failure here, which will ultimately throw an error. */ | |
910 | ||
911 | static bool | |
912 | set_limited_array_length (struct value *val) | |
913 | { | |
382d927f | 914 | ULONGEST limit = val->m_limited_length; |
d0c97917 | 915 | ULONGEST len = val->type ()->length (); |
a0c07915 AB |
916 | |
917 | if (array_length_limiting_element_count.has_value ()) | |
d0c97917 | 918 | len = calculate_limited_array_length (val->type ()); |
a0c07915 AB |
919 | |
920 | if (limit != 0 && len > limit) | |
921 | len = limit; | |
922 | if (len > max_value_size) | |
923 | return false; | |
924 | ||
382d927f | 925 | val->m_limited_length = max_value_size; |
a0c07915 AB |
926 | return true; |
927 | } | |
928 | ||
bae19789 MR |
929 | /* Allocate the contents of VAL if it has not been allocated yet. |
930 | If CHECK_SIZE is true, then apply the usual max-value-size checks. */ | |
3e3d7139 | 931 | |
548b762d | 932 | static void |
bae19789 | 933 | allocate_value_contents (struct value *val, bool check_size) |
3e3d7139 | 934 | { |
382d927f | 935 | if (!val->m_contents) |
5fdf6324 | 936 | { |
463b870d | 937 | struct type *enclosing_type = val->enclosing_type (); |
a0c07915 AB |
938 | ULONGEST len = enclosing_type->length (); |
939 | ||
bae19789 | 940 | if (check_size) |
a0c07915 AB |
941 | { |
942 | /* If we are allocating the contents of an array, which | |
943 | is greater in size than max_value_size, and there is | |
944 | an element limit in effect, then we can possibly try | |
945 | to load only a sub-set of the array contents into | |
946 | GDB's memory. */ | |
d0c97917 TT |
947 | if (val->type () == enclosing_type |
948 | && val->type ()->code () == TYPE_CODE_ARRAY | |
a0c07915 AB |
949 | && len > max_value_size |
950 | && set_limited_array_length (val)) | |
382d927f | 951 | len = val->m_limited_length; |
a0c07915 AB |
952 | else |
953 | check_type_length_before_alloc (enclosing_type); | |
954 | } | |
955 | ||
382d927f | 956 | val->m_contents.reset ((gdb_byte *) xzalloc (len)); |
5fdf6324 | 957 | } |
3e3d7139 JG |
958 | } |
959 | ||
bae19789 MR |
960 | /* Allocate a value and its contents for type TYPE. If CHECK_SIZE is true, |
961 | then apply the usual max-value-size checks. */ | |
3e3d7139 | 962 | |
bae19789 MR |
963 | static struct value * |
964 | allocate_value (struct type *type, bool check_size) | |
3e3d7139 JG |
965 | { |
966 | struct value *val = allocate_value_lazy (type); | |
a109c7c1 | 967 | |
bae19789 | 968 | allocate_value_contents (val, check_size); |
382d927f | 969 | val->m_lazy = 0; |
3e3d7139 JG |
970 | return val; |
971 | } | |
972 | ||
bae19789 MR |
973 | /* Allocate a value and its contents for type TYPE. */ |
974 | ||
975 | struct value * | |
976 | allocate_value (struct type *type) | |
977 | { | |
978 | return allocate_value (type, true); | |
979 | } | |
980 | ||
c906108c | 981 | /* Allocate a value that has the correct length |
938f5214 | 982 | for COUNT repetitions of type TYPE. */ |
c906108c | 983 | |
f23631e4 | 984 | struct value * |
fba45db2 | 985 | allocate_repeat_value (struct type *type, int count) |
c906108c | 986 | { |
22c12a6c AB |
987 | /* Despite the fact that we are really creating an array of TYPE here, we |
988 | use the string lower bound as the array lower bound. This seems to | |
989 | work fine for now. */ | |
990 | int low_bound = current_language->string_lower_bound (); | |
c906108c SS |
991 | /* FIXME-type-allocation: need a way to free this type when we are |
992 | done with it. */ | |
e3506a9f UW |
993 | struct type *array_type |
994 | = lookup_array_range_type (type, low_bound, count + low_bound - 1); | |
a109c7c1 | 995 | |
e3506a9f | 996 | return allocate_value (array_type); |
c906108c SS |
997 | } |
998 | ||
5f5233d4 PA |
999 | struct value * |
1000 | allocate_computed_value (struct type *type, | |
dda83cd7 SM |
1001 | const struct lval_funcs *funcs, |
1002 | void *closure) | |
5f5233d4 | 1003 | { |
41e8491f | 1004 | struct value *v = allocate_value_lazy (type); |
a109c7c1 | 1005 | |
5f5233d4 | 1006 | VALUE_LVAL (v) = lval_computed; |
382d927f TT |
1007 | v->m_location.computed.funcs = funcs; |
1008 | v->m_location.computed.closure = closure; | |
5f5233d4 PA |
1009 | |
1010 | return v; | |
1011 | } | |
1012 | ||
a7035dbb JK |
1013 | /* Allocate NOT_LVAL value for type TYPE being OPTIMIZED_OUT. */ |
1014 | ||
1015 | struct value * | |
1016 | allocate_optimized_out_value (struct type *type) | |
1017 | { | |
1018 | struct value *retval = allocate_value_lazy (type); | |
1019 | ||
df86565b | 1020 | mark_value_bytes_optimized_out (retval, 0, type->length ()); |
9a0dc9e3 | 1021 | set_value_lazy (retval, 0); |
a7035dbb JK |
1022 | return retval; |
1023 | } | |
1024 | ||
df407dfe AC |
1025 | /* Accessor methods. */ |
1026 | ||
50888e42 | 1027 | gdb::array_view<gdb_byte> |
990a07ab AC |
1028 | value_contents_raw (struct value *value) |
1029 | { | |
f9ee742c | 1030 | struct gdbarch *arch = value->arch (); |
3ae385af SM |
1031 | int unit_size = gdbarch_addressable_memory_unit_size (arch); |
1032 | ||
bae19789 | 1033 | allocate_value_contents (value, true); |
50888e42 | 1034 | |
d0c97917 | 1035 | ULONGEST length = value->type ()->length (); |
5612b5d2 | 1036 | return gdb::make_array_view |
382d927f | 1037 | (value->m_contents.get () + value->m_embedded_offset * unit_size, length); |
990a07ab AC |
1038 | } |
1039 | ||
50888e42 | 1040 | gdb::array_view<gdb_byte> |
990a07ab AC |
1041 | value_contents_all_raw (struct value *value) |
1042 | { | |
bae19789 | 1043 | allocate_value_contents (value, true); |
50888e42 | 1044 | |
463b870d | 1045 | ULONGEST length = value->enclosing_type ()->length (); |
382d927f | 1046 | return gdb::make_array_view (value->m_contents.get (), length); |
990a07ab AC |
1047 | } |
1048 | ||
8264ba82 AG |
1049 | /* Look at value.h for description. */ |
1050 | ||
1051 | struct type * | |
1052 | value_actual_type (struct value *value, int resolve_simple_types, | |
1053 | int *real_type_found) | |
1054 | { | |
1055 | struct value_print_options opts; | |
8264ba82 AG |
1056 | struct type *result; |
1057 | ||
1058 | get_user_print_options (&opts); | |
1059 | ||
1060 | if (real_type_found) | |
1061 | *real_type_found = 0; | |
d0c97917 | 1062 | result = value->type (); |
8264ba82 AG |
1063 | if (opts.objectprint) |
1064 | { | |
5e34c6c3 LM |
1065 | /* If result's target type is TYPE_CODE_STRUCT, proceed to |
1066 | fetch its rtti type. */ | |
809f3be1 | 1067 | if (result->is_pointer_or_reference () |
27710edb | 1068 | && (check_typedef (result->target_type ())->code () |
78134374 | 1069 | == TYPE_CODE_STRUCT) |
ecf2e90c | 1070 | && !value_optimized_out (value)) |
dda83cd7 SM |
1071 | { |
1072 | struct type *real_type; | |
1073 | ||
1074 | real_type = value_rtti_indirect_type (value, NULL, NULL, NULL); | |
1075 | if (real_type) | |
1076 | { | |
1077 | if (real_type_found) | |
1078 | *real_type_found = 1; | |
1079 | result = real_type; | |
1080 | } | |
1081 | } | |
8264ba82 | 1082 | else if (resolve_simple_types) |
dda83cd7 SM |
1083 | { |
1084 | if (real_type_found) | |
1085 | *real_type_found = 1; | |
463b870d | 1086 | result = value->enclosing_type (); |
dda83cd7 | 1087 | } |
8264ba82 AG |
1088 | } |
1089 | ||
1090 | return result; | |
1091 | } | |
1092 | ||
901461f8 PA |
1093 | void |
1094 | error_value_optimized_out (void) | |
1095 | { | |
a6e7fea1 | 1096 | throw_error (OPTIMIZED_OUT_ERROR, _("value has been optimized out")); |
901461f8 PA |
1097 | } |
1098 | ||
0e03807e | 1099 | static void |
4e07d55f | 1100 | require_not_optimized_out (const struct value *value) |
0e03807e | 1101 | { |
382d927f | 1102 | if (!value->m_optimized_out.empty ()) |
901461f8 | 1103 | { |
382d927f | 1104 | if (value->m_lval == lval_register) |
a6e7fea1 AB |
1105 | throw_error (OPTIMIZED_OUT_ERROR, |
1106 | _("register has not been saved in frame")); | |
901461f8 PA |
1107 | else |
1108 | error_value_optimized_out (); | |
1109 | } | |
0e03807e TT |
1110 | } |
1111 | ||
4e07d55f PA |
1112 | static void |
1113 | require_available (const struct value *value) | |
1114 | { | |
382d927f | 1115 | if (!value->m_unavailable.empty ()) |
8af8e3bc | 1116 | throw_error (NOT_AVAILABLE_ERROR, _("value is not available")); |
4e07d55f PA |
1117 | } |
1118 | ||
50888e42 | 1119 | gdb::array_view<const gdb_byte> |
0e03807e | 1120 | value_contents_for_printing (struct value *value) |
46615f07 | 1121 | { |
382d927f | 1122 | if (value->m_lazy) |
46615f07 | 1123 | value_fetch_lazy (value); |
50888e42 | 1124 | |
463b870d | 1125 | ULONGEST length = value->enclosing_type ()->length (); |
382d927f | 1126 | return gdb::make_array_view (value->m_contents.get (), length); |
46615f07 AC |
1127 | } |
1128 | ||
50888e42 | 1129 | gdb::array_view<const gdb_byte> |
de4127a3 PA |
1130 | value_contents_for_printing_const (const struct value *value) |
1131 | { | |
382d927f | 1132 | gdb_assert (!value->m_lazy); |
50888e42 | 1133 | |
463b870d | 1134 | ULONGEST length = value->enclosing_type ()->length (); |
382d927f | 1135 | return gdb::make_array_view (value->m_contents.get (), length); |
de4127a3 PA |
1136 | } |
1137 | ||
50888e42 | 1138 | gdb::array_view<const gdb_byte> |
0e03807e TT |
1139 | value_contents_all (struct value *value) |
1140 | { | |
50888e42 | 1141 | gdb::array_view<const gdb_byte> result = value_contents_for_printing (value); |
0e03807e | 1142 | require_not_optimized_out (value); |
4e07d55f | 1143 | require_available (value); |
0e03807e TT |
1144 | return result; |
1145 | } | |
1146 | ||
9a0dc9e3 PA |
1147 | /* Copy ranges in SRC_RANGE that overlap [SRC_BIT_OFFSET, |
1148 | SRC_BIT_OFFSET+BIT_LENGTH) ranges into *DST_RANGE, adjusted. */ | |
1149 | ||
1150 | static void | |
0c7e6dd8 TT |
1151 | ranges_copy_adjusted (std::vector<range> *dst_range, int dst_bit_offset, |
1152 | const std::vector<range> &src_range, int src_bit_offset, | |
4f82620c | 1153 | unsigned int bit_length) |
9a0dc9e3 | 1154 | { |
0c7e6dd8 | 1155 | for (const range &r : src_range) |
9a0dc9e3 | 1156 | { |
4f82620c | 1157 | LONGEST h, l; |
9a0dc9e3 | 1158 | |
0c7e6dd8 | 1159 | l = std::max (r.offset, (LONGEST) src_bit_offset); |
4f82620c | 1160 | h = std::min ((LONGEST) (r.offset + r.length), |
325fac50 | 1161 | (LONGEST) src_bit_offset + bit_length); |
9a0dc9e3 PA |
1162 | |
1163 | if (l < h) | |
1164 | insert_into_bit_range_vector (dst_range, | |
1165 | dst_bit_offset + (l - src_bit_offset), | |
1166 | h - l); | |
1167 | } | |
1168 | } | |
1169 | ||
4875ffdb PA |
1170 | /* Copy the ranges metadata in SRC that overlaps [SRC_BIT_OFFSET, |
1171 | SRC_BIT_OFFSET+BIT_LENGTH) into DST, adjusted. */ | |
1172 | ||
1173 | static void | |
1174 | value_ranges_copy_adjusted (struct value *dst, int dst_bit_offset, | |
1175 | const struct value *src, int src_bit_offset, | |
1176 | int bit_length) | |
1177 | { | |
382d927f TT |
1178 | ranges_copy_adjusted (&dst->m_unavailable, dst_bit_offset, |
1179 | src->m_unavailable, src_bit_offset, | |
4875ffdb | 1180 | bit_length); |
382d927f TT |
1181 | ranges_copy_adjusted (&dst->m_optimized_out, dst_bit_offset, |
1182 | src->m_optimized_out, src_bit_offset, | |
4875ffdb PA |
1183 | bit_length); |
1184 | } | |
1185 | ||
3ae385af | 1186 | /* Copy LENGTH target addressable memory units of SRC value's (all) contents |
29976f3f PA |
1187 | (value_contents_all) starting at SRC_OFFSET, into DST value's (all) |
1188 | contents, starting at DST_OFFSET. If unavailable contents are | |
1189 | being copied from SRC, the corresponding DST contents are marked | |
1190 | unavailable accordingly. Neither DST nor SRC may be lazy | |
1191 | values. | |
1192 | ||
1193 | It is assumed the contents of DST in the [DST_OFFSET, | |
1194 | DST_OFFSET+LENGTH) range are wholly available. */ | |
39d37385 | 1195 | |
f73e424f | 1196 | static void |
6b850546 DT |
1197 | value_contents_copy_raw (struct value *dst, LONGEST dst_offset, |
1198 | struct value *src, LONGEST src_offset, LONGEST length) | |
39d37385 | 1199 | { |
6b850546 | 1200 | LONGEST src_bit_offset, dst_bit_offset, bit_length; |
f9ee742c | 1201 | struct gdbarch *arch = src->arch (); |
3ae385af | 1202 | int unit_size = gdbarch_addressable_memory_unit_size (arch); |
39d37385 PA |
1203 | |
1204 | /* A lazy DST would make that this copy operation useless, since as | |
1205 | soon as DST's contents were un-lazied (by a later value_contents | |
1206 | call, say), the contents would be overwritten. A lazy SRC would | |
1207 | mean we'd be copying garbage. */ | |
382d927f | 1208 | gdb_assert (!dst->m_lazy && !src->m_lazy); |
39d37385 | 1209 | |
29976f3f PA |
1210 | /* The overwritten DST range gets unavailability ORed in, not |
1211 | replaced. Make sure to remember to implement replacing if it | |
1212 | turns out actually necessary. */ | |
1213 | gdb_assert (value_bytes_available (dst, dst_offset, length)); | |
9a0dc9e3 PA |
1214 | gdb_assert (!value_bits_any_optimized_out (dst, |
1215 | TARGET_CHAR_BIT * dst_offset, | |
1216 | TARGET_CHAR_BIT * length)); | |
29976f3f | 1217 | |
39d37385 | 1218 | /* Copy the data. */ |
4bce7cda SM |
1219 | gdb::array_view<gdb_byte> dst_contents |
1220 | = value_contents_all_raw (dst).slice (dst_offset * unit_size, | |
1221 | length * unit_size); | |
1222 | gdb::array_view<const gdb_byte> src_contents | |
1223 | = value_contents_all_raw (src).slice (src_offset * unit_size, | |
1224 | length * unit_size); | |
1225 | copy (src_contents, dst_contents); | |
39d37385 PA |
1226 | |
1227 | /* Copy the meta-data, adjusted. */ | |
3ae385af SM |
1228 | src_bit_offset = src_offset * unit_size * HOST_CHAR_BIT; |
1229 | dst_bit_offset = dst_offset * unit_size * HOST_CHAR_BIT; | |
1230 | bit_length = length * unit_size * HOST_CHAR_BIT; | |
39d37385 | 1231 | |
4875ffdb PA |
1232 | value_ranges_copy_adjusted (dst, dst_bit_offset, |
1233 | src, src_bit_offset, | |
1234 | bit_length); | |
39d37385 PA |
1235 | } |
1236 | ||
e379f652 TT |
1237 | /* A helper for value_from_component_bitsize that copies bits from SRC |
1238 | to DEST. */ | |
1239 | ||
1240 | static void | |
1241 | value_contents_copy_raw_bitwise (struct value *dst, LONGEST dst_bit_offset, | |
1242 | struct value *src, LONGEST src_bit_offset, | |
1243 | LONGEST bit_length) | |
1244 | { | |
1245 | /* A lazy DST would make that this copy operation useless, since as | |
1246 | soon as DST's contents were un-lazied (by a later value_contents | |
1247 | call, say), the contents would be overwritten. A lazy SRC would | |
1248 | mean we'd be copying garbage. */ | |
382d927f | 1249 | gdb_assert (!dst->m_lazy && !src->m_lazy); |
e379f652 TT |
1250 | |
1251 | /* The overwritten DST range gets unavailability ORed in, not | |
1252 | replaced. Make sure to remember to implement replacing if it | |
1253 | turns out actually necessary. */ | |
1254 | LONGEST dst_offset = dst_bit_offset / TARGET_CHAR_BIT; | |
1255 | LONGEST length = bit_length / TARGET_CHAR_BIT; | |
1256 | gdb_assert (value_bytes_available (dst, dst_offset, length)); | |
1257 | gdb_assert (!value_bits_any_optimized_out (dst, dst_bit_offset, | |
1258 | bit_length)); | |
1259 | ||
1260 | /* Copy the data. */ | |
1261 | gdb::array_view<gdb_byte> dst_contents = value_contents_all_raw (dst); | |
1262 | gdb::array_view<const gdb_byte> src_contents = value_contents_all_raw (src); | |
1263 | copy_bitwise (dst_contents.data (), dst_bit_offset, | |
1264 | src_contents.data (), src_bit_offset, | |
1265 | bit_length, | |
d0c97917 | 1266 | type_byte_order (src->type ()) == BFD_ENDIAN_BIG); |
e379f652 TT |
1267 | |
1268 | /* Copy the meta-data. */ | |
1269 | value_ranges_copy_adjusted (dst, dst_bit_offset, | |
1270 | src, src_bit_offset, | |
1271 | bit_length); | |
1272 | } | |
1273 | ||
29976f3f PA |
1274 | /* Copy LENGTH bytes of SRC value's (all) contents |
1275 | (value_contents_all) starting at SRC_OFFSET byte, into DST value's | |
1276 | (all) contents, starting at DST_OFFSET. If unavailable contents | |
1277 | are being copied from SRC, the corresponding DST contents are | |
1278 | marked unavailable accordingly. DST must not be lazy. If SRC is | |
9a0dc9e3 | 1279 | lazy, it will be fetched now. |
29976f3f PA |
1280 | |
1281 | It is assumed the contents of DST in the [DST_OFFSET, | |
1282 | DST_OFFSET+LENGTH) range are wholly available. */ | |
39d37385 PA |
1283 | |
1284 | void | |
6b850546 DT |
1285 | value_contents_copy (struct value *dst, LONGEST dst_offset, |
1286 | struct value *src, LONGEST src_offset, LONGEST length) | |
39d37385 | 1287 | { |
382d927f | 1288 | if (src->m_lazy) |
39d37385 PA |
1289 | value_fetch_lazy (src); |
1290 | ||
1291 | value_contents_copy_raw (dst, dst_offset, src, src_offset, length); | |
1292 | } | |
1293 | ||
d69fe07e | 1294 | int |
4bf7b526 | 1295 | value_lazy (const struct value *value) |
d69fe07e | 1296 | { |
382d927f | 1297 | return value->m_lazy; |
d69fe07e AC |
1298 | } |
1299 | ||
dfa52d88 AC |
1300 | void |
1301 | set_value_lazy (struct value *value, int val) | |
1302 | { | |
382d927f | 1303 | value->m_lazy = val; |
dfa52d88 AC |
1304 | } |
1305 | ||
4e5d721f | 1306 | int |
4bf7b526 | 1307 | value_stack (const struct value *value) |
4e5d721f | 1308 | { |
382d927f | 1309 | return value->m_stack; |
4e5d721f DE |
1310 | } |
1311 | ||
1312 | void | |
1313 | set_value_stack (struct value *value, int val) | |
1314 | { | |
382d927f | 1315 | value->m_stack = val; |
4e5d721f DE |
1316 | } |
1317 | ||
50888e42 | 1318 | gdb::array_view<const gdb_byte> |
0fd88904 AC |
1319 | value_contents (struct value *value) |
1320 | { | |
50888e42 | 1321 | gdb::array_view<const gdb_byte> result = value_contents_writeable (value); |
0e03807e | 1322 | require_not_optimized_out (value); |
4e07d55f | 1323 | require_available (value); |
0e03807e | 1324 | return result; |
0fd88904 AC |
1325 | } |
1326 | ||
50888e42 | 1327 | gdb::array_view<gdb_byte> |
0fd88904 AC |
1328 | value_contents_writeable (struct value *value) |
1329 | { | |
382d927f | 1330 | if (value->m_lazy) |
0fd88904 | 1331 | value_fetch_lazy (value); |
fc0c53a0 | 1332 | return value_contents_raw (value); |
0fd88904 AC |
1333 | } |
1334 | ||
feb13ab0 AC |
1335 | int |
1336 | value_optimized_out (struct value *value) | |
1337 | { | |
382d927f | 1338 | if (value->m_lazy) |
ecf2e90c | 1339 | { |
a519e8ff TT |
1340 | /* See if we can compute the result without fetching the |
1341 | value. */ | |
1342 | if (VALUE_LVAL (value) == lval_memory) | |
1343 | return false; | |
1344 | else if (VALUE_LVAL (value) == lval_computed) | |
1345 | { | |
382d927f | 1346 | const struct lval_funcs *funcs = value->m_location.computed.funcs; |
a519e8ff TT |
1347 | |
1348 | if (funcs->is_optimized_out != nullptr) | |
1349 | return funcs->is_optimized_out (value); | |
1350 | } | |
1351 | ||
1352 | /* Fall back to fetching. */ | |
a70b8144 | 1353 | try |
ecf2e90c DB |
1354 | { |
1355 | value_fetch_lazy (value); | |
1356 | } | |
230d2906 | 1357 | catch (const gdb_exception_error &ex) |
ecf2e90c | 1358 | { |
6d7aa592 PA |
1359 | switch (ex.error) |
1360 | { | |
1361 | case MEMORY_ERROR: | |
1362 | case OPTIMIZED_OUT_ERROR: | |
1363 | case NOT_AVAILABLE_ERROR: | |
1364 | /* These can normally happen when we try to access an | |
1365 | optimized out or unavailable register, either in a | |
1366 | physical register or spilled to memory. */ | |
1367 | break; | |
1368 | default: | |
1369 | throw; | |
1370 | } | |
ecf2e90c | 1371 | } |
ecf2e90c | 1372 | } |
691a26f5 | 1373 | |
382d927f | 1374 | return !value->m_optimized_out.empty (); |
feb13ab0 AC |
1375 | } |
1376 | ||
9a0dc9e3 PA |
1377 | /* Mark contents of VALUE as optimized out, starting at OFFSET bytes, and |
1378 | the following LENGTH bytes. */ | |
eca07816 | 1379 | |
feb13ab0 | 1380 | void |
9a0dc9e3 | 1381 | mark_value_bytes_optimized_out (struct value *value, int offset, int length) |
feb13ab0 | 1382 | { |
9a0dc9e3 PA |
1383 | mark_value_bits_optimized_out (value, |
1384 | offset * TARGET_CHAR_BIT, | |
1385 | length * TARGET_CHAR_BIT); | |
feb13ab0 | 1386 | } |
13c3b5f5 | 1387 | |
9a0dc9e3 | 1388 | /* See value.h. */ |
0e03807e | 1389 | |
9a0dc9e3 | 1390 | void |
6b850546 DT |
1391 | mark_value_bits_optimized_out (struct value *value, |
1392 | LONGEST offset, LONGEST length) | |
0e03807e | 1393 | { |
382d927f | 1394 | insert_into_bit_range_vector (&value->m_optimized_out, offset, length); |
0e03807e TT |
1395 | } |
1396 | ||
8cf6f0b1 TT |
1397 | int |
1398 | value_bits_synthetic_pointer (const struct value *value, | |
6b850546 | 1399 | LONGEST offset, LONGEST length) |
8cf6f0b1 | 1400 | { |
382d927f TT |
1401 | if (value->m_lval != lval_computed |
1402 | || !value->m_location.computed.funcs->check_synthetic_pointer) | |
8cf6f0b1 | 1403 | return 0; |
382d927f | 1404 | return value->m_location.computed.funcs->check_synthetic_pointer (value, |
8cf6f0b1 TT |
1405 | offset, |
1406 | length); | |
1407 | } | |
1408 | ||
c8f2448a | 1409 | const struct lval_funcs * |
a471c594 | 1410 | value_computed_funcs (const struct value *v) |
5f5233d4 | 1411 | { |
a471c594 | 1412 | gdb_assert (value_lval_const (v) == lval_computed); |
5f5233d4 | 1413 | |
382d927f | 1414 | return v->m_location.computed.funcs; |
5f5233d4 PA |
1415 | } |
1416 | ||
1417 | void * | |
0e03807e | 1418 | value_computed_closure (const struct value *v) |
5f5233d4 | 1419 | { |
382d927f | 1420 | gdb_assert (v->m_lval == lval_computed); |
5f5233d4 | 1421 | |
382d927f | 1422 | return v->m_location.computed.closure; |
5f5233d4 PA |
1423 | } |
1424 | ||
13bb5560 AC |
1425 | enum lval_type * |
1426 | deprecated_value_lval_hack (struct value *value) | |
1427 | { | |
382d927f | 1428 | return &value->m_lval; |
13bb5560 AC |
1429 | } |
1430 | ||
a471c594 JK |
1431 | enum lval_type |
1432 | value_lval_const (const struct value *value) | |
1433 | { | |
382d927f | 1434 | return value->m_lval; |
a471c594 JK |
1435 | } |
1436 | ||
42ae5230 | 1437 | CORE_ADDR |
de4127a3 | 1438 | value_address (const struct value *value) |
42ae5230 | 1439 | { |
382d927f | 1440 | if (value->m_lval != lval_memory) |
42ae5230 | 1441 | return 0; |
382d927f TT |
1442 | if (value->m_parent != NULL) |
1443 | return value_address (value->m_parent.get ()) + value->m_offset; | |
d0c97917 | 1444 | if (NULL != TYPE_DATA_LOCATION (value->type ())) |
9920b434 | 1445 | { |
d0c97917 TT |
1446 | gdb_assert (PROP_CONST == TYPE_DATA_LOCATION_KIND (value->type ())); |
1447 | return TYPE_DATA_LOCATION_ADDR (value->type ()); | |
9920b434 BH |
1448 | } |
1449 | ||
382d927f | 1450 | return value->m_location.address + value->m_offset; |
42ae5230 TT |
1451 | } |
1452 | ||
1453 | CORE_ADDR | |
4bf7b526 | 1454 | value_raw_address (const struct value *value) |
42ae5230 | 1455 | { |
382d927f | 1456 | if (value->m_lval != lval_memory) |
42ae5230 | 1457 | return 0; |
382d927f | 1458 | return value->m_location.address; |
42ae5230 TT |
1459 | } |
1460 | ||
1461 | void | |
1462 | set_value_address (struct value *value, CORE_ADDR addr) | |
13bb5560 | 1463 | { |
382d927f TT |
1464 | gdb_assert (value->m_lval == lval_memory); |
1465 | value->m_location.address = addr; | |
13bb5560 AC |
1466 | } |
1467 | ||
1468 | struct internalvar ** | |
1469 | deprecated_value_internalvar_hack (struct value *value) | |
1470 | { | |
382d927f | 1471 | return &value->m_location.internalvar; |
13bb5560 AC |
1472 | } |
1473 | ||
1474 | struct frame_id * | |
41b56feb | 1475 | deprecated_value_next_frame_id_hack (struct value *value) |
13bb5560 | 1476 | { |
382d927f TT |
1477 | gdb_assert (value->m_lval == lval_register); |
1478 | return &value->m_location.reg.next_frame_id; | |
13bb5560 AC |
1479 | } |
1480 | ||
7dc54575 | 1481 | int * |
13bb5560 AC |
1482 | deprecated_value_regnum_hack (struct value *value) |
1483 | { | |
382d927f TT |
1484 | gdb_assert (value->m_lval == lval_register); |
1485 | return &value->m_location.reg.regnum; | |
13bb5560 | 1486 | } |
88e3b34b | 1487 | |
990a07ab | 1488 | \f |
c906108c SS |
1489 | /* Return a mark in the value chain. All values allocated after the |
1490 | mark is obtained (except for those released) are subject to being freed | |
1491 | if a subsequent value_free_to_mark is passed the mark. */ | |
f23631e4 | 1492 | struct value * |
fba45db2 | 1493 | value_mark (void) |
c906108c | 1494 | { |
062d818d TT |
1495 | if (all_values.empty ()) |
1496 | return nullptr; | |
1497 | return all_values.back ().get (); | |
c906108c SS |
1498 | } |
1499 | ||
bbfa6f00 | 1500 | /* See value.h. */ |
828d3400 | 1501 | |
bbfa6f00 | 1502 | void |
828d3400 DJ |
1503 | value_incref (struct value *val) |
1504 | { | |
382d927f | 1505 | val->m_reference_count++; |
828d3400 DJ |
1506 | } |
1507 | ||
1508 | /* Release a reference to VAL, which was acquired with value_incref. | |
1509 | This function is also called to deallocate values from the value | |
1510 | chain. */ | |
1511 | ||
3e3d7139 | 1512 | void |
22bc8444 | 1513 | value_decref (struct value *val) |
3e3d7139 | 1514 | { |
466ce3ae | 1515 | if (val != nullptr) |
5f5233d4 | 1516 | { |
382d927f TT |
1517 | gdb_assert (val->m_reference_count > 0); |
1518 | val->m_reference_count--; | |
1519 | if (val->m_reference_count == 0) | |
466ce3ae | 1520 | delete val; |
5f5233d4 | 1521 | } |
3e3d7139 JG |
1522 | } |
1523 | ||
c906108c SS |
1524 | /* Free all values allocated since MARK was obtained by value_mark |
1525 | (except for those released). */ | |
1526 | void | |
4bf7b526 | 1527 | value_free_to_mark (const struct value *mark) |
c906108c | 1528 | { |
062d818d TT |
1529 | auto iter = std::find (all_values.begin (), all_values.end (), mark); |
1530 | if (iter == all_values.end ()) | |
1531 | all_values.clear (); | |
1532 | else | |
1533 | all_values.erase (iter + 1, all_values.end ()); | |
c906108c SS |
1534 | } |
1535 | ||
c906108c SS |
1536 | /* Remove VAL from the chain all_values |
1537 | so it will not be freed automatically. */ | |
1538 | ||
22bc8444 | 1539 | value_ref_ptr |
f23631e4 | 1540 | release_value (struct value *val) |
c906108c | 1541 | { |
850645cf TT |
1542 | if (val == nullptr) |
1543 | return value_ref_ptr (); | |
1544 | ||
062d818d TT |
1545 | std::vector<value_ref_ptr>::reverse_iterator iter; |
1546 | for (iter = all_values.rbegin (); iter != all_values.rend (); ++iter) | |
c906108c | 1547 | { |
062d818d | 1548 | if (*iter == val) |
c906108c | 1549 | { |
062d818d TT |
1550 | value_ref_ptr result = *iter; |
1551 | all_values.erase (iter.base () - 1); | |
1552 | return result; | |
c906108c SS |
1553 | } |
1554 | } | |
c906108c | 1555 | |
062d818d TT |
1556 | /* We must always return an owned reference. Normally this happens |
1557 | because we transfer the reference from the value chain, but in | |
1558 | this case the value was not on the chain. */ | |
bbfa6f00 | 1559 | return value_ref_ptr::new_reference (val); |
e848a8a5 TT |
1560 | } |
1561 | ||
a6535de1 TT |
1562 | /* See value.h. */ |
1563 | ||
1564 | std::vector<value_ref_ptr> | |
4bf7b526 | 1565 | value_release_to_mark (const struct value *mark) |
c906108c | 1566 | { |
a6535de1 | 1567 | std::vector<value_ref_ptr> result; |
c906108c | 1568 | |
062d818d TT |
1569 | auto iter = std::find (all_values.begin (), all_values.end (), mark); |
1570 | if (iter == all_values.end ()) | |
1571 | std::swap (result, all_values); | |
1572 | else | |
e848a8a5 | 1573 | { |
062d818d TT |
1574 | std::move (iter + 1, all_values.end (), std::back_inserter (result)); |
1575 | all_values.erase (iter + 1, all_values.end ()); | |
e848a8a5 | 1576 | } |
062d818d | 1577 | std::reverse (result.begin (), result.end ()); |
a6535de1 | 1578 | return result; |
c906108c SS |
1579 | } |
1580 | ||
bae19789 MR |
1581 | /* Return a copy of the value ARG. It contains the same contents, |
1582 | for the same memory address, but it's a different block of storage. */ | |
c906108c | 1583 | |
f23631e4 | 1584 | struct value * |
5f8ab46b | 1585 | value_copy (const value *arg) |
c906108c | 1586 | { |
463b870d | 1587 | struct type *encl_type = arg->enclosing_type (); |
3e3d7139 JG |
1588 | struct value *val; |
1589 | ||
a0c07915 | 1590 | val = allocate_value_lazy (encl_type); |
382d927f TT |
1591 | val->m_type = arg->m_type; |
1592 | VALUE_LVAL (val) = arg->m_lval; | |
1593 | val->m_location = arg->m_location; | |
1594 | val->m_offset = arg->m_offset; | |
1595 | val->m_bitpos = arg->m_bitpos; | |
1596 | val->m_bitsize = arg->m_bitsize; | |
1597 | val->m_lazy = arg->m_lazy; | |
391f8628 | 1598 | val->m_embedded_offset = arg->embedded_offset (); |
382d927f TT |
1599 | val->m_pointed_to_offset = arg->m_pointed_to_offset; |
1600 | val->m_modifiable = arg->m_modifiable; | |
1601 | val->m_stack = arg->m_stack; | |
1602 | val->m_is_zero = arg->m_is_zero; | |
1603 | val->m_in_history = arg->m_in_history; | |
1604 | val->m_initialized = arg->m_initialized; | |
1605 | val->m_unavailable = arg->m_unavailable; | |
1606 | val->m_optimized_out = arg->m_optimized_out; | |
1607 | val->m_parent = arg->m_parent; | |
1608 | val->m_limited_length = arg->m_limited_length; | |
4bce7cda | 1609 | |
a0c07915 AB |
1610 | if (!value_lazy (val) |
1611 | && !(value_entirely_optimized_out (val) | |
1612 | || value_entirely_unavailable (val))) | |
5f8ab46b | 1613 | { |
382d927f | 1614 | ULONGEST length = val->m_limited_length; |
a0c07915 | 1615 | if (length == 0) |
463b870d | 1616 | length = val->enclosing_type ()->length (); |
a0c07915 | 1617 | |
382d927f | 1618 | gdb_assert (arg->m_contents != nullptr); |
5f8ab46b | 1619 | const auto &arg_view |
382d927f | 1620 | = gdb::make_array_view (arg->m_contents.get (), length); |
a0c07915 AB |
1621 | |
1622 | allocate_value_contents (val, false); | |
1623 | gdb::array_view<gdb_byte> val_contents | |
1624 | = value_contents_all_raw (val).slice (0, length); | |
1625 | ||
1626 | copy (arg_view, val_contents); | |
5f8ab46b | 1627 | } |
c906108c | 1628 | |
5f5233d4 PA |
1629 | if (VALUE_LVAL (val) == lval_computed) |
1630 | { | |
382d927f | 1631 | const struct lval_funcs *funcs = val->m_location.computed.funcs; |
5f5233d4 PA |
1632 | |
1633 | if (funcs->copy_closure) | |
382d927f | 1634 | val->m_location.computed.closure = funcs->copy_closure (val); |
5f5233d4 | 1635 | } |
c906108c SS |
1636 | return val; |
1637 | } | |
74bcbdf3 | 1638 | |
4c082a81 SC |
1639 | /* Return a "const" and/or "volatile" qualified version of the value V. |
1640 | If CNST is true, then the returned value will be qualified with | |
1641 | "const". | |
1642 | if VOLTL is true, then the returned value will be qualified with | |
1643 | "volatile". */ | |
1644 | ||
1645 | struct value * | |
1646 | make_cv_value (int cnst, int voltl, struct value *v) | |
1647 | { | |
d0c97917 | 1648 | struct type *val_type = v->type (); |
463b870d | 1649 | struct type *m_enclosing_type = v->enclosing_type (); |
4c082a81 SC |
1650 | struct value *cv_val = value_copy (v); |
1651 | ||
81ae560c | 1652 | cv_val->deprecated_set_type (make_cv_type (cnst, voltl, val_type, NULL)); |
463b870d | 1653 | cv_val->set_enclosing_type (make_cv_type (cnst, voltl, m_enclosing_type, NULL)); |
4c082a81 SC |
1654 | |
1655 | return cv_val; | |
1656 | } | |
1657 | ||
c37f7098 KW |
1658 | /* Return a version of ARG that is non-lvalue. */ |
1659 | ||
1660 | struct value * | |
1661 | value_non_lval (struct value *arg) | |
1662 | { | |
1663 | if (VALUE_LVAL (arg) != not_lval) | |
1664 | { | |
463b870d | 1665 | struct type *enc_type = arg->enclosing_type (); |
c37f7098 KW |
1666 | struct value *val = allocate_value (enc_type); |
1667 | ||
4bce7cda | 1668 | copy (value_contents_all (arg), value_contents_all_raw (val)); |
382d927f | 1669 | val->m_type = arg->m_type; |
391f8628 TT |
1670 | val->set_embedded_offset (arg->embedded_offset ()); |
1671 | val->set_pointed_to_offset (arg->pointed_to_offset ()); | |
c37f7098 KW |
1672 | return val; |
1673 | } | |
1674 | return arg; | |
1675 | } | |
1676 | ||
6c659fc2 SC |
1677 | /* Write contents of V at ADDR and set its lval type to be LVAL_MEMORY. */ |
1678 | ||
1679 | void | |
1680 | value_force_lval (struct value *v, CORE_ADDR addr) | |
1681 | { | |
1682 | gdb_assert (VALUE_LVAL (v) == not_lval); | |
1683 | ||
d0c97917 | 1684 | write_memory (addr, value_contents_raw (v).data (), v->type ()->length ()); |
382d927f TT |
1685 | v->m_lval = lval_memory; |
1686 | v->m_location.address = addr; | |
6c659fc2 SC |
1687 | } |
1688 | ||
74bcbdf3 | 1689 | void |
0e03807e TT |
1690 | set_value_component_location (struct value *component, |
1691 | const struct value *whole) | |
74bcbdf3 | 1692 | { |
9920b434 BH |
1693 | struct type *type; |
1694 | ||
382d927f | 1695 | gdb_assert (whole->m_lval != lval_xcallable); |
e81e7f5e | 1696 | |
382d927f | 1697 | if (whole->m_lval == lval_internalvar) |
74bcbdf3 PA |
1698 | VALUE_LVAL (component) = lval_internalvar_component; |
1699 | else | |
382d927f | 1700 | VALUE_LVAL (component) = whole->m_lval; |
5f5233d4 | 1701 | |
382d927f TT |
1702 | component->m_location = whole->m_location; |
1703 | if (whole->m_lval == lval_computed) | |
5f5233d4 | 1704 | { |
382d927f | 1705 | const struct lval_funcs *funcs = whole->m_location.computed.funcs; |
5f5233d4 PA |
1706 | |
1707 | if (funcs->copy_closure) | |
382d927f | 1708 | component->m_location.computed.closure = funcs->copy_closure (whole); |
5f5233d4 | 1709 | } |
9920b434 | 1710 | |
3c8c6de2 AB |
1711 | /* If the WHOLE value has a dynamically resolved location property then |
1712 | update the address of the COMPONENT. */ | |
d0c97917 | 1713 | type = whole->type (); |
9920b434 BH |
1714 | if (NULL != TYPE_DATA_LOCATION (type) |
1715 | && TYPE_DATA_LOCATION_KIND (type) == PROP_CONST) | |
1716 | set_value_address (component, TYPE_DATA_LOCATION_ADDR (type)); | |
3c8c6de2 AB |
1717 | |
1718 | /* Similarly, if the COMPONENT value has a dynamically resolved location | |
1719 | property then update its address. */ | |
d0c97917 | 1720 | type = component->type (); |
3c8c6de2 AB |
1721 | if (NULL != TYPE_DATA_LOCATION (type) |
1722 | && TYPE_DATA_LOCATION_KIND (type) == PROP_CONST) | |
1723 | { | |
1724 | /* If the COMPONENT has a dynamic location, and is an | |
1725 | lval_internalvar_component, then we change it to a lval_memory. | |
1726 | ||
1727 | Usually a component of an internalvar is created non-lazy, and has | |
1728 | its content immediately copied from the parent internalvar. | |
1729 | However, for components with a dynamic location, the content of | |
1730 | the component is not contained within the parent, but is instead | |
1731 | accessed indirectly. Further, the component will be created as a | |
1732 | lazy value. | |
1733 | ||
1734 | By changing the type of the component to lval_memory we ensure | |
1735 | that value_fetch_lazy can successfully load the component. | |
1736 | ||
1737 | This solution isn't ideal, but a real fix would require values to | |
1738 | carry around both the parent value contents, and the contents of | |
1739 | any dynamic fields within the parent. This is a substantial | |
1740 | change to how values work in GDB. */ | |
1741 | if (VALUE_LVAL (component) == lval_internalvar_component) | |
1742 | { | |
1743 | gdb_assert (value_lazy (component)); | |
1744 | VALUE_LVAL (component) = lval_memory; | |
1745 | } | |
1746 | else | |
1747 | gdb_assert (VALUE_LVAL (component) == lval_memory); | |
1748 | set_value_address (component, TYPE_DATA_LOCATION_ADDR (type)); | |
1749 | } | |
74bcbdf3 PA |
1750 | } |
1751 | ||
c906108c SS |
1752 | /* Access to the value history. */ |
1753 | ||
1754 | /* Record a new value in the value history. | |
eddf0bae | 1755 | Returns the absolute history index of the entry. */ |
c906108c SS |
1756 | |
1757 | int | |
f23631e4 | 1758 | record_latest_value (struct value *val) |
c906108c | 1759 | { |
463b870d | 1760 | struct type *enclosing_type = val->enclosing_type (); |
d0c97917 | 1761 | struct type *type = val->type (); |
a0c07915 | 1762 | |
c906108c SS |
1763 | /* We don't want this value to have anything to do with the inferior anymore. |
1764 | In particular, "set $1 = 50" should not affect the variable from which | |
1765 | the value was taken, and fast watchpoints should be able to assume that | |
1766 | a value on the value history never changes. */ | |
d69fe07e | 1767 | if (value_lazy (val)) |
a0c07915 AB |
1768 | { |
1769 | /* We know that this is a _huge_ array, any attempt to fetch this | |
1770 | is going to cause GDB to throw an error. However, to allow | |
1771 | the array to still be displayed we fetch its contents up to | |
1772 | `max_value_size' and mark anything beyond "unavailable" in | |
1773 | the history. */ | |
1774 | if (type->code () == TYPE_CODE_ARRAY | |
1775 | && type->length () > max_value_size | |
1776 | && array_length_limiting_element_count.has_value () | |
1777 | && enclosing_type == type | |
1778 | && calculate_limited_array_length (type) <= max_value_size) | |
382d927f | 1779 | val->m_limited_length = max_value_size; |
a0c07915 AB |
1780 | |
1781 | value_fetch_lazy (val); | |
1782 | } | |
1783 | ||
382d927f | 1784 | ULONGEST limit = val->m_limited_length; |
a0c07915 AB |
1785 | if (limit != 0) |
1786 | mark_value_bytes_unavailable (val, limit, | |
1787 | enclosing_type->length () - limit); | |
aaab5fce MR |
1788 | |
1789 | /* Mark the value as recorded in the history for the availability check. */ | |
382d927f | 1790 | val->m_in_history = true; |
aaab5fce | 1791 | |
c906108c SS |
1792 | /* We preserve VALUE_LVAL so that the user can find out where it was fetched |
1793 | from. This is a bit dubious, because then *&$1 does not just return $1 | |
1794 | but the current contents of that location. c'est la vie... */ | |
382d927f | 1795 | val->m_modifiable = 0; |
350e1a76 | 1796 | |
4d0266a0 | 1797 | value_history.push_back (release_value (val)); |
a109c7c1 | 1798 | |
4d0266a0 | 1799 | return value_history.size (); |
c906108c SS |
1800 | } |
1801 | ||
1802 | /* Return a copy of the value in the history with sequence number NUM. */ | |
1803 | ||
f23631e4 | 1804 | struct value * |
fba45db2 | 1805 | access_value_history (int num) |
c906108c | 1806 | { |
52f0bd74 | 1807 | int absnum = num; |
c906108c SS |
1808 | |
1809 | if (absnum <= 0) | |
4d0266a0 | 1810 | absnum += value_history.size (); |
c906108c SS |
1811 | |
1812 | if (absnum <= 0) | |
1813 | { | |
1814 | if (num == 0) | |
8a3fe4f8 | 1815 | error (_("The history is empty.")); |
c906108c | 1816 | else if (num == 1) |
8a3fe4f8 | 1817 | error (_("There is only one value in the history.")); |
c906108c | 1818 | else |
8a3fe4f8 | 1819 | error (_("History does not go back to $$%d."), -num); |
c906108c | 1820 | } |
4d0266a0 | 1821 | if (absnum > value_history.size ()) |
8a3fe4f8 | 1822 | error (_("History has not yet reached $%d."), absnum); |
c906108c SS |
1823 | |
1824 | absnum--; | |
1825 | ||
4d0266a0 | 1826 | return value_copy (value_history[absnum].get ()); |
c906108c SS |
1827 | } |
1828 | ||
30a87e90 AB |
1829 | /* See value.h. */ |
1830 | ||
1831 | ULONGEST | |
1832 | value_history_count () | |
1833 | { | |
1834 | return value_history.size (); | |
1835 | } | |
1836 | ||
c906108c | 1837 | static void |
5fed81ff | 1838 | show_values (const char *num_exp, int from_tty) |
c906108c | 1839 | { |
52f0bd74 | 1840 | int i; |
f23631e4 | 1841 | struct value *val; |
c906108c SS |
1842 | static int num = 1; |
1843 | ||
1844 | if (num_exp) | |
1845 | { | |
f132ba9d | 1846 | /* "show values +" should print from the stored position. |
dda83cd7 | 1847 | "show values <exp>" should print around value number <exp>. */ |
c906108c | 1848 | if (num_exp[0] != '+' || num_exp[1] != '\0') |
bb518678 | 1849 | num = parse_and_eval_long (num_exp) - 5; |
c906108c SS |
1850 | } |
1851 | else | |
1852 | { | |
f132ba9d | 1853 | /* "show values" means print the last 10 values. */ |
4d0266a0 | 1854 | num = value_history.size () - 9; |
c906108c SS |
1855 | } |
1856 | ||
1857 | if (num <= 0) | |
1858 | num = 1; | |
1859 | ||
4d0266a0 | 1860 | for (i = num; i < num + 10 && i <= value_history.size (); i++) |
c906108c | 1861 | { |
79a45b7d | 1862 | struct value_print_options opts; |
a109c7c1 | 1863 | |
c906108c | 1864 | val = access_value_history (i); |
6cb06a8c | 1865 | gdb_printf (("$%d = "), i); |
79a45b7d TT |
1866 | get_user_print_options (&opts); |
1867 | value_print (val, gdb_stdout, &opts); | |
6cb06a8c | 1868 | gdb_printf (("\n")); |
c906108c SS |
1869 | } |
1870 | ||
f132ba9d | 1871 | /* The next "show values +" should start after what we just printed. */ |
c906108c SS |
1872 | num += 10; |
1873 | ||
1874 | /* Hitting just return after this command should do the same thing as | |
f132ba9d TJB |
1875 | "show values +". If num_exp is null, this is unnecessary, since |
1876 | "show values +" is not useful after "show values". */ | |
c906108c | 1877 | if (from_tty && num_exp) |
85c4be7c | 1878 | set_repeat_arguments ("+"); |
c906108c SS |
1879 | } |
1880 | \f | |
52059ffd TT |
1881 | enum internalvar_kind |
1882 | { | |
1883 | /* The internal variable is empty. */ | |
1884 | INTERNALVAR_VOID, | |
1885 | ||
1886 | /* The value of the internal variable is provided directly as | |
1887 | a GDB value object. */ | |
1888 | INTERNALVAR_VALUE, | |
1889 | ||
1890 | /* A fresh value is computed via a call-back routine on every | |
1891 | access to the internal variable. */ | |
1892 | INTERNALVAR_MAKE_VALUE, | |
1893 | ||
1894 | /* The internal variable holds a GDB internal convenience function. */ | |
1895 | INTERNALVAR_FUNCTION, | |
1896 | ||
1897 | /* The variable holds an integer value. */ | |
1898 | INTERNALVAR_INTEGER, | |
1899 | ||
1900 | /* The variable holds a GDB-provided string. */ | |
1901 | INTERNALVAR_STRING, | |
1902 | }; | |
1903 | ||
1904 | union internalvar_data | |
1905 | { | |
1906 | /* A value object used with INTERNALVAR_VALUE. */ | |
1907 | struct value *value; | |
1908 | ||
1909 | /* The call-back routine used with INTERNALVAR_MAKE_VALUE. */ | |
1910 | struct | |
1911 | { | |
1912 | /* The functions to call. */ | |
1913 | const struct internalvar_funcs *functions; | |
1914 | ||
1915 | /* The function's user-data. */ | |
1916 | void *data; | |
1917 | } make_value; | |
1918 | ||
1919 | /* The internal function used with INTERNALVAR_FUNCTION. */ | |
1920 | struct | |
1921 | { | |
1922 | struct internal_function *function; | |
1923 | /* True if this is the canonical name for the function. */ | |
1924 | int canonical; | |
1925 | } fn; | |
1926 | ||
1927 | /* An integer value used with INTERNALVAR_INTEGER. */ | |
1928 | struct | |
1929 | { | |
1930 | /* If type is non-NULL, it will be used as the type to generate | |
1931 | a value for this internal variable. If type is NULL, a default | |
1932 | integer type for the architecture is used. */ | |
1933 | struct type *type; | |
1934 | LONGEST val; | |
1935 | } integer; | |
1936 | ||
1937 | /* A string value used with INTERNALVAR_STRING. */ | |
1938 | char *string; | |
1939 | }; | |
1940 | ||
c906108c SS |
1941 | /* Internal variables. These are variables within the debugger |
1942 | that hold values assigned by debugger commands. | |
1943 | The user refers to them with a '$' prefix | |
1944 | that does not appear in the variable names stored internally. */ | |
1945 | ||
4fa62494 UW |
1946 | struct internalvar |
1947 | { | |
1948 | struct internalvar *next; | |
1949 | char *name; | |
4fa62494 | 1950 | |
78267919 UW |
1951 | /* We support various different kinds of content of an internal variable. |
1952 | enum internalvar_kind specifies the kind, and union internalvar_data | |
1953 | provides the data associated with this particular kind. */ | |
1954 | ||
52059ffd | 1955 | enum internalvar_kind kind; |
4fa62494 | 1956 | |
52059ffd | 1957 | union internalvar_data u; |
4fa62494 UW |
1958 | }; |
1959 | ||
c906108c SS |
1960 | static struct internalvar *internalvars; |
1961 | ||
3e43a32a MS |
1962 | /* If the variable does not already exist create it and give it the |
1963 | value given. If no value is given then the default is zero. */ | |
53e5f3cf | 1964 | static void |
0b39b52e | 1965 | init_if_undefined_command (const char* args, int from_tty) |
53e5f3cf | 1966 | { |
413403fc | 1967 | struct internalvar *intvar = nullptr; |
53e5f3cf AS |
1968 | |
1969 | /* Parse the expression - this is taken from set_command(). */ | |
4d01a485 | 1970 | expression_up expr = parse_expression (args); |
53e5f3cf AS |
1971 | |
1972 | /* Validate the expression. | |
1973 | Was the expression an assignment? | |
1974 | Or even an expression at all? */ | |
3dd93bf8 | 1975 | if (expr->first_opcode () != BINOP_ASSIGN) |
53e5f3cf AS |
1976 | error (_("Init-if-undefined requires an assignment expression.")); |
1977 | ||
1eaebe02 TT |
1978 | /* Extract the variable from the parsed expression. */ |
1979 | expr::assign_operation *assign | |
1980 | = dynamic_cast<expr::assign_operation *> (expr->op.get ()); | |
1981 | if (assign != nullptr) | |
413403fc | 1982 | { |
1eaebe02 TT |
1983 | expr::operation *lhs = assign->get_lhs (); |
1984 | expr::internalvar_operation *ivarop | |
1985 | = dynamic_cast<expr::internalvar_operation *> (lhs); | |
1986 | if (ivarop != nullptr) | |
1987 | intvar = ivarop->get_internalvar (); | |
413403fc TT |
1988 | } |
1989 | ||
1990 | if (intvar == nullptr) | |
3e43a32a MS |
1991 | error (_("The first parameter to init-if-undefined " |
1992 | "should be a GDB variable.")); | |
53e5f3cf AS |
1993 | |
1994 | /* Only evaluate the expression if the lvalue is void. | |
85102364 | 1995 | This may still fail if the expression is invalid. */ |
78267919 | 1996 | if (intvar->kind == INTERNALVAR_VOID) |
4d01a485 | 1997 | evaluate_expression (expr.get ()); |
53e5f3cf AS |
1998 | } |
1999 | ||
2000 | ||
c906108c SS |
2001 | /* Look up an internal variable with name NAME. NAME should not |
2002 | normally include a dollar sign. | |
2003 | ||
2004 | If the specified internal variable does not exist, | |
c4a3d09a | 2005 | the return value is NULL. */ |
c906108c SS |
2006 | |
2007 | struct internalvar * | |
bc3b79fd | 2008 | lookup_only_internalvar (const char *name) |
c906108c | 2009 | { |
52f0bd74 | 2010 | struct internalvar *var; |
c906108c SS |
2011 | |
2012 | for (var = internalvars; var; var = var->next) | |
5cb316ef | 2013 | if (strcmp (var->name, name) == 0) |
c906108c SS |
2014 | return var; |
2015 | ||
c4a3d09a MF |
2016 | return NULL; |
2017 | } | |
2018 | ||
eb3ff9a5 PA |
2019 | /* Complete NAME by comparing it to the names of internal |
2020 | variables. */ | |
d55637df | 2021 | |
eb3ff9a5 PA |
2022 | void |
2023 | complete_internalvar (completion_tracker &tracker, const char *name) | |
d55637df | 2024 | { |
d55637df TT |
2025 | struct internalvar *var; |
2026 | int len; | |
2027 | ||
2028 | len = strlen (name); | |
2029 | ||
2030 | for (var = internalvars; var; var = var->next) | |
2031 | if (strncmp (var->name, name, len) == 0) | |
b02f78f9 | 2032 | tracker.add_completion (make_unique_xstrdup (var->name)); |
d55637df | 2033 | } |
c4a3d09a MF |
2034 | |
2035 | /* Create an internal variable with name NAME and with a void value. | |
2036 | NAME should not normally include a dollar sign. */ | |
2037 | ||
2038 | struct internalvar * | |
bc3b79fd | 2039 | create_internalvar (const char *name) |
c4a3d09a | 2040 | { |
8d749320 | 2041 | struct internalvar *var = XNEW (struct internalvar); |
a109c7c1 | 2042 | |
395f9c91 | 2043 | var->name = xstrdup (name); |
78267919 | 2044 | var->kind = INTERNALVAR_VOID; |
c906108c SS |
2045 | var->next = internalvars; |
2046 | internalvars = var; | |
2047 | return var; | |
2048 | } | |
2049 | ||
4aa995e1 PA |
2050 | /* Create an internal variable with name NAME and register FUN as the |
2051 | function that value_of_internalvar uses to create a value whenever | |
2052 | this variable is referenced. NAME should not normally include a | |
22d2b532 SDJ |
2053 | dollar sign. DATA is passed uninterpreted to FUN when it is |
2054 | called. CLEANUP, if not NULL, is called when the internal variable | |
2055 | is destroyed. It is passed DATA as its only argument. */ | |
4aa995e1 PA |
2056 | |
2057 | struct internalvar * | |
22d2b532 SDJ |
2058 | create_internalvar_type_lazy (const char *name, |
2059 | const struct internalvar_funcs *funcs, | |
2060 | void *data) | |
4aa995e1 | 2061 | { |
4fa62494 | 2062 | struct internalvar *var = create_internalvar (name); |
a109c7c1 | 2063 | |
78267919 | 2064 | var->kind = INTERNALVAR_MAKE_VALUE; |
22d2b532 SDJ |
2065 | var->u.make_value.functions = funcs; |
2066 | var->u.make_value.data = data; | |
4aa995e1 PA |
2067 | return var; |
2068 | } | |
c4a3d09a | 2069 | |
22d2b532 SDJ |
2070 | /* See documentation in value.h. */ |
2071 | ||
2072 | int | |
2073 | compile_internalvar_to_ax (struct internalvar *var, | |
2074 | struct agent_expr *expr, | |
2075 | struct axs_value *value) | |
2076 | { | |
2077 | if (var->kind != INTERNALVAR_MAKE_VALUE | |
2078 | || var->u.make_value.functions->compile_to_ax == NULL) | |
2079 | return 0; | |
2080 | ||
2081 | var->u.make_value.functions->compile_to_ax (var, expr, value, | |
2082 | var->u.make_value.data); | |
2083 | return 1; | |
2084 | } | |
2085 | ||
c4a3d09a MF |
2086 | /* Look up an internal variable with name NAME. NAME should not |
2087 | normally include a dollar sign. | |
2088 | ||
2089 | If the specified internal variable does not exist, | |
2090 | one is created, with a void value. */ | |
2091 | ||
2092 | struct internalvar * | |
bc3b79fd | 2093 | lookup_internalvar (const char *name) |
c4a3d09a MF |
2094 | { |
2095 | struct internalvar *var; | |
2096 | ||
2097 | var = lookup_only_internalvar (name); | |
2098 | if (var) | |
2099 | return var; | |
2100 | ||
2101 | return create_internalvar (name); | |
2102 | } | |
2103 | ||
78267919 UW |
2104 | /* Return current value of internal variable VAR. For variables that |
2105 | are not inherently typed, use a value type appropriate for GDBARCH. */ | |
2106 | ||
f23631e4 | 2107 | struct value * |
78267919 | 2108 | value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var) |
c906108c | 2109 | { |
f23631e4 | 2110 | struct value *val; |
0914bcdb SS |
2111 | struct trace_state_variable *tsv; |
2112 | ||
2113 | /* If there is a trace state variable of the same name, assume that | |
2114 | is what we really want to see. */ | |
2115 | tsv = find_trace_state_variable (var->name); | |
2116 | if (tsv) | |
2117 | { | |
2118 | tsv->value_known = target_get_trace_state_variable_value (tsv->number, | |
2119 | &(tsv->value)); | |
2120 | if (tsv->value_known) | |
2121 | val = value_from_longest (builtin_type (gdbarch)->builtin_int64, | |
2122 | tsv->value); | |
2123 | else | |
2124 | val = allocate_value (builtin_type (gdbarch)->builtin_void); | |
2125 | return val; | |
2126 | } | |
c906108c | 2127 | |
78267919 | 2128 | switch (var->kind) |
5f5233d4 | 2129 | { |
78267919 UW |
2130 | case INTERNALVAR_VOID: |
2131 | val = allocate_value (builtin_type (gdbarch)->builtin_void); | |
2132 | break; | |
4fa62494 | 2133 | |
78267919 UW |
2134 | case INTERNALVAR_FUNCTION: |
2135 | val = allocate_value (builtin_type (gdbarch)->internal_fn); | |
2136 | break; | |
4fa62494 | 2137 | |
cab0c772 UW |
2138 | case INTERNALVAR_INTEGER: |
2139 | if (!var->u.integer.type) | |
78267919 | 2140 | val = value_from_longest (builtin_type (gdbarch)->builtin_int, |
cab0c772 | 2141 | var->u.integer.val); |
78267919 | 2142 | else |
cab0c772 UW |
2143 | val = value_from_longest (var->u.integer.type, var->u.integer.val); |
2144 | break; | |
2145 | ||
78267919 UW |
2146 | case INTERNALVAR_STRING: |
2147 | val = value_cstring (var->u.string, strlen (var->u.string), | |
2148 | builtin_type (gdbarch)->builtin_char); | |
2149 | break; | |
4fa62494 | 2150 | |
78267919 UW |
2151 | case INTERNALVAR_VALUE: |
2152 | val = value_copy (var->u.value); | |
4aa995e1 PA |
2153 | if (value_lazy (val)) |
2154 | value_fetch_lazy (val); | |
78267919 | 2155 | break; |
4aa995e1 | 2156 | |
78267919 | 2157 | case INTERNALVAR_MAKE_VALUE: |
22d2b532 SDJ |
2158 | val = (*var->u.make_value.functions->make_value) (gdbarch, var, |
2159 | var->u.make_value.data); | |
78267919 UW |
2160 | break; |
2161 | ||
2162 | default: | |
f34652de | 2163 | internal_error (_("bad kind")); |
78267919 UW |
2164 | } |
2165 | ||
2166 | /* Change the VALUE_LVAL to lval_internalvar so that future operations | |
2167 | on this value go back to affect the original internal variable. | |
2168 | ||
2169 | Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have | |
30baf67b | 2170 | no underlying modifiable state in the internal variable. |
78267919 UW |
2171 | |
2172 | Likewise, if the variable's value is a computed lvalue, we want | |
2173 | references to it to produce another computed lvalue, where | |
2174 | references and assignments actually operate through the | |
2175 | computed value's functions. | |
2176 | ||
2177 | This means that internal variables with computed values | |
2178 | behave a little differently from other internal variables: | |
2179 | assignments to them don't just replace the previous value | |
2180 | altogether. At the moment, this seems like the behavior we | |
2181 | want. */ | |
2182 | ||
2183 | if (var->kind != INTERNALVAR_MAKE_VALUE | |
382d927f | 2184 | && val->m_lval != lval_computed) |
78267919 UW |
2185 | { |
2186 | VALUE_LVAL (val) = lval_internalvar; | |
2187 | VALUE_INTERNALVAR (val) = var; | |
5f5233d4 | 2188 | } |
d3c139e9 | 2189 | |
4fa62494 UW |
2190 | return val; |
2191 | } | |
d3c139e9 | 2192 | |
4fa62494 UW |
2193 | int |
2194 | get_internalvar_integer (struct internalvar *var, LONGEST *result) | |
2195 | { | |
3158c6ed | 2196 | if (var->kind == INTERNALVAR_INTEGER) |
4fa62494 | 2197 | { |
cab0c772 UW |
2198 | *result = var->u.integer.val; |
2199 | return 1; | |
3158c6ed | 2200 | } |
d3c139e9 | 2201 | |
3158c6ed PA |
2202 | if (var->kind == INTERNALVAR_VALUE) |
2203 | { | |
d0c97917 | 2204 | struct type *type = check_typedef (var->u.value->type ()); |
3158c6ed | 2205 | |
78134374 | 2206 | if (type->code () == TYPE_CODE_INT) |
3158c6ed PA |
2207 | { |
2208 | *result = value_as_long (var->u.value); | |
2209 | return 1; | |
2210 | } | |
4fa62494 | 2211 | } |
3158c6ed PA |
2212 | |
2213 | return 0; | |
4fa62494 | 2214 | } |
d3c139e9 | 2215 | |
4fa62494 UW |
2216 | static int |
2217 | get_internalvar_function (struct internalvar *var, | |
2218 | struct internal_function **result) | |
2219 | { | |
78267919 | 2220 | switch (var->kind) |
d3c139e9 | 2221 | { |
78267919 UW |
2222 | case INTERNALVAR_FUNCTION: |
2223 | *result = var->u.fn.function; | |
4fa62494 | 2224 | return 1; |
d3c139e9 | 2225 | |
4fa62494 UW |
2226 | default: |
2227 | return 0; | |
2228 | } | |
c906108c SS |
2229 | } |
2230 | ||
2231 | void | |
6b850546 DT |
2232 | set_internalvar_component (struct internalvar *var, |
2233 | LONGEST offset, LONGEST bitpos, | |
2234 | LONGEST bitsize, struct value *newval) | |
c906108c | 2235 | { |
4fa62494 | 2236 | gdb_byte *addr; |
3ae385af SM |
2237 | struct gdbarch *arch; |
2238 | int unit_size; | |
c906108c | 2239 | |
78267919 | 2240 | switch (var->kind) |
4fa62494 | 2241 | { |
78267919 | 2242 | case INTERNALVAR_VALUE: |
50888e42 | 2243 | addr = value_contents_writeable (var->u.value).data (); |
f9ee742c | 2244 | arch = var->u.value->arch (); |
3ae385af | 2245 | unit_size = gdbarch_addressable_memory_unit_size (arch); |
4fa62494 UW |
2246 | |
2247 | if (bitsize) | |
d0c97917 | 2248 | modify_field (var->u.value->type (), addr + offset, |
4fa62494 UW |
2249 | value_as_long (newval), bitpos, bitsize); |
2250 | else | |
50888e42 | 2251 | memcpy (addr + offset * unit_size, value_contents (newval).data (), |
d0c97917 | 2252 | newval->type ()->length ()); |
4fa62494 | 2253 | break; |
78267919 UW |
2254 | |
2255 | default: | |
2256 | /* We can never get a component of any other kind. */ | |
f34652de | 2257 | internal_error (_("set_internalvar_component")); |
4fa62494 | 2258 | } |
c906108c SS |
2259 | } |
2260 | ||
2261 | void | |
f23631e4 | 2262 | set_internalvar (struct internalvar *var, struct value *val) |
c906108c | 2263 | { |
78267919 | 2264 | enum internalvar_kind new_kind; |
4fa62494 | 2265 | union internalvar_data new_data = { 0 }; |
c906108c | 2266 | |
78267919 | 2267 | if (var->kind == INTERNALVAR_FUNCTION && var->u.fn.canonical) |
bc3b79fd TJB |
2268 | error (_("Cannot overwrite convenience function %s"), var->name); |
2269 | ||
4fa62494 | 2270 | /* Prepare new contents. */ |
d0c97917 | 2271 | switch (check_typedef (val->type ())->code ()) |
4fa62494 UW |
2272 | { |
2273 | case TYPE_CODE_VOID: | |
78267919 | 2274 | new_kind = INTERNALVAR_VOID; |
4fa62494 UW |
2275 | break; |
2276 | ||
2277 | case TYPE_CODE_INTERNAL_FUNCTION: | |
2278 | gdb_assert (VALUE_LVAL (val) == lval_internalvar); | |
78267919 UW |
2279 | new_kind = INTERNALVAR_FUNCTION; |
2280 | get_internalvar_function (VALUE_INTERNALVAR (val), | |
2281 | &new_data.fn.function); | |
2282 | /* Copies created here are never canonical. */ | |
4fa62494 UW |
2283 | break; |
2284 | ||
4fa62494 | 2285 | default: |
78267919 | 2286 | new_kind = INTERNALVAR_VALUE; |
895dafa6 | 2287 | struct value *copy = value_copy (val); |
382d927f | 2288 | copy->m_modifiable = 1; |
4fa62494 UW |
2289 | |
2290 | /* Force the value to be fetched from the target now, to avoid problems | |
2291 | later when this internalvar is referenced and the target is gone or | |
2292 | has changed. */ | |
895dafa6 TT |
2293 | if (value_lazy (copy)) |
2294 | value_fetch_lazy (copy); | |
4fa62494 UW |
2295 | |
2296 | /* Release the value from the value chain to prevent it from being | |
2297 | deleted by free_all_values. From here on this function should not | |
2298 | call error () until new_data is installed into the var->u to avoid | |
2299 | leaking memory. */ | |
895dafa6 | 2300 | new_data.value = release_value (copy).release (); |
9920b434 BH |
2301 | |
2302 | /* Internal variables which are created from values with a dynamic | |
dda83cd7 SM |
2303 | location don't need the location property of the origin anymore. |
2304 | The resolved dynamic location is used prior then any other address | |
2305 | when accessing the value. | |
2306 | If we keep it, we would still refer to the origin value. | |
2307 | Remove the location property in case it exist. */ | |
d0c97917 | 2308 | new_data.value->type ()->remove_dyn_prop (DYN_PROP_DATA_LOCATION); |
9920b434 | 2309 | |
4fa62494 UW |
2310 | break; |
2311 | } | |
2312 | ||
2313 | /* Clean up old contents. */ | |
2314 | clear_internalvar (var); | |
2315 | ||
2316 | /* Switch over. */ | |
78267919 | 2317 | var->kind = new_kind; |
4fa62494 | 2318 | var->u = new_data; |
c906108c SS |
2319 | /* End code which must not call error(). */ |
2320 | } | |
2321 | ||
4fa62494 UW |
2322 | void |
2323 | set_internalvar_integer (struct internalvar *var, LONGEST l) | |
2324 | { | |
2325 | /* Clean up old contents. */ | |
2326 | clear_internalvar (var); | |
2327 | ||
cab0c772 UW |
2328 | var->kind = INTERNALVAR_INTEGER; |
2329 | var->u.integer.type = NULL; | |
2330 | var->u.integer.val = l; | |
78267919 UW |
2331 | } |
2332 | ||
2333 | void | |
2334 | set_internalvar_string (struct internalvar *var, const char *string) | |
2335 | { | |
2336 | /* Clean up old contents. */ | |
2337 | clear_internalvar (var); | |
2338 | ||
2339 | var->kind = INTERNALVAR_STRING; | |
2340 | var->u.string = xstrdup (string); | |
4fa62494 UW |
2341 | } |
2342 | ||
2343 | static void | |
2344 | set_internalvar_function (struct internalvar *var, struct internal_function *f) | |
2345 | { | |
2346 | /* Clean up old contents. */ | |
2347 | clear_internalvar (var); | |
2348 | ||
78267919 UW |
2349 | var->kind = INTERNALVAR_FUNCTION; |
2350 | var->u.fn.function = f; | |
2351 | var->u.fn.canonical = 1; | |
2352 | /* Variables installed here are always the canonical version. */ | |
4fa62494 UW |
2353 | } |
2354 | ||
2355 | void | |
2356 | clear_internalvar (struct internalvar *var) | |
2357 | { | |
2358 | /* Clean up old contents. */ | |
78267919 | 2359 | switch (var->kind) |
4fa62494 | 2360 | { |
78267919 | 2361 | case INTERNALVAR_VALUE: |
22bc8444 | 2362 | value_decref (var->u.value); |
78267919 UW |
2363 | break; |
2364 | ||
2365 | case INTERNALVAR_STRING: | |
2366 | xfree (var->u.string); | |
4fa62494 UW |
2367 | break; |
2368 | ||
2369 | default: | |
4fa62494 UW |
2370 | break; |
2371 | } | |
2372 | ||
78267919 UW |
2373 | /* Reset to void kind. */ |
2374 | var->kind = INTERNALVAR_VOID; | |
4fa62494 UW |
2375 | } |
2376 | ||
baf20f76 | 2377 | const char * |
4bf7b526 | 2378 | internalvar_name (const struct internalvar *var) |
c906108c SS |
2379 | { |
2380 | return var->name; | |
2381 | } | |
2382 | ||
4fa62494 UW |
2383 | static struct internal_function * |
2384 | create_internal_function (const char *name, | |
2385 | internal_function_fn handler, void *cookie) | |
bc3b79fd | 2386 | { |
bc3b79fd | 2387 | struct internal_function *ifn = XNEW (struct internal_function); |
a109c7c1 | 2388 | |
bc3b79fd TJB |
2389 | ifn->name = xstrdup (name); |
2390 | ifn->handler = handler; | |
2391 | ifn->cookie = cookie; | |
4fa62494 | 2392 | return ifn; |
bc3b79fd TJB |
2393 | } |
2394 | ||
91f87213 | 2395 | const char * |
bc3b79fd TJB |
2396 | value_internal_function_name (struct value *val) |
2397 | { | |
4fa62494 UW |
2398 | struct internal_function *ifn; |
2399 | int result; | |
2400 | ||
2401 | gdb_assert (VALUE_LVAL (val) == lval_internalvar); | |
2402 | result = get_internalvar_function (VALUE_INTERNALVAR (val), &ifn); | |
2403 | gdb_assert (result); | |
2404 | ||
bc3b79fd TJB |
2405 | return ifn->name; |
2406 | } | |
2407 | ||
2408 | struct value * | |
d452c4bc UW |
2409 | call_internal_function (struct gdbarch *gdbarch, |
2410 | const struct language_defn *language, | |
2411 | struct value *func, int argc, struct value **argv) | |
bc3b79fd | 2412 | { |
4fa62494 UW |
2413 | struct internal_function *ifn; |
2414 | int result; | |
2415 | ||
2416 | gdb_assert (VALUE_LVAL (func) == lval_internalvar); | |
2417 | result = get_internalvar_function (VALUE_INTERNALVAR (func), &ifn); | |
2418 | gdb_assert (result); | |
2419 | ||
d452c4bc | 2420 | return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv); |
bc3b79fd TJB |
2421 | } |
2422 | ||
2423 | /* The 'function' command. This does nothing -- it is just a | |
2424 | placeholder to let "help function NAME" work. This is also used as | |
2425 | the implementation of the sub-command that is created when | |
2426 | registering an internal function. */ | |
2427 | static void | |
981a3fb3 | 2428 | function_command (const char *command, int from_tty) |
bc3b79fd TJB |
2429 | { |
2430 | /* Do nothing. */ | |
2431 | } | |
2432 | ||
1a6d41c6 TT |
2433 | /* Helper function that does the work for add_internal_function. */ |
2434 | ||
2435 | static struct cmd_list_element * | |
2436 | do_add_internal_function (const char *name, const char *doc, | |
2437 | internal_function_fn handler, void *cookie) | |
bc3b79fd | 2438 | { |
4fa62494 | 2439 | struct internal_function *ifn; |
bc3b79fd | 2440 | struct internalvar *var = lookup_internalvar (name); |
4fa62494 UW |
2441 | |
2442 | ifn = create_internal_function (name, handler, cookie); | |
2443 | set_internalvar_function (var, ifn); | |
bc3b79fd | 2444 | |
3ea16160 | 2445 | return add_cmd (name, no_class, function_command, doc, &functionlist); |
1a6d41c6 TT |
2446 | } |
2447 | ||
2448 | /* See value.h. */ | |
2449 | ||
2450 | void | |
2451 | add_internal_function (const char *name, const char *doc, | |
2452 | internal_function_fn handler, void *cookie) | |
2453 | { | |
2454 | do_add_internal_function (name, doc, handler, cookie); | |
2455 | } | |
2456 | ||
2457 | /* See value.h. */ | |
2458 | ||
2459 | void | |
3ea16160 TT |
2460 | add_internal_function (gdb::unique_xmalloc_ptr<char> &&name, |
2461 | gdb::unique_xmalloc_ptr<char> &&doc, | |
1a6d41c6 TT |
2462 | internal_function_fn handler, void *cookie) |
2463 | { | |
2464 | struct cmd_list_element *cmd | |
3ea16160 | 2465 | = do_add_internal_function (name.get (), doc.get (), handler, cookie); |
1a6d41c6 TT |
2466 | doc.release (); |
2467 | cmd->doc_allocated = 1; | |
3ea16160 TT |
2468 | name.release (); |
2469 | cmd->name_allocated = 1; | |
bc3b79fd TJB |
2470 | } |
2471 | ||
ae5a43e0 DJ |
2472 | /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to |
2473 | prevent cycles / duplicates. */ | |
2474 | ||
4e7a5ef5 | 2475 | void |
ae5a43e0 DJ |
2476 | preserve_one_value (struct value *value, struct objfile *objfile, |
2477 | htab_t copied_types) | |
2478 | { | |
382d927f TT |
2479 | if (value->m_type->objfile_owner () == objfile) |
2480 | value->m_type = copy_type_recursive (value->m_type, copied_types); | |
ae5a43e0 | 2481 | |
382d927f TT |
2482 | if (value->m_enclosing_type->objfile_owner () == objfile) |
2483 | value->m_enclosing_type = copy_type_recursive (value->m_enclosing_type, | |
ae5a43e0 DJ |
2484 | copied_types); |
2485 | } | |
2486 | ||
78267919 UW |
2487 | /* Likewise for internal variable VAR. */ |
2488 | ||
2489 | static void | |
2490 | preserve_one_internalvar (struct internalvar *var, struct objfile *objfile, | |
2491 | htab_t copied_types) | |
2492 | { | |
2493 | switch (var->kind) | |
2494 | { | |
cab0c772 | 2495 | case INTERNALVAR_INTEGER: |
6ac37371 SM |
2496 | if (var->u.integer.type |
2497 | && var->u.integer.type->objfile_owner () == objfile) | |
cab0c772 | 2498 | var->u.integer.type |
bde539c2 | 2499 | = copy_type_recursive (var->u.integer.type, copied_types); |
cab0c772 UW |
2500 | break; |
2501 | ||
78267919 UW |
2502 | case INTERNALVAR_VALUE: |
2503 | preserve_one_value (var->u.value, objfile, copied_types); | |
2504 | break; | |
2505 | } | |
2506 | } | |
2507 | ||
bc20e562 LS |
2508 | /* Make sure that all types and values referenced by VAROBJ are updated before |
2509 | OBJFILE is discarded. COPIED_TYPES is used to prevent cycles and | |
2510 | duplicates. */ | |
2511 | ||
2512 | static void | |
2513 | preserve_one_varobj (struct varobj *varobj, struct objfile *objfile, | |
2514 | htab_t copied_types) | |
2515 | { | |
2516 | if (varobj->type->is_objfile_owned () | |
2517 | && varobj->type->objfile_owner () == objfile) | |
2518 | { | |
2519 | varobj->type | |
bde539c2 | 2520 | = copy_type_recursive (varobj->type, copied_types); |
bc20e562 LS |
2521 | } |
2522 | ||
2523 | if (varobj->value != nullptr) | |
2524 | preserve_one_value (varobj->value.get (), objfile, copied_types); | |
2525 | } | |
2526 | ||
ae5a43e0 DJ |
2527 | /* Update the internal variables and value history when OBJFILE is |
2528 | discarded; we must copy the types out of the objfile. New global types | |
2529 | will be created for every convenience variable which currently points to | |
2530 | this objfile's types, and the convenience variables will be adjusted to | |
2531 | use the new global types. */ | |
c906108c SS |
2532 | |
2533 | void | |
ae5a43e0 | 2534 | preserve_values (struct objfile *objfile) |
c906108c | 2535 | { |
52f0bd74 | 2536 | struct internalvar *var; |
c906108c | 2537 | |
ae5a43e0 DJ |
2538 | /* Create the hash table. We allocate on the objfile's obstack, since |
2539 | it is soon to be deleted. */ | |
bde539c2 | 2540 | htab_up copied_types = create_copied_types_hash (); |
ae5a43e0 | 2541 | |
4d0266a0 | 2542 | for (const value_ref_ptr &item : value_history) |
6108fd18 | 2543 | preserve_one_value (item.get (), objfile, copied_types.get ()); |
ae5a43e0 DJ |
2544 | |
2545 | for (var = internalvars; var; var = var->next) | |
6108fd18 | 2546 | preserve_one_internalvar (var, objfile, copied_types.get ()); |
ae5a43e0 | 2547 | |
bc20e562 LS |
2548 | /* For the remaining varobj, check that none has type owned by OBJFILE. */ |
2549 | all_root_varobjs ([&copied_types, objfile] (struct varobj *varobj) | |
2550 | { | |
2551 | preserve_one_varobj (varobj, objfile, | |
2552 | copied_types.get ()); | |
2553 | }); | |
2554 | ||
6108fd18 | 2555 | preserve_ext_lang_values (objfile, copied_types.get ()); |
c906108c SS |
2556 | } |
2557 | ||
2558 | static void | |
ad25e423 | 2559 | show_convenience (const char *ignore, int from_tty) |
c906108c | 2560 | { |
e17c207e | 2561 | struct gdbarch *gdbarch = get_current_arch (); |
52f0bd74 | 2562 | struct internalvar *var; |
c906108c | 2563 | int varseen = 0; |
79a45b7d | 2564 | struct value_print_options opts; |
c906108c | 2565 | |
79a45b7d | 2566 | get_user_print_options (&opts); |
c906108c SS |
2567 | for (var = internalvars; var; var = var->next) |
2568 | { | |
c709acd1 | 2569 | |
c906108c SS |
2570 | if (!varseen) |
2571 | { | |
2572 | varseen = 1; | |
2573 | } | |
6cb06a8c | 2574 | gdb_printf (("$%s = "), var->name); |
c709acd1 | 2575 | |
a70b8144 | 2576 | try |
c709acd1 PA |
2577 | { |
2578 | struct value *val; | |
2579 | ||
2580 | val = value_of_internalvar (gdbarch, var); | |
2581 | value_print (val, gdb_stdout, &opts); | |
2582 | } | |
230d2906 | 2583 | catch (const gdb_exception_error &ex) |
492d29ea | 2584 | { |
7f6aba03 TT |
2585 | fprintf_styled (gdb_stdout, metadata_style.style (), |
2586 | _("<error: %s>"), ex.what ()); | |
492d29ea | 2587 | } |
492d29ea | 2588 | |
6cb06a8c | 2589 | gdb_printf (("\n")); |
c906108c SS |
2590 | } |
2591 | if (!varseen) | |
f47f77df DE |
2592 | { |
2593 | /* This text does not mention convenience functions on purpose. | |
2594 | The user can't create them except via Python, and if Python support | |
2595 | is installed this message will never be printed ($_streq will | |
2596 | exist). */ | |
6cb06a8c TT |
2597 | gdb_printf (_("No debugger convenience variables now defined.\n" |
2598 | "Convenience variables have " | |
2599 | "names starting with \"$\";\n" | |
2600 | "use \"set\" as in \"set " | |
2601 | "$foo = 5\" to define them.\n")); | |
f47f77df | 2602 | } |
c906108c SS |
2603 | } |
2604 | \f | |
ba18742c SM |
2605 | |
2606 | /* See value.h. */ | |
e81e7f5e SC |
2607 | |
2608 | struct value * | |
ba18742c | 2609 | value_from_xmethod (xmethod_worker_up &&worker) |
e81e7f5e | 2610 | { |
ba18742c | 2611 | struct value *v; |
e81e7f5e | 2612 | |
ba18742c | 2613 | v = allocate_value (builtin_type (target_gdbarch ())->xmethod); |
382d927f TT |
2614 | v->m_lval = lval_xcallable; |
2615 | v->m_location.xm_worker = worker.release (); | |
2616 | v->m_modifiable = 0; | |
e81e7f5e | 2617 | |
ba18742c | 2618 | return v; |
e81e7f5e SC |
2619 | } |
2620 | ||
2ce1cdbf DE |
2621 | /* Return the type of the result of TYPE_CODE_XMETHOD value METHOD. */ |
2622 | ||
2623 | struct type * | |
6b1747cd | 2624 | result_type_of_xmethod (struct value *method, gdb::array_view<value *> argv) |
2ce1cdbf | 2625 | { |
d0c97917 | 2626 | gdb_assert (method->type ()->code () == TYPE_CODE_XMETHOD |
382d927f | 2627 | && method->m_lval == lval_xcallable && !argv.empty ()); |
2ce1cdbf | 2628 | |
382d927f | 2629 | return method->m_location.xm_worker->get_result_type (argv[0], argv.slice (1)); |
2ce1cdbf DE |
2630 | } |
2631 | ||
e81e7f5e SC |
2632 | /* Call the xmethod corresponding to the TYPE_CODE_XMETHOD value METHOD. */ |
2633 | ||
2634 | struct value * | |
6b1747cd | 2635 | call_xmethod (struct value *method, gdb::array_view<value *> argv) |
e81e7f5e | 2636 | { |
d0c97917 | 2637 | gdb_assert (method->type ()->code () == TYPE_CODE_XMETHOD |
382d927f | 2638 | && method->m_lval == lval_xcallable && !argv.empty ()); |
e81e7f5e | 2639 | |
382d927f | 2640 | return method->m_location.xm_worker->invoke (argv[0], argv.slice (1)); |
e81e7f5e SC |
2641 | } |
2642 | \f | |
c906108c SS |
2643 | /* Extract a value as a C number (either long or double). |
2644 | Knows how to convert fixed values to double, or | |
2645 | floating values to long. | |
2646 | Does not deallocate the value. */ | |
2647 | ||
2648 | LONGEST | |
f23631e4 | 2649 | value_as_long (struct value *val) |
c906108c SS |
2650 | { |
2651 | /* This coerces arrays and functions, which is necessary (e.g. | |
2652 | in disassemble_command). It also dereferences references, which | |
2653 | I suspect is the most logical thing to do. */ | |
994b9211 | 2654 | val = coerce_array (val); |
d0c97917 | 2655 | return unpack_long (val->type (), value_contents (val).data ()); |
c906108c SS |
2656 | } |
2657 | ||
581e13c1 | 2658 | /* Extract a value as a C pointer. Does not deallocate the value. |
4478b372 JB |
2659 | Note that val's type may not actually be a pointer; value_as_long |
2660 | handles all the cases. */ | |
c906108c | 2661 | CORE_ADDR |
f23631e4 | 2662 | value_as_address (struct value *val) |
c906108c | 2663 | { |
d0c97917 | 2664 | struct gdbarch *gdbarch = val->type ()->arch (); |
50810684 | 2665 | |
c906108c SS |
2666 | /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure |
2667 | whether we want this to be true eventually. */ | |
2668 | #if 0 | |
bf6ae464 | 2669 | /* gdbarch_addr_bits_remove is wrong if we are being called for a |
c906108c SS |
2670 | non-address (e.g. argument to "signal", "info break", etc.), or |
2671 | for pointers to char, in which the low bits *are* significant. */ | |
50810684 | 2672 | return gdbarch_addr_bits_remove (gdbarch, value_as_long (val)); |
c906108c | 2673 | #else |
f312f057 JB |
2674 | |
2675 | /* There are several targets (IA-64, PowerPC, and others) which | |
2676 | don't represent pointers to functions as simply the address of | |
2677 | the function's entry point. For example, on the IA-64, a | |
2678 | function pointer points to a two-word descriptor, generated by | |
2679 | the linker, which contains the function's entry point, and the | |
2680 | value the IA-64 "global pointer" register should have --- to | |
2681 | support position-independent code. The linker generates | |
2682 | descriptors only for those functions whose addresses are taken. | |
2683 | ||
2684 | On such targets, it's difficult for GDB to convert an arbitrary | |
2685 | function address into a function pointer; it has to either find | |
2686 | an existing descriptor for that function, or call malloc and | |
2687 | build its own. On some targets, it is impossible for GDB to | |
2688 | build a descriptor at all: the descriptor must contain a jump | |
2689 | instruction; data memory cannot be executed; and code memory | |
2690 | cannot be modified. | |
2691 | ||
2692 | Upon entry to this function, if VAL is a value of type `function' | |
d0c97917 | 2693 | (that is, TYPE_CODE (val->type ()) == TYPE_CODE_FUNC), then |
42ae5230 | 2694 | value_address (val) is the address of the function. This is what |
f312f057 JB |
2695 | you'll get if you evaluate an expression like `main'. The call |
2696 | to COERCE_ARRAY below actually does all the usual unary | |
2697 | conversions, which includes converting values of type `function' | |
2698 | to `pointer to function'. This is the challenging conversion | |
2699 | discussed above. Then, `unpack_long' will convert that pointer | |
2700 | back into an address. | |
2701 | ||
2702 | So, suppose the user types `disassemble foo' on an architecture | |
2703 | with a strange function pointer representation, on which GDB | |
2704 | cannot build its own descriptors, and suppose further that `foo' | |
2705 | has no linker-built descriptor. The address->pointer conversion | |
2706 | will signal an error and prevent the command from running, even | |
2707 | though the next step would have been to convert the pointer | |
2708 | directly back into the same address. | |
2709 | ||
2710 | The following shortcut avoids this whole mess. If VAL is a | |
2711 | function, just return its address directly. */ | |
d0c97917 TT |
2712 | if (val->type ()->code () == TYPE_CODE_FUNC |
2713 | || val->type ()->code () == TYPE_CODE_METHOD) | |
42ae5230 | 2714 | return value_address (val); |
f312f057 | 2715 | |
994b9211 | 2716 | val = coerce_array (val); |
fc0c74b1 AC |
2717 | |
2718 | /* Some architectures (e.g. Harvard), map instruction and data | |
2719 | addresses onto a single large unified address space. For | |
2720 | instance: An architecture may consider a large integer in the | |
2721 | range 0x10000000 .. 0x1000ffff to already represent a data | |
2722 | addresses (hence not need a pointer to address conversion) while | |
2723 | a small integer would still need to be converted integer to | |
2724 | pointer to address. Just assume such architectures handle all | |
2725 | integer conversions in a single function. */ | |
2726 | ||
2727 | /* JimB writes: | |
2728 | ||
2729 | I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we | |
2730 | must admonish GDB hackers to make sure its behavior matches the | |
2731 | compiler's, whenever possible. | |
2732 | ||
2733 | In general, I think GDB should evaluate expressions the same way | |
2734 | the compiler does. When the user copies an expression out of | |
2735 | their source code and hands it to a `print' command, they should | |
2736 | get the same value the compiler would have computed. Any | |
2737 | deviation from this rule can cause major confusion and annoyance, | |
2738 | and needs to be justified carefully. In other words, GDB doesn't | |
2739 | really have the freedom to do these conversions in clever and | |
2740 | useful ways. | |
2741 | ||
2742 | AndrewC pointed out that users aren't complaining about how GDB | |
2743 | casts integers to pointers; they are complaining that they can't | |
2744 | take an address from a disassembly listing and give it to `x/i'. | |
2745 | This is certainly important. | |
2746 | ||
79dd2d24 | 2747 | Adding an architecture method like integer_to_address() certainly |
fc0c74b1 AC |
2748 | makes it possible for GDB to "get it right" in all circumstances |
2749 | --- the target has complete control over how things get done, so | |
2750 | people can Do The Right Thing for their target without breaking | |
2751 | anyone else. The standard doesn't specify how integers get | |
2752 | converted to pointers; usually, the ABI doesn't either, but | |
2753 | ABI-specific code is a more reasonable place to handle it. */ | |
2754 | ||
d0c97917 | 2755 | if (!val->type ()->is_pointer_or_reference () |
50810684 | 2756 | && gdbarch_integer_to_address_p (gdbarch)) |
d0c97917 | 2757 | return gdbarch_integer_to_address (gdbarch, val->type (), |
50888e42 | 2758 | value_contents (val).data ()); |
fc0c74b1 | 2759 | |
d0c97917 | 2760 | return unpack_long (val->type (), value_contents (val).data ()); |
c906108c SS |
2761 | #endif |
2762 | } | |
2763 | \f | |
2764 | /* Unpack raw data (copied from debugee, target byte order) at VALADDR | |
2765 | as a long, or as a double, assuming the raw data is described | |
2766 | by type TYPE. Knows how to convert different sizes of values | |
2767 | and can convert between fixed and floating point. We don't assume | |
2768 | any alignment for the raw data. Return value is in host byte order. | |
2769 | ||
2770 | If you want functions and arrays to be coerced to pointers, and | |
2771 | references to be dereferenced, call value_as_long() instead. | |
2772 | ||
2773 | C++: It is assumed that the front-end has taken care of | |
2774 | all matters concerning pointers to members. A pointer | |
2775 | to member which reaches here is considered to be equivalent | |
2776 | to an INT (or some size). After all, it is only an offset. */ | |
2777 | ||
2778 | LONGEST | |
fc1a4b47 | 2779 | unpack_long (struct type *type, const gdb_byte *valaddr) |
c906108c | 2780 | { |
09584414 | 2781 | if (is_fixed_point_type (type)) |
d19937a7 | 2782 | type = type->fixed_point_type_base_type (); |
09584414 | 2783 | |
34877895 | 2784 | enum bfd_endian byte_order = type_byte_order (type); |
78134374 | 2785 | enum type_code code = type->code (); |
df86565b | 2786 | int len = type->length (); |
c6d940a9 | 2787 | int nosign = type->is_unsigned (); |
c906108c | 2788 | |
c906108c SS |
2789 | switch (code) |
2790 | { | |
2791 | case TYPE_CODE_TYPEDEF: | |
2792 | return unpack_long (check_typedef (type), valaddr); | |
2793 | case TYPE_CODE_ENUM: | |
4f2aea11 | 2794 | case TYPE_CODE_FLAGS: |
c906108c SS |
2795 | case TYPE_CODE_BOOL: |
2796 | case TYPE_CODE_INT: | |
2797 | case TYPE_CODE_CHAR: | |
2798 | case TYPE_CODE_RANGE: | |
0d5de010 | 2799 | case TYPE_CODE_MEMBERPTR: |
4e962e74 TT |
2800 | { |
2801 | LONGEST result; | |
20a5fcbd TT |
2802 | |
2803 | if (type->bit_size_differs_p ()) | |
2804 | { | |
2805 | unsigned bit_off = type->bit_offset (); | |
2806 | unsigned bit_size = type->bit_size (); | |
2807 | if (bit_size == 0) | |
2808 | { | |
2809 | /* unpack_bits_as_long doesn't handle this case the | |
2810 | way we'd like, so handle it here. */ | |
2811 | result = 0; | |
2812 | } | |
2813 | else | |
2814 | result = unpack_bits_as_long (type, valaddr, bit_off, bit_size); | |
2815 | } | |
4e962e74 | 2816 | else |
20a5fcbd TT |
2817 | { |
2818 | if (nosign) | |
2819 | result = extract_unsigned_integer (valaddr, len, byte_order); | |
2820 | else | |
2821 | result = extract_signed_integer (valaddr, len, byte_order); | |
2822 | } | |
4e962e74 | 2823 | if (code == TYPE_CODE_RANGE) |
599088e3 | 2824 | result += type->bounds ()->bias; |
4e962e74 TT |
2825 | return result; |
2826 | } | |
c906108c SS |
2827 | |
2828 | case TYPE_CODE_FLT: | |
4ef30785 | 2829 | case TYPE_CODE_DECFLOAT: |
50637b26 | 2830 | return target_float_to_longest (valaddr, type); |
4ef30785 | 2831 | |
09584414 JB |
2832 | case TYPE_CODE_FIXED_POINT: |
2833 | { | |
2834 | gdb_mpq vq; | |
c9f0b43f JB |
2835 | vq.read_fixed_point (gdb::make_array_view (valaddr, len), |
2836 | byte_order, nosign, | |
e6fcee3a | 2837 | type->fixed_point_scaling_factor ()); |
09584414 JB |
2838 | |
2839 | gdb_mpz vz; | |
2840 | mpz_tdiv_q (vz.val, mpq_numref (vq.val), mpq_denref (vq.val)); | |
2841 | return vz.as_integer<LONGEST> (); | |
2842 | } | |
2843 | ||
c906108c SS |
2844 | case TYPE_CODE_PTR: |
2845 | case TYPE_CODE_REF: | |
aa006118 | 2846 | case TYPE_CODE_RVALUE_REF: |
c906108c | 2847 | /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure |
dda83cd7 | 2848 | whether we want this to be true eventually. */ |
4478b372 | 2849 | return extract_typed_address (valaddr, type); |
c906108c | 2850 | |
c906108c | 2851 | default: |
8a3fe4f8 | 2852 | error (_("Value can't be converted to integer.")); |
c906108c | 2853 | } |
c906108c SS |
2854 | } |
2855 | ||
c906108c SS |
2856 | /* Unpack raw data (copied from debugee, target byte order) at VALADDR |
2857 | as a CORE_ADDR, assuming the raw data is described by type TYPE. | |
2858 | We don't assume any alignment for the raw data. Return value is in | |
2859 | host byte order. | |
2860 | ||
2861 | If you want functions and arrays to be coerced to pointers, and | |
1aa20aa8 | 2862 | references to be dereferenced, call value_as_address() instead. |
c906108c SS |
2863 | |
2864 | C++: It is assumed that the front-end has taken care of | |
2865 | all matters concerning pointers to members. A pointer | |
2866 | to member which reaches here is considered to be equivalent | |
2867 | to an INT (or some size). After all, it is only an offset. */ | |
2868 | ||
2869 | CORE_ADDR | |
fc1a4b47 | 2870 | unpack_pointer (struct type *type, const gdb_byte *valaddr) |
c906108c SS |
2871 | { |
2872 | /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure | |
2873 | whether we want this to be true eventually. */ | |
2874 | return unpack_long (type, valaddr); | |
2875 | } | |
4478b372 | 2876 | |
70100014 UW |
2877 | bool |
2878 | is_floating_value (struct value *val) | |
2879 | { | |
d0c97917 | 2880 | struct type *type = check_typedef (val->type ()); |
70100014 UW |
2881 | |
2882 | if (is_floating_type (type)) | |
2883 | { | |
50888e42 | 2884 | if (!target_float_is_valid (value_contents (val).data (), type)) |
70100014 UW |
2885 | error (_("Invalid floating value found in program.")); |
2886 | return true; | |
2887 | } | |
2888 | ||
2889 | return false; | |
2890 | } | |
2891 | ||
c906108c | 2892 | \f |
1596cb5d | 2893 | /* Get the value of the FIELDNO'th field (which must be static) of |
686d4def | 2894 | TYPE. */ |
c906108c | 2895 | |
f23631e4 | 2896 | struct value * |
fba45db2 | 2897 | value_static_field (struct type *type, int fieldno) |
c906108c | 2898 | { |
948e66d9 DJ |
2899 | struct value *retval; |
2900 | ||
2ad53ea1 | 2901 | switch (type->field (fieldno).loc_kind ()) |
c906108c | 2902 | { |
1596cb5d | 2903 | case FIELD_LOC_KIND_PHYSADDR: |
940da03e | 2904 | retval = value_at_lazy (type->field (fieldno).type (), |
e06c3e11 | 2905 | type->field (fieldno).loc_physaddr ()); |
1596cb5d DE |
2906 | break; |
2907 | case FIELD_LOC_KIND_PHYSNAME: | |
c906108c | 2908 | { |
fcbbbd90 | 2909 | const char *phys_name = type->field (fieldno).loc_physname (); |
33d16dd9 | 2910 | /* type->field (fieldno).name (); */ |
d12307c1 | 2911 | struct block_symbol sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0); |
94af9270 | 2912 | |
d12307c1 | 2913 | if (sym.symbol == NULL) |
c906108c | 2914 | { |
a109c7c1 | 2915 | /* With some compilers, e.g. HP aCC, static data members are |
581e13c1 | 2916 | reported as non-debuggable symbols. */ |
3b7344d5 TT |
2917 | struct bound_minimal_symbol msym |
2918 | = lookup_minimal_symbol (phys_name, NULL, NULL); | |
940da03e | 2919 | struct type *field_type = type->field (fieldno).type (); |
a109c7c1 | 2920 | |
3b7344d5 | 2921 | if (!msym.minsym) |
c2e0e465 | 2922 | retval = allocate_optimized_out_value (field_type); |
c906108c | 2923 | else |
4aeddc50 | 2924 | retval = value_at_lazy (field_type, msym.value_address ()); |
c906108c SS |
2925 | } |
2926 | else | |
d12307c1 | 2927 | retval = value_of_variable (sym.symbol, sym.block); |
1596cb5d | 2928 | break; |
c906108c | 2929 | } |
1596cb5d | 2930 | default: |
f3574227 | 2931 | gdb_assert_not_reached ("unexpected field location kind"); |
1596cb5d DE |
2932 | } |
2933 | ||
948e66d9 | 2934 | return retval; |
c906108c SS |
2935 | } |
2936 | ||
4dfea560 DE |
2937 | /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE. |
2938 | You have to be careful here, since the size of the data area for the value | |
2939 | is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger | |
2940 | than the old enclosing type, you have to allocate more space for the | |
2941 | data. */ | |
2b127877 | 2942 | |
4dfea560 | 2943 | void |
463b870d | 2944 | value::set_enclosing_type (struct type *new_encl_type) |
2b127877 | 2945 | { |
463b870d | 2946 | if (new_encl_type->length () > enclosing_type ()->length ()) |
5fdf6324 AB |
2947 | { |
2948 | check_type_length_before_alloc (new_encl_type); | |
463b870d TT |
2949 | m_contents.reset ((gdb_byte *) xrealloc (m_contents.release (), |
2950 | new_encl_type->length ())); | |
5fdf6324 | 2951 | } |
3e3d7139 | 2952 | |
463b870d | 2953 | m_enclosing_type = new_encl_type; |
2b127877 DB |
2954 | } |
2955 | ||
c906108c SS |
2956 | /* Given a value ARG1 (offset by OFFSET bytes) |
2957 | of a struct or union type ARG_TYPE, | |
2958 | extract and return the value of one of its (non-static) fields. | |
581e13c1 | 2959 | FIELDNO says which field. */ |
c906108c | 2960 | |
f23631e4 | 2961 | struct value * |
6b850546 | 2962 | value_primitive_field (struct value *arg1, LONGEST offset, |
aa1ee363 | 2963 | int fieldno, struct type *arg_type) |
c906108c | 2964 | { |
f23631e4 | 2965 | struct value *v; |
52f0bd74 | 2966 | struct type *type; |
f9ee742c | 2967 | struct gdbarch *arch = arg1->arch (); |
3ae385af | 2968 | int unit_size = gdbarch_addressable_memory_unit_size (arch); |
c906108c | 2969 | |
f168693b | 2970 | arg_type = check_typedef (arg_type); |
940da03e | 2971 | type = arg_type->field (fieldno).type (); |
c54eabfa JK |
2972 | |
2973 | /* Call check_typedef on our type to make sure that, if TYPE | |
2974 | is a TYPE_CODE_TYPEDEF, its length is set to the length | |
2975 | of the target type instead of zero. However, we do not | |
2976 | replace the typedef type by the target type, because we want | |
2977 | to keep the typedef in order to be able to print the type | |
2978 | description correctly. */ | |
2979 | check_typedef (type); | |
c906108c | 2980 | |
691a26f5 | 2981 | if (TYPE_FIELD_BITSIZE (arg_type, fieldno)) |
c906108c | 2982 | { |
22c05d8a JK |
2983 | /* Handle packed fields. |
2984 | ||
2985 | Create a new value for the bitfield, with bitpos and bitsize | |
4ea48cc1 DJ |
2986 | set. If possible, arrange offset and bitpos so that we can |
2987 | do a single aligned read of the size of the containing type. | |
2988 | Otherwise, adjust offset to the byte containing the first | |
2989 | bit. Assume that the address, offset, and embedded offset | |
2990 | are sufficiently aligned. */ | |
22c05d8a | 2991 | |
b610c045 | 2992 | LONGEST bitpos = arg_type->field (fieldno).loc_bitpos (); |
df86565b | 2993 | LONGEST container_bitsize = type->length () * 8; |
4ea48cc1 | 2994 | |
9a0dc9e3 | 2995 | v = allocate_value_lazy (type); |
382d927f TT |
2996 | v->m_bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno); |
2997 | if ((bitpos % container_bitsize) + v->m_bitsize <= container_bitsize | |
df86565b | 2998 | && type->length () <= (int) sizeof (LONGEST)) |
382d927f | 2999 | v->m_bitpos = bitpos % container_bitsize; |
4ea48cc1 | 3000 | else |
382d927f | 3001 | v->m_bitpos = bitpos % 8; |
391f8628 | 3002 | v->m_offset = (arg1->embedded_offset () |
9a0dc9e3 | 3003 | + offset |
382d927f | 3004 | + (bitpos - v->m_bitpos) / 8); |
fac7bdaa | 3005 | v->set_parent (arg1); |
9a0dc9e3 PA |
3006 | if (!value_lazy (arg1)) |
3007 | value_fetch_lazy (v); | |
c906108c SS |
3008 | } |
3009 | else if (fieldno < TYPE_N_BASECLASSES (arg_type)) | |
3010 | { | |
3011 | /* This field is actually a base subobject, so preserve the | |
39d37385 PA |
3012 | entire object's contents for later references to virtual |
3013 | bases, etc. */ | |
6b850546 | 3014 | LONGEST boffset; |
a4e2ee12 DJ |
3015 | |
3016 | /* Lazy register values with offsets are not supported. */ | |
3017 | if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1)) | |
3018 | value_fetch_lazy (arg1); | |
3019 | ||
9a0dc9e3 PA |
3020 | /* We special case virtual inheritance here because this |
3021 | requires access to the contents, which we would rather avoid | |
3022 | for references to ordinary fields of unavailable values. */ | |
3023 | if (BASETYPE_VIA_VIRTUAL (arg_type, fieldno)) | |
3024 | boffset = baseclass_offset (arg_type, fieldno, | |
50888e42 | 3025 | value_contents (arg1).data (), |
391f8628 | 3026 | arg1->embedded_offset (), |
9a0dc9e3 PA |
3027 | value_address (arg1), |
3028 | arg1); | |
c906108c | 3029 | else |
b610c045 | 3030 | boffset = arg_type->field (fieldno).loc_bitpos () / 8; |
691a26f5 | 3031 | |
9a0dc9e3 | 3032 | if (value_lazy (arg1)) |
463b870d | 3033 | v = allocate_value_lazy (arg1->enclosing_type ()); |
9a0dc9e3 PA |
3034 | else |
3035 | { | |
463b870d | 3036 | v = allocate_value (arg1->enclosing_type ()); |
9a0dc9e3 | 3037 | value_contents_copy_raw (v, 0, arg1, 0, |
463b870d | 3038 | arg1->enclosing_type ()->length ()); |
3e3d7139 | 3039 | } |
382d927f | 3040 | v->m_type = type; |
76675c4d | 3041 | v->m_offset = arg1->offset (); |
391f8628 | 3042 | v->m_embedded_offset = offset + arg1->embedded_offset () + boffset; |
c906108c | 3043 | } |
9920b434 BH |
3044 | else if (NULL != TYPE_DATA_LOCATION (type)) |
3045 | { | |
3046 | /* Field is a dynamic data member. */ | |
3047 | ||
3048 | gdb_assert (0 == offset); | |
3049 | /* We expect an already resolved data location. */ | |
3050 | gdb_assert (PROP_CONST == TYPE_DATA_LOCATION_KIND (type)); | |
3051 | /* For dynamic data types defer memory allocation | |
dda83cd7 | 3052 | until we actual access the value. */ |
9920b434 BH |
3053 | v = allocate_value_lazy (type); |
3054 | } | |
c906108c SS |
3055 | else |
3056 | { | |
3057 | /* Plain old data member */ | |
b610c045 | 3058 | offset += (arg_type->field (fieldno).loc_bitpos () |
dda83cd7 | 3059 | / (HOST_CHAR_BIT * unit_size)); |
a4e2ee12 DJ |
3060 | |
3061 | /* Lazy register values with offsets are not supported. */ | |
3062 | if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1)) | |
3063 | value_fetch_lazy (arg1); | |
3064 | ||
9a0dc9e3 | 3065 | if (value_lazy (arg1)) |
3e3d7139 | 3066 | v = allocate_value_lazy (type); |
c906108c | 3067 | else |
3e3d7139 JG |
3068 | { |
3069 | v = allocate_value (type); | |
391f8628 TT |
3070 | value_contents_copy_raw (v, v->embedded_offset (), |
3071 | arg1, arg1->embedded_offset () + offset, | |
3ae385af | 3072 | type_length_units (type)); |
3e3d7139 | 3073 | } |
76675c4d | 3074 | v->m_offset = (arg1->offset () + offset |
391f8628 | 3075 | + arg1->embedded_offset ()); |
c906108c | 3076 | } |
74bcbdf3 | 3077 | set_value_component_location (v, arg1); |
c906108c SS |
3078 | return v; |
3079 | } | |
3080 | ||
3081 | /* Given a value ARG1 of a struct or union type, | |
3082 | extract and return the value of one of its (non-static) fields. | |
581e13c1 | 3083 | FIELDNO says which field. */ |
c906108c | 3084 | |
f23631e4 | 3085 | struct value * |
aa1ee363 | 3086 | value_field (struct value *arg1, int fieldno) |
c906108c | 3087 | { |
d0c97917 | 3088 | return value_primitive_field (arg1, 0, fieldno, arg1->type ()); |
c906108c SS |
3089 | } |
3090 | ||
3091 | /* Return a non-virtual function as a value. | |
3092 | F is the list of member functions which contains the desired method. | |
0478d61c FF |
3093 | J is an index into F which provides the desired method. |
3094 | ||
3095 | We only use the symbol for its address, so be happy with either a | |
581e13c1 | 3096 | full symbol or a minimal symbol. */ |
c906108c | 3097 | |
f23631e4 | 3098 | struct value * |
3e43a32a MS |
3099 | value_fn_field (struct value **arg1p, struct fn_field *f, |
3100 | int j, struct type *type, | |
6b850546 | 3101 | LONGEST offset) |
c906108c | 3102 | { |
f23631e4 | 3103 | struct value *v; |
52f0bd74 | 3104 | struct type *ftype = TYPE_FN_FIELD_TYPE (f, j); |
1d06ead6 | 3105 | const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j); |
c906108c | 3106 | struct symbol *sym; |
7c7b6655 | 3107 | struct bound_minimal_symbol msym; |
c906108c | 3108 | |
d12307c1 | 3109 | sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0).symbol; |
29ba33db | 3110 | if (sym == nullptr) |
0478d61c | 3111 | { |
7c7b6655 TT |
3112 | msym = lookup_bound_minimal_symbol (physname); |
3113 | if (msym.minsym == NULL) | |
5ae326fa | 3114 | return NULL; |
0478d61c FF |
3115 | } |
3116 | ||
c906108c | 3117 | v = allocate_value (ftype); |
1a088441 | 3118 | VALUE_LVAL (v) = lval_memory; |
0478d61c FF |
3119 | if (sym) |
3120 | { | |
6395b628 | 3121 | set_value_address (v, sym->value_block ()->entry_pc ()); |
0478d61c FF |
3122 | } |
3123 | else | |
3124 | { | |
bccdca4a UW |
3125 | /* The minimal symbol might point to a function descriptor; |
3126 | resolve it to the actual code address instead. */ | |
7c7b6655 | 3127 | struct objfile *objfile = msym.objfile; |
08feed99 | 3128 | struct gdbarch *gdbarch = objfile->arch (); |
bccdca4a | 3129 | |
42ae5230 TT |
3130 | set_value_address (v, |
3131 | gdbarch_convert_from_func_ptr_addr | |
4aeddc50 | 3132 | (gdbarch, msym.value_address (), |
328d42d8 | 3133 | current_inferior ()->top_target ())); |
0478d61c | 3134 | } |
c906108c SS |
3135 | |
3136 | if (arg1p) | |
c5aa993b | 3137 | { |
d0c97917 | 3138 | if (type != (*arg1p)->type ()) |
c5aa993b JM |
3139 | *arg1p = value_ind (value_cast (lookup_pointer_type (type), |
3140 | value_addr (*arg1p))); | |
3141 | ||
070ad9f0 | 3142 | /* Move the `this' pointer according to the offset. |
76675c4d | 3143 | (*arg1p)->offset () += offset; */ |
c906108c SS |
3144 | } |
3145 | ||
3146 | return v; | |
3147 | } | |
3148 | ||
c906108c | 3149 | \f |
c906108c | 3150 | |
ef83a141 TT |
3151 | /* See value.h. */ |
3152 | ||
3153 | LONGEST | |
4875ffdb | 3154 | unpack_bits_as_long (struct type *field_type, const gdb_byte *valaddr, |
6b850546 | 3155 | LONGEST bitpos, LONGEST bitsize) |
c906108c | 3156 | { |
34877895 | 3157 | enum bfd_endian byte_order = type_byte_order (field_type); |
c906108c SS |
3158 | ULONGEST val; |
3159 | ULONGEST valmask; | |
c906108c | 3160 | int lsbcount; |
6b850546 DT |
3161 | LONGEST bytes_read; |
3162 | LONGEST read_offset; | |
c906108c | 3163 | |
4a76eae5 DJ |
3164 | /* Read the minimum number of bytes required; there may not be |
3165 | enough bytes to read an entire ULONGEST. */ | |
f168693b | 3166 | field_type = check_typedef (field_type); |
4a76eae5 DJ |
3167 | if (bitsize) |
3168 | bytes_read = ((bitpos % 8) + bitsize + 7) / 8; | |
3169 | else | |
15ce8941 | 3170 | { |
df86565b | 3171 | bytes_read = field_type->length (); |
15ce8941 TT |
3172 | bitsize = 8 * bytes_read; |
3173 | } | |
4a76eae5 | 3174 | |
5467c6c8 PA |
3175 | read_offset = bitpos / 8; |
3176 | ||
4875ffdb | 3177 | val = extract_unsigned_integer (valaddr + read_offset, |
4a76eae5 | 3178 | bytes_read, byte_order); |
c906108c | 3179 | |
581e13c1 | 3180 | /* Extract bits. See comment above. */ |
c906108c | 3181 | |
d5a22e77 | 3182 | if (byte_order == BFD_ENDIAN_BIG) |
4a76eae5 | 3183 | lsbcount = (bytes_read * 8 - bitpos % 8 - bitsize); |
c906108c SS |
3184 | else |
3185 | lsbcount = (bitpos % 8); | |
3186 | val >>= lsbcount; | |
3187 | ||
3188 | /* If the field does not entirely fill a LONGEST, then zero the sign bits. | |
581e13c1 | 3189 | If the field is signed, and is negative, then sign extend. */ |
c906108c | 3190 | |
15ce8941 | 3191 | if (bitsize < 8 * (int) sizeof (val)) |
c906108c SS |
3192 | { |
3193 | valmask = (((ULONGEST) 1) << bitsize) - 1; | |
3194 | val &= valmask; | |
c6d940a9 | 3195 | if (!field_type->is_unsigned ()) |
c906108c SS |
3196 | { |
3197 | if (val & (valmask ^ (valmask >> 1))) | |
3198 | { | |
3199 | val |= ~valmask; | |
3200 | } | |
3201 | } | |
3202 | } | |
5467c6c8 | 3203 | |
4875ffdb | 3204 | return val; |
5467c6c8 PA |
3205 | } |
3206 | ||
3207 | /* Unpack a field FIELDNO of the specified TYPE, from the object at | |
3208 | VALADDR + EMBEDDED_OFFSET. VALADDR points to the contents of | |
3209 | ORIGINAL_VALUE, which must not be NULL. See | |
3210 | unpack_value_bits_as_long for more details. */ | |
3211 | ||
3212 | int | |
3213 | unpack_value_field_as_long (struct type *type, const gdb_byte *valaddr, | |
6b850546 | 3214 | LONGEST embedded_offset, int fieldno, |
5467c6c8 PA |
3215 | const struct value *val, LONGEST *result) |
3216 | { | |
b610c045 | 3217 | int bitpos = type->field (fieldno).loc_bitpos (); |
4875ffdb | 3218 | int bitsize = TYPE_FIELD_BITSIZE (type, fieldno); |
940da03e | 3219 | struct type *field_type = type->field (fieldno).type (); |
4875ffdb PA |
3220 | int bit_offset; |
3221 | ||
5467c6c8 PA |
3222 | gdb_assert (val != NULL); |
3223 | ||
4875ffdb PA |
3224 | bit_offset = embedded_offset * TARGET_CHAR_BIT + bitpos; |
3225 | if (value_bits_any_optimized_out (val, bit_offset, bitsize) | |
3226 | || !value_bits_available (val, bit_offset, bitsize)) | |
3227 | return 0; | |
3228 | ||
3229 | *result = unpack_bits_as_long (field_type, valaddr + embedded_offset, | |
3230 | bitpos, bitsize); | |
3231 | return 1; | |
5467c6c8 PA |
3232 | } |
3233 | ||
3234 | /* Unpack a field FIELDNO of the specified TYPE, from the anonymous | |
4875ffdb | 3235 | object at VALADDR. See unpack_bits_as_long for more details. */ |
5467c6c8 PA |
3236 | |
3237 | LONGEST | |
3238 | unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno) | |
3239 | { | |
b610c045 | 3240 | int bitpos = type->field (fieldno).loc_bitpos (); |
4875ffdb | 3241 | int bitsize = TYPE_FIELD_BITSIZE (type, fieldno); |
940da03e | 3242 | struct type *field_type = type->field (fieldno).type (); |
5467c6c8 | 3243 | |
4875ffdb PA |
3244 | return unpack_bits_as_long (field_type, valaddr, bitpos, bitsize); |
3245 | } | |
3246 | ||
3247 | /* Unpack a bitfield of BITSIZE bits found at BITPOS in the object at | |
3248 | VALADDR + EMBEDDEDOFFSET that has the type of DEST_VAL and store | |
3249 | the contents in DEST_VAL, zero or sign extending if the type of | |
3250 | DEST_VAL is wider than BITSIZE. VALADDR points to the contents of | |
3251 | VAL. If the VAL's contents required to extract the bitfield from | |
3252 | are unavailable/optimized out, DEST_VAL is correspondingly | |
3253 | marked unavailable/optimized out. */ | |
3254 | ||
bb9d5f81 | 3255 | void |
4875ffdb | 3256 | unpack_value_bitfield (struct value *dest_val, |
6b850546 DT |
3257 | LONGEST bitpos, LONGEST bitsize, |
3258 | const gdb_byte *valaddr, LONGEST embedded_offset, | |
4875ffdb PA |
3259 | const struct value *val) |
3260 | { | |
3261 | enum bfd_endian byte_order; | |
3262 | int src_bit_offset; | |
3263 | int dst_bit_offset; | |
d0c97917 | 3264 | struct type *field_type = dest_val->type (); |
4875ffdb | 3265 | |
34877895 | 3266 | byte_order = type_byte_order (field_type); |
e5ca03b4 PA |
3267 | |
3268 | /* First, unpack and sign extend the bitfield as if it was wholly | |
3269 | valid. Optimized out/unavailable bits are read as zero, but | |
3270 | that's OK, as they'll end up marked below. If the VAL is | |
3271 | wholly-invalid we may have skipped allocating its contents, | |
3272 | though. See allocate_optimized_out_value. */ | |
3273 | if (valaddr != NULL) | |
3274 | { | |
3275 | LONGEST num; | |
3276 | ||
3277 | num = unpack_bits_as_long (field_type, valaddr + embedded_offset, | |
3278 | bitpos, bitsize); | |
50888e42 | 3279 | store_signed_integer (value_contents_raw (dest_val).data (), |
df86565b | 3280 | field_type->length (), byte_order, num); |
e5ca03b4 | 3281 | } |
4875ffdb PA |
3282 | |
3283 | /* Now copy the optimized out / unavailability ranges to the right | |
3284 | bits. */ | |
3285 | src_bit_offset = embedded_offset * TARGET_CHAR_BIT + bitpos; | |
3286 | if (byte_order == BFD_ENDIAN_BIG) | |
df86565b | 3287 | dst_bit_offset = field_type->length () * TARGET_CHAR_BIT - bitsize; |
4875ffdb PA |
3288 | else |
3289 | dst_bit_offset = 0; | |
3290 | value_ranges_copy_adjusted (dest_val, dst_bit_offset, | |
3291 | val, src_bit_offset, bitsize); | |
5467c6c8 PA |
3292 | } |
3293 | ||
3294 | /* Return a new value with type TYPE, which is FIELDNO field of the | |
3295 | object at VALADDR + EMBEDDEDOFFSET. VALADDR points to the contents | |
3296 | of VAL. If the VAL's contents required to extract the bitfield | |
4875ffdb PA |
3297 | from are unavailable/optimized out, the new value is |
3298 | correspondingly marked unavailable/optimized out. */ | |
5467c6c8 PA |
3299 | |
3300 | struct value * | |
3301 | value_field_bitfield (struct type *type, int fieldno, | |
3302 | const gdb_byte *valaddr, | |
6b850546 | 3303 | LONGEST embedded_offset, const struct value *val) |
5467c6c8 | 3304 | { |
b610c045 | 3305 | int bitpos = type->field (fieldno).loc_bitpos (); |
4875ffdb | 3306 | int bitsize = TYPE_FIELD_BITSIZE (type, fieldno); |
940da03e | 3307 | struct value *res_val = allocate_value (type->field (fieldno).type ()); |
5467c6c8 | 3308 | |
4875ffdb PA |
3309 | unpack_value_bitfield (res_val, bitpos, bitsize, |
3310 | valaddr, embedded_offset, val); | |
3311 | ||
3312 | return res_val; | |
4ea48cc1 DJ |
3313 | } |
3314 | ||
c906108c SS |
3315 | /* Modify the value of a bitfield. ADDR points to a block of memory in |
3316 | target byte order; the bitfield starts in the byte pointed to. FIELDVAL | |
3317 | is the desired value of the field, in host byte order. BITPOS and BITSIZE | |
581e13c1 | 3318 | indicate which bits (in target bit order) comprise the bitfield. |
19f220c3 | 3319 | Requires 0 < BITSIZE <= lbits, 0 <= BITPOS % 8 + BITSIZE <= lbits, and |
f4e88c8e | 3320 | 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */ |
c906108c SS |
3321 | |
3322 | void | |
50810684 | 3323 | modify_field (struct type *type, gdb_byte *addr, |
6b850546 | 3324 | LONGEST fieldval, LONGEST bitpos, LONGEST bitsize) |
c906108c | 3325 | { |
34877895 | 3326 | enum bfd_endian byte_order = type_byte_order (type); |
f4e88c8e PH |
3327 | ULONGEST oword; |
3328 | ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize); | |
6b850546 | 3329 | LONGEST bytesize; |
19f220c3 JK |
3330 | |
3331 | /* Normalize BITPOS. */ | |
3332 | addr += bitpos / 8; | |
3333 | bitpos %= 8; | |
c906108c SS |
3334 | |
3335 | /* If a negative fieldval fits in the field in question, chop | |
3336 | off the sign extension bits. */ | |
f4e88c8e PH |
3337 | if ((~fieldval & ~(mask >> 1)) == 0) |
3338 | fieldval &= mask; | |
c906108c SS |
3339 | |
3340 | /* Warn if value is too big to fit in the field in question. */ | |
f4e88c8e | 3341 | if (0 != (fieldval & ~mask)) |
c906108c SS |
3342 | { |
3343 | /* FIXME: would like to include fieldval in the message, but | |
dda83cd7 | 3344 | we don't have a sprintf_longest. */ |
6b850546 | 3345 | warning (_("Value does not fit in %s bits."), plongest (bitsize)); |
c906108c SS |
3346 | |
3347 | /* Truncate it, otherwise adjoining fields may be corrupted. */ | |
f4e88c8e | 3348 | fieldval &= mask; |
c906108c SS |
3349 | } |
3350 | ||
19f220c3 JK |
3351 | /* Ensure no bytes outside of the modified ones get accessed as it may cause |
3352 | false valgrind reports. */ | |
3353 | ||
3354 | bytesize = (bitpos + bitsize + 7) / 8; | |
3355 | oword = extract_unsigned_integer (addr, bytesize, byte_order); | |
c906108c SS |
3356 | |
3357 | /* Shifting for bit field depends on endianness of the target machine. */ | |
d5a22e77 | 3358 | if (byte_order == BFD_ENDIAN_BIG) |
19f220c3 | 3359 | bitpos = bytesize * 8 - bitpos - bitsize; |
c906108c | 3360 | |
f4e88c8e | 3361 | oword &= ~(mask << bitpos); |
c906108c SS |
3362 | oword |= fieldval << bitpos; |
3363 | ||
19f220c3 | 3364 | store_unsigned_integer (addr, bytesize, byte_order, oword); |
c906108c SS |
3365 | } |
3366 | \f | |
14d06750 | 3367 | /* Pack NUM into BUF using a target format of TYPE. */ |
c906108c | 3368 | |
14d06750 DJ |
3369 | void |
3370 | pack_long (gdb_byte *buf, struct type *type, LONGEST num) | |
c906108c | 3371 | { |
34877895 | 3372 | enum bfd_endian byte_order = type_byte_order (type); |
6b850546 | 3373 | LONGEST len; |
14d06750 DJ |
3374 | |
3375 | type = check_typedef (type); | |
df86565b | 3376 | len = type->length (); |
c906108c | 3377 | |
78134374 | 3378 | switch (type->code ()) |
c906108c | 3379 | { |
4e962e74 | 3380 | case TYPE_CODE_RANGE: |
599088e3 | 3381 | num -= type->bounds ()->bias; |
4e962e74 | 3382 | /* Fall through. */ |
c906108c SS |
3383 | case TYPE_CODE_INT: |
3384 | case TYPE_CODE_CHAR: | |
3385 | case TYPE_CODE_ENUM: | |
4f2aea11 | 3386 | case TYPE_CODE_FLAGS: |
c906108c | 3387 | case TYPE_CODE_BOOL: |
0d5de010 | 3388 | case TYPE_CODE_MEMBERPTR: |
20a5fcbd TT |
3389 | if (type->bit_size_differs_p ()) |
3390 | { | |
3391 | unsigned bit_off = type->bit_offset (); | |
3392 | unsigned bit_size = type->bit_size (); | |
3393 | num &= ((ULONGEST) 1 << bit_size) - 1; | |
3394 | num <<= bit_off; | |
3395 | } | |
e17a4113 | 3396 | store_signed_integer (buf, len, byte_order, num); |
c906108c | 3397 | break; |
c5aa993b | 3398 | |
c906108c | 3399 | case TYPE_CODE_REF: |
aa006118 | 3400 | case TYPE_CODE_RVALUE_REF: |
c906108c | 3401 | case TYPE_CODE_PTR: |
14d06750 | 3402 | store_typed_address (buf, type, (CORE_ADDR) num); |
c906108c | 3403 | break; |
c5aa993b | 3404 | |
50637b26 UW |
3405 | case TYPE_CODE_FLT: |
3406 | case TYPE_CODE_DECFLOAT: | |
3407 | target_float_from_longest (buf, type, num); | |
3408 | break; | |
3409 | ||
c906108c | 3410 | default: |
14d06750 | 3411 | error (_("Unexpected type (%d) encountered for integer constant."), |
78134374 | 3412 | type->code ()); |
c906108c | 3413 | } |
14d06750 DJ |
3414 | } |
3415 | ||
3416 | ||
595939de PM |
3417 | /* Pack NUM into BUF using a target format of TYPE. */ |
3418 | ||
70221824 | 3419 | static void |
595939de PM |
3420 | pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num) |
3421 | { | |
6b850546 | 3422 | LONGEST len; |
595939de PM |
3423 | enum bfd_endian byte_order; |
3424 | ||
3425 | type = check_typedef (type); | |
df86565b | 3426 | len = type->length (); |
34877895 | 3427 | byte_order = type_byte_order (type); |
595939de | 3428 | |
78134374 | 3429 | switch (type->code ()) |
595939de PM |
3430 | { |
3431 | case TYPE_CODE_INT: | |
3432 | case TYPE_CODE_CHAR: | |
3433 | case TYPE_CODE_ENUM: | |
3434 | case TYPE_CODE_FLAGS: | |
3435 | case TYPE_CODE_BOOL: | |
3436 | case TYPE_CODE_RANGE: | |
3437 | case TYPE_CODE_MEMBERPTR: | |
20a5fcbd TT |
3438 | if (type->bit_size_differs_p ()) |
3439 | { | |
3440 | unsigned bit_off = type->bit_offset (); | |
3441 | unsigned bit_size = type->bit_size (); | |
3442 | num &= ((ULONGEST) 1 << bit_size) - 1; | |
3443 | num <<= bit_off; | |
3444 | } | |
595939de PM |
3445 | store_unsigned_integer (buf, len, byte_order, num); |
3446 | break; | |
3447 | ||
3448 | case TYPE_CODE_REF: | |
aa006118 | 3449 | case TYPE_CODE_RVALUE_REF: |
595939de PM |
3450 | case TYPE_CODE_PTR: |
3451 | store_typed_address (buf, type, (CORE_ADDR) num); | |
3452 | break; | |
3453 | ||
50637b26 UW |
3454 | case TYPE_CODE_FLT: |
3455 | case TYPE_CODE_DECFLOAT: | |
3456 | target_float_from_ulongest (buf, type, num); | |
3457 | break; | |
3458 | ||
595939de | 3459 | default: |
3e43a32a MS |
3460 | error (_("Unexpected type (%d) encountered " |
3461 | "for unsigned integer constant."), | |
78134374 | 3462 | type->code ()); |
595939de PM |
3463 | } |
3464 | } | |
3465 | ||
3466 | ||
3e44c304 TT |
3467 | /* Create a value of type TYPE that is zero, and return it. */ |
3468 | ||
3469 | struct value * | |
3470 | value_zero (struct type *type, enum lval_type lv) | |
3471 | { | |
3472 | struct value *val = allocate_value_lazy (type); | |
3473 | ||
3474 | VALUE_LVAL (val) = (lv == lval_computed ? not_lval : lv); | |
382d927f | 3475 | val->m_is_zero = true; |
3e44c304 TT |
3476 | return val; |
3477 | } | |
3478 | ||
14d06750 DJ |
3479 | /* Convert C numbers into newly allocated values. */ |
3480 | ||
3481 | struct value * | |
3482 | value_from_longest (struct type *type, LONGEST num) | |
3483 | { | |
3484 | struct value *val = allocate_value (type); | |
3485 | ||
50888e42 | 3486 | pack_long (value_contents_raw (val).data (), type, num); |
c906108c SS |
3487 | return val; |
3488 | } | |
3489 | ||
4478b372 | 3490 | |
595939de PM |
3491 | /* Convert C unsigned numbers into newly allocated values. */ |
3492 | ||
3493 | struct value * | |
3494 | value_from_ulongest (struct type *type, ULONGEST num) | |
3495 | { | |
3496 | struct value *val = allocate_value (type); | |
3497 | ||
50888e42 | 3498 | pack_unsigned_long (value_contents_raw (val).data (), type, num); |
595939de PM |
3499 | |
3500 | return val; | |
3501 | } | |
3502 | ||
3503 | ||
4478b372 | 3504 | /* Create a value representing a pointer of type TYPE to the address |
cb417230 | 3505 | ADDR. */ |
80180f79 | 3506 | |
f23631e4 | 3507 | struct value * |
4478b372 JB |
3508 | value_from_pointer (struct type *type, CORE_ADDR addr) |
3509 | { | |
cb417230 | 3510 | struct value *val = allocate_value (type); |
a109c7c1 | 3511 | |
50888e42 | 3512 | store_typed_address (value_contents_raw (val).data (), |
cb417230 | 3513 | check_typedef (type), addr); |
4478b372 JB |
3514 | return val; |
3515 | } | |
3516 | ||
7584bb30 AB |
3517 | /* Create and return a value object of TYPE containing the value D. The |
3518 | TYPE must be of TYPE_CODE_FLT, and must be large enough to hold D once | |
3519 | it is converted to target format. */ | |
3520 | ||
3521 | struct value * | |
3522 | value_from_host_double (struct type *type, double d) | |
3523 | { | |
3524 | struct value *value = allocate_value (type); | |
78134374 | 3525 | gdb_assert (type->code () == TYPE_CODE_FLT); |
50888e42 | 3526 | target_float_from_host_double (value_contents_raw (value).data (), |
d0c97917 | 3527 | value->type (), d); |
7584bb30 AB |
3528 | return value; |
3529 | } | |
4478b372 | 3530 | |
012370f6 TT |
3531 | /* Create a value of type TYPE whose contents come from VALADDR, if it |
3532 | is non-null, and whose memory address (in the inferior) is | |
3533 | ADDRESS. The type of the created value may differ from the passed | |
3534 | type TYPE. Make sure to retrieve values new type after this call. | |
3535 | Note that TYPE is not passed through resolve_dynamic_type; this is | |
3536 | a special API intended for use only by Ada. */ | |
3537 | ||
3538 | struct value * | |
3539 | value_from_contents_and_address_unresolved (struct type *type, | |
3540 | const gdb_byte *valaddr, | |
3541 | CORE_ADDR address) | |
3542 | { | |
3543 | struct value *v; | |
3544 | ||
3545 | if (valaddr == NULL) | |
3546 | v = allocate_value_lazy (type); | |
3547 | else | |
3548 | v = value_from_contents (type, valaddr); | |
012370f6 | 3549 | VALUE_LVAL (v) = lval_memory; |
1a088441 | 3550 | set_value_address (v, address); |
012370f6 TT |
3551 | return v; |
3552 | } | |
3553 | ||
8acb6b92 TT |
3554 | /* Create a value of type TYPE whose contents come from VALADDR, if it |
3555 | is non-null, and whose memory address (in the inferior) is | |
80180f79 SA |
3556 | ADDRESS. The type of the created value may differ from the passed |
3557 | type TYPE. Make sure to retrieve values new type after this call. */ | |
8acb6b92 TT |
3558 | |
3559 | struct value * | |
3560 | value_from_contents_and_address (struct type *type, | |
3561 | const gdb_byte *valaddr, | |
3562 | CORE_ADDR address) | |
3563 | { | |
b249d2c2 TT |
3564 | gdb::array_view<const gdb_byte> view; |
3565 | if (valaddr != nullptr) | |
df86565b | 3566 | view = gdb::make_array_view (valaddr, type->length ()); |
b249d2c2 | 3567 | struct type *resolved_type = resolve_dynamic_type (type, view, address); |
d36430db | 3568 | struct type *resolved_type_no_typedef = check_typedef (resolved_type); |
41e8491f | 3569 | struct value *v; |
a109c7c1 | 3570 | |
8acb6b92 | 3571 | if (valaddr == NULL) |
80180f79 | 3572 | v = allocate_value_lazy (resolved_type); |
8acb6b92 | 3573 | else |
80180f79 | 3574 | v = value_from_contents (resolved_type, valaddr); |
d36430db JB |
3575 | if (TYPE_DATA_LOCATION (resolved_type_no_typedef) != NULL |
3576 | && TYPE_DATA_LOCATION_KIND (resolved_type_no_typedef) == PROP_CONST) | |
3577 | address = TYPE_DATA_LOCATION_ADDR (resolved_type_no_typedef); | |
33d502b4 | 3578 | VALUE_LVAL (v) = lval_memory; |
1a088441 | 3579 | set_value_address (v, address); |
8acb6b92 TT |
3580 | return v; |
3581 | } | |
3582 | ||
8a9b8146 TT |
3583 | /* Create a value of type TYPE holding the contents CONTENTS. |
3584 | The new value is `not_lval'. */ | |
3585 | ||
3586 | struct value * | |
3587 | value_from_contents (struct type *type, const gdb_byte *contents) | |
3588 | { | |
3589 | struct value *result; | |
3590 | ||
3591 | result = allocate_value (type); | |
df86565b | 3592 | memcpy (value_contents_raw (result).data (), contents, type->length ()); |
8a9b8146 TT |
3593 | return result; |
3594 | } | |
3595 | ||
3bd0f5ef MS |
3596 | /* Extract a value from the history file. Input will be of the form |
3597 | $digits or $$digits. See block comment above 'write_dollar_variable' | |
3598 | for details. */ | |
3599 | ||
3600 | struct value * | |
e799154c | 3601 | value_from_history_ref (const char *h, const char **endp) |
3bd0f5ef MS |
3602 | { |
3603 | int index, len; | |
3604 | ||
3605 | if (h[0] == '$') | |
3606 | len = 1; | |
3607 | else | |
3608 | return NULL; | |
3609 | ||
3610 | if (h[1] == '$') | |
3611 | len = 2; | |
3612 | ||
3613 | /* Find length of numeral string. */ | |
3614 | for (; isdigit (h[len]); len++) | |
3615 | ; | |
3616 | ||
3617 | /* Make sure numeral string is not part of an identifier. */ | |
3618 | if (h[len] == '_' || isalpha (h[len])) | |
3619 | return NULL; | |
3620 | ||
3621 | /* Now collect the index value. */ | |
3622 | if (h[1] == '$') | |
3623 | { | |
3624 | if (len == 2) | |
3625 | { | |
3626 | /* For some bizarre reason, "$$" is equivalent to "$$1", | |
3627 | rather than to "$$0" as it ought to be! */ | |
3628 | index = -1; | |
3629 | *endp += len; | |
3630 | } | |
3631 | else | |
e799154c TT |
3632 | { |
3633 | char *local_end; | |
3634 | ||
3635 | index = -strtol (&h[2], &local_end, 10); | |
3636 | *endp = local_end; | |
3637 | } | |
3bd0f5ef MS |
3638 | } |
3639 | else | |
3640 | { | |
3641 | if (len == 1) | |
3642 | { | |
3643 | /* "$" is equivalent to "$0". */ | |
3644 | index = 0; | |
3645 | *endp += len; | |
3646 | } | |
3647 | else | |
e799154c TT |
3648 | { |
3649 | char *local_end; | |
3650 | ||
3651 | index = strtol (&h[1], &local_end, 10); | |
3652 | *endp = local_end; | |
3653 | } | |
3bd0f5ef MS |
3654 | } |
3655 | ||
3656 | return access_value_history (index); | |
3657 | } | |
3658 | ||
3fff9862 YQ |
3659 | /* Get the component value (offset by OFFSET bytes) of a struct or |
3660 | union WHOLE. Component's type is TYPE. */ | |
3661 | ||
3662 | struct value * | |
3663 | value_from_component (struct value *whole, struct type *type, LONGEST offset) | |
3664 | { | |
3665 | struct value *v; | |
3666 | ||
3667 | if (VALUE_LVAL (whole) == lval_memory && value_lazy (whole)) | |
3668 | v = allocate_value_lazy (type); | |
3669 | else | |
3670 | { | |
3671 | v = allocate_value (type); | |
391f8628 TT |
3672 | value_contents_copy (v, v->embedded_offset (), |
3673 | whole, whole->embedded_offset () + offset, | |
3fff9862 YQ |
3674 | type_length_units (type)); |
3675 | } | |
391f8628 | 3676 | v->m_offset = whole->offset () + offset + whole->embedded_offset (); |
3fff9862 | 3677 | set_value_component_location (v, whole); |
3fff9862 YQ |
3678 | |
3679 | return v; | |
3680 | } | |
3681 | ||
e379f652 TT |
3682 | /* See value.h. */ |
3683 | ||
3684 | struct value * | |
3685 | value_from_component_bitsize (struct value *whole, struct type *type, | |
3686 | LONGEST bit_offset, LONGEST bit_length) | |
3687 | { | |
3688 | gdb_assert (!value_lazy (whole)); | |
3689 | ||
3690 | /* Preserve lvalue-ness if possible. This is needed to avoid | |
3691 | array-printing failures (including crashes) when printing Ada | |
3692 | arrays in programs compiled with -fgnat-encodings=all. */ | |
3693 | if ((bit_offset % TARGET_CHAR_BIT) == 0 | |
3694 | && (bit_length % TARGET_CHAR_BIT) == 0 | |
3695 | && bit_length == TARGET_CHAR_BIT * type->length ()) | |
3696 | return value_from_component (whole, type, bit_offset / TARGET_CHAR_BIT); | |
3697 | ||
3698 | struct value *v = allocate_value (type); | |
3699 | ||
391f8628 | 3700 | LONGEST dst_offset = TARGET_CHAR_BIT * v->embedded_offset (); |
e379f652 TT |
3701 | if (is_scalar_type (type) && type_byte_order (type) == BFD_ENDIAN_BIG) |
3702 | dst_offset += TARGET_CHAR_BIT * type->length () - bit_length; | |
3703 | ||
3704 | value_contents_copy_raw_bitwise (v, dst_offset, | |
3705 | whole, | |
3706 | TARGET_CHAR_BIT | |
391f8628 | 3707 | * whole->embedded_offset () |
e379f652 TT |
3708 | + bit_offset, |
3709 | bit_length); | |
3710 | return v; | |
3711 | } | |
3712 | ||
a471c594 JK |
3713 | struct value * |
3714 | coerce_ref_if_computed (const struct value *arg) | |
3715 | { | |
3716 | const struct lval_funcs *funcs; | |
3717 | ||
d0c97917 | 3718 | if (!TYPE_IS_REFERENCE (check_typedef (arg->type ()))) |
a471c594 JK |
3719 | return NULL; |
3720 | ||
3721 | if (value_lval_const (arg) != lval_computed) | |
3722 | return NULL; | |
3723 | ||
3724 | funcs = value_computed_funcs (arg); | |
3725 | if (funcs->coerce_ref == NULL) | |
3726 | return NULL; | |
3727 | ||
3728 | return funcs->coerce_ref (arg); | |
3729 | } | |
3730 | ||
dfcee124 AG |
3731 | /* Look at value.h for description. */ |
3732 | ||
3733 | struct value * | |
3734 | readjust_indirect_value_type (struct value *value, struct type *enc_type, | |
4bf7b526 | 3735 | const struct type *original_type, |
e79eb02f AB |
3736 | struct value *original_value, |
3737 | CORE_ADDR original_value_address) | |
dfcee124 | 3738 | { |
809f3be1 | 3739 | gdb_assert (original_type->is_pointer_or_reference ()); |
e79eb02f | 3740 | |
27710edb | 3741 | struct type *original_target_type = original_type->target_type (); |
e79eb02f AB |
3742 | gdb::array_view<const gdb_byte> view; |
3743 | struct type *resolved_original_target_type | |
3744 | = resolve_dynamic_type (original_target_type, view, | |
3745 | original_value_address); | |
3746 | ||
dfcee124 | 3747 | /* Re-adjust type. */ |
81ae560c | 3748 | value->deprecated_set_type (resolved_original_target_type); |
dfcee124 AG |
3749 | |
3750 | /* Add embedding info. */ | |
463b870d | 3751 | value->set_enclosing_type (enc_type); |
391f8628 | 3752 | value->set_embedded_offset (original_value->pointed_to_offset ()); |
dfcee124 AG |
3753 | |
3754 | /* We may be pointing to an object of some derived type. */ | |
3755 | return value_full_object (value, NULL, 0, 0, 0); | |
3756 | } | |
3757 | ||
994b9211 AC |
3758 | struct value * |
3759 | coerce_ref (struct value *arg) | |
3760 | { | |
d0c97917 | 3761 | struct type *value_type_arg_tmp = check_typedef (arg->type ()); |
a471c594 | 3762 | struct value *retval; |
dfcee124 | 3763 | struct type *enc_type; |
a109c7c1 | 3764 | |
a471c594 JK |
3765 | retval = coerce_ref_if_computed (arg); |
3766 | if (retval) | |
3767 | return retval; | |
3768 | ||
aa006118 | 3769 | if (!TYPE_IS_REFERENCE (value_type_arg_tmp)) |
a471c594 JK |
3770 | return arg; |
3771 | ||
463b870d | 3772 | enc_type = check_typedef (arg->enclosing_type ()); |
27710edb | 3773 | enc_type = enc_type->target_type (); |
dfcee124 | 3774 | |
d0c97917 | 3775 | CORE_ADDR addr = unpack_pointer (arg->type (), value_contents (arg).data ()); |
e79eb02f | 3776 | retval = value_at_lazy (enc_type, addr); |
d0c97917 | 3777 | enc_type = retval->type (); |
e79eb02f AB |
3778 | return readjust_indirect_value_type (retval, enc_type, value_type_arg_tmp, |
3779 | arg, addr); | |
994b9211 AC |
3780 | } |
3781 | ||
3782 | struct value * | |
3783 | coerce_array (struct value *arg) | |
3784 | { | |
f3134b88 TT |
3785 | struct type *type; |
3786 | ||
994b9211 | 3787 | arg = coerce_ref (arg); |
d0c97917 | 3788 | type = check_typedef (arg->type ()); |
f3134b88 | 3789 | |
78134374 | 3790 | switch (type->code ()) |
f3134b88 TT |
3791 | { |
3792 | case TYPE_CODE_ARRAY: | |
67bd3fd5 | 3793 | if (!type->is_vector () && current_language->c_style_arrays_p ()) |
f3134b88 TT |
3794 | arg = value_coerce_array (arg); |
3795 | break; | |
3796 | case TYPE_CODE_FUNC: | |
3797 | arg = value_coerce_function (arg); | |
3798 | break; | |
3799 | } | |
994b9211 AC |
3800 | return arg; |
3801 | } | |
c906108c | 3802 | \f |
c906108c | 3803 | |
bbfdfe1c DM |
3804 | /* Return the return value convention that will be used for the |
3805 | specified type. */ | |
3806 | ||
3807 | enum return_value_convention | |
3808 | struct_return_convention (struct gdbarch *gdbarch, | |
3809 | struct value *function, struct type *value_type) | |
3810 | { | |
78134374 | 3811 | enum type_code code = value_type->code (); |
bbfdfe1c DM |
3812 | |
3813 | if (code == TYPE_CODE_ERROR) | |
3814 | error (_("Function return type unknown.")); | |
3815 | ||
3816 | /* Probe the architecture for the return-value convention. */ | |
4e1d2f58 TT |
3817 | return gdbarch_return_value_as_value (gdbarch, function, value_type, |
3818 | NULL, NULL, NULL); | |
bbfdfe1c DM |
3819 | } |
3820 | ||
48436ce6 AC |
3821 | /* Return true if the function returning the specified type is using |
3822 | the convention of returning structures in memory (passing in the | |
82585c72 | 3823 | address as a hidden first parameter). */ |
c906108c SS |
3824 | |
3825 | int | |
d80b854b | 3826 | using_struct_return (struct gdbarch *gdbarch, |
6a3a010b | 3827 | struct value *function, struct type *value_type) |
c906108c | 3828 | { |
78134374 | 3829 | if (value_type->code () == TYPE_CODE_VOID) |
667e784f | 3830 | /* A void return value is never in memory. See also corresponding |
44e5158b | 3831 | code in "print_return_value". */ |
667e784f AC |
3832 | return 0; |
3833 | ||
bbfdfe1c | 3834 | return (struct_return_convention (gdbarch, function, value_type) |
31db7b6c | 3835 | != RETURN_VALUE_REGISTER_CONVENTION); |
c906108c SS |
3836 | } |
3837 | ||
42be36b3 CT |
3838 | /* Set the initialized field in a value struct. */ |
3839 | ||
3840 | void | |
3841 | set_value_initialized (struct value *val, int status) | |
3842 | { | |
382d927f | 3843 | val->m_initialized = status; |
42be36b3 CT |
3844 | } |
3845 | ||
3846 | /* Return the initialized field in a value struct. */ | |
3847 | ||
3848 | int | |
4bf7b526 | 3849 | value_initialized (const struct value *val) |
42be36b3 | 3850 | { |
382d927f | 3851 | return val->m_initialized; |
42be36b3 CT |
3852 | } |
3853 | ||
41c60b4b SM |
3854 | /* Helper for value_fetch_lazy when the value is a bitfield. */ |
3855 | ||
3856 | static void | |
3857 | value_fetch_lazy_bitfield (struct value *val) | |
3858 | { | |
f49d5fa2 | 3859 | gdb_assert (val->bitsize () != 0); |
41c60b4b SM |
3860 | |
3861 | /* To read a lazy bitfield, read the entire enclosing value. This | |
3862 | prevents reading the same block of (possibly volatile) memory once | |
3863 | per bitfield. It would be even better to read only the containing | |
3864 | word, but we have no way to record that just specific bits of a | |
3865 | value have been fetched. */ | |
fac7bdaa | 3866 | struct value *parent = val->parent (); |
41c60b4b SM |
3867 | |
3868 | if (value_lazy (parent)) | |
3869 | value_fetch_lazy (parent); | |
3870 | ||
5011c493 | 3871 | unpack_value_bitfield (val, val->bitpos (), val->bitsize (), |
50888e42 | 3872 | value_contents_for_printing (parent).data (), |
76675c4d | 3873 | val->offset (), parent); |
41c60b4b SM |
3874 | } |
3875 | ||
3876 | /* Helper for value_fetch_lazy when the value is in memory. */ | |
3877 | ||
3878 | static void | |
3879 | value_fetch_lazy_memory (struct value *val) | |
3880 | { | |
3881 | gdb_assert (VALUE_LVAL (val) == lval_memory); | |
3882 | ||
3883 | CORE_ADDR addr = value_address (val); | |
463b870d | 3884 | struct type *type = check_typedef (val->enclosing_type ()); |
41c60b4b | 3885 | |
a0c07915 AB |
3886 | /* Figure out how much we should copy from memory. Usually, this is just |
3887 | the size of the type, but, for arrays, we might only be loading a | |
3888 | small part of the array (this is only done for very large arrays). */ | |
3889 | int len = 0; | |
382d927f | 3890 | if (val->m_limited_length > 0) |
a0c07915 | 3891 | { |
d0c97917 | 3892 | gdb_assert (val->type ()->code () == TYPE_CODE_ARRAY); |
382d927f | 3893 | len = val->m_limited_length; |
a0c07915 AB |
3894 | } |
3895 | else if (type->length () > 0) | |
3896 | len = type_length_units (type); | |
3897 | ||
3898 | gdb_assert (len >= 0); | |
3899 | ||
3900 | if (len > 0) | |
3901 | read_value_memory (val, 0, value_stack (val), addr, | |
3902 | value_contents_all_raw (val).data (), len); | |
41c60b4b SM |
3903 | } |
3904 | ||
3905 | /* Helper for value_fetch_lazy when the value is in a register. */ | |
3906 | ||
3907 | static void | |
3908 | value_fetch_lazy_register (struct value *val) | |
3909 | { | |
bd2b40ac | 3910 | frame_info_ptr next_frame; |
41c60b4b | 3911 | int regnum; |
d0c97917 | 3912 | struct type *type = check_typedef (val->type ()); |
41c60b4b SM |
3913 | struct value *new_val = val, *mark = value_mark (); |
3914 | ||
3915 | /* Offsets are not supported here; lazy register values must | |
3916 | refer to the entire register. */ | |
76675c4d | 3917 | gdb_assert (val->offset () == 0); |
41c60b4b SM |
3918 | |
3919 | while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val)) | |
3920 | { | |
3921 | struct frame_id next_frame_id = VALUE_NEXT_FRAME_ID (new_val); | |
3922 | ||
3923 | next_frame = frame_find_by_id (next_frame_id); | |
3924 | regnum = VALUE_REGNUM (new_val); | |
3925 | ||
3926 | gdb_assert (next_frame != NULL); | |
3927 | ||
3928 | /* Convertible register routines are used for multi-register | |
3929 | values and for interpretation in different types | |
3930 | (e.g. float or int from a double register). Lazy | |
3931 | register values should have the register's natural type, | |
3932 | so they do not apply. */ | |
3933 | gdb_assert (!gdbarch_convert_register_p (get_frame_arch (next_frame), | |
3934 | regnum, type)); | |
3935 | ||
3936 | /* FRAME was obtained, above, via VALUE_NEXT_FRAME_ID. | |
3937 | Since a "->next" operation was performed when setting | |
3938 | this field, we do not need to perform a "next" operation | |
3939 | again when unwinding the register. That's why | |
3940 | frame_unwind_register_value() is called here instead of | |
3941 | get_frame_register_value(). */ | |
3942 | new_val = frame_unwind_register_value (next_frame, regnum); | |
3943 | ||
3944 | /* If we get another lazy lval_register value, it means the | |
3945 | register is found by reading it from NEXT_FRAME's next frame. | |
3946 | frame_unwind_register_value should never return a value with | |
3947 | the frame id pointing to NEXT_FRAME. If it does, it means we | |
3948 | either have two consecutive frames with the same frame id | |
3949 | in the frame chain, or some code is trying to unwind | |
3950 | behind get_prev_frame's back (e.g., a frame unwind | |
3951 | sniffer trying to unwind), bypassing its validations. In | |
3952 | any case, it should always be an internal error to end up | |
3953 | in this situation. */ | |
3954 | if (VALUE_LVAL (new_val) == lval_register | |
3955 | && value_lazy (new_val) | |
a0cbd650 | 3956 | && VALUE_NEXT_FRAME_ID (new_val) == next_frame_id) |
f34652de | 3957 | internal_error (_("infinite loop while fetching a register")); |
41c60b4b SM |
3958 | } |
3959 | ||
3960 | /* If it's still lazy (for instance, a saved register on the | |
3961 | stack), fetch it. */ | |
3962 | if (value_lazy (new_val)) | |
3963 | value_fetch_lazy (new_val); | |
3964 | ||
3965 | /* Copy the contents and the unavailability/optimized-out | |
3966 | meta-data from NEW_VAL to VAL. */ | |
3967 | set_value_lazy (val, 0); | |
391f8628 TT |
3968 | value_contents_copy (val, val->embedded_offset (), |
3969 | new_val, new_val->embedded_offset (), | |
41c60b4b SM |
3970 | type_length_units (type)); |
3971 | ||
3972 | if (frame_debug) | |
3973 | { | |
3974 | struct gdbarch *gdbarch; | |
bd2b40ac | 3975 | frame_info_ptr frame; |
ca89bdf8 AB |
3976 | frame = frame_find_by_id (VALUE_NEXT_FRAME_ID (val)); |
3977 | frame = get_prev_frame_always (frame); | |
41c60b4b SM |
3978 | regnum = VALUE_REGNUM (val); |
3979 | gdbarch = get_frame_arch (frame); | |
3980 | ||
a05a883f | 3981 | string_file debug_file; |
6cb06a8c TT |
3982 | gdb_printf (&debug_file, |
3983 | "(frame=%d, regnum=%d(%s), ...) ", | |
3984 | frame_relative_level (frame), regnum, | |
3985 | user_reg_map_regnum_to_name (gdbarch, regnum)); | |
41c60b4b | 3986 | |
6cb06a8c | 3987 | gdb_printf (&debug_file, "->"); |
41c60b4b SM |
3988 | if (value_optimized_out (new_val)) |
3989 | { | |
6cb06a8c | 3990 | gdb_printf (&debug_file, " "); |
a05a883f | 3991 | val_print_optimized_out (new_val, &debug_file); |
41c60b4b SM |
3992 | } |
3993 | else | |
3994 | { | |
3995 | int i; | |
46680d22 | 3996 | gdb::array_view<const gdb_byte> buf = value_contents (new_val); |
41c60b4b SM |
3997 | |
3998 | if (VALUE_LVAL (new_val) == lval_register) | |
6cb06a8c TT |
3999 | gdb_printf (&debug_file, " register=%d", |
4000 | VALUE_REGNUM (new_val)); | |
41c60b4b | 4001 | else if (VALUE_LVAL (new_val) == lval_memory) |
6cb06a8c TT |
4002 | gdb_printf (&debug_file, " address=%s", |
4003 | paddress (gdbarch, | |
4004 | value_address (new_val))); | |
41c60b4b | 4005 | else |
6cb06a8c | 4006 | gdb_printf (&debug_file, " computed"); |
41c60b4b | 4007 | |
6cb06a8c TT |
4008 | gdb_printf (&debug_file, " bytes="); |
4009 | gdb_printf (&debug_file, "["); | |
41c60b4b | 4010 | for (i = 0; i < register_size (gdbarch, regnum); i++) |
6cb06a8c TT |
4011 | gdb_printf (&debug_file, "%02x", buf[i]); |
4012 | gdb_printf (&debug_file, "]"); | |
41c60b4b SM |
4013 | } |
4014 | ||
a05a883f | 4015 | frame_debug_printf ("%s", debug_file.c_str ()); |
41c60b4b SM |
4016 | } |
4017 | ||
4018 | /* Dispose of the intermediate values. This prevents | |
4019 | watchpoints from trying to watch the saved frame pointer. */ | |
4020 | value_free_to_mark (mark); | |
4021 | } | |
4022 | ||
a844296a SM |
4023 | /* Load the actual content of a lazy value. Fetch the data from the |
4024 | user's process and clear the lazy flag to indicate that the data in | |
4025 | the buffer is valid. | |
a58e2656 AB |
4026 | |
4027 | If the value is zero-length, we avoid calling read_memory, which | |
4028 | would abort. We mark the value as fetched anyway -- all 0 bytes of | |
a844296a | 4029 | it. */ |
a58e2656 | 4030 | |
a844296a | 4031 | void |
a58e2656 AB |
4032 | value_fetch_lazy (struct value *val) |
4033 | { | |
4034 | gdb_assert (value_lazy (val)); | |
bae19789 | 4035 | allocate_value_contents (val, true); |
9a0dc9e3 PA |
4036 | /* A value is either lazy, or fully fetched. The |
4037 | availability/validity is only established as we try to fetch a | |
4038 | value. */ | |
382d927f TT |
4039 | gdb_assert (val->m_optimized_out.empty ()); |
4040 | gdb_assert (val->m_unavailable.empty ()); | |
4041 | if (val->m_is_zero) | |
3e44c304 TT |
4042 | { |
4043 | /* Nothing. */ | |
4044 | } | |
f49d5fa2 | 4045 | else if (val->bitsize ()) |
41c60b4b | 4046 | value_fetch_lazy_bitfield (val); |
a58e2656 | 4047 | else if (VALUE_LVAL (val) == lval_memory) |
41c60b4b | 4048 | value_fetch_lazy_memory (val); |
a58e2656 | 4049 | else if (VALUE_LVAL (val) == lval_register) |
41c60b4b | 4050 | value_fetch_lazy_register (val); |
a58e2656 AB |
4051 | else if (VALUE_LVAL (val) == lval_computed |
4052 | && value_computed_funcs (val)->read != NULL) | |
4053 | value_computed_funcs (val)->read (val); | |
a58e2656 | 4054 | else |
f34652de | 4055 | internal_error (_("Unexpected lazy value type.")); |
a58e2656 AB |
4056 | |
4057 | set_value_lazy (val, 0); | |
a58e2656 AB |
4058 | } |
4059 | ||
a280dbd1 SDJ |
4060 | /* Implementation of the convenience function $_isvoid. */ |
4061 | ||
4062 | static struct value * | |
4063 | isvoid_internal_fn (struct gdbarch *gdbarch, | |
4064 | const struct language_defn *language, | |
4065 | void *cookie, int argc, struct value **argv) | |
4066 | { | |
4067 | int ret; | |
4068 | ||
4069 | if (argc != 1) | |
6bc305f5 | 4070 | error (_("You must provide one argument for $_isvoid.")); |
a280dbd1 | 4071 | |
d0c97917 | 4072 | ret = argv[0]->type ()->code () == TYPE_CODE_VOID; |
a280dbd1 SDJ |
4073 | |
4074 | return value_from_longest (builtin_type (gdbarch)->builtin_int, ret); | |
4075 | } | |
4076 | ||
53a008a6 | 4077 | /* Implementation of the convenience function $_creal. Extracts the |
8bdc1658 AB |
4078 | real part from a complex number. */ |
4079 | ||
4080 | static struct value * | |
4081 | creal_internal_fn (struct gdbarch *gdbarch, | |
4082 | const struct language_defn *language, | |
4083 | void *cookie, int argc, struct value **argv) | |
4084 | { | |
4085 | if (argc != 1) | |
4086 | error (_("You must provide one argument for $_creal.")); | |
4087 | ||
4088 | value *cval = argv[0]; | |
d0c97917 | 4089 | type *ctype = check_typedef (cval->type ()); |
78134374 | 4090 | if (ctype->code () != TYPE_CODE_COMPLEX) |
8bdc1658 | 4091 | error (_("expected a complex number")); |
4c99290d | 4092 | return value_real_part (cval); |
8bdc1658 AB |
4093 | } |
4094 | ||
4095 | /* Implementation of the convenience function $_cimag. Extracts the | |
4096 | imaginary part from a complex number. */ | |
4097 | ||
4098 | static struct value * | |
4099 | cimag_internal_fn (struct gdbarch *gdbarch, | |
4100 | const struct language_defn *language, | |
4101 | void *cookie, int argc, | |
4102 | struct value **argv) | |
4103 | { | |
4104 | if (argc != 1) | |
4105 | error (_("You must provide one argument for $_cimag.")); | |
4106 | ||
4107 | value *cval = argv[0]; | |
d0c97917 | 4108 | type *ctype = check_typedef (cval->type ()); |
78134374 | 4109 | if (ctype->code () != TYPE_CODE_COMPLEX) |
8bdc1658 | 4110 | error (_("expected a complex number")); |
4c99290d | 4111 | return value_imaginary_part (cval); |
8bdc1658 AB |
4112 | } |
4113 | ||
d5f4488f SM |
4114 | #if GDB_SELF_TEST |
4115 | namespace selftests | |
4116 | { | |
4117 | ||
4118 | /* Test the ranges_contain function. */ | |
4119 | ||
4120 | static void | |
4121 | test_ranges_contain () | |
4122 | { | |
4123 | std::vector<range> ranges; | |
4124 | range r; | |
4125 | ||
4126 | /* [10, 14] */ | |
4127 | r.offset = 10; | |
4128 | r.length = 5; | |
4129 | ranges.push_back (r); | |
4130 | ||
4131 | /* [20, 24] */ | |
4132 | r.offset = 20; | |
4133 | r.length = 5; | |
4134 | ranges.push_back (r); | |
4135 | ||
4136 | /* [2, 6] */ | |
4137 | SELF_CHECK (!ranges_contain (ranges, 2, 5)); | |
4138 | /* [9, 13] */ | |
4139 | SELF_CHECK (ranges_contain (ranges, 9, 5)); | |
4140 | /* [10, 11] */ | |
4141 | SELF_CHECK (ranges_contain (ranges, 10, 2)); | |
4142 | /* [10, 14] */ | |
4143 | SELF_CHECK (ranges_contain (ranges, 10, 5)); | |
4144 | /* [13, 18] */ | |
4145 | SELF_CHECK (ranges_contain (ranges, 13, 6)); | |
4146 | /* [14, 18] */ | |
4147 | SELF_CHECK (ranges_contain (ranges, 14, 5)); | |
4148 | /* [15, 18] */ | |
4149 | SELF_CHECK (!ranges_contain (ranges, 15, 4)); | |
4150 | /* [16, 19] */ | |
4151 | SELF_CHECK (!ranges_contain (ranges, 16, 4)); | |
4152 | /* [16, 21] */ | |
4153 | SELF_CHECK (ranges_contain (ranges, 16, 6)); | |
4154 | /* [21, 21] */ | |
4155 | SELF_CHECK (ranges_contain (ranges, 21, 1)); | |
4156 | /* [21, 25] */ | |
4157 | SELF_CHECK (ranges_contain (ranges, 21, 5)); | |
4158 | /* [26, 28] */ | |
4159 | SELF_CHECK (!ranges_contain (ranges, 26, 3)); | |
4160 | } | |
4161 | ||
4162 | /* Check that RANGES contains the same ranges as EXPECTED. */ | |
4163 | ||
4164 | static bool | |
4165 | check_ranges_vector (gdb::array_view<const range> ranges, | |
4166 | gdb::array_view<const range> expected) | |
4167 | { | |
4168 | return ranges == expected; | |
4169 | } | |
4170 | ||
4171 | /* Test the insert_into_bit_range_vector function. */ | |
4172 | ||
4173 | static void | |
4174 | test_insert_into_bit_range_vector () | |
4175 | { | |
4176 | std::vector<range> ranges; | |
4177 | ||
4178 | /* [10, 14] */ | |
4179 | { | |
4180 | insert_into_bit_range_vector (&ranges, 10, 5); | |
4181 | static const range expected[] = { | |
4182 | {10, 5} | |
4183 | }; | |
4184 | SELF_CHECK (check_ranges_vector (ranges, expected)); | |
4185 | } | |
4186 | ||
4187 | /* [10, 14] */ | |
4188 | { | |
4189 | insert_into_bit_range_vector (&ranges, 11, 4); | |
4190 | static const range expected = {10, 5}; | |
4191 | SELF_CHECK (check_ranges_vector (ranges, expected)); | |
4192 | } | |
4193 | ||
4194 | /* [10, 14] [20, 24] */ | |
4195 | { | |
4196 | insert_into_bit_range_vector (&ranges, 20, 5); | |
4197 | static const range expected[] = { | |
4198 | {10, 5}, | |
4199 | {20, 5}, | |
4200 | }; | |
4201 | SELF_CHECK (check_ranges_vector (ranges, expected)); | |
4202 | } | |
4203 | ||
4204 | /* [10, 14] [17, 24] */ | |
4205 | { | |
4206 | insert_into_bit_range_vector (&ranges, 17, 5); | |
4207 | static const range expected[] = { | |
4208 | {10, 5}, | |
4209 | {17, 8}, | |
4210 | }; | |
4211 | SELF_CHECK (check_ranges_vector (ranges, expected)); | |
4212 | } | |
4213 | ||
4214 | /* [2, 8] [10, 14] [17, 24] */ | |
4215 | { | |
4216 | insert_into_bit_range_vector (&ranges, 2, 7); | |
4217 | static const range expected[] = { | |
4218 | {2, 7}, | |
4219 | {10, 5}, | |
4220 | {17, 8}, | |
4221 | }; | |
4222 | SELF_CHECK (check_ranges_vector (ranges, expected)); | |
4223 | } | |
4224 | ||
4225 | /* [2, 14] [17, 24] */ | |
4226 | { | |
4227 | insert_into_bit_range_vector (&ranges, 9, 1); | |
4228 | static const range expected[] = { | |
4229 | {2, 13}, | |
4230 | {17, 8}, | |
4231 | }; | |
4232 | SELF_CHECK (check_ranges_vector (ranges, expected)); | |
4233 | } | |
4234 | ||
4235 | /* [2, 14] [17, 24] */ | |
4236 | { | |
4237 | insert_into_bit_range_vector (&ranges, 9, 1); | |
4238 | static const range expected[] = { | |
4239 | {2, 13}, | |
4240 | {17, 8}, | |
4241 | }; | |
4242 | SELF_CHECK (check_ranges_vector (ranges, expected)); | |
4243 | } | |
4244 | ||
4245 | /* [2, 33] */ | |
4246 | { | |
4247 | insert_into_bit_range_vector (&ranges, 4, 30); | |
4248 | static const range expected = {2, 32}; | |
4249 | SELF_CHECK (check_ranges_vector (ranges, expected)); | |
4250 | } | |
4251 | } | |
4252 | ||
6d088eb9 SM |
4253 | static void |
4254 | test_value_copy () | |
4255 | { | |
4256 | type *type = builtin_type (current_inferior ()->gdbarch)->builtin_int; | |
4257 | ||
4258 | /* Verify that we can copy an entirely optimized out value, that may not have | |
4259 | its contents allocated. */ | |
4260 | value_ref_ptr val = release_value (allocate_optimized_out_value (type)); | |
4261 | value_ref_ptr copy = release_value (value_copy (val.get ())); | |
4262 | ||
4263 | SELF_CHECK (value_entirely_optimized_out (val.get ())); | |
4264 | SELF_CHECK (value_entirely_optimized_out (copy.get ())); | |
4265 | } | |
4266 | ||
d5f4488f SM |
4267 | } /* namespace selftests */ |
4268 | #endif /* GDB_SELF_TEST */ | |
4269 | ||
6c265988 | 4270 | void _initialize_values (); |
c906108c | 4271 | void |
6c265988 | 4272 | _initialize_values () |
c906108c | 4273 | { |
5e84b7ee SM |
4274 | cmd_list_element *show_convenience_cmd |
4275 | = add_cmd ("convenience", no_class, show_convenience, _("\ | |
f47f77df DE |
4276 | Debugger convenience (\"$foo\") variables and functions.\n\ |
4277 | Convenience variables are created when you assign them values;\n\ | |
4278 | thus, \"set $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\ | |
1a966eab | 4279 | \n\ |
c906108c SS |
4280 | A few convenience variables are given values automatically:\n\ |
4281 | \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\ | |
f47f77df DE |
4282 | \"$__\" holds the contents of the last address examined with \"x\"." |
4283 | #ifdef HAVE_PYTHON | |
4284 | "\n\n\ | |
4285 | Convenience functions are defined via the Python API." | |
4286 | #endif | |
4287 | ), &showlist); | |
5e84b7ee | 4288 | add_alias_cmd ("conv", show_convenience_cmd, no_class, 1, &showlist); |
c906108c | 4289 | |
db5f229b | 4290 | add_cmd ("values", no_set_class, show_values, _("\ |
3e43a32a | 4291 | Elements of value history around item number IDX (or last ten)."), |
c906108c | 4292 | &showlist); |
53e5f3cf AS |
4293 | |
4294 | add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\ | |
4295 | Initialize a convenience variable if necessary.\n\ | |
4296 | init-if-undefined VARIABLE = EXPRESSION\n\ | |
4297 | Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\ | |
4298 | exist or does not contain a value. The EXPRESSION is not evaluated if the\n\ | |
4299 | VARIABLE is already initialized.")); | |
bc3b79fd TJB |
4300 | |
4301 | add_prefix_cmd ("function", no_class, function_command, _("\ | |
4302 | Placeholder command for showing help on convenience functions."), | |
2f822da5 | 4303 | &functionlist, 0, &cmdlist); |
a280dbd1 SDJ |
4304 | |
4305 | add_internal_function ("_isvoid", _("\ | |
4306 | Check whether an expression is void.\n\ | |
4307 | Usage: $_isvoid (expression)\n\ | |
4308 | Return 1 if the expression is void, zero otherwise."), | |
4309 | isvoid_internal_fn, NULL); | |
5fdf6324 | 4310 | |
8bdc1658 AB |
4311 | add_internal_function ("_creal", _("\ |
4312 | Extract the real part of a complex number.\n\ | |
4313 | Usage: $_creal (expression)\n\ | |
4314 | Return the real part of a complex number, the type depends on the\n\ | |
4315 | type of a complex number."), | |
4316 | creal_internal_fn, NULL); | |
4317 | ||
4318 | add_internal_function ("_cimag", _("\ | |
4319 | Extract the imaginary part of a complex number.\n\ | |
4320 | Usage: $_cimag (expression)\n\ | |
4321 | Return the imaginary part of a complex number, the type depends on the\n\ | |
4322 | type of a complex number."), | |
4323 | cimag_internal_fn, NULL); | |
4324 | ||
5fdf6324 AB |
4325 | add_setshow_zuinteger_unlimited_cmd ("max-value-size", |
4326 | class_support, &max_value_size, _("\ | |
4327 | Set maximum sized value gdb will load from the inferior."), _("\ | |
4328 | Show maximum sized value gdb will load from the inferior."), _("\ | |
4329 | Use this to control the maximum size, in bytes, of a value that gdb\n\ | |
4330 | will load from the inferior. Setting this value to 'unlimited'\n\ | |
4331 | disables checking.\n\ | |
4332 | Setting this does not invalidate already allocated values, it only\n\ | |
4333 | prevents future values, larger than this size, from being allocated."), | |
4334 | set_max_value_size, | |
4335 | show_max_value_size, | |
4336 | &setlist, &showlist); | |
acbf4a58 TT |
4337 | set_show_commands vsize_limit |
4338 | = add_setshow_zuinteger_unlimited_cmd ("varsize-limit", class_support, | |
4339 | &max_value_size, _("\ | |
4340 | Set the maximum number of bytes allowed in a variable-size object."), _("\ | |
4341 | Show the maximum number of bytes allowed in a variable-size object."), _("\ | |
4342 | Attempts to access an object whose size is not a compile-time constant\n\ | |
4343 | and exceeds this limit will cause an error."), | |
4344 | NULL, NULL, &setlist, &showlist); | |
4345 | deprecate_cmd (vsize_limit.set, "set max-value-size"); | |
4346 | ||
d5f4488f SM |
4347 | #if GDB_SELF_TEST |
4348 | selftests::register_test ("ranges_contain", selftests::test_ranges_contain); | |
4349 | selftests::register_test ("insert_into_bit_range_vector", | |
4350 | selftests::test_insert_into_bit_range_vector); | |
6d088eb9 | 4351 | selftests::register_test ("value_copy", selftests::test_value_copy); |
d5f4488f | 4352 | #endif |
c906108c | 4353 | } |
9d1447e0 SDJ |
4354 | |
4355 | /* See value.h. */ | |
4356 | ||
4357 | void | |
4358 | finalize_values () | |
4359 | { | |
4360 | all_values.clear (); | |
4361 | } |