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
0936ad1d
SS
45static void
46dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
47 gdb_byte **start, size_t *length);
48
0d53c4c4
DJ
49/* A helper function for dealing with location lists. Given a
50 symbol baton (BATON) and a pc value (PC), find the appropriate
51 location expression, set *LOCEXPR_LENGTH, and return a pointer
52 to the beginning of the expression. Returns NULL on failure.
53
54 For now, only return the first matching location expression; there
55 can be more than one in the list. */
56
852483bc 57static gdb_byte *
0d53c4c4 58find_location_expression (struct dwarf2_loclist_baton *baton,
b6b08ebf 59 size_t *locexpr_length, CORE_ADDR pc)
0d53c4c4 60{
0d53c4c4 61 CORE_ADDR low, high;
852483bc
MK
62 gdb_byte *loc_ptr, *buf_end;
63 int length;
ae0d2f24 64 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
f7fd4728 65 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e17a4113 66 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
ae0d2f24 67 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
0d53c4c4 68 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8edfa926 69 /* Adjust base_address for relocatable objects. */
ae0d2f24
UW
70 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
71 SECT_OFF_TEXT (objfile));
8edfa926 72 CORE_ADDR base_address = baton->base_address + base_offset;
0d53c4c4
DJ
73
74 loc_ptr = baton->data;
75 buf_end = baton->data + baton->size;
76
77 while (1)
78 {
b5758fe4
UW
79 if (buf_end - loc_ptr < 2 * addr_size)
80 error (_("find_location_expression: Corrupted DWARF expression."));
0d53c4c4 81
b5758fe4
UW
82 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
83 loc_ptr += addr_size;
0d53c4c4
DJ
84
85 /* A base-address-selection entry. */
b5758fe4 86 if (low == base_mask)
0d53c4c4 87 {
b5758fe4
UW
88 base_address = dwarf2_read_address (gdbarch,
89 loc_ptr, buf_end, addr_size);
90 loc_ptr += addr_size;
0d53c4c4
DJ
91 continue;
92 }
93
b5758fe4
UW
94 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
95 loc_ptr += addr_size;
96
97 /* An end-of-list entry. */
98 if (low == 0 && high == 0)
99 return NULL;
100
0d53c4c4
DJ
101 /* Otherwise, a location expression entry. */
102 low += base_address;
103 high += base_address;
104
e17a4113 105 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
0d53c4c4
DJ
106 loc_ptr += 2;
107
108 if (pc >= low && pc < high)
109 {
110 *locexpr_length = length;
111 return loc_ptr;
112 }
113
114 loc_ptr += length;
115 }
116}
117
4c2df51b
DJ
118/* This is the baton used when performing dwarf2 expression
119 evaluation. */
120struct dwarf_expr_baton
121{
122 struct frame_info *frame;
123 struct objfile *objfile;
124};
125
126/* Helper functions for dwarf2_evaluate_loc_desc. */
127
4bc9efe1 128/* Using the frame specified in BATON, return the value of register
0b2b0195 129 REGNUM, treated as a pointer. */
4c2df51b 130static CORE_ADDR
61fbb938 131dwarf_expr_read_reg (void *baton, int dwarf_regnum)
4c2df51b 132{
4c2df51b 133 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
5e2b427d 134 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
e5192dd8 135 CORE_ADDR result;
0b2b0195 136 int regnum;
e4adbba9 137
5e2b427d
UW
138 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
139 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
0b2b0195 140 regnum, debaton->frame);
4c2df51b
DJ
141 return result;
142}
143
144/* Read memory at ADDR (length LEN) into BUF. */
145
146static void
852483bc 147dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
4c2df51b
DJ
148{
149 read_memory (addr, buf, len);
150}
151
152/* Using the frame specified in BATON, find the location expression
153 describing the frame base. Return a pointer to it in START and
154 its length in LENGTH. */
155static void
852483bc 156dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
4c2df51b 157{
da62e633
AC
158 /* FIXME: cagney/2003-03-26: This code should be using
159 get_frame_base_address(), and then implement a dwarf2 specific
160 this_base method. */
4c2df51b 161 struct symbol *framefunc;
4c2df51b 162 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
0d53c4c4 163
edb3359d
DJ
164 /* Use block_linkage_function, which returns a real (not inlined)
165 function, instead of get_frame_function, which may return an
166 inlined function. */
167 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
0d53c4c4 168
eff4f95e
JG
169 /* If we found a frame-relative symbol then it was certainly within
170 some function associated with a frame. If we can't find the frame,
171 something has gone wrong. */
172 gdb_assert (framefunc != NULL);
173
0936ad1d
SS
174 dwarf_expr_frame_base_1 (framefunc,
175 get_frame_address_in_block (debaton->frame),
176 start, length);
177}
178
179static void
180dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
181 gdb_byte **start, size_t *length)
182{
edb3359d
DJ
183 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
184 *start = NULL;
185 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
0d53c4c4
DJ
186 {
187 struct dwarf2_loclist_baton *symbaton;
22c6caba 188
0d53c4c4 189 symbaton = SYMBOL_LOCATION_BATON (framefunc);
0936ad1d 190 *start = find_location_expression (symbaton, length, pc);
0d53c4c4
DJ
191 }
192 else
193 {
194 struct dwarf2_locexpr_baton *symbaton;
9a619af0 195
0d53c4c4 196 symbaton = SYMBOL_LOCATION_BATON (framefunc);
ebd3bcc1
JK
197 if (symbaton != NULL)
198 {
199 *length = symbaton->size;
200 *start = symbaton->data;
201 }
202 else
203 *start = NULL;
0d53c4c4
DJ
204 }
205
206 if (*start == NULL)
8a3fe4f8 207 error (_("Could not find the frame base for \"%s\"."),
0d53c4c4 208 SYMBOL_NATURAL_NAME (framefunc));
4c2df51b
DJ
209}
210
e7802207
TT
211/* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
212 the frame in BATON. */
213
214static CORE_ADDR
215dwarf_expr_frame_cfa (void *baton)
216{
217 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
9a619af0 218
e7802207
TT
219 return dwarf2_frame_cfa (debaton->frame);
220}
221
4c2df51b
DJ
222/* Using the objfile specified in BATON, find the address for the
223 current thread's thread-local storage with offset OFFSET. */
224static CORE_ADDR
225dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
226{
227 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
4c2df51b 228
9e35dae4 229 return target_translate_tls_address (debaton->objfile, offset);
4c2df51b
DJ
230}
231
052b9502
NF
232struct piece_closure
233{
234 /* The number of pieces used to describe this variable. */
235 int n_pieces;
236
6063c216
UW
237 /* The target address size, used only for DWARF_VALUE_STACK. */
238 int addr_size;
cec03d70 239
052b9502
NF
240 /* The pieces themselves. */
241 struct dwarf_expr_piece *pieces;
242};
243
244/* Allocate a closure for a value formed from separately-described
245 PIECES. */
246
247static struct piece_closure *
cec03d70 248allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
6063c216 249 int addr_size)
052b9502
NF
250{
251 struct piece_closure *c = XZALLOC (struct piece_closure);
252
253 c->n_pieces = n_pieces;
6063c216 254 c->addr_size = addr_size;
052b9502
NF
255 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
256
257 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
258
259 return c;
260}
261
262static void
263read_pieced_value (struct value *v)
264{
265 int i;
266 long offset = 0;
afd74c5f 267 ULONGEST bytes_to_skip;
052b9502
NF
268 gdb_byte *contents;
269 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
270 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
afd74c5f
TT
271 size_t type_len;
272
273 if (value_type (v) != value_enclosing_type (v))
274 internal_error (__FILE__, __LINE__,
275 _("Should not be able to create a lazy value with "
276 "an enclosing type"));
052b9502
NF
277
278 contents = value_contents_raw (v);
afd74c5f
TT
279 bytes_to_skip = value_offset (v);
280 type_len = TYPE_LENGTH (value_type (v));
281 for (i = 0; i < c->n_pieces && offset < type_len; i++)
052b9502
NF
282 {
283 struct dwarf_expr_piece *p = &c->pieces[i];
afd74c5f
TT
284 size_t this_size;
285 long dest_offset, source_offset;
286
287 if (bytes_to_skip > 0 && bytes_to_skip >= p->size)
288 {
289 bytes_to_skip -= p->size;
290 continue;
291 }
292 this_size = p->size;
293 if (this_size > type_len - offset)
294 this_size = type_len - offset;
295 if (bytes_to_skip > 0)
296 {
297 dest_offset = 0;
298 source_offset = bytes_to_skip;
299 this_size -= bytes_to_skip;
300 bytes_to_skip = 0;
301 }
302 else
303 {
304 dest_offset = offset;
305 source_offset = 0;
306 }
9a619af0 307
cec03d70 308 switch (p->location)
052b9502 309 {
cec03d70
TT
310 case DWARF_VALUE_REGISTER:
311 {
312 struct gdbarch *arch = get_frame_arch (frame);
cec03d70 313 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
44353522 314 p->v.expr.value);
afd74c5f 315 int reg_offset = source_offset;
dcbf108f
UW
316
317 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
afd74c5f 318 && this_size < register_size (arch, gdb_regnum))
dcbf108f 319 /* Big-endian, and we want less than full size. */
afd74c5f 320 reg_offset = register_size (arch, gdb_regnum) - this_size;
dcbf108f 321
63b4f126
MGD
322 if (gdb_regnum != -1)
323 {
324 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
afd74c5f 325 this_size, contents + dest_offset);
63b4f126
MGD
326 }
327 else
328 {
329 error (_("Unable to access DWARF register number %s"),
330 paddress (arch, p->v.expr.value));
331 }
cec03d70
TT
332 }
333 break;
334
335 case DWARF_VALUE_MEMORY:
44353522 336 if (p->v.expr.in_stack_memory)
afd74c5f
TT
337 read_stack (p->v.expr.value + source_offset,
338 contents + dest_offset, this_size);
44353522 339 else
afd74c5f
TT
340 read_memory (p->v.expr.value + source_offset,
341 contents + dest_offset, this_size);
cec03d70
TT
342 break;
343
344 case DWARF_VALUE_STACK:
345 {
6063c216 346 struct gdbarch *gdbarch = get_type_arch (value_type (v));
afd74c5f 347 size_t n = this_size;
9a619af0 348
afd74c5f
TT
349 if (n > c->addr_size - source_offset)
350 n = (c->addr_size >= source_offset
351 ? c->addr_size - source_offset
352 : 0);
353 if (n == 0)
354 {
355 /* Nothing. */
356 }
357 else if (source_offset == 0)
358 store_unsigned_integer (contents + dest_offset, n,
359 gdbarch_byte_order (gdbarch),
360 p->v.expr.value);
361 else
362 {
363 gdb_byte bytes[sizeof (ULONGEST)];
364
365 store_unsigned_integer (bytes, n + source_offset,
366 gdbarch_byte_order (gdbarch),
367 p->v.expr.value);
368 memcpy (contents + dest_offset, bytes + source_offset, n);
369 }
cec03d70
TT
370 }
371 break;
372
373 case DWARF_VALUE_LITERAL:
374 {
afd74c5f
TT
375 size_t n = this_size;
376
377 if (n > p->v.literal.length - source_offset)
378 n = (p->v.literal.length >= source_offset
379 ? p->v.literal.length - source_offset
380 : 0);
381 if (n != 0)
382 memcpy (contents + dest_offset,
383 p->v.literal.data + source_offset, n);
cec03d70
TT
384 }
385 break;
386
387 default:
388 internal_error (__FILE__, __LINE__, _("invalid location type"));
052b9502 389 }
afd74c5f 390 offset += this_size;
052b9502
NF
391 }
392}
393
394static void
395write_pieced_value (struct value *to, struct value *from)
396{
397 int i;
398 long offset = 0;
afd74c5f
TT
399 ULONGEST bytes_to_skip;
400 const gdb_byte *contents;
052b9502
NF
401 struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
402 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
afd74c5f 403 size_t type_len;
052b9502
NF
404
405 if (frame == NULL)
406 {
407 set_value_optimized_out (to, 1);
408 return;
409 }
410
afd74c5f
TT
411 contents = value_contents (from);
412 bytes_to_skip = value_offset (to);
413 type_len = TYPE_LENGTH (value_type (to));
414 for (i = 0; i < c->n_pieces && offset < type_len; i++)
052b9502
NF
415 {
416 struct dwarf_expr_piece *p = &c->pieces[i];
afd74c5f
TT
417 size_t this_size;
418 long dest_offset, source_offset;
419
420 if (bytes_to_skip > 0 && bytes_to_skip >= p->size)
421 {
422 bytes_to_skip -= p->size;
423 continue;
424 }
425 this_size = p->size;
426 if (this_size > type_len - offset)
427 this_size = type_len - offset;
428 if (bytes_to_skip > 0)
429 {
430 dest_offset = bytes_to_skip;
431 source_offset = 0;
432 this_size -= bytes_to_skip;
433 bytes_to_skip = 0;
434 }
435 else
436 {
437 dest_offset = 0;
438 source_offset = offset;
439 }
9a619af0 440
cec03d70 441 switch (p->location)
052b9502 442 {
cec03d70
TT
443 case DWARF_VALUE_REGISTER:
444 {
445 struct gdbarch *arch = get_frame_arch (frame);
44353522 446 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
afd74c5f 447 int reg_offset = dest_offset;
dcbf108f
UW
448
449 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
afd74c5f 450 && this_size <= register_size (arch, gdb_regnum))
dcbf108f 451 /* Big-endian, and we want less than full size. */
afd74c5f 452 reg_offset = register_size (arch, gdb_regnum) - this_size;
dcbf108f 453
63b4f126
MGD
454 if (gdb_regnum != -1)
455 {
456 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
afd74c5f 457 this_size, contents + source_offset);
63b4f126
MGD
458 }
459 else
460 {
461 error (_("Unable to write to DWARF register number %s"),
462 paddress (arch, p->v.expr.value));
463 }
cec03d70
TT
464 }
465 break;
466 case DWARF_VALUE_MEMORY:
afd74c5f
TT
467 write_memory (p->v.expr.value + dest_offset,
468 contents + source_offset, this_size);
cec03d70
TT
469 break;
470 default:
471 set_value_optimized_out (to, 1);
472 return;
052b9502 473 }
afd74c5f 474 offset += this_size;
052b9502
NF
475 }
476}
477
478static void *
479copy_pieced_value_closure (struct value *v)
480{
481 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
482
6063c216 483 return allocate_piece_closure (c->n_pieces, c->pieces, c->addr_size);
052b9502
NF
484}
485
486static void
487free_pieced_value_closure (struct value *v)
488{
489 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
490
491 xfree (c->pieces);
492 xfree (c);
493}
494
495/* Functions for accessing a variable described by DW_OP_piece. */
496static struct lval_funcs pieced_value_funcs = {
497 read_pieced_value,
498 write_pieced_value,
499 copy_pieced_value_closure,
500 free_pieced_value_closure
501};
502
4c2df51b 503/* Evaluate a location description, starting at DATA and with length
a2d33775 504 SIZE, to find the current location of variable of TYPE in the context
4c2df51b 505 of FRAME. */
a2d33775 506
4c2df51b 507static struct value *
a2d33775 508dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
852483bc 509 gdb_byte *data, unsigned short size,
ae0d2f24 510 struct dwarf2_per_cu_data *per_cu)
4c2df51b 511{
4c2df51b
DJ
512 struct value *retval;
513 struct dwarf_expr_baton baton;
514 struct dwarf_expr_context *ctx;
4a227398 515 struct cleanup *old_chain;
4c2df51b 516
0d53c4c4
DJ
517 if (size == 0)
518 {
a2d33775 519 retval = allocate_value (type);
0d53c4c4 520 VALUE_LVAL (retval) = not_lval;
feb13ab0 521 set_value_optimized_out (retval, 1);
10fb19b6 522 return retval;
0d53c4c4
DJ
523 }
524
4c2df51b 525 baton.frame = frame;
ae0d2f24 526 baton.objfile = dwarf2_per_cu_objfile (per_cu);
4c2df51b
DJ
527
528 ctx = new_dwarf_expr_context ();
4a227398
TT
529 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
530
f7fd4728 531 ctx->gdbarch = get_objfile_arch (baton.objfile);
ae0d2f24 532 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
4c2df51b
DJ
533 ctx->baton = &baton;
534 ctx->read_reg = dwarf_expr_read_reg;
535 ctx->read_mem = dwarf_expr_read_mem;
536 ctx->get_frame_base = dwarf_expr_frame_base;
e7802207 537 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
4c2df51b
DJ
538 ctx->get_tls_address = dwarf_expr_tls_address;
539
540 dwarf_expr_eval (ctx, data, size);
87808bd6
JB
541 if (ctx->num_pieces > 0)
542 {
052b9502
NF
543 struct piece_closure *c;
544 struct frame_id frame_id = get_frame_id (frame);
545
6063c216
UW
546 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
547 ctx->addr_size);
a2d33775 548 retval = allocate_computed_value (type, &pieced_value_funcs, c);
052b9502 549 VALUE_FRAME_ID (retval) = frame_id;
87808bd6 550 }
4c2df51b
DJ
551 else
552 {
cec03d70
TT
553 switch (ctx->location)
554 {
555 case DWARF_VALUE_REGISTER:
556 {
557 struct gdbarch *arch = get_frame_arch (frame);
558 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
559 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
9a619af0 560
63b4f126 561 if (gdb_regnum != -1)
a2d33775 562 retval = value_from_register (type, gdb_regnum, frame);
63b4f126 563 else
a2d33775
JK
564 error (_("Unable to access DWARF register number %s"),
565 paddress (arch, dwarf_regnum));
cec03d70
TT
566 }
567 break;
568
569 case DWARF_VALUE_MEMORY:
570 {
571 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
44353522 572 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
cec03d70 573
a2d33775 574 retval = allocate_value (type);
cec03d70
TT
575 VALUE_LVAL (retval) = lval_memory;
576 set_value_lazy (retval, 1);
44353522
DE
577 if (in_stack_memory)
578 set_value_stack (retval, 1);
cec03d70
TT
579 set_value_address (retval, address);
580 }
581 break;
582
583 case DWARF_VALUE_STACK:
584 {
cec03d70
TT
585 ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
586 bfd_byte *contents;
587 size_t n = ctx->addr_size;
588
a2d33775 589 retval = allocate_value (type);
cec03d70 590 contents = value_contents_raw (retval);
a2d33775
JK
591 if (n > TYPE_LENGTH (type))
592 n = TYPE_LENGTH (type);
05566b3b
TT
593 store_unsigned_integer (contents, n,
594 gdbarch_byte_order (ctx->gdbarch),
595 value);
cec03d70
TT
596 }
597 break;
598
599 case DWARF_VALUE_LITERAL:
600 {
601 bfd_byte *contents;
602 size_t n = ctx->len;
603
a2d33775 604 retval = allocate_value (type);
cec03d70 605 contents = value_contents_raw (retval);
a2d33775
JK
606 if (n > TYPE_LENGTH (type))
607 n = TYPE_LENGTH (type);
cec03d70
TT
608 memcpy (contents, ctx->data, n);
609 }
610 break;
611
612 default:
613 internal_error (__FILE__, __LINE__, _("invalid location type"));
614 }
4c2df51b
DJ
615 }
616
42be36b3
CT
617 set_value_initialized (retval, ctx->initialized);
618
4a227398 619 do_cleanups (old_chain);
4c2df51b
DJ
620
621 return retval;
622}
4c2df51b
DJ
623\f
624/* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
625
626struct needs_frame_baton
627{
628 int needs_frame;
629};
630
631/* Reads from registers do require a frame. */
632static CORE_ADDR
61fbb938 633needs_frame_read_reg (void *baton, int regnum)
4c2df51b
DJ
634{
635 struct needs_frame_baton *nf_baton = baton;
9a619af0 636
4c2df51b
DJ
637 nf_baton->needs_frame = 1;
638 return 1;
639}
640
641/* Reads from memory do not require a frame. */
642static void
852483bc 643needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
4c2df51b
DJ
644{
645 memset (buf, 0, len);
646}
647
648/* Frame-relative accesses do require a frame. */
649static void
852483bc 650needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
4c2df51b 651{
852483bc 652 static gdb_byte lit0 = DW_OP_lit0;
4c2df51b
DJ
653 struct needs_frame_baton *nf_baton = baton;
654
655 *start = &lit0;
656 *length = 1;
657
658 nf_baton->needs_frame = 1;
659}
660
e7802207
TT
661/* CFA accesses require a frame. */
662
663static CORE_ADDR
664needs_frame_frame_cfa (void *baton)
665{
666 struct needs_frame_baton *nf_baton = baton;
9a619af0 667
e7802207
TT
668 nf_baton->needs_frame = 1;
669 return 1;
670}
671
4c2df51b
DJ
672/* Thread-local accesses do require a frame. */
673static CORE_ADDR
674needs_frame_tls_address (void *baton, CORE_ADDR offset)
675{
676 struct needs_frame_baton *nf_baton = baton;
9a619af0 677
4c2df51b
DJ
678 nf_baton->needs_frame = 1;
679 return 1;
680}
681
682/* Return non-zero iff the location expression at DATA (length SIZE)
683 requires a frame to evaluate. */
684
685static int
ae0d2f24
UW
686dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
687 struct dwarf2_per_cu_data *per_cu)
4c2df51b
DJ
688{
689 struct needs_frame_baton baton;
690 struct dwarf_expr_context *ctx;
f630a401 691 int in_reg;
4a227398 692 struct cleanup *old_chain;
4c2df51b
DJ
693
694 baton.needs_frame = 0;
695
696 ctx = new_dwarf_expr_context ();
4a227398
TT
697 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
698
f7fd4728 699 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
ae0d2f24 700 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
4c2df51b
DJ
701 ctx->baton = &baton;
702 ctx->read_reg = needs_frame_read_reg;
703 ctx->read_mem = needs_frame_read_mem;
704 ctx->get_frame_base = needs_frame_frame_base;
e7802207 705 ctx->get_frame_cfa = needs_frame_frame_cfa;
4c2df51b
DJ
706 ctx->get_tls_address = needs_frame_tls_address;
707
708 dwarf_expr_eval (ctx, data, size);
709
cec03d70 710 in_reg = ctx->location == DWARF_VALUE_REGISTER;
f630a401 711
87808bd6
JB
712 if (ctx->num_pieces > 0)
713 {
714 int i;
715
716 /* If the location has several pieces, and any of them are in
717 registers, then we will need a frame to fetch them from. */
718 for (i = 0; i < ctx->num_pieces; i++)
cec03d70 719 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
87808bd6
JB
720 in_reg = 1;
721 }
722
4a227398 723 do_cleanups (old_chain);
4c2df51b 724
f630a401 725 return baton.needs_frame || in_reg;
4c2df51b
DJ
726}
727
08922a10
SS
728/* This struct keeps track of the pieces that make up a multi-location
729 object, for use in agent expression generation. It is
730 superficially similar to struct dwarf_expr_piece, but
731 dwarf_expr_piece is designed for use in immediate evaluation, and
732 does not, for example, have a way to record both base register and
733 offset. */
734
735struct axs_var_loc
0d53c4c4 736{
08922a10
SS
737 /* Memory vs register, etc */
738 enum axs_lvalue_kind kind;
739
740 /* If non-zero, number of bytes in this fragment */
741 unsigned bytes;
742
743 /* (GDB-numbered) reg, or base reg if >= 0 */
744 int reg;
745
746 /* offset from reg */
747 LONGEST offset;
748};
749
750static gdb_byte *
751dwarf2_tracepoint_var_loc (struct symbol *symbol,
752 struct agent_expr *ax,
753 struct axs_var_loc *loc,
754 struct gdbarch *gdbarch,
755 gdb_byte *data, gdb_byte *end)
756{
757 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
0d53c4c4 758 {
08922a10
SS
759 loc->kind = axs_lvalue_register;
760 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
761 data += 1;
0d53c4c4
DJ
762 }
763 else if (data[0] == DW_OP_regx)
764 {
765 ULONGEST reg;
9a619af0 766
08922a10
SS
767 data = read_uleb128 (data + 1, end, &reg);
768 loc->kind = axs_lvalue_register;
769 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
0d53c4c4
DJ
770 }
771 else if (data[0] == DW_OP_fbreg)
772 {
0936ad1d
SS
773 struct block *b;
774 struct symbol *framefunc;
775 int frame_reg = 0;
0d53c4c4 776 LONGEST frame_offset;
0936ad1d
SS
777 gdb_byte *base_data;
778 size_t base_size;
779 LONGEST base_offset = 0;
780
781 b = block_for_pc (ax->scope);
782
783 if (!b)
784 error (_("No block found for address"));
785
786 framefunc = block_linkage_function (b);
787
788 if (!framefunc)
789 error (_("No function found for block"));
790
791 dwarf_expr_frame_base_1 (framefunc, ax->scope,
792 &base_data, &base_size);
793
08922a10 794 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
0936ad1d 795 {
08922a10
SS
796 gdb_byte *buf_end;
797
0936ad1d 798 frame_reg = base_data[0] - DW_OP_breg0;
08922a10
SS
799 buf_end = read_sleb128 (base_data + 1,
800 base_data + base_size, &base_offset);
0936ad1d
SS
801 if (buf_end != base_data + base_size)
802 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
803 frame_reg, SYMBOL_PRINT_NAME (symbol));
804 }
08922a10
SS
805 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
806 {
807 /* The frame base is just the register, with no offset. */
808 frame_reg = base_data[0] - DW_OP_reg0;
809 base_offset = 0;
810 }
0936ad1d
SS
811 else
812 {
813 /* We don't know what to do with the frame base expression,
814 so we can't trace this variable; give up. */
08922a10
SS
815 error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
816 SYMBOL_PRINT_NAME (symbol), base_data[0]);
0936ad1d 817 }
0d53c4c4 818
08922a10 819 data = read_sleb128 (data + 1, end, &frame_offset);
4c2df51b 820
08922a10
SS
821 loc->kind = axs_lvalue_memory;
822 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
823 loc->offset = base_offset + frame_offset;
9c238357 824 }
08922a10 825 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
9c238357
RC
826 {
827 unsigned int reg;
828 LONGEST offset;
9c238357
RC
829
830 reg = data[0] - DW_OP_breg0;
08922a10 831 data = read_sleb128 (data + 1, end, &offset);
9c238357 832
08922a10
SS
833 loc->kind = axs_lvalue_memory;
834 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
835 loc->offset = offset;
0d53c4c4
DJ
836 }
837 else
9c238357
RC
838 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
839 data[0], SYMBOL_PRINT_NAME (symbol));
08922a10
SS
840
841 return data;
0d53c4c4 842}
08922a10
SS
843
844/* Given the location of a piece, issue bytecodes that will access it. */
845
846static void
847dwarf2_tracepoint_var_access (struct agent_expr *ax,
848 struct axs_value *value,
849 struct axs_var_loc *loc)
850{
851 value->kind = loc->kind;
852
853 switch (loc->kind)
854 {
855 case axs_lvalue_register:
856 value->u.reg = loc->reg;
857 break;
858
859 case axs_lvalue_memory:
860 ax_reg (ax, loc->reg);
861 if (loc->offset)
862 {
863 ax_const_l (ax, loc->offset);
864 ax_simple (ax, aop_add);
865 }
866 break;
867
868 default:
869 internal_error (__FILE__, __LINE__, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
870 }
871}
872
873static void
874dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
875 struct agent_expr *ax, struct axs_value *value,
876 gdb_byte *data, int size)
877{
878 gdb_byte *end = data + size;
879 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
880 /* In practice, a variable is not going to be spread across
881 dozens of registers or memory locations. If someone comes up
882 with a real-world example, revisit this. */
883#define MAX_FRAGS 16
884 struct axs_var_loc fragments[MAX_FRAGS];
885 int nfrags = 0, frag;
886 int length = 0;
887 int piece_ok = 0;
888 int bad = 0;
889 int first = 1;
890
891 if (!data || size == 0)
892 {
893 value->optimized_out = 1;
894 return;
895 }
896
897 while (data < end)
898 {
899 if (!piece_ok)
900 {
901 if (nfrags == MAX_FRAGS)
902 error (_("Too many pieces in location for \"%s\"."),
903 SYMBOL_PRINT_NAME (symbol));
904
905 fragments[nfrags].bytes = 0;
906 data = dwarf2_tracepoint_var_loc (symbol, ax, &fragments[nfrags],
907 gdbarch, data, end);
908 nfrags++;
909 piece_ok = 1;
910 }
911 else if (data[0] == DW_OP_piece)
912 {
913 ULONGEST bytes;
914
915 data = read_uleb128 (data + 1, end, &bytes);
916 /* Only deal with 4 byte fragments for now. */
917 if (bytes != 4)
918 error (_("DW_OP_piece %s not supported in location for \"%s\"."),
919 pulongest (bytes), SYMBOL_PRINT_NAME (symbol));
920 fragments[nfrags - 1].bytes = bytes;
921 length += bytes;
922 piece_ok = 0;
923 }
924 else
925 {
926 bad = 1;
927 break;
928 }
929 }
930
931 if (bad || data > end)
932 error (_("Corrupted DWARF expression for \"%s\"."),
933 SYMBOL_PRINT_NAME (symbol));
934
935 /* If single expression, no pieces, convert to external format. */
936 if (length == 0)
937 {
938 dwarf2_tracepoint_var_access (ax, value, &fragments[0]);
939 return;
940 }
941
942 if (length != TYPE_LENGTH (value->type))
943 error (_("Inconsistent piece information for \"%s\"."),
944 SYMBOL_PRINT_NAME (symbol));
945
946 /* Emit bytecodes to assemble the pieces into a single stack entry. */
947
948 for ((frag = (byte_order == BFD_ENDIAN_BIG ? 0 : nfrags - 1));
949 nfrags--;
950 (frag += (byte_order == BFD_ENDIAN_BIG ? 1 : -1)))
951 {
952 if (!first)
953 {
954 /* shift the previous fragment up 32 bits */
955 ax_const_l (ax, 32);
956 ax_simple (ax, aop_lsh);
957 }
958
959 dwarf2_tracepoint_var_access (ax, value, &fragments[frag]);
960
961 switch (value->kind)
962 {
963 case axs_lvalue_register:
964 ax_reg (ax, value->u.reg);
965 break;
966
967 case axs_lvalue_memory:
968 {
969 extern int trace_kludge; /* Ugh. */
970
971 gdb_assert (fragments[frag].bytes == 4);
972 if (trace_kludge)
973 ax_trace_quick (ax, 4);
974 ax_simple (ax, aop_ref32);
975 }
976 break;
977 }
978
979 if (!first)
980 {
981 /* or the new fragment into the previous */
982 ax_zero_ext (ax, 32);
983 ax_simple (ax, aop_bit_or);
984 }
985 first = 0;
986 }
987 value->kind = axs_rvalue;
988}
989
4c2df51b
DJ
990\f
991/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
992 evaluator to calculate the location. */
993static struct value *
994locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
995{
996 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
997 struct value *val;
9a619af0 998
a2d33775
JK
999 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1000 dlbaton->size, dlbaton->per_cu);
4c2df51b
DJ
1001
1002 return val;
1003}
1004
1005/* Return non-zero iff we need a frame to evaluate SYMBOL. */
1006static int
1007locexpr_read_needs_frame (struct symbol *symbol)
1008{
1009 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
9a619af0 1010
ae0d2f24
UW
1011 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1012 dlbaton->per_cu);
4c2df51b
DJ
1013}
1014
08922a10
SS
1015/* Describe a single piece of a location, returning an updated
1016 position in the bytecode sequence. */
1017
1018static gdb_byte *
1019locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1020 CORE_ADDR addr, struct objfile *objfile,
1021 gdb_byte *data, int size, unsigned int addr_size)
4c2df51b 1022{
08922a10
SS
1023 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1024 int regno;
1025
1026 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1027 {
1028 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1029 fprintf_filtered (stream, _("a variable in $%s"),
1030 gdbarch_register_name (gdbarch, regno));
1031 data += 1;
1032 }
1033 else if (data[0] == DW_OP_regx)
1034 {
1035 ULONGEST reg;
4c2df51b 1036
08922a10
SS
1037 data = read_uleb128 (data + 1, data + size, &reg);
1038 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1039 fprintf_filtered (stream, _("a variable in $%s"),
1040 gdbarch_register_name (gdbarch, regno));
1041 }
1042 else if (data[0] == DW_OP_fbreg)
4c2df51b 1043 {
08922a10
SS
1044 struct block *b;
1045 struct symbol *framefunc;
1046 int frame_reg = 0;
1047 LONGEST frame_offset;
1048 gdb_byte *base_data;
1049 size_t base_size;
1050 LONGEST base_offset = 0;
1051
1052 b = block_for_pc (addr);
1053
1054 if (!b)
1055 error (_("No block found for address for symbol \"%s\"."),
1056 SYMBOL_PRINT_NAME (symbol));
1057
1058 framefunc = block_linkage_function (b);
1059
1060 if (!framefunc)
1061 error (_("No function found for block for symbol \"%s\"."),
1062 SYMBOL_PRINT_NAME (symbol));
1063
1064 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1065
1066 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1067 {
1068 gdb_byte *buf_end;
1069
1070 frame_reg = base_data[0] - DW_OP_breg0;
1071 buf_end = read_sleb128 (base_data + 1,
1072 base_data + base_size, &base_offset);
1073 if (buf_end != base_data + base_size)
1074 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1075 frame_reg, SYMBOL_PRINT_NAME (symbol));
1076 }
1077 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1078 {
1079 /* The frame base is just the register, with no offset. */
1080 frame_reg = base_data[0] - DW_OP_reg0;
1081 base_offset = 0;
1082 }
1083 else
1084 {
1085 /* We don't know what to do with the frame base expression,
1086 so we can't trace this variable; give up. */
1087 error (_("Cannot describe location of symbol \"%s\"; "
1088 "DWARF 2 encoding not handled, "
1089 "first opcode in base data is 0x%x."),
1090 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1091 }
1092
1093 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1094
1095 data = read_sleb128 (data + 1, data + size, &frame_offset);
1096
1097 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1098 gdbarch_register_name (gdbarch, regno),
1099 plongest (base_offset), plongest (frame_offset));
1100 }
1101 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
1102 {
1103 LONGEST offset;
1104
1105 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1106
1107 data = read_sleb128 (data + 1, data + size, &offset);
1108
4c2df51b 1109 fprintf_filtered (stream,
08922a10
SS
1110 _("a variable at offset %s from base reg $%s"),
1111 plongest (offset),
5e2b427d 1112 gdbarch_register_name (gdbarch, regno));
4c2df51b
DJ
1113 }
1114
c3228f12
EZ
1115 /* The location expression for a TLS variable looks like this (on a
1116 64-bit LE machine):
1117
1118 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1119 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1120
1121 0x3 is the encoding for DW_OP_addr, which has an operand as long
1122 as the size of an address on the target machine (here is 8
1123 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1124 The operand represents the offset at which the variable is within
1125 the thread local storage. */
1126
08922a10
SS
1127 else if (size > 1
1128 && data[size - 1] == DW_OP_GNU_push_tls_address
1129 && data[0] == DW_OP_addr)
1130 {
1131 CORE_ADDR offset = dwarf2_read_address (gdbarch,
1132 data + 1,
1133 data + size - 1,
1134 addr_size);
9a619af0 1135
08922a10
SS
1136 fprintf_filtered (stream,
1137 _("a thread-local variable at offset %s "
1138 "in the thread-local storage for `%s'"),
1139 paddress (gdbarch, offset), objfile->name);
1140
1141 data += 1 + addr_size + 1;
1142 }
1143 else
1144 fprintf_filtered (stream,
1145 _("a variable with complex or multiple locations (DWARF2)"));
c3228f12 1146
08922a10 1147 return data;
4c2df51b
DJ
1148}
1149
08922a10
SS
1150/* Describe a single location, which may in turn consist of multiple
1151 pieces. */
a55cc764 1152
08922a10
SS
1153static void
1154locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
1155 struct ui_file *stream, gdb_byte *data, int size,
1156 struct objfile *objfile, unsigned int addr_size)
1157{
1158 gdb_byte *end = data + size;
1159 int piece_done = 0, first_piece = 1, bad = 0;
1160
1161 /* A multi-piece description consists of multiple sequences of bytes
1162 each followed by DW_OP_piece + length of piece. */
1163 while (data < end)
1164 {
1165 if (!piece_done)
1166 {
1167 if (first_piece)
1168 first_piece = 0;
1169 else
1170 fprintf_filtered (stream, _(", and "));
1171
1172 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
1173 data, size, addr_size);
1174 piece_done = 1;
1175 }
1176 else if (data[0] == DW_OP_piece)
1177 {
1178 ULONGEST bytes;
1179
1180 data = read_uleb128 (data + 1, end, &bytes);
1181
1182 fprintf_filtered (stream, _(" [%s-byte piece]"), pulongest (bytes));
1183
1184 piece_done = 0;
1185 }
1186 else
1187 {
1188 bad = 1;
1189 break;
1190 }
1191 }
1192
1193 if (bad || data > end)
1194 error (_("Corrupted DWARF2 expression for \"%s\"."),
1195 SYMBOL_PRINT_NAME (symbol));
1196}
1197
1198/* Print a natural-language description of SYMBOL to STREAM. This
1199 version is for a symbol with a single location. */
a55cc764 1200
08922a10
SS
1201static void
1202locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
1203 struct ui_file *stream)
1204{
1205 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1206 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1207 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1208
1209 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
1210 objfile, addr_size);
1211}
1212
1213/* Describe the location of SYMBOL as an agent value in VALUE, generating
1214 any necessary bytecode in AX. */
a55cc764 1215
0d53c4c4 1216static void
505e835d
UW
1217locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1218 struct agent_expr *ax, struct axs_value *value)
a55cc764
DJ
1219{
1220 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1221
505e835d
UW
1222 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
1223 dlbaton->data, dlbaton->size);
a55cc764
DJ
1224}
1225
4c2df51b
DJ
1226/* The set of location functions used with the DWARF-2 expression
1227 evaluator. */
768a979c 1228const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4c2df51b
DJ
1229 locexpr_read_variable,
1230 locexpr_read_needs_frame,
1231 locexpr_describe_location,
a55cc764 1232 locexpr_tracepoint_var_ref
4c2df51b 1233};
0d53c4c4
DJ
1234
1235
1236/* Wrapper functions for location lists. These generally find
1237 the appropriate location expression and call something above. */
1238
1239/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1240 evaluator to calculate the location. */
1241static struct value *
1242loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
1243{
1244 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1245 struct value *val;
852483bc 1246 gdb_byte *data;
b6b08ebf 1247 size_t size;
0d53c4c4
DJ
1248
1249 data = find_location_expression (dlbaton, &size,
22c6caba
JW
1250 frame ? get_frame_address_in_block (frame)
1251 : 0);
0d53c4c4 1252 if (data == NULL)
806048c6
DJ
1253 {
1254 val = allocate_value (SYMBOL_TYPE (symbol));
1255 VALUE_LVAL (val) = not_lval;
1256 set_value_optimized_out (val, 1);
1257 }
1258 else
a2d33775 1259 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
ae0d2f24 1260 dlbaton->per_cu);
0d53c4c4
DJ
1261
1262 return val;
1263}
1264
1265/* Return non-zero iff we need a frame to evaluate SYMBOL. */
1266static int
1267loclist_read_needs_frame (struct symbol *symbol)
1268{
1269 /* If there's a location list, then assume we need to have a frame
1270 to choose the appropriate location expression. With tracking of
1271 global variables this is not necessarily true, but such tracking
1272 is disabled in GCC at the moment until we figure out how to
1273 represent it. */
1274
1275 return 1;
1276}
1277
08922a10
SS
1278/* Print a natural-language description of SYMBOL to STREAM. This
1279 version applies when there is a list of different locations, each
1280 with a specified address range. */
1281
1282static void
1283loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
1284 struct ui_file *stream)
0d53c4c4 1285{
08922a10
SS
1286 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1287 CORE_ADDR low, high;
1288 gdb_byte *loc_ptr, *buf_end;
1289 int length, first = 1;
1290 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1291 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1292 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1293 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1294 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1295 /* Adjust base_address for relocatable objects. */
1296 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
1297 SECT_OFF_TEXT (objfile));
1298 CORE_ADDR base_address = dlbaton->base_address + base_offset;
1299
1300 loc_ptr = dlbaton->data;
1301 buf_end = dlbaton->data + dlbaton->size;
1302
1303 fprintf_filtered (stream, _("multi-location ("));
1304
1305 /* Iterate through locations until we run out. */
1306 while (1)
1307 {
1308 if (buf_end - loc_ptr < 2 * addr_size)
1309 error (_("Corrupted DWARF expression for symbol \"%s\"."),
1310 SYMBOL_PRINT_NAME (symbol));
1311
1312 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1313 loc_ptr += addr_size;
1314
1315 /* A base-address-selection entry. */
1316 if (low == base_mask)
1317 {
1318 base_address = dwarf2_read_address (gdbarch,
1319 loc_ptr, buf_end, addr_size);
1320 fprintf_filtered (stream, _("[base address %s]"),
1321 paddress (gdbarch, base_address));
1322 loc_ptr += addr_size;
1323 continue;
1324 }
1325
1326 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1327 loc_ptr += addr_size;
1328
1329 /* An end-of-list entry. */
1330 if (low == 0 && high == 0)
1331 {
1332 /* Indicate the end of the list, for readability. */
1333 fprintf_filtered (stream, _(")"));
1334 return;
1335 }
1336
1337 /* Otherwise, a location expression entry. */
1338 low += base_address;
1339 high += base_address;
1340
1341 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
1342 loc_ptr += 2;
1343
1344 /* Separate the different locations with a semicolon. */
1345 if (first)
1346 first = 0;
1347 else
1348 fprintf_filtered (stream, _("; "));
1349
1350 /* (It would improve readability to print only the minimum
1351 necessary digits of the second number of the range.) */
1352 fprintf_filtered (stream, _("range %s-%s, "),
1353 paddress (gdbarch, low), paddress (gdbarch, high));
1354
1355 /* Now describe this particular location. */
1356 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
1357 objfile, addr_size);
1358
1359 loc_ptr += length;
1360 }
0d53c4c4
DJ
1361}
1362
1363/* Describe the location of SYMBOL as an agent value in VALUE, generating
1364 any necessary bytecode in AX. */
1365static void
505e835d
UW
1366loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1367 struct agent_expr *ax, struct axs_value *value)
0d53c4c4
DJ
1368{
1369 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
852483bc 1370 gdb_byte *data;
b6b08ebf 1371 size_t size;
0d53c4c4
DJ
1372
1373 data = find_location_expression (dlbaton, &size, ax->scope);
0d53c4c4 1374
505e835d 1375 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
0d53c4c4
DJ
1376}
1377
1378/* The set of location functions used with the DWARF-2 expression
1379 evaluator and location lists. */
768a979c 1380const struct symbol_computed_ops dwarf2_loclist_funcs = {
0d53c4c4
DJ
1381 loclist_read_variable,
1382 loclist_read_needs_frame,
1383 loclist_describe_location,
1384 loclist_tracepoint_var_ref
1385};
This page took 0.628248 seconds and 4 git commands to generate.