gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2loc.c
CommitLineData
4c2df51b 1/* DWARF 2 location expression support for GDB.
feb13ab0 2
4c38e0a4
JB
3 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
feb13ab0 5
4c2df51b
DJ
6 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7
JB
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
4c2df51b 14
a9762ec7
JB
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
4c2df51b
DJ
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4c2df51b
DJ
22
23#include "defs.h"
24#include "ui-out.h"
25#include "value.h"
26#include "frame.h"
27#include "gdbcore.h"
28#include "target.h"
29#include "inferior.h"
a55cc764
DJ
30#include "ax.h"
31#include "ax-gdb.h"
e4adbba9 32#include "regcache.h"
c3228f12 33#include "objfiles.h"
93ad78a7 34#include "exceptions.h"
edb3359d 35#include "block.h"
4c2df51b 36
fa8f86ff 37#include "dwarf2.h"
4c2df51b
DJ
38#include "dwarf2expr.h"
39#include "dwarf2loc.h"
e7802207 40#include "dwarf2-frame.h"
4c2df51b
DJ
41
42#include "gdb_string.h"
eff4f95e 43#include "gdb_assert.h"
4c2df51b 44
9eae7c52
TT
45extern int dwarf2_always_disassemble;
46
0936ad1d
SS
47static void
48dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
0d45f56e 49 const gdb_byte **start, size_t *length);
0936ad1d 50
0d53c4c4
DJ
51/* A helper function for dealing with location lists. Given a
52 symbol baton (BATON) and a pc value (PC), find the appropriate
53 location expression, set *LOCEXPR_LENGTH, and return a pointer
54 to the beginning of the expression. Returns NULL on failure.
55
56 For now, only return the first matching location expression; there
57 can be more than one in the list. */
58
947bb88f 59static const gdb_byte *
0d53c4c4 60find_location_expression (struct dwarf2_loclist_baton *baton,
b6b08ebf 61 size_t *locexpr_length, CORE_ADDR pc)
0d53c4c4 62{
0d53c4c4 63 CORE_ADDR low, high;
947bb88f 64 const gdb_byte *loc_ptr, *buf_end;
852483bc 65 int length;
ae0d2f24 66 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
f7fd4728 67 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e17a4113 68 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
ae0d2f24 69 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
0d53c4c4 70 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8edfa926 71 /* Adjust base_address for relocatable objects. */
ae0d2f24
UW
72 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
73 SECT_OFF_TEXT (objfile));
8edfa926 74 CORE_ADDR base_address = baton->base_address + base_offset;
0d53c4c4
DJ
75
76 loc_ptr = baton->data;
77 buf_end = baton->data + baton->size;
78
79 while (1)
80 {
b5758fe4
UW
81 if (buf_end - loc_ptr < 2 * addr_size)
82 error (_("find_location_expression: Corrupted DWARF expression."));
0d53c4c4 83
b5758fe4
UW
84 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
85 loc_ptr += addr_size;
0d53c4c4
DJ
86
87 /* A base-address-selection entry. */
b5758fe4 88 if (low == base_mask)
0d53c4c4 89 {
b5758fe4
UW
90 base_address = dwarf2_read_address (gdbarch,
91 loc_ptr, buf_end, addr_size);
92 loc_ptr += addr_size;
0d53c4c4
DJ
93 continue;
94 }
95
b5758fe4
UW
96 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
97 loc_ptr += addr_size;
98
99 /* An end-of-list entry. */
100 if (low == 0 && high == 0)
101 return NULL;
102
0d53c4c4
DJ
103 /* Otherwise, a location expression entry. */
104 low += base_address;
105 high += base_address;
106
e17a4113 107 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
0d53c4c4
DJ
108 loc_ptr += 2;
109
110 if (pc >= low && pc < high)
111 {
112 *locexpr_length = length;
113 return loc_ptr;
114 }
115
116 loc_ptr += length;
117 }
118}
119
4c2df51b
DJ
120/* This is the baton used when performing dwarf2 expression
121 evaluation. */
122struct dwarf_expr_baton
123{
124 struct frame_info *frame;
17ea53c3 125 struct dwarf2_per_cu_data *per_cu;
4c2df51b
DJ
126};
127
128/* Helper functions for dwarf2_evaluate_loc_desc. */
129
4bc9efe1 130/* Using the frame specified in BATON, return the value of register
0b2b0195 131 REGNUM, treated as a pointer. */
4c2df51b 132static CORE_ADDR
61fbb938 133dwarf_expr_read_reg (void *baton, int dwarf_regnum)
4c2df51b 134{
4c2df51b 135 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
5e2b427d 136 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
e5192dd8 137 CORE_ADDR result;
0b2b0195 138 int regnum;
e4adbba9 139
5e2b427d
UW
140 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
141 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
0b2b0195 142 regnum, debaton->frame);
4c2df51b
DJ
143 return result;
144}
145
146/* Read memory at ADDR (length LEN) into BUF. */
147
148static void
852483bc 149dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
4c2df51b
DJ
150{
151 read_memory (addr, buf, len);
152}
153
154/* Using the frame specified in BATON, find the location expression
155 describing the frame base. Return a pointer to it in START and
156 its length in LENGTH. */
157static void
0d45f56e 158dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
4c2df51b 159{
da62e633
AC
160 /* FIXME: cagney/2003-03-26: This code should be using
161 get_frame_base_address(), and then implement a dwarf2 specific
162 this_base method. */
4c2df51b 163 struct symbol *framefunc;
4c2df51b 164 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
0d53c4c4 165
edb3359d
DJ
166 /* Use block_linkage_function, which returns a real (not inlined)
167 function, instead of get_frame_function, which may return an
168 inlined function. */
169 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
0d53c4c4 170
eff4f95e
JG
171 /* If we found a frame-relative symbol then it was certainly within
172 some function associated with a frame. If we can't find the frame,
173 something has gone wrong. */
174 gdb_assert (framefunc != NULL);
175
0936ad1d
SS
176 dwarf_expr_frame_base_1 (framefunc,
177 get_frame_address_in_block (debaton->frame),
178 start, length);
179}
180
181static void
182dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
0d45f56e 183 const gdb_byte **start, size_t *length)
0936ad1d 184{
edb3359d
DJ
185 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
186 *start = NULL;
187 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
0d53c4c4
DJ
188 {
189 struct dwarf2_loclist_baton *symbaton;
22c6caba 190
0d53c4c4 191 symbaton = SYMBOL_LOCATION_BATON (framefunc);
0936ad1d 192 *start = find_location_expression (symbaton, length, pc);
0d53c4c4
DJ
193 }
194 else
195 {
196 struct dwarf2_locexpr_baton *symbaton;
9a619af0 197
0d53c4c4 198 symbaton = SYMBOL_LOCATION_BATON (framefunc);
ebd3bcc1
JK
199 if (symbaton != NULL)
200 {
201 *length = symbaton->size;
202 *start = symbaton->data;
203 }
204 else
205 *start = NULL;
0d53c4c4
DJ
206 }
207
208 if (*start == NULL)
8a3fe4f8 209 error (_("Could not find the frame base for \"%s\"."),
0d53c4c4 210 SYMBOL_NATURAL_NAME (framefunc));
4c2df51b
DJ
211}
212
e7802207
TT
213/* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
214 the frame in BATON. */
215
216static CORE_ADDR
217dwarf_expr_frame_cfa (void *baton)
218{
219 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
9a619af0 220
e7802207
TT
221 return dwarf2_frame_cfa (debaton->frame);
222}
223
4c2df51b
DJ
224/* Using the objfile specified in BATON, find the address for the
225 current thread's thread-local storage with offset OFFSET. */
226static CORE_ADDR
227dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
228{
229 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
17ea53c3 230 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
4c2df51b 231
17ea53c3 232 return target_translate_tls_address (objfile, offset);
4c2df51b
DJ
233}
234
052b9502
NF
235struct piece_closure
236{
88bfdde4
TT
237 /* Reference count. */
238 int refc;
239
052b9502
NF
240 /* The number of pieces used to describe this variable. */
241 int n_pieces;
242
6063c216
UW
243 /* The target address size, used only for DWARF_VALUE_STACK. */
244 int addr_size;
cec03d70 245
052b9502
NF
246 /* The pieces themselves. */
247 struct dwarf_expr_piece *pieces;
248};
249
250/* Allocate a closure for a value formed from separately-described
251 PIECES. */
252
253static struct piece_closure *
cec03d70 254allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
6063c216 255 int addr_size)
052b9502
NF
256{
257 struct piece_closure *c = XZALLOC (struct piece_closure);
258
88bfdde4 259 c->refc = 1;
052b9502 260 c->n_pieces = n_pieces;
6063c216 261 c->addr_size = addr_size;
052b9502
NF
262 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
263
264 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
265
266 return c;
267}
268
d3b1e874
TT
269/* The lowest-level function to extract bits from a byte buffer.
270 SOURCE is the buffer. It is updated if we read to the end of a
271 byte.
272 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
273 updated to reflect the number of bits actually read.
274 NBITS is the number of bits we want to read. It is updated to
275 reflect the number of bits actually read. This function may read
276 fewer bits.
277 BITS_BIG_ENDIAN is taken directly from gdbarch.
278 This function returns the extracted bits. */
279
280static unsigned int
281extract_bits_primitive (const gdb_byte **source,
282 unsigned int *source_offset_bits,
283 int *nbits, int bits_big_endian)
284{
285 unsigned int avail, mask, datum;
286
287 gdb_assert (*source_offset_bits < 8);
288
289 avail = 8 - *source_offset_bits;
290 if (avail > *nbits)
291 avail = *nbits;
292
293 mask = (1 << avail) - 1;
294 datum = **source;
295 if (bits_big_endian)
296 datum >>= 8 - (*source_offset_bits + *nbits);
297 else
298 datum >>= *source_offset_bits;
299 datum &= mask;
300
301 *nbits -= avail;
302 *source_offset_bits += avail;
303 if (*source_offset_bits >= 8)
304 {
305 *source_offset_bits -= 8;
306 ++*source;
307 }
308
309 return datum;
310}
311
312/* Extract some bits from a source buffer and move forward in the
313 buffer.
314
315 SOURCE is the source buffer. It is updated as bytes are read.
316 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
317 bits are read.
318 NBITS is the number of bits to read.
319 BITS_BIG_ENDIAN is taken directly from gdbarch.
320
321 This function returns the bits that were read. */
322
323static unsigned int
324extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
325 int nbits, int bits_big_endian)
326{
327 unsigned int datum;
328
329 gdb_assert (nbits > 0 && nbits <= 8);
330
331 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
332 bits_big_endian);
333 if (nbits > 0)
334 {
335 unsigned int more;
336
337 more = extract_bits_primitive (source, source_offset_bits, &nbits,
338 bits_big_endian);
339 if (bits_big_endian)
340 datum <<= nbits;
341 else
342 more <<= nbits;
343 datum |= more;
344 }
345
346 return datum;
347}
348
349/* Write some bits into a buffer and move forward in the buffer.
350
351 DATUM is the bits to write. The low-order bits of DATUM are used.
352 DEST is the destination buffer. It is updated as bytes are
353 written.
354 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
355 done.
356 NBITS is the number of valid bits in DATUM.
357 BITS_BIG_ENDIAN is taken directly from gdbarch. */
358
359static void
360insert_bits (unsigned int datum,
361 gdb_byte *dest, unsigned int dest_offset_bits,
362 int nbits, int bits_big_endian)
363{
364 unsigned int mask;
365
366 gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
367
368 mask = (1 << nbits) - 1;
369 if (bits_big_endian)
370 {
371 datum <<= 8 - (dest_offset_bits + nbits);
372 mask <<= 8 - (dest_offset_bits + nbits);
373 }
374 else
375 {
376 datum <<= dest_offset_bits;
377 mask <<= dest_offset_bits;
378 }
379
380 gdb_assert ((datum & ~mask) == 0);
381
382 *dest = (*dest & ~mask) | datum;
383}
384
385/* Copy bits from a source to a destination.
386
387 DEST is where the bits should be written.
388 DEST_OFFSET_BITS is the bit offset into DEST.
389 SOURCE is the source of bits.
390 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
391 BIT_COUNT is the number of bits to copy.
392 BITS_BIG_ENDIAN is taken directly from gdbarch. */
393
394static void
395copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
396 const gdb_byte *source, unsigned int source_offset_bits,
397 unsigned int bit_count,
398 int bits_big_endian)
399{
400 unsigned int dest_avail;
401 int datum;
402
403 /* Reduce everything to byte-size pieces. */
404 dest += dest_offset_bits / 8;
405 dest_offset_bits %= 8;
406 source += source_offset_bits / 8;
407 source_offset_bits %= 8;
408
409 dest_avail = 8 - dest_offset_bits % 8;
410
411 /* See if we can fill the first destination byte. */
412 if (dest_avail < bit_count)
413 {
414 datum = extract_bits (&source, &source_offset_bits, dest_avail,
415 bits_big_endian);
416 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
417 ++dest;
418 dest_offset_bits = 0;
419 bit_count -= dest_avail;
420 }
421
422 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
423 than 8 bits remaining. */
424 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
425 for (; bit_count >= 8; bit_count -= 8)
426 {
427 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
428 *dest++ = (gdb_byte) datum;
429 }
430
431 /* Finally, we may have a few leftover bits. */
432 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
433 if (bit_count > 0)
434 {
435 datum = extract_bits (&source, &source_offset_bits, bit_count,
436 bits_big_endian);
437 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
438 }
439}
440
052b9502
NF
441static void
442read_pieced_value (struct value *v)
443{
444 int i;
445 long offset = 0;
d3b1e874 446 ULONGEST bits_to_skip;
052b9502
NF
447 gdb_byte *contents;
448 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
449 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
afd74c5f 450 size_t type_len;
d3b1e874
TT
451 size_t buffer_size = 0;
452 char *buffer = NULL;
453 struct cleanup *cleanup;
454 int bits_big_endian
455 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
afd74c5f
TT
456
457 if (value_type (v) != value_enclosing_type (v))
458 internal_error (__FILE__, __LINE__,
459 _("Should not be able to create a lazy value with "
460 "an enclosing type"));
052b9502 461
d3b1e874
TT
462 cleanup = make_cleanup (free_current_contents, &buffer);
463
052b9502 464 contents = value_contents_raw (v);
d3b1e874
TT
465 bits_to_skip = 8 * value_offset (v);
466 type_len = 8 * TYPE_LENGTH (value_type (v));
467
afd74c5f 468 for (i = 0; i < c->n_pieces && offset < type_len; i++)
052b9502
NF
469 {
470 struct dwarf_expr_piece *p = &c->pieces[i];
d3b1e874
TT
471 size_t this_size, this_size_bits;
472 long dest_offset_bits, source_offset_bits, source_offset;
0d45f56e 473 const gdb_byte *intermediate_buffer;
d3b1e874
TT
474
475 /* Compute size, source, and destination offsets for copying, in
476 bits. */
477 this_size_bits = p->size;
478 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
afd74c5f 479 {
d3b1e874 480 bits_to_skip -= this_size_bits;
afd74c5f
TT
481 continue;
482 }
d3b1e874
TT
483 if (this_size_bits > type_len - offset)
484 this_size_bits = type_len - offset;
485 if (bits_to_skip > 0)
afd74c5f 486 {
d3b1e874
TT
487 dest_offset_bits = 0;
488 source_offset_bits = bits_to_skip;
489 this_size_bits -= bits_to_skip;
490 bits_to_skip = 0;
afd74c5f
TT
491 }
492 else
493 {
d3b1e874
TT
494 dest_offset_bits = offset;
495 source_offset_bits = 0;
afd74c5f 496 }
9a619af0 497
d3b1e874
TT
498 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
499 source_offset = source_offset_bits / 8;
500 if (buffer_size < this_size)
501 {
502 buffer_size = this_size;
503 buffer = xrealloc (buffer, buffer_size);
504 }
505 intermediate_buffer = buffer;
506
507 /* Copy from the source to DEST_BUFFER. */
cec03d70 508 switch (p->location)
052b9502 509 {
cec03d70
TT
510 case DWARF_VALUE_REGISTER:
511 {
512 struct gdbarch *arch = get_frame_arch (frame);
cec03d70 513 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
44353522 514 p->v.expr.value);
afd74c5f 515 int reg_offset = source_offset;
dcbf108f
UW
516
517 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
afd74c5f 518 && this_size < register_size (arch, gdb_regnum))
d3b1e874
TT
519 {
520 /* Big-endian, and we want less than full size. */
521 reg_offset = register_size (arch, gdb_regnum) - this_size;
522 /* We want the lower-order THIS_SIZE_BITS of the bytes
523 we extract from the register. */
524 source_offset_bits += 8 * this_size - this_size_bits;
525 }
dcbf108f 526
63b4f126
MGD
527 if (gdb_regnum != -1)
528 {
529 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
d3b1e874 530 this_size, buffer);
63b4f126
MGD
531 }
532 else
533 {
534 error (_("Unable to access DWARF register number %s"),
535 paddress (arch, p->v.expr.value));
536 }
cec03d70
TT
537 }
538 break;
539
540 case DWARF_VALUE_MEMORY:
44353522 541 if (p->v.expr.in_stack_memory)
d3b1e874 542 read_stack (p->v.expr.value + source_offset, buffer, this_size);
44353522 543 else
d3b1e874 544 read_memory (p->v.expr.value + source_offset, buffer, this_size);
cec03d70
TT
545 break;
546
547 case DWARF_VALUE_STACK:
548 {
6063c216 549 struct gdbarch *gdbarch = get_type_arch (value_type (v));
afd74c5f 550 size_t n = this_size;
9a619af0 551
afd74c5f
TT
552 if (n > c->addr_size - source_offset)
553 n = (c->addr_size >= source_offset
554 ? c->addr_size - source_offset
555 : 0);
556 if (n == 0)
557 {
558 /* Nothing. */
559 }
560 else if (source_offset == 0)
d3b1e874 561 store_unsigned_integer (buffer, n,
afd74c5f
TT
562 gdbarch_byte_order (gdbarch),
563 p->v.expr.value);
564 else
565 {
566 gdb_byte bytes[sizeof (ULONGEST)];
567
568 store_unsigned_integer (bytes, n + source_offset,
569 gdbarch_byte_order (gdbarch),
570 p->v.expr.value);
d3b1e874 571 memcpy (buffer, bytes + source_offset, n);
afd74c5f 572 }
cec03d70
TT
573 }
574 break;
575
576 case DWARF_VALUE_LITERAL:
577 {
afd74c5f
TT
578 size_t n = this_size;
579
580 if (n > p->v.literal.length - source_offset)
581 n = (p->v.literal.length >= source_offset
582 ? p->v.literal.length - source_offset
583 : 0);
584 if (n != 0)
d3b1e874 585 intermediate_buffer = p->v.literal.data + source_offset;
cec03d70
TT
586 }
587 break;
588
cb826367
TT
589 case DWARF_VALUE_OPTIMIZED_OUT:
590 /* We just leave the bits empty for now. This is not ideal
591 but gdb currently does not have a nice way to represent
592 optimized-out pieces. */
d3b1e874 593 warning (_("bits %ld-%ld in computed object were optimized out; "
cb826367
TT
594 "replacing with zeroes"),
595 offset,
d3b1e874 596 offset + (long) this_size_bits);
cb826367
TT
597 break;
598
cec03d70
TT
599 default:
600 internal_error (__FILE__, __LINE__, _("invalid location type"));
052b9502 601 }
d3b1e874
TT
602
603 if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
604 copy_bitwise (contents, dest_offset_bits,
605 intermediate_buffer, source_offset_bits % 8,
606 this_size_bits, bits_big_endian);
607
608 offset += this_size_bits;
052b9502 609 }
d3b1e874
TT
610
611 do_cleanups (cleanup);
052b9502
NF
612}
613
614static void
615write_pieced_value (struct value *to, struct value *from)
616{
617 int i;
618 long offset = 0;
d3b1e874 619 ULONGEST bits_to_skip;
afd74c5f 620 const gdb_byte *contents;
052b9502
NF
621 struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
622 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
afd74c5f 623 size_t type_len;
d3b1e874
TT
624 size_t buffer_size = 0;
625 char *buffer = NULL;
626 struct cleanup *cleanup;
627 int bits_big_endian
628 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
052b9502
NF
629
630 if (frame == NULL)
631 {
632 set_value_optimized_out (to, 1);
633 return;
634 }
635
d3b1e874
TT
636 cleanup = make_cleanup (free_current_contents, &buffer);
637
afd74c5f 638 contents = value_contents (from);
d3b1e874
TT
639 bits_to_skip = 8 * value_offset (to);
640 type_len = 8 * TYPE_LENGTH (value_type (to));
afd74c5f 641 for (i = 0; i < c->n_pieces && offset < type_len; i++)
052b9502
NF
642 {
643 struct dwarf_expr_piece *p = &c->pieces[i];
d3b1e874
TT
644 size_t this_size_bits, this_size;
645 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
646 int need_bitwise;
647 const gdb_byte *source_buffer;
afd74c5f 648
d3b1e874
TT
649 this_size_bits = p->size;
650 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
afd74c5f 651 {
d3b1e874 652 bits_to_skip -= this_size_bits;
afd74c5f
TT
653 continue;
654 }
d3b1e874
TT
655 if (this_size_bits > type_len - offset)
656 this_size_bits = type_len - offset;
657 if (bits_to_skip > 0)
afd74c5f 658 {
d3b1e874
TT
659 dest_offset_bits = bits_to_skip;
660 source_offset_bits = 0;
661 this_size_bits -= bits_to_skip;
662 bits_to_skip = 0;
afd74c5f
TT
663 }
664 else
665 {
d3b1e874
TT
666 dest_offset_bits = 0;
667 source_offset_bits = offset;
668 }
669
670 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
671 source_offset = source_offset_bits / 8;
672 dest_offset = dest_offset_bits / 8;
673 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
674 {
675 source_buffer = contents + source_offset;
676 need_bitwise = 0;
677 }
678 else
679 {
680 if (buffer_size < this_size)
681 {
682 buffer_size = this_size;
683 buffer = xrealloc (buffer, buffer_size);
684 }
685 source_buffer = buffer;
686 need_bitwise = 1;
afd74c5f 687 }
9a619af0 688
cec03d70 689 switch (p->location)
052b9502 690 {
cec03d70
TT
691 case DWARF_VALUE_REGISTER:
692 {
693 struct gdbarch *arch = get_frame_arch (frame);
44353522 694 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
afd74c5f 695 int reg_offset = dest_offset;
dcbf108f
UW
696
697 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
afd74c5f 698 && this_size <= register_size (arch, gdb_regnum))
dcbf108f 699 /* Big-endian, and we want less than full size. */
afd74c5f 700 reg_offset = register_size (arch, gdb_regnum) - this_size;
dcbf108f 701
63b4f126
MGD
702 if (gdb_regnum != -1)
703 {
d3b1e874
TT
704 if (need_bitwise)
705 {
706 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
707 this_size, buffer);
708 copy_bitwise (buffer, dest_offset_bits,
709 contents, source_offset_bits,
710 this_size_bits,
711 bits_big_endian);
712 }
713
63b4f126 714 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
d3b1e874 715 this_size, source_buffer);
63b4f126
MGD
716 }
717 else
718 {
719 error (_("Unable to write to DWARF register number %s"),
720 paddress (arch, p->v.expr.value));
721 }
cec03d70
TT
722 }
723 break;
724 case DWARF_VALUE_MEMORY:
d3b1e874
TT
725 if (need_bitwise)
726 {
727 /* Only the first and last bytes can possibly have any
728 bits reused. */
729 read_memory (p->v.expr.value + dest_offset, buffer, 1);
730 read_memory (p->v.expr.value + dest_offset + this_size - 1,
731 buffer + this_size - 1, 1);
732 copy_bitwise (buffer, dest_offset_bits,
733 contents, source_offset_bits,
734 this_size_bits,
735 bits_big_endian);
736 }
737
afd74c5f 738 write_memory (p->v.expr.value + dest_offset,
d3b1e874 739 source_buffer, this_size);
cec03d70
TT
740 break;
741 default:
742 set_value_optimized_out (to, 1);
d3b1e874 743 goto done;
052b9502 744 }
d3b1e874 745 offset += this_size_bits;
052b9502 746 }
d3b1e874
TT
747
748 done:
749 do_cleanups (cleanup);
052b9502
NF
750}
751
752static void *
753copy_pieced_value_closure (struct value *v)
754{
755 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
756
88bfdde4
TT
757 ++c->refc;
758 return c;
052b9502
NF
759}
760
761static void
762free_pieced_value_closure (struct value *v)
763{
764 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
765
88bfdde4
TT
766 --c->refc;
767 if (c->refc == 0)
768 {
769 xfree (c->pieces);
770 xfree (c);
771 }
052b9502
NF
772}
773
774/* Functions for accessing a variable described by DW_OP_piece. */
775static struct lval_funcs pieced_value_funcs = {
776 read_pieced_value,
777 write_pieced_value,
778 copy_pieced_value_closure,
779 free_pieced_value_closure
780};
781
4c2df51b 782/* Evaluate a location description, starting at DATA and with length
a2d33775 783 SIZE, to find the current location of variable of TYPE in the context
4c2df51b 784 of FRAME. */
a2d33775 785
4c2df51b 786static struct value *
a2d33775 787dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
947bb88f 788 const gdb_byte *data, unsigned short size,
ae0d2f24 789 struct dwarf2_per_cu_data *per_cu)
4c2df51b 790{
4c2df51b
DJ
791 struct value *retval;
792 struct dwarf_expr_baton baton;
793 struct dwarf_expr_context *ctx;
4a227398 794 struct cleanup *old_chain;
4c2df51b 795
0d53c4c4
DJ
796 if (size == 0)
797 {
a2d33775 798 retval = allocate_value (type);
0d53c4c4 799 VALUE_LVAL (retval) = not_lval;
feb13ab0 800 set_value_optimized_out (retval, 1);
10fb19b6 801 return retval;
0d53c4c4
DJ
802 }
803
4c2df51b 804 baton.frame = frame;
17ea53c3 805 baton.per_cu = per_cu;
4c2df51b
DJ
806
807 ctx = new_dwarf_expr_context ();
4a227398
TT
808 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
809
17ea53c3 810 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
ae0d2f24 811 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
4c2df51b
DJ
812 ctx->baton = &baton;
813 ctx->read_reg = dwarf_expr_read_reg;
814 ctx->read_mem = dwarf_expr_read_mem;
815 ctx->get_frame_base = dwarf_expr_frame_base;
e7802207 816 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
4c2df51b
DJ
817 ctx->get_tls_address = dwarf_expr_tls_address;
818
819 dwarf_expr_eval (ctx, data, size);
87808bd6
JB
820 if (ctx->num_pieces > 0)
821 {
052b9502
NF
822 struct piece_closure *c;
823 struct frame_id frame_id = get_frame_id (frame);
824
6063c216
UW
825 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
826 ctx->addr_size);
a2d33775 827 retval = allocate_computed_value (type, &pieced_value_funcs, c);
052b9502 828 VALUE_FRAME_ID (retval) = frame_id;
87808bd6 829 }
4c2df51b
DJ
830 else
831 {
cec03d70
TT
832 switch (ctx->location)
833 {
834 case DWARF_VALUE_REGISTER:
835 {
836 struct gdbarch *arch = get_frame_arch (frame);
837 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
838 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
9a619af0 839
63b4f126 840 if (gdb_regnum != -1)
a2d33775 841 retval = value_from_register (type, gdb_regnum, frame);
63b4f126 842 else
a2d33775
JK
843 error (_("Unable to access DWARF register number %s"),
844 paddress (arch, dwarf_regnum));
cec03d70
TT
845 }
846 break;
847
848 case DWARF_VALUE_MEMORY:
849 {
850 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
44353522 851 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
cec03d70 852
a2d33775 853 retval = allocate_value (type);
cec03d70
TT
854 VALUE_LVAL (retval) = lval_memory;
855 set_value_lazy (retval, 1);
44353522
DE
856 if (in_stack_memory)
857 set_value_stack (retval, 1);
cec03d70
TT
858 set_value_address (retval, address);
859 }
860 break;
861
862 case DWARF_VALUE_STACK:
863 {
cec03d70
TT
864 ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
865 bfd_byte *contents;
866 size_t n = ctx->addr_size;
867
a2d33775 868 retval = allocate_value (type);
cec03d70 869 contents = value_contents_raw (retval);
a2d33775
JK
870 if (n > TYPE_LENGTH (type))
871 n = TYPE_LENGTH (type);
05566b3b
TT
872 store_unsigned_integer (contents, n,
873 gdbarch_byte_order (ctx->gdbarch),
874 value);
cec03d70
TT
875 }
876 break;
877
878 case DWARF_VALUE_LITERAL:
879 {
880 bfd_byte *contents;
881 size_t n = ctx->len;
882
a2d33775 883 retval = allocate_value (type);
cec03d70 884 contents = value_contents_raw (retval);
a2d33775
JK
885 if (n > TYPE_LENGTH (type))
886 n = TYPE_LENGTH (type);
cec03d70
TT
887 memcpy (contents, ctx->data, n);
888 }
889 break;
890
cb826367
TT
891 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
892 it can only be encountered when making a piece. */
893 case DWARF_VALUE_OPTIMIZED_OUT:
cec03d70
TT
894 default:
895 internal_error (__FILE__, __LINE__, _("invalid location type"));
896 }
4c2df51b
DJ
897 }
898
42be36b3
CT
899 set_value_initialized (retval, ctx->initialized);
900
4a227398 901 do_cleanups (old_chain);
4c2df51b
DJ
902
903 return retval;
904}
4c2df51b
DJ
905\f
906/* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
907
908struct needs_frame_baton
909{
910 int needs_frame;
17ea53c3 911 struct dwarf2_per_cu_data *per_cu;
4c2df51b
DJ
912};
913
914/* Reads from registers do require a frame. */
915static CORE_ADDR
61fbb938 916needs_frame_read_reg (void *baton, int regnum)
4c2df51b
DJ
917{
918 struct needs_frame_baton *nf_baton = baton;
9a619af0 919
4c2df51b
DJ
920 nf_baton->needs_frame = 1;
921 return 1;
922}
923
924/* Reads from memory do not require a frame. */
925static void
852483bc 926needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
4c2df51b
DJ
927{
928 memset (buf, 0, len);
929}
930
931/* Frame-relative accesses do require a frame. */
932static void
0d45f56e 933needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
4c2df51b 934{
852483bc 935 static gdb_byte lit0 = DW_OP_lit0;
4c2df51b
DJ
936 struct needs_frame_baton *nf_baton = baton;
937
938 *start = &lit0;
939 *length = 1;
940
941 nf_baton->needs_frame = 1;
942}
943
e7802207
TT
944/* CFA accesses require a frame. */
945
946static CORE_ADDR
947needs_frame_frame_cfa (void *baton)
948{
949 struct needs_frame_baton *nf_baton = baton;
9a619af0 950
e7802207
TT
951 nf_baton->needs_frame = 1;
952 return 1;
953}
954
4c2df51b
DJ
955/* Thread-local accesses do require a frame. */
956static CORE_ADDR
957needs_frame_tls_address (void *baton, CORE_ADDR offset)
958{
959 struct needs_frame_baton *nf_baton = baton;
9a619af0 960
4c2df51b
DJ
961 nf_baton->needs_frame = 1;
962 return 1;
963}
964
965/* Return non-zero iff the location expression at DATA (length SIZE)
966 requires a frame to evaluate. */
967
968static int
947bb88f 969dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
ae0d2f24 970 struct dwarf2_per_cu_data *per_cu)
4c2df51b
DJ
971{
972 struct needs_frame_baton baton;
973 struct dwarf_expr_context *ctx;
f630a401 974 int in_reg;
4a227398 975 struct cleanup *old_chain;
4c2df51b
DJ
976
977 baton.needs_frame = 0;
17ea53c3 978 baton.per_cu = per_cu;
4c2df51b
DJ
979
980 ctx = new_dwarf_expr_context ();
4a227398
TT
981 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
982
f7fd4728 983 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
ae0d2f24 984 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
4c2df51b
DJ
985 ctx->baton = &baton;
986 ctx->read_reg = needs_frame_read_reg;
987 ctx->read_mem = needs_frame_read_mem;
988 ctx->get_frame_base = needs_frame_frame_base;
e7802207 989 ctx->get_frame_cfa = needs_frame_frame_cfa;
4c2df51b
DJ
990 ctx->get_tls_address = needs_frame_tls_address;
991
992 dwarf_expr_eval (ctx, data, size);
993
cec03d70 994 in_reg = ctx->location == DWARF_VALUE_REGISTER;
f630a401 995
87808bd6
JB
996 if (ctx->num_pieces > 0)
997 {
998 int i;
999
1000 /* If the location has several pieces, and any of them are in
1001 registers, then we will need a frame to fetch them from. */
1002 for (i = 0; i < ctx->num_pieces; i++)
cec03d70 1003 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
87808bd6
JB
1004 in_reg = 1;
1005 }
1006
4a227398 1007 do_cleanups (old_chain);
4c2df51b 1008
f630a401 1009 return baton.needs_frame || in_reg;
4c2df51b
DJ
1010}
1011
08922a10
SS
1012/* This struct keeps track of the pieces that make up a multi-location
1013 object, for use in agent expression generation. It is
1014 superficially similar to struct dwarf_expr_piece, but
1015 dwarf_expr_piece is designed for use in immediate evaluation, and
1016 does not, for example, have a way to record both base register and
1017 offset. */
1018
1019struct axs_var_loc
0d53c4c4 1020{
08922a10
SS
1021 /* Memory vs register, etc */
1022 enum axs_lvalue_kind kind;
1023
1024 /* If non-zero, number of bytes in this fragment */
1025 unsigned bytes;
1026
1027 /* (GDB-numbered) reg, or base reg if >= 0 */
1028 int reg;
1029
1030 /* offset from reg */
1031 LONGEST offset;
1032};
1033
0d45f56e 1034static const gdb_byte *
08922a10
SS
1035dwarf2_tracepoint_var_loc (struct symbol *symbol,
1036 struct agent_expr *ax,
1037 struct axs_var_loc *loc,
1038 struct gdbarch *gdbarch,
0d45f56e 1039 const gdb_byte *data, const gdb_byte *end)
08922a10
SS
1040{
1041 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
0d53c4c4 1042 {
08922a10
SS
1043 loc->kind = axs_lvalue_register;
1044 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1045 data += 1;
0d53c4c4
DJ
1046 }
1047 else if (data[0] == DW_OP_regx)
1048 {
1049 ULONGEST reg;
9a619af0 1050
08922a10
SS
1051 data = read_uleb128 (data + 1, end, &reg);
1052 loc->kind = axs_lvalue_register;
1053 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
0d53c4c4
DJ
1054 }
1055 else if (data[0] == DW_OP_fbreg)
1056 {
0936ad1d
SS
1057 struct block *b;
1058 struct symbol *framefunc;
1059 int frame_reg = 0;
0d53c4c4 1060 LONGEST frame_offset;
0d45f56e 1061 const gdb_byte *base_data;
0936ad1d
SS
1062 size_t base_size;
1063 LONGEST base_offset = 0;
1064
1065 b = block_for_pc (ax->scope);
1066
1067 if (!b)
1068 error (_("No block found for address"));
1069
1070 framefunc = block_linkage_function (b);
1071
1072 if (!framefunc)
1073 error (_("No function found for block"));
1074
1075 dwarf_expr_frame_base_1 (framefunc, ax->scope,
1076 &base_data, &base_size);
1077
08922a10 1078 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
0936ad1d 1079 {
0d45f56e
TT
1080 const gdb_byte *buf_end;
1081
0936ad1d 1082 frame_reg = base_data[0] - DW_OP_breg0;
08922a10
SS
1083 buf_end = read_sleb128 (base_data + 1,
1084 base_data + base_size, &base_offset);
0936ad1d
SS
1085 if (buf_end != base_data + base_size)
1086 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1087 frame_reg, SYMBOL_PRINT_NAME (symbol));
1088 }
08922a10
SS
1089 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1090 {
1091 /* The frame base is just the register, with no offset. */
1092 frame_reg = base_data[0] - DW_OP_reg0;
1093 base_offset = 0;
1094 }
0936ad1d
SS
1095 else
1096 {
1097 /* We don't know what to do with the frame base expression,
1098 so we can't trace this variable; give up. */
08922a10
SS
1099 error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
1100 SYMBOL_PRINT_NAME (symbol), base_data[0]);
0936ad1d 1101 }
0d53c4c4 1102
08922a10 1103 data = read_sleb128 (data + 1, end, &frame_offset);
4c2df51b 1104
08922a10
SS
1105 loc->kind = axs_lvalue_memory;
1106 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1107 loc->offset = base_offset + frame_offset;
9c238357 1108 }
08922a10 1109 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
9c238357
RC
1110 {
1111 unsigned int reg;
1112 LONGEST offset;
9c238357
RC
1113
1114 reg = data[0] - DW_OP_breg0;
08922a10 1115 data = read_sleb128 (data + 1, end, &offset);
9c238357 1116
08922a10
SS
1117 loc->kind = axs_lvalue_memory;
1118 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1119 loc->offset = offset;
0d53c4c4
DJ
1120 }
1121 else
9c238357
RC
1122 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
1123 data[0], SYMBOL_PRINT_NAME (symbol));
08922a10
SS
1124
1125 return data;
0d53c4c4 1126}
08922a10
SS
1127
1128/* Given the location of a piece, issue bytecodes that will access it. */
1129
1130static void
1131dwarf2_tracepoint_var_access (struct agent_expr *ax,
1132 struct axs_value *value,
1133 struct axs_var_loc *loc)
1134{
1135 value->kind = loc->kind;
1136
1137 switch (loc->kind)
1138 {
1139 case axs_lvalue_register:
1140 value->u.reg = loc->reg;
1141 break;
1142
1143 case axs_lvalue_memory:
1144 ax_reg (ax, loc->reg);
1145 if (loc->offset)
1146 {
1147 ax_const_l (ax, loc->offset);
1148 ax_simple (ax, aop_add);
1149 }
1150 break;
1151
1152 default:
1153 internal_error (__FILE__, __LINE__, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
1154 }
1155}
1156
1157static void
1158dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1159 struct agent_expr *ax, struct axs_value *value,
0d45f56e 1160 const gdb_byte *data, int size)
08922a10 1161{
0d45f56e 1162 const gdb_byte *end = data + size;
08922a10
SS
1163 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1164 /* In practice, a variable is not going to be spread across
1165 dozens of registers or memory locations. If someone comes up
1166 with a real-world example, revisit this. */
1167#define MAX_FRAGS 16
1168 struct axs_var_loc fragments[MAX_FRAGS];
1169 int nfrags = 0, frag;
1170 int length = 0;
1171 int piece_ok = 0;
1172 int bad = 0;
1173 int first = 1;
1174
1175 if (!data || size == 0)
1176 {
1177 value->optimized_out = 1;
1178 return;
1179 }
1180
1181 while (data < end)
1182 {
1183 if (!piece_ok)
1184 {
1185 if (nfrags == MAX_FRAGS)
1186 error (_("Too many pieces in location for \"%s\"."),
1187 SYMBOL_PRINT_NAME (symbol));
1188
1189 fragments[nfrags].bytes = 0;
1190 data = dwarf2_tracepoint_var_loc (symbol, ax, &fragments[nfrags],
1191 gdbarch, data, end);
1192 nfrags++;
1193 piece_ok = 1;
1194 }
1195 else if (data[0] == DW_OP_piece)
1196 {
1197 ULONGEST bytes;
1198
1199 data = read_uleb128 (data + 1, end, &bytes);
1200 /* Only deal with 4 byte fragments for now. */
1201 if (bytes != 4)
1202 error (_("DW_OP_piece %s not supported in location for \"%s\"."),
1203 pulongest (bytes), SYMBOL_PRINT_NAME (symbol));
1204 fragments[nfrags - 1].bytes = bytes;
1205 length += bytes;
1206 piece_ok = 0;
1207 }
1208 else
1209 {
1210 bad = 1;
1211 break;
1212 }
1213 }
1214
1215 if (bad || data > end)
1216 error (_("Corrupted DWARF expression for \"%s\"."),
1217 SYMBOL_PRINT_NAME (symbol));
1218
1219 /* If single expression, no pieces, convert to external format. */
1220 if (length == 0)
1221 {
1222 dwarf2_tracepoint_var_access (ax, value, &fragments[0]);
1223 return;
1224 }
1225
1226 if (length != TYPE_LENGTH (value->type))
1227 error (_("Inconsistent piece information for \"%s\"."),
1228 SYMBOL_PRINT_NAME (symbol));
1229
1230 /* Emit bytecodes to assemble the pieces into a single stack entry. */
1231
1232 for ((frag = (byte_order == BFD_ENDIAN_BIG ? 0 : nfrags - 1));
1233 nfrags--;
1234 (frag += (byte_order == BFD_ENDIAN_BIG ? 1 : -1)))
1235 {
1236 if (!first)
1237 {
1238 /* shift the previous fragment up 32 bits */
1239 ax_const_l (ax, 32);
1240 ax_simple (ax, aop_lsh);
1241 }
1242
1243 dwarf2_tracepoint_var_access (ax, value, &fragments[frag]);
1244
1245 switch (value->kind)
1246 {
1247 case axs_lvalue_register:
1248 ax_reg (ax, value->u.reg);
1249 break;
1250
1251 case axs_lvalue_memory:
1252 {
1253 extern int trace_kludge; /* Ugh. */
1254
1255 gdb_assert (fragments[frag].bytes == 4);
1256 if (trace_kludge)
1257 ax_trace_quick (ax, 4);
1258 ax_simple (ax, aop_ref32);
1259 }
1260 break;
1261 }
1262
1263 if (!first)
1264 {
1265 /* or the new fragment into the previous */
1266 ax_zero_ext (ax, 32);
1267 ax_simple (ax, aop_bit_or);
1268 }
1269 first = 0;
1270 }
1271 value->kind = axs_rvalue;
1272}
1273
4c2df51b
DJ
1274\f
1275/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1276 evaluator to calculate the location. */
1277static struct value *
1278locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1279{
1280 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1281 struct value *val;
9a619af0 1282
a2d33775
JK
1283 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1284 dlbaton->size, dlbaton->per_cu);
4c2df51b
DJ
1285
1286 return val;
1287}
1288
1289/* Return non-zero iff we need a frame to evaluate SYMBOL. */
1290static int
1291locexpr_read_needs_frame (struct symbol *symbol)
1292{
1293 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
9a619af0 1294
ae0d2f24
UW
1295 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1296 dlbaton->per_cu);
4c2df51b
DJ
1297}
1298
9eae7c52
TT
1299/* Return true if DATA points to the end of a piece. END is one past
1300 the last byte in the expression. */
1301
1302static int
1303piece_end_p (const gdb_byte *data, const gdb_byte *end)
1304{
1305 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1306}
1307
1308/* Nicely describe a single piece of a location, returning an updated
1309 position in the bytecode sequence. This function cannot recognize
1310 all locations; if a location is not recognized, it simply returns
1311 DATA. */
08922a10 1312
0d45f56e 1313static const gdb_byte *
08922a10
SS
1314locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1315 CORE_ADDR addr, struct objfile *objfile,
9eae7c52 1316 const gdb_byte *data, const gdb_byte *end,
0d45f56e 1317 unsigned int addr_size)
4c2df51b 1318{
08922a10
SS
1319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1320 int regno;
1321
1322 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1323 {
1324 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1325 fprintf_filtered (stream, _("a variable in $%s"),
1326 gdbarch_register_name (gdbarch, regno));
1327 data += 1;
1328 }
1329 else if (data[0] == DW_OP_regx)
1330 {
1331 ULONGEST reg;
4c2df51b 1332
9eae7c52 1333 data = read_uleb128 (data + 1, end, &reg);
08922a10
SS
1334 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1335 fprintf_filtered (stream, _("a variable in $%s"),
1336 gdbarch_register_name (gdbarch, regno));
1337 }
1338 else if (data[0] == DW_OP_fbreg)
4c2df51b 1339 {
08922a10
SS
1340 struct block *b;
1341 struct symbol *framefunc;
1342 int frame_reg = 0;
1343 LONGEST frame_offset;
9eae7c52 1344 const gdb_byte *base_data, *new_data;
08922a10
SS
1345 size_t base_size;
1346 LONGEST base_offset = 0;
1347
9eae7c52
TT
1348 new_data = read_sleb128 (data + 1, end, &frame_offset);
1349 if (!piece_end_p (new_data, end))
1350 return data;
1351 data = new_data;
1352
08922a10
SS
1353 b = block_for_pc (addr);
1354
1355 if (!b)
1356 error (_("No block found for address for symbol \"%s\"."),
1357 SYMBOL_PRINT_NAME (symbol));
1358
1359 framefunc = block_linkage_function (b);
1360
1361 if (!framefunc)
1362 error (_("No function found for block for symbol \"%s\"."),
1363 SYMBOL_PRINT_NAME (symbol));
1364
1365 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1366
1367 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1368 {
0d45f56e 1369 const gdb_byte *buf_end;
08922a10
SS
1370
1371 frame_reg = base_data[0] - DW_OP_breg0;
1372 buf_end = read_sleb128 (base_data + 1,
1373 base_data + base_size, &base_offset);
1374 if (buf_end != base_data + base_size)
1375 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1376 frame_reg, SYMBOL_PRINT_NAME (symbol));
1377 }
1378 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1379 {
1380 /* The frame base is just the register, with no offset. */
1381 frame_reg = base_data[0] - DW_OP_reg0;
1382 base_offset = 0;
1383 }
1384 else
1385 {
1386 /* We don't know what to do with the frame base expression,
1387 so we can't trace this variable; give up. */
1388 error (_("Cannot describe location of symbol \"%s\"; "
1389 "DWARF 2 encoding not handled, "
1390 "first opcode in base data is 0x%x."),
1391 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1392 }
1393
1394 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1395
08922a10
SS
1396 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1397 gdbarch_register_name (gdbarch, regno),
1398 plongest (base_offset), plongest (frame_offset));
1399 }
9eae7c52
TT
1400 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1401 && piece_end_p (data, end))
08922a10
SS
1402 {
1403 LONGEST offset;
1404
1405 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1406
9eae7c52 1407 data = read_sleb128 (data + 1, end, &offset);
08922a10 1408
4c2df51b 1409 fprintf_filtered (stream,
08922a10
SS
1410 _("a variable at offset %s from base reg $%s"),
1411 plongest (offset),
5e2b427d 1412 gdbarch_register_name (gdbarch, regno));
4c2df51b
DJ
1413 }
1414
c3228f12
EZ
1415 /* The location expression for a TLS variable looks like this (on a
1416 64-bit LE machine):
1417
1418 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1419 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1420
1421 0x3 is the encoding for DW_OP_addr, which has an operand as long
1422 as the size of an address on the target machine (here is 8
1423 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1424 The operand represents the offset at which the variable is within
1425 the thread local storage. */
1426
9eae7c52
TT
1427 else if (data + 1 + addr_size < end
1428 && data[0] == DW_OP_addr
1429 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
1430 && piece_end_p (data + 2 + addr_size, end))
08922a10
SS
1431 {
1432 CORE_ADDR offset = dwarf2_read_address (gdbarch,
1433 data + 1,
9eae7c52 1434 end,
08922a10 1435 addr_size);
9a619af0 1436
08922a10
SS
1437 fprintf_filtered (stream,
1438 _("a thread-local variable at offset %s "
1439 "in the thread-local storage for `%s'"),
1440 paddress (gdbarch, offset), objfile->name);
1441
1442 data += 1 + addr_size + 1;
1443 }
9eae7c52
TT
1444 else if (data[0] >= DW_OP_lit0
1445 && data[0] <= DW_OP_lit31
1446 && data + 1 < end
1447 && data[1] == DW_OP_stack_value)
1448 {
1449 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
1450 data += 2;
1451 }
1452
1453 return data;
1454}
1455
1456/* Disassemble an expression, stopping at the end of a piece or at the
1457 end of the expression. Returns a pointer to the next unread byte
1458 in the input expression. If ALL is nonzero, then this function
1459 will keep going until it reaches the end of the expression. */
1460
1461static const gdb_byte *
1462disassemble_dwarf_expression (struct ui_file *stream,
1463 struct gdbarch *arch, unsigned int addr_size,
1464 int offset_size,
1465 const gdb_byte *data, const gdb_byte *end,
1466 int all)
1467{
1468 const gdb_byte *start = data;
1469
1470 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
1471
1472 while (data < end
1473 && (all
1474 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
1475 {
1476 enum dwarf_location_atom op = *data++;
1477 CORE_ADDR addr;
1478 ULONGEST ul;
1479 LONGEST l;
1480 const char *name;
1481
1482 name = dwarf_stack_op_name (op, 0);
1483
1484 if (!name)
1485 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
1486 op, (long) (data - start));
1487 fprintf_filtered (stream, " % 4ld: %s", (long) (data - start), name);
1488
1489 switch (op)
1490 {
1491 case DW_OP_addr:
1492 addr = dwarf2_read_address (arch, data, end, addr_size);
1493 data += addr_size;
1494 fprintf_filtered (stream, " %s", paddress (arch, addr));
1495 break;
1496
1497 case DW_OP_const1u:
1498 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
1499 data += 1;
1500 fprintf_filtered (stream, " %s", pulongest (ul));
1501 break;
1502 case DW_OP_const1s:
1503 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
1504 data += 1;
1505 fprintf_filtered (stream, " %s", plongest (l));
1506 break;
1507 case DW_OP_const2u:
1508 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1509 data += 2;
1510 fprintf_filtered (stream, " %s", pulongest (ul));
1511 break;
1512 case DW_OP_const2s:
1513 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1514 data += 2;
1515 fprintf_filtered (stream, " %s", plongest (l));
1516 break;
1517 case DW_OP_const4u:
1518 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1519 data += 4;
1520 fprintf_filtered (stream, " %s", pulongest (ul));
1521 break;
1522 case DW_OP_const4s:
1523 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
1524 data += 4;
1525 fprintf_filtered (stream, " %s", plongest (l));
1526 break;
1527 case DW_OP_const8u:
1528 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
1529 data += 8;
1530 fprintf_filtered (stream, " %s", pulongest (ul));
1531 break;
1532 case DW_OP_const8s:
1533 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
1534 data += 8;
1535 fprintf_filtered (stream, " %s", plongest (l));
1536 break;
1537 case DW_OP_constu:
1538 data = read_uleb128 (data, end, &ul);
1539 fprintf_filtered (stream, " %s", pulongest (ul));
1540 break;
1541 case DW_OP_consts:
44b5680a 1542 data = read_sleb128 (data, end, &l);
9eae7c52
TT
1543 fprintf_filtered (stream, " %s", plongest (l));
1544 break;
1545
1546 case DW_OP_reg0:
1547 case DW_OP_reg1:
1548 case DW_OP_reg2:
1549 case DW_OP_reg3:
1550 case DW_OP_reg4:
1551 case DW_OP_reg5:
1552 case DW_OP_reg6:
1553 case DW_OP_reg7:
1554 case DW_OP_reg8:
1555 case DW_OP_reg9:
1556 case DW_OP_reg10:
1557 case DW_OP_reg11:
1558 case DW_OP_reg12:
1559 case DW_OP_reg13:
1560 case DW_OP_reg14:
1561 case DW_OP_reg15:
1562 case DW_OP_reg16:
1563 case DW_OP_reg17:
1564 case DW_OP_reg18:
1565 case DW_OP_reg19:
1566 case DW_OP_reg20:
1567 case DW_OP_reg21:
1568 case DW_OP_reg22:
1569 case DW_OP_reg23:
1570 case DW_OP_reg24:
1571 case DW_OP_reg25:
1572 case DW_OP_reg26:
1573 case DW_OP_reg27:
1574 case DW_OP_reg28:
1575 case DW_OP_reg29:
1576 case DW_OP_reg30:
1577 case DW_OP_reg31:
1578 fprintf_filtered (stream, " [$%s]",
1579 gdbarch_register_name (arch, op - DW_OP_reg0));
1580 break;
1581
1582 case DW_OP_regx:
1583 data = read_uleb128 (data, end, &ul);
1584 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1585 gdbarch_register_name (arch, (int) ul));
1586 break;
1587
1588 case DW_OP_implicit_value:
1589 data = read_uleb128 (data, end, &ul);
1590 data += ul;
1591 fprintf_filtered (stream, " %s", pulongest (ul));
1592 break;
1593
1594 case DW_OP_breg0:
1595 case DW_OP_breg1:
1596 case DW_OP_breg2:
1597 case DW_OP_breg3:
1598 case DW_OP_breg4:
1599 case DW_OP_breg5:
1600 case DW_OP_breg6:
1601 case DW_OP_breg7:
1602 case DW_OP_breg8:
1603 case DW_OP_breg9:
1604 case DW_OP_breg10:
1605 case DW_OP_breg11:
1606 case DW_OP_breg12:
1607 case DW_OP_breg13:
1608 case DW_OP_breg14:
1609 case DW_OP_breg15:
1610 case DW_OP_breg16:
1611 case DW_OP_breg17:
1612 case DW_OP_breg18:
1613 case DW_OP_breg19:
1614 case DW_OP_breg20:
1615 case DW_OP_breg21:
1616 case DW_OP_breg22:
1617 case DW_OP_breg23:
1618 case DW_OP_breg24:
1619 case DW_OP_breg25:
1620 case DW_OP_breg26:
1621 case DW_OP_breg27:
1622 case DW_OP_breg28:
1623 case DW_OP_breg29:
1624 case DW_OP_breg30:
1625 case DW_OP_breg31:
1626 data = read_sleb128 (data, end, &ul);
1627 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1628 gdbarch_register_name (arch, op - DW_OP_breg0));
1629 break;
1630
1631 case DW_OP_bregx:
1632 {
1633 ULONGEST offset;
1634
1635 data = read_uleb128 (data, end, &ul);
1636 data = read_sleb128 (data, end, &offset);
1637 fprintf_filtered (stream, " register %s [$%s] offset %s",
1638 pulongest (ul),
1639 gdbarch_register_name (arch, (int) ul),
1640 pulongest (offset));
1641 }
1642 break;
1643
1644 case DW_OP_fbreg:
1645 data = read_sleb128 (data, end, &ul);
1646 fprintf_filtered (stream, " %s", pulongest (ul));
1647 break;
1648
1649 case DW_OP_xderef_size:
1650 case DW_OP_deref_size:
1651 case DW_OP_pick:
1652 fprintf_filtered (stream, " %d", *data);
1653 ++data;
1654 break;
1655
1656 case DW_OP_plus_uconst:
1657 data = read_uleb128 (data, end, &ul);
1658 fprintf_filtered (stream, " %s", pulongest (ul));
1659 break;
1660
1661 case DW_OP_skip:
1662 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1663 data += 2;
1664 fprintf_filtered (stream, " to %ld",
1665 (long) (data + l - start));
1666 break;
1667
1668 case DW_OP_bra:
1669 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1670 data += 2;
1671 fprintf_filtered (stream, " %ld",
1672 (long) (data + l - start));
1673 break;
1674
1675 case DW_OP_call2:
1676 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1677 data += 2;
1678 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
1679 break;
1680
1681 case DW_OP_call4:
1682 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1683 data += 4;
1684 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
1685 break;
1686
1687 case DW_OP_call_ref:
1688 ul = extract_unsigned_integer (data, offset_size,
1689 gdbarch_byte_order (arch));
1690 data += offset_size;
1691 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
1692 break;
1693
1694 case DW_OP_piece:
1695 data = read_uleb128 (data, end, &ul);
1696 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
1697 break;
1698
1699 case DW_OP_bit_piece:
1700 {
1701 ULONGEST offset;
1702
1703 data = read_uleb128 (data, end, &ul);
1704 data = read_uleb128 (data, end, &offset);
1705 fprintf_filtered (stream, " size %s offset %s (bits)",
1706 pulongest (ul), pulongest (offset));
1707 }
1708 break;
1709 }
1710
1711 fprintf_filtered (stream, "\n");
1712 }
c3228f12 1713
08922a10 1714 return data;
4c2df51b
DJ
1715}
1716
08922a10
SS
1717/* Describe a single location, which may in turn consist of multiple
1718 pieces. */
a55cc764 1719
08922a10
SS
1720static void
1721locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
0d45f56e
TT
1722 struct ui_file *stream,
1723 const gdb_byte *data, int size,
9eae7c52
TT
1724 struct objfile *objfile, unsigned int addr_size,
1725 int offset_size)
08922a10 1726{
0d45f56e 1727 const gdb_byte *end = data + size;
9eae7c52 1728 int first_piece = 1, bad = 0;
08922a10 1729
08922a10
SS
1730 while (data < end)
1731 {
9eae7c52
TT
1732 const gdb_byte *here = data;
1733 int disassemble = 1;
1734
1735 if (first_piece)
1736 first_piece = 0;
1737 else
1738 fprintf_filtered (stream, _(", and "));
08922a10 1739
9eae7c52
TT
1740 if (!dwarf2_always_disassemble)
1741 {
08922a10 1742 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
9eae7c52
TT
1743 data, end, addr_size);
1744 /* If we printed anything, or if we have an empty piece,
1745 then don't disassemble. */
1746 if (data != here
1747 || data[0] == DW_OP_piece
1748 || data[0] == DW_OP_bit_piece)
1749 disassemble = 0;
08922a10 1750 }
9eae7c52
TT
1751 if (disassemble)
1752 data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
1753 addr_size, offset_size, data, end,
1754 dwarf2_always_disassemble);
1755
1756 if (data < end)
08922a10 1757 {
9eae7c52 1758 int empty = data == here;
08922a10 1759
9eae7c52
TT
1760 if (disassemble)
1761 fprintf_filtered (stream, " ");
1762 if (data[0] == DW_OP_piece)
1763 {
1764 ULONGEST bytes;
08922a10 1765
9eae7c52 1766 data = read_uleb128 (data + 1, end, &bytes);
08922a10 1767
9eae7c52
TT
1768 if (empty)
1769 fprintf_filtered (stream, _("an empty %s-byte piece"),
1770 pulongest (bytes));
1771 else
1772 fprintf_filtered (stream, _(" [%s-byte piece]"),
1773 pulongest (bytes));
1774 }
1775 else if (data[0] == DW_OP_bit_piece)
1776 {
1777 ULONGEST bits, offset;
1778
1779 data = read_uleb128 (data + 1, end, &bits);
1780 data = read_uleb128 (data, end, &offset);
1781
1782 if (empty)
1783 fprintf_filtered (stream,
1784 _("an empty %s-bit piece"),
1785 pulongest (bits));
1786 else
1787 fprintf_filtered (stream,
1788 _(" [%s-bit piece, offset %s bits]"),
1789 pulongest (bits), pulongest (offset));
1790 }
1791 else
1792 {
1793 bad = 1;
1794 break;
1795 }
08922a10
SS
1796 }
1797 }
1798
1799 if (bad || data > end)
1800 error (_("Corrupted DWARF2 expression for \"%s\"."),
1801 SYMBOL_PRINT_NAME (symbol));
1802}
1803
1804/* Print a natural-language description of SYMBOL to STREAM. This
1805 version is for a symbol with a single location. */
a55cc764 1806
08922a10
SS
1807static void
1808locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
1809 struct ui_file *stream)
1810{
1811 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1812 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1813 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
9eae7c52 1814 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
08922a10
SS
1815
1816 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
9eae7c52 1817 objfile, addr_size, offset_size);
08922a10
SS
1818}
1819
1820/* Describe the location of SYMBOL as an agent value in VALUE, generating
1821 any necessary bytecode in AX. */
a55cc764 1822
0d53c4c4 1823static void
505e835d
UW
1824locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1825 struct agent_expr *ax, struct axs_value *value)
a55cc764
DJ
1826{
1827 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1828
505e835d
UW
1829 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
1830 dlbaton->data, dlbaton->size);
a55cc764
DJ
1831}
1832
4c2df51b
DJ
1833/* The set of location functions used with the DWARF-2 expression
1834 evaluator. */
768a979c 1835const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4c2df51b
DJ
1836 locexpr_read_variable,
1837 locexpr_read_needs_frame,
1838 locexpr_describe_location,
a55cc764 1839 locexpr_tracepoint_var_ref
4c2df51b 1840};
0d53c4c4
DJ
1841
1842
1843/* Wrapper functions for location lists. These generally find
1844 the appropriate location expression and call something above. */
1845
1846/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1847 evaluator to calculate the location. */
1848static struct value *
1849loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
1850{
1851 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1852 struct value *val;
947bb88f 1853 const gdb_byte *data;
b6b08ebf 1854 size_t size;
0d53c4c4
DJ
1855
1856 data = find_location_expression (dlbaton, &size,
22c6caba
JW
1857 frame ? get_frame_address_in_block (frame)
1858 : 0);
0d53c4c4 1859 if (data == NULL)
806048c6
DJ
1860 {
1861 val = allocate_value (SYMBOL_TYPE (symbol));
1862 VALUE_LVAL (val) = not_lval;
1863 set_value_optimized_out (val, 1);
1864 }
1865 else
a2d33775 1866 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
ae0d2f24 1867 dlbaton->per_cu);
0d53c4c4
DJ
1868
1869 return val;
1870}
1871
1872/* Return non-zero iff we need a frame to evaluate SYMBOL. */
1873static int
1874loclist_read_needs_frame (struct symbol *symbol)
1875{
1876 /* If there's a location list, then assume we need to have a frame
1877 to choose the appropriate location expression. With tracking of
1878 global variables this is not necessarily true, but such tracking
1879 is disabled in GCC at the moment until we figure out how to
1880 represent it. */
1881
1882 return 1;
1883}
1884
08922a10
SS
1885/* Print a natural-language description of SYMBOL to STREAM. This
1886 version applies when there is a list of different locations, each
1887 with a specified address range. */
1888
1889static void
1890loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
1891 struct ui_file *stream)
0d53c4c4 1892{
08922a10
SS
1893 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1894 CORE_ADDR low, high;
947bb88f 1895 const gdb_byte *loc_ptr, *buf_end;
08922a10
SS
1896 int length, first = 1;
1897 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1898 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1899 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1900 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
9eae7c52 1901 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
08922a10
SS
1902 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1903 /* Adjust base_address for relocatable objects. */
1904 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
1905 SECT_OFF_TEXT (objfile));
1906 CORE_ADDR base_address = dlbaton->base_address + base_offset;
1907
1908 loc_ptr = dlbaton->data;
1909 buf_end = dlbaton->data + dlbaton->size;
1910
9eae7c52 1911 fprintf_filtered (stream, _("multi-location:\n"));
08922a10
SS
1912
1913 /* Iterate through locations until we run out. */
1914 while (1)
1915 {
1916 if (buf_end - loc_ptr < 2 * addr_size)
1917 error (_("Corrupted DWARF expression for symbol \"%s\"."),
1918 SYMBOL_PRINT_NAME (symbol));
1919
1920 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1921 loc_ptr += addr_size;
1922
1923 /* A base-address-selection entry. */
1924 if (low == base_mask)
1925 {
1926 base_address = dwarf2_read_address (gdbarch,
1927 loc_ptr, buf_end, addr_size);
9eae7c52 1928 fprintf_filtered (stream, _(" Base address %s"),
08922a10
SS
1929 paddress (gdbarch, base_address));
1930 loc_ptr += addr_size;
1931 continue;
1932 }
1933
1934 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1935 loc_ptr += addr_size;
1936
1937 /* An end-of-list entry. */
1938 if (low == 0 && high == 0)
9eae7c52 1939 break;
08922a10
SS
1940
1941 /* Otherwise, a location expression entry. */
1942 low += base_address;
1943 high += base_address;
1944
1945 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
1946 loc_ptr += 2;
1947
08922a10
SS
1948 /* (It would improve readability to print only the minimum
1949 necessary digits of the second number of the range.) */
9eae7c52 1950 fprintf_filtered (stream, _(" Range %s-%s: "),
08922a10
SS
1951 paddress (gdbarch, low), paddress (gdbarch, high));
1952
1953 /* Now describe this particular location. */
1954 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
9eae7c52
TT
1955 objfile, addr_size, offset_size);
1956
1957 fprintf_filtered (stream, "\n");
08922a10
SS
1958
1959 loc_ptr += length;
1960 }
0d53c4c4
DJ
1961}
1962
1963/* Describe the location of SYMBOL as an agent value in VALUE, generating
1964 any necessary bytecode in AX. */
1965static void
505e835d
UW
1966loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1967 struct agent_expr *ax, struct axs_value *value)
0d53c4c4
DJ
1968{
1969 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
947bb88f 1970 const gdb_byte *data;
b6b08ebf 1971 size_t size;
0d53c4c4
DJ
1972
1973 data = find_location_expression (dlbaton, &size, ax->scope);
0d53c4c4 1974
505e835d 1975 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
0d53c4c4
DJ
1976}
1977
1978/* The set of location functions used with the DWARF-2 expression
1979 evaluator and location lists. */
768a979c 1980const struct symbol_computed_ops dwarf2_loclist_funcs = {
0d53c4c4
DJ
1981 loclist_read_variable,
1982 loclist_read_needs_frame,
1983 loclist_describe_location,
1984 loclist_tracepoint_var_ref
1985};
This page took 0.596365 seconds and 4 git commands to generate.