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