2009-05-27 Tom Tromey <tromey@redhat.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3 Copyright (C) 2003, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "inferior.h"
29 #include "ax.h"
30 #include "ax-gdb.h"
31 #include "regcache.h"
32 #include "objfiles.h"
33 #include "exceptions.h"
34
35 #include "elf/dwarf2.h"
36 #include "dwarf2expr.h"
37 #include "dwarf2loc.h"
38
39 #include "gdb_string.h"
40 #include "gdb_assert.h"
41
42 /* A helper function for dealing with location lists. Given a
43 symbol baton (BATON) and a pc value (PC), find the appropriate
44 location expression, set *LOCEXPR_LENGTH, and return a pointer
45 to the beginning of the expression. Returns NULL on failure.
46
47 For now, only return the first matching location expression; there
48 can be more than one in the list. */
49
50 static gdb_byte *
51 find_location_expression (struct dwarf2_loclist_baton *baton,
52 size_t *locexpr_length, CORE_ADDR pc)
53 {
54 CORE_ADDR low, high;
55 gdb_byte *loc_ptr, *buf_end;
56 int length;
57 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
58 struct gdbarch *gdbarch = get_objfile_arch (objfile);
59 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
60 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
61 /* Adjust base_address for relocatable objects. */
62 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
63 SECT_OFF_TEXT (objfile));
64 CORE_ADDR base_address = baton->base_address + base_offset;
65
66 loc_ptr = baton->data;
67 buf_end = baton->data + baton->size;
68
69 while (1)
70 {
71 low = dwarf2_read_address (gdbarch, loc_ptr, buf_end, addr_size);
72 loc_ptr += addr_size;
73 high = dwarf2_read_address (gdbarch, loc_ptr, buf_end, addr_size);
74 loc_ptr += addr_size;
75
76 /* An end-of-list entry. */
77 if (low == 0 && high == 0)
78 return NULL;
79
80 /* A base-address-selection entry. */
81 if ((low & base_mask) == base_mask)
82 {
83 base_address = high;
84 continue;
85 }
86
87 /* Otherwise, a location expression entry. */
88 low += base_address;
89 high += base_address;
90
91 length = extract_unsigned_integer (loc_ptr, 2);
92 loc_ptr += 2;
93
94 if (pc >= low && pc < high)
95 {
96 *locexpr_length = length;
97 return loc_ptr;
98 }
99
100 loc_ptr += length;
101 }
102 }
103
104 /* This is the baton used when performing dwarf2 expression
105 evaluation. */
106 struct dwarf_expr_baton
107 {
108 struct frame_info *frame;
109 struct objfile *objfile;
110 };
111
112 /* Helper functions for dwarf2_evaluate_loc_desc. */
113
114 /* Using the frame specified in BATON, return the value of register
115 REGNUM, treated as a pointer. */
116 static CORE_ADDR
117 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
118 {
119 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
120 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
121 CORE_ADDR result;
122 int regnum;
123
124 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
125 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
126 regnum, debaton->frame);
127 return result;
128 }
129
130 /* Read memory at ADDR (length LEN) into BUF. */
131
132 static void
133 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
134 {
135 read_memory (addr, buf, len);
136 }
137
138 /* Using the frame specified in BATON, find the location expression
139 describing the frame base. Return a pointer to it in START and
140 its length in LENGTH. */
141 static void
142 dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
143 {
144 /* FIXME: cagney/2003-03-26: This code should be using
145 get_frame_base_address(), and then implement a dwarf2 specific
146 this_base method. */
147 struct symbol *framefunc;
148 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
149
150 framefunc = get_frame_function (debaton->frame);
151
152 /* If we found a frame-relative symbol then it was certainly within
153 some function associated with a frame. If we can't find the frame,
154 something has gone wrong. */
155 gdb_assert (framefunc != NULL);
156
157 if (SYMBOL_OPS (framefunc) == &dwarf2_loclist_funcs)
158 {
159 struct dwarf2_loclist_baton *symbaton;
160 struct frame_info *frame = debaton->frame;
161
162 symbaton = SYMBOL_LOCATION_BATON (framefunc);
163 *start = find_location_expression (symbaton, length,
164 get_frame_address_in_block (frame));
165 }
166 else
167 {
168 struct dwarf2_locexpr_baton *symbaton;
169 symbaton = SYMBOL_LOCATION_BATON (framefunc);
170 if (symbaton != NULL)
171 {
172 *length = symbaton->size;
173 *start = symbaton->data;
174 }
175 else
176 *start = NULL;
177 }
178
179 if (*start == NULL)
180 error (_("Could not find the frame base for \"%s\"."),
181 SYMBOL_NATURAL_NAME (framefunc));
182 }
183
184 /* Using the objfile specified in BATON, find the address for the
185 current thread's thread-local storage with offset OFFSET. */
186 static CORE_ADDR
187 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
188 {
189 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
190
191 return target_translate_tls_address (debaton->objfile, offset);
192 }
193
194 /* Evaluate a location description, starting at DATA and with length
195 SIZE, to find the current location of variable VAR in the context
196 of FRAME. */
197 static struct value *
198 dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
199 gdb_byte *data, unsigned short size,
200 struct dwarf2_per_cu_data *per_cu)
201 {
202 struct value *retval;
203 struct dwarf_expr_baton baton;
204 struct dwarf_expr_context *ctx;
205
206 if (size == 0)
207 {
208 retval = allocate_value (SYMBOL_TYPE (var));
209 VALUE_LVAL (retval) = not_lval;
210 set_value_optimized_out (retval, 1);
211 return retval;
212 }
213
214 baton.frame = frame;
215 baton.objfile = dwarf2_per_cu_objfile (per_cu);
216
217 ctx = new_dwarf_expr_context ();
218 ctx->gdbarch = get_objfile_arch (baton.objfile);
219 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
220 ctx->baton = &baton;
221 ctx->read_reg = dwarf_expr_read_reg;
222 ctx->read_mem = dwarf_expr_read_mem;
223 ctx->get_frame_base = dwarf_expr_frame_base;
224 ctx->get_tls_address = dwarf_expr_tls_address;
225
226 dwarf_expr_eval (ctx, data, size);
227 if (ctx->num_pieces > 0)
228 {
229 int i;
230 long offset = 0;
231 bfd_byte *contents;
232
233 retval = allocate_value (SYMBOL_TYPE (var));
234 contents = value_contents_raw (retval);
235 for (i = 0; i < ctx->num_pieces; i++)
236 {
237 struct dwarf_expr_piece *p = &ctx->pieces[i];
238 if (p->in_reg)
239 {
240 struct gdbarch *arch = get_frame_arch (frame);
241 bfd_byte regval[MAX_REGISTER_SIZE];
242 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->value);
243 get_frame_register (frame, gdb_regnum, regval);
244 memcpy (contents + offset, regval, p->size);
245 }
246 else /* In memory? */
247 {
248 read_memory (p->value, contents + offset, p->size);
249 }
250 offset += p->size;
251 }
252 }
253 else if (ctx->in_reg)
254 {
255 struct gdbarch *arch = get_frame_arch (frame);
256 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
257 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
258 retval = value_from_register (SYMBOL_TYPE (var), gdb_regnum, frame);
259 }
260 else
261 {
262 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
263
264 retval = allocate_value (SYMBOL_TYPE (var));
265 VALUE_LVAL (retval) = lval_memory;
266 set_value_lazy (retval, 1);
267 set_value_address (retval, address);
268 }
269
270 set_value_initialized (retval, ctx->initialized);
271
272 free_dwarf_expr_context (ctx);
273
274 return retval;
275 }
276
277
278
279
280 \f
281 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
282
283 struct needs_frame_baton
284 {
285 int needs_frame;
286 };
287
288 /* Reads from registers do require a frame. */
289 static CORE_ADDR
290 needs_frame_read_reg (void *baton, int regnum)
291 {
292 struct needs_frame_baton *nf_baton = baton;
293 nf_baton->needs_frame = 1;
294 return 1;
295 }
296
297 /* Reads from memory do not require a frame. */
298 static void
299 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
300 {
301 memset (buf, 0, len);
302 }
303
304 /* Frame-relative accesses do require a frame. */
305 static void
306 needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
307 {
308 static gdb_byte lit0 = DW_OP_lit0;
309 struct needs_frame_baton *nf_baton = baton;
310
311 *start = &lit0;
312 *length = 1;
313
314 nf_baton->needs_frame = 1;
315 }
316
317 /* Thread-local accesses do require a frame. */
318 static CORE_ADDR
319 needs_frame_tls_address (void *baton, CORE_ADDR offset)
320 {
321 struct needs_frame_baton *nf_baton = baton;
322 nf_baton->needs_frame = 1;
323 return 1;
324 }
325
326 /* Return non-zero iff the location expression at DATA (length SIZE)
327 requires a frame to evaluate. */
328
329 static int
330 dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
331 struct dwarf2_per_cu_data *per_cu)
332 {
333 struct needs_frame_baton baton;
334 struct dwarf_expr_context *ctx;
335 int in_reg;
336
337 baton.needs_frame = 0;
338
339 ctx = new_dwarf_expr_context ();
340 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
341 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
342 ctx->baton = &baton;
343 ctx->read_reg = needs_frame_read_reg;
344 ctx->read_mem = needs_frame_read_mem;
345 ctx->get_frame_base = needs_frame_frame_base;
346 ctx->get_tls_address = needs_frame_tls_address;
347
348 dwarf_expr_eval (ctx, data, size);
349
350 in_reg = ctx->in_reg;
351
352 if (ctx->num_pieces > 0)
353 {
354 int i;
355
356 /* If the location has several pieces, and any of them are in
357 registers, then we will need a frame to fetch them from. */
358 for (i = 0; i < ctx->num_pieces; i++)
359 if (ctx->pieces[i].in_reg)
360 in_reg = 1;
361 }
362
363 free_dwarf_expr_context (ctx);
364
365 return baton.needs_frame || in_reg;
366 }
367
368 static void
369 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
370 struct axs_value *value, gdb_byte *data,
371 int size)
372 {
373 if (size == 0)
374 error (_("Symbol \"%s\" has been optimized out."),
375 SYMBOL_PRINT_NAME (symbol));
376
377 if (size == 1
378 && data[0] >= DW_OP_reg0
379 && data[0] <= DW_OP_reg31)
380 {
381 value->kind = axs_lvalue_register;
382 value->u.reg = data[0] - DW_OP_reg0;
383 }
384 else if (data[0] == DW_OP_regx)
385 {
386 ULONGEST reg;
387 read_uleb128 (data + 1, data + size, &reg);
388 value->kind = axs_lvalue_register;
389 value->u.reg = reg;
390 }
391 else if (data[0] == DW_OP_fbreg)
392 {
393 /* And this is worse than just minimal; we should honor the frame base
394 as above. */
395 int frame_reg;
396 LONGEST frame_offset;
397 gdb_byte *buf_end;
398
399 buf_end = read_sleb128 (data + 1, data + size, &frame_offset);
400 if (buf_end != data + size)
401 error (_("Unexpected opcode after DW_OP_fbreg for symbol \"%s\"."),
402 SYMBOL_PRINT_NAME (symbol));
403
404 gdbarch_virtual_frame_pointer (current_gdbarch,
405 ax->scope, &frame_reg, &frame_offset);
406 ax_reg (ax, frame_reg);
407 ax_const_l (ax, frame_offset);
408 ax_simple (ax, aop_add);
409
410 value->kind = axs_lvalue_memory;
411 }
412 else if (data[0] >= DW_OP_breg0
413 && data[0] <= DW_OP_breg31)
414 {
415 unsigned int reg;
416 LONGEST offset;
417 gdb_byte *buf_end;
418
419 reg = data[0] - DW_OP_breg0;
420 buf_end = read_sleb128 (data + 1, data + size, &offset);
421 if (buf_end != data + size)
422 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
423 reg, SYMBOL_PRINT_NAME (symbol));
424
425 ax_reg (ax, reg);
426 ax_const_l (ax, offset);
427 ax_simple (ax, aop_add);
428
429 value->kind = axs_lvalue_memory;
430 }
431 else
432 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
433 data[0], SYMBOL_PRINT_NAME (symbol));
434 }
435 \f
436 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
437 evaluator to calculate the location. */
438 static struct value *
439 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
440 {
441 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
442 struct value *val;
443 val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size,
444 dlbaton->per_cu);
445
446 return val;
447 }
448
449 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
450 static int
451 locexpr_read_needs_frame (struct symbol *symbol)
452 {
453 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
454 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
455 dlbaton->per_cu);
456 }
457
458 /* Print a natural-language description of SYMBOL to STREAM. */
459 static int
460 locexpr_describe_location (struct symbol *symbol, struct ui_file *stream)
461 {
462 /* FIXME: be more extensive. */
463 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
464 int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
465
466 if (dlbaton->size == 1
467 && dlbaton->data[0] >= DW_OP_reg0
468 && dlbaton->data[0] <= DW_OP_reg31)
469 {
470 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
471 struct gdbarch *gdbarch = get_objfile_arch (objfile);
472 int regno = gdbarch_dwarf2_reg_to_regnum (gdbarch,
473 dlbaton->data[0] - DW_OP_reg0);
474 fprintf_filtered (stream,
475 "a variable in register %s",
476 gdbarch_register_name (gdbarch, regno));
477 return 1;
478 }
479
480 /* The location expression for a TLS variable looks like this (on a
481 64-bit LE machine):
482
483 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
484 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
485
486 0x3 is the encoding for DW_OP_addr, which has an operand as long
487 as the size of an address on the target machine (here is 8
488 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
489 The operand represents the offset at which the variable is within
490 the thread local storage. */
491
492 if (dlbaton->size > 1
493 && dlbaton->data[dlbaton->size - 1] == DW_OP_GNU_push_tls_address)
494 if (dlbaton->data[0] == DW_OP_addr)
495 {
496 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
497 struct gdbarch *gdbarch = get_objfile_arch (objfile);
498 CORE_ADDR offset = dwarf2_read_address (gdbarch,
499 &dlbaton->data[1],
500 &dlbaton->data[dlbaton->size - 1],
501 addr_size);
502 fprintf_filtered (stream,
503 "a thread-local variable at offset %s in the "
504 "thread-local storage for `%s'",
505 paddr_nz (offset), objfile->name);
506 return 1;
507 }
508
509
510 fprintf_filtered (stream,
511 "a variable with complex or multiple locations (DWARF2)");
512 return 1;
513 }
514
515
516 /* Describe the location of SYMBOL as an agent value in VALUE, generating
517 any necessary bytecode in AX.
518
519 NOTE drow/2003-02-26: This function is extremely minimal, because
520 doing it correctly is extremely complicated and there is no
521 publicly available stub with tracepoint support for me to test
522 against. When there is one this function should be revisited. */
523
524 static void
525 locexpr_tracepoint_var_ref (struct symbol * symbol, struct agent_expr * ax,
526 struct axs_value * value)
527 {
528 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
529
530 dwarf2_tracepoint_var_ref (symbol, ax, value, dlbaton->data, dlbaton->size);
531 }
532
533 /* The set of location functions used with the DWARF-2 expression
534 evaluator. */
535 const struct symbol_ops dwarf2_locexpr_funcs = {
536 locexpr_read_variable,
537 locexpr_read_needs_frame,
538 locexpr_describe_location,
539 locexpr_tracepoint_var_ref
540 };
541
542
543 /* Wrapper functions for location lists. These generally find
544 the appropriate location expression and call something above. */
545
546 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
547 evaluator to calculate the location. */
548 static struct value *
549 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
550 {
551 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
552 struct value *val;
553 gdb_byte *data;
554 size_t size;
555
556 data = find_location_expression (dlbaton, &size,
557 frame ? get_frame_address_in_block (frame)
558 : 0);
559 if (data == NULL)
560 {
561 val = allocate_value (SYMBOL_TYPE (symbol));
562 VALUE_LVAL (val) = not_lval;
563 set_value_optimized_out (val, 1);
564 }
565 else
566 val = dwarf2_evaluate_loc_desc (symbol, frame, data, size,
567 dlbaton->per_cu);
568
569 return val;
570 }
571
572 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
573 static int
574 loclist_read_needs_frame (struct symbol *symbol)
575 {
576 /* If there's a location list, then assume we need to have a frame
577 to choose the appropriate location expression. With tracking of
578 global variables this is not necessarily true, but such tracking
579 is disabled in GCC at the moment until we figure out how to
580 represent it. */
581
582 return 1;
583 }
584
585 /* Print a natural-language description of SYMBOL to STREAM. */
586 static int
587 loclist_describe_location (struct symbol *symbol, struct ui_file *stream)
588 {
589 /* FIXME: Could print the entire list of locations. */
590 fprintf_filtered (stream, "a variable with multiple locations");
591 return 1;
592 }
593
594 /* Describe the location of SYMBOL as an agent value in VALUE, generating
595 any necessary bytecode in AX. */
596 static void
597 loclist_tracepoint_var_ref (struct symbol * symbol, struct agent_expr * ax,
598 struct axs_value * value)
599 {
600 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
601 gdb_byte *data;
602 size_t size;
603
604 data = find_location_expression (dlbaton, &size, ax->scope);
605 if (data == NULL)
606 error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol));
607
608 dwarf2_tracepoint_var_ref (symbol, ax, value, data, size);
609 }
610
611 /* The set of location functions used with the DWARF-2 expression
612 evaluator and location lists. */
613 const struct symbol_ops dwarf2_loclist_funcs = {
614 loclist_read_variable,
615 loclist_read_needs_frame,
616 loclist_describe_location,
617 loclist_tracepoint_var_ref
618 };
This page took 0.061243 seconds and 4 git commands to generate.