1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
5 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
33 #include "exceptions.h"
37 #include "dwarf2expr.h"
38 #include "dwarf2loc.h"
39 #include "dwarf2-frame.h"
41 #include "gdb_string.h"
42 #include "gdb_assert.h"
44 /* A helper function for dealing with location lists. Given a
45 symbol baton (BATON) and a pc value (PC), find the appropriate
46 location expression, set *LOCEXPR_LENGTH, and return a pointer
47 to the beginning of the expression. Returns NULL on failure.
49 For now, only return the first matching location expression; there
50 can be more than one in the list. */
53 find_location_expression (struct dwarf2_loclist_baton
*baton
,
54 size_t *locexpr_length
, CORE_ADDR pc
)
57 gdb_byte
*loc_ptr
, *buf_end
;
59 struct objfile
*objfile
= dwarf2_per_cu_objfile (baton
->per_cu
);
60 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
61 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
62 unsigned int addr_size
= dwarf2_per_cu_addr_size (baton
->per_cu
);
63 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
64 /* Adjust base_address for relocatable objects. */
65 CORE_ADDR base_offset
= ANOFFSET (objfile
->section_offsets
,
66 SECT_OFF_TEXT (objfile
));
67 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
69 loc_ptr
= baton
->data
;
70 buf_end
= baton
->data
+ baton
->size
;
74 if (buf_end
- loc_ptr
< 2 * addr_size
)
75 error (_("find_location_expression: Corrupted DWARF expression."));
77 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
80 /* A base-address-selection entry. */
83 base_address
= dwarf2_read_address (gdbarch
,
84 loc_ptr
, buf_end
, addr_size
);
89 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
92 /* An end-of-list entry. */
93 if (low
== 0 && high
== 0)
96 /* Otherwise, a location expression entry. */
100 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
103 if (pc
>= low
&& pc
< high
)
105 *locexpr_length
= length
;
113 /* This is the baton used when performing dwarf2 expression
115 struct dwarf_expr_baton
117 struct frame_info
*frame
;
118 struct objfile
*objfile
;
121 /* Helper functions for dwarf2_evaluate_loc_desc. */
123 /* Using the frame specified in BATON, return the value of register
124 REGNUM, treated as a pointer. */
126 dwarf_expr_read_reg (void *baton
, int dwarf_regnum
)
128 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
129 struct gdbarch
*gdbarch
= get_frame_arch (debaton
->frame
);
133 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
134 result
= address_from_register (builtin_type (gdbarch
)->builtin_data_ptr
,
135 regnum
, debaton
->frame
);
139 /* Read memory at ADDR (length LEN) into BUF. */
142 dwarf_expr_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
144 read_memory (addr
, buf
, len
);
147 /* Using the frame specified in BATON, find the location expression
148 describing the frame base. Return a pointer to it in START and
149 its length in LENGTH. */
151 dwarf_expr_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
153 /* FIXME: cagney/2003-03-26: This code should be using
154 get_frame_base_address(), and then implement a dwarf2 specific
156 struct symbol
*framefunc
;
157 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
159 /* Use block_linkage_function, which returns a real (not inlined)
160 function, instead of get_frame_function, which may return an
162 framefunc
= block_linkage_function (get_frame_block (debaton
->frame
, NULL
));
164 /* If we found a frame-relative symbol then it was certainly within
165 some function associated with a frame. If we can't find the frame,
166 something has gone wrong. */
167 gdb_assert (framefunc
!= NULL
);
169 if (SYMBOL_LOCATION_BATON (framefunc
) == NULL
)
171 else if (SYMBOL_COMPUTED_OPS (framefunc
) == &dwarf2_loclist_funcs
)
173 struct dwarf2_loclist_baton
*symbaton
;
174 struct frame_info
*frame
= debaton
->frame
;
176 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
177 *start
= find_location_expression (symbaton
, length
,
178 get_frame_address_in_block (frame
));
182 struct dwarf2_locexpr_baton
*symbaton
;
183 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
184 if (symbaton
!= NULL
)
186 *length
= symbaton
->size
;
187 *start
= symbaton
->data
;
194 error (_("Could not find the frame base for \"%s\"."),
195 SYMBOL_NATURAL_NAME (framefunc
));
198 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
199 the frame in BATON. */
202 dwarf_expr_frame_cfa (void *baton
)
204 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
205 return dwarf2_frame_cfa (debaton
->frame
);
208 /* Using the objfile specified in BATON, find the address for the
209 current thread's thread-local storage with offset OFFSET. */
211 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
213 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
215 return target_translate_tls_address (debaton
->objfile
, offset
);
220 /* The number of pieces used to describe this variable. */
223 /* The pieces themselves. */
224 struct dwarf_expr_piece
*pieces
;
227 /* Allocate a closure for a value formed from separately-described
230 static struct piece_closure
*
231 allocate_piece_closure (int n_pieces
, struct dwarf_expr_piece
*pieces
)
233 struct piece_closure
*c
= XZALLOC (struct piece_closure
);
235 c
->n_pieces
= n_pieces
;
236 c
->pieces
= XCALLOC (n_pieces
, struct dwarf_expr_piece
);
238 memcpy (c
->pieces
, pieces
, n_pieces
* sizeof (struct dwarf_expr_piece
));
244 read_pieced_value (struct value
*v
)
249 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
250 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (v
));
252 contents
= value_contents_raw (v
);
253 for (i
= 0; i
< c
->n_pieces
; i
++)
255 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
259 memset (contents
+ offset
, 0, p
->size
);
260 set_value_optimized_out (v
, 1);
264 struct gdbarch
*arch
= get_frame_arch (frame
);
265 gdb_byte regval
[MAX_REGISTER_SIZE
];
266 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->value
);
268 get_frame_register (frame
, gdb_regnum
, regval
);
269 memcpy (contents
+ offset
, regval
, p
->size
);
273 read_memory (p
->value
, contents
+ offset
, p
->size
);
280 write_pieced_value (struct value
*to
, struct value
*from
)
285 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (to
);
286 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (to
));
290 set_value_optimized_out (to
, 1);
294 contents
= value_contents_raw (from
);
295 for (i
= 0; i
< c
->n_pieces
; i
++)
297 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
300 struct gdbarch
*arch
= get_frame_arch (frame
);
301 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->value
);
302 put_frame_register (frame
, gdb_regnum
, contents
+ offset
);
306 write_memory (p
->value
, contents
+ offset
, p
->size
);
313 copy_pieced_value_closure (struct value
*v
)
315 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
317 return allocate_piece_closure (c
->n_pieces
, c
->pieces
);
321 free_pieced_value_closure (struct value
*v
)
323 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
329 /* Functions for accessing a variable described by DW_OP_piece. */
330 static struct lval_funcs pieced_value_funcs
= {
333 copy_pieced_value_closure
,
334 free_pieced_value_closure
337 /* Evaluate a location description, starting at DATA and with length
338 SIZE, to find the current location of variable VAR in the context
340 static struct value
*
341 dwarf2_evaluate_loc_desc (struct symbol
*var
, struct frame_info
*frame
,
342 gdb_byte
*data
, unsigned short size
,
343 struct dwarf2_per_cu_data
*per_cu
)
345 struct value
*retval
;
346 struct dwarf_expr_baton baton
;
347 struct dwarf_expr_context
*ctx
;
348 struct cleanup
*old_chain
;
352 retval
= allocate_value (SYMBOL_TYPE (var
));
353 VALUE_LVAL (retval
) = not_lval
;
354 set_value_optimized_out (retval
, 1);
359 baton
.objfile
= dwarf2_per_cu_objfile (per_cu
);
361 ctx
= new_dwarf_expr_context ();
362 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
364 ctx
->gdbarch
= get_objfile_arch (baton
.objfile
);
365 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
367 ctx
->read_reg
= dwarf_expr_read_reg
;
368 ctx
->read_mem
= dwarf_expr_read_mem
;
369 ctx
->get_frame_base
= dwarf_expr_frame_base
;
370 ctx
->get_frame_cfa
= dwarf_expr_frame_cfa
;
371 ctx
->get_tls_address
= dwarf_expr_tls_address
;
373 dwarf_expr_eval (ctx
, data
, size
);
374 if (ctx
->num_pieces
> 0)
376 struct piece_closure
*c
;
377 struct frame_id frame_id
= get_frame_id (frame
);
379 c
= allocate_piece_closure (ctx
->num_pieces
, ctx
->pieces
);
380 retval
= allocate_computed_value (SYMBOL_TYPE (var
),
383 VALUE_FRAME_ID (retval
) = frame_id
;
385 else if (ctx
->in_reg
)
387 struct gdbarch
*arch
= get_frame_arch (frame
);
388 CORE_ADDR dwarf_regnum
= dwarf_expr_fetch (ctx
, 0);
389 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_regnum
);
390 retval
= value_from_register (SYMBOL_TYPE (var
), gdb_regnum
, frame
);
394 CORE_ADDR address
= dwarf_expr_fetch (ctx
, 0);
396 retval
= allocate_value (SYMBOL_TYPE (var
));
397 VALUE_LVAL (retval
) = lval_memory
;
398 set_value_lazy (retval
, 1);
399 set_value_stack (retval
, 1);
400 set_value_address (retval
, address
);
403 set_value_initialized (retval
, ctx
->initialized
);
405 do_cleanups (old_chain
);
414 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
416 struct needs_frame_baton
421 /* Reads from registers do require a frame. */
423 needs_frame_read_reg (void *baton
, int regnum
)
425 struct needs_frame_baton
*nf_baton
= baton
;
426 nf_baton
->needs_frame
= 1;
430 /* Reads from memory do not require a frame. */
432 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
434 memset (buf
, 0, len
);
437 /* Frame-relative accesses do require a frame. */
439 needs_frame_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
441 static gdb_byte lit0
= DW_OP_lit0
;
442 struct needs_frame_baton
*nf_baton
= baton
;
447 nf_baton
->needs_frame
= 1;
450 /* CFA accesses require a frame. */
453 needs_frame_frame_cfa (void *baton
)
455 struct needs_frame_baton
*nf_baton
= baton
;
456 nf_baton
->needs_frame
= 1;
460 /* Thread-local accesses do require a frame. */
462 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
464 struct needs_frame_baton
*nf_baton
= baton
;
465 nf_baton
->needs_frame
= 1;
469 /* Return non-zero iff the location expression at DATA (length SIZE)
470 requires a frame to evaluate. */
473 dwarf2_loc_desc_needs_frame (gdb_byte
*data
, unsigned short size
,
474 struct dwarf2_per_cu_data
*per_cu
)
476 struct needs_frame_baton baton
;
477 struct dwarf_expr_context
*ctx
;
479 struct cleanup
*old_chain
;
481 baton
.needs_frame
= 0;
483 ctx
= new_dwarf_expr_context ();
484 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
486 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (per_cu
));
487 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
489 ctx
->read_reg
= needs_frame_read_reg
;
490 ctx
->read_mem
= needs_frame_read_mem
;
491 ctx
->get_frame_base
= needs_frame_frame_base
;
492 ctx
->get_frame_cfa
= needs_frame_frame_cfa
;
493 ctx
->get_tls_address
= needs_frame_tls_address
;
495 dwarf_expr_eval (ctx
, data
, size
);
497 in_reg
= ctx
->in_reg
;
499 if (ctx
->num_pieces
> 0)
503 /* If the location has several pieces, and any of them are in
504 registers, then we will need a frame to fetch them from. */
505 for (i
= 0; i
< ctx
->num_pieces
; i
++)
506 if (ctx
->pieces
[i
].in_reg
)
510 do_cleanups (old_chain
);
512 return baton
.needs_frame
|| in_reg
;
516 dwarf2_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
517 struct agent_expr
*ax
, struct axs_value
*value
,
518 gdb_byte
*data
, int size
)
521 error (_("Symbol \"%s\" has been optimized out."),
522 SYMBOL_PRINT_NAME (symbol
));
525 && data
[0] >= DW_OP_reg0
526 && data
[0] <= DW_OP_reg31
)
528 value
->kind
= axs_lvalue_register
;
529 value
->u
.reg
= data
[0] - DW_OP_reg0
;
531 else if (data
[0] == DW_OP_regx
)
534 read_uleb128 (data
+ 1, data
+ size
, ®
);
535 value
->kind
= axs_lvalue_register
;
538 else if (data
[0] == DW_OP_fbreg
)
540 /* And this is worse than just minimal; we should honor the frame base
543 LONGEST frame_offset
;
546 buf_end
= read_sleb128 (data
+ 1, data
+ size
, &frame_offset
);
547 if (buf_end
!= data
+ size
)
548 error (_("Unexpected opcode after DW_OP_fbreg for symbol \"%s\"."),
549 SYMBOL_PRINT_NAME (symbol
));
551 gdbarch_virtual_frame_pointer (gdbarch
,
552 ax
->scope
, &frame_reg
, &frame_offset
);
553 ax_reg (ax
, frame_reg
);
554 ax_const_l (ax
, frame_offset
);
555 ax_simple (ax
, aop_add
);
557 value
->kind
= axs_lvalue_memory
;
559 else if (data
[0] >= DW_OP_breg0
560 && data
[0] <= DW_OP_breg31
)
566 reg
= data
[0] - DW_OP_breg0
;
567 buf_end
= read_sleb128 (data
+ 1, data
+ size
, &offset
);
568 if (buf_end
!= data
+ size
)
569 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
570 reg
, SYMBOL_PRINT_NAME (symbol
));
573 ax_const_l (ax
, offset
);
574 ax_simple (ax
, aop_add
);
576 value
->kind
= axs_lvalue_memory
;
579 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
580 data
[0], SYMBOL_PRINT_NAME (symbol
));
583 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
584 evaluator to calculate the location. */
585 static struct value
*
586 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
588 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
590 val
= dwarf2_evaluate_loc_desc (symbol
, frame
, dlbaton
->data
, dlbaton
->size
,
596 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
598 locexpr_read_needs_frame (struct symbol
*symbol
)
600 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
601 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
605 /* Print a natural-language description of SYMBOL to STREAM. */
607 locexpr_describe_location (struct symbol
*symbol
, struct ui_file
*stream
)
609 /* FIXME: be more extensive. */
610 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
611 int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
613 if (dlbaton
->size
== 1
614 && dlbaton
->data
[0] >= DW_OP_reg0
615 && dlbaton
->data
[0] <= DW_OP_reg31
)
617 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
618 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
619 int regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
,
620 dlbaton
->data
[0] - DW_OP_reg0
);
621 fprintf_filtered (stream
,
622 "a variable in register %s",
623 gdbarch_register_name (gdbarch
, regno
));
627 /* The location expression for a TLS variable looks like this (on a
630 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
631 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
633 0x3 is the encoding for DW_OP_addr, which has an operand as long
634 as the size of an address on the target machine (here is 8
635 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
636 The operand represents the offset at which the variable is within
637 the thread local storage. */
639 if (dlbaton
->size
> 1
640 && dlbaton
->data
[dlbaton
->size
- 1] == DW_OP_GNU_push_tls_address
)
641 if (dlbaton
->data
[0] == DW_OP_addr
)
643 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
644 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
645 CORE_ADDR offset
= dwarf2_read_address (gdbarch
,
647 &dlbaton
->data
[dlbaton
->size
- 1],
649 fprintf_filtered (stream
,
650 "a thread-local variable at offset %s in the "
651 "thread-local storage for `%s'",
652 paddress (gdbarch
, offset
), objfile
->name
);
657 fprintf_filtered (stream
,
658 "a variable with complex or multiple locations (DWARF2)");
663 /* Describe the location of SYMBOL as an agent value in VALUE, generating
664 any necessary bytecode in AX.
666 NOTE drow/2003-02-26: This function is extremely minimal, because
667 doing it correctly is extremely complicated and there is no
668 publicly available stub with tracepoint support for me to test
669 against. When there is one this function should be revisited. */
672 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
673 struct agent_expr
*ax
, struct axs_value
*value
)
675 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
677 dwarf2_tracepoint_var_ref (symbol
, gdbarch
, ax
, value
,
678 dlbaton
->data
, dlbaton
->size
);
681 /* The set of location functions used with the DWARF-2 expression
683 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
684 locexpr_read_variable
,
685 locexpr_read_needs_frame
,
686 locexpr_describe_location
,
687 locexpr_tracepoint_var_ref
691 /* Wrapper functions for location lists. These generally find
692 the appropriate location expression and call something above. */
694 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
695 evaluator to calculate the location. */
696 static struct value
*
697 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
699 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
704 data
= find_location_expression (dlbaton
, &size
,
705 frame
? get_frame_address_in_block (frame
)
709 val
= allocate_value (SYMBOL_TYPE (symbol
));
710 VALUE_LVAL (val
) = not_lval
;
711 set_value_optimized_out (val
, 1);
714 val
= dwarf2_evaluate_loc_desc (symbol
, frame
, data
, size
,
720 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
722 loclist_read_needs_frame (struct symbol
*symbol
)
724 /* If there's a location list, then assume we need to have a frame
725 to choose the appropriate location expression. With tracking of
726 global variables this is not necessarily true, but such tracking
727 is disabled in GCC at the moment until we figure out how to
733 /* Print a natural-language description of SYMBOL to STREAM. */
735 loclist_describe_location (struct symbol
*symbol
, struct ui_file
*stream
)
737 /* FIXME: Could print the entire list of locations. */
738 fprintf_filtered (stream
, "a variable with multiple locations");
742 /* Describe the location of SYMBOL as an agent value in VALUE, generating
743 any necessary bytecode in AX. */
745 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
746 struct agent_expr
*ax
, struct axs_value
*value
)
748 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
752 data
= find_location_expression (dlbaton
, &size
, ax
->scope
);
754 error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol
));
756 dwarf2_tracepoint_var_ref (symbol
, gdbarch
, ax
, value
, data
, size
);
759 /* The set of location functions used with the DWARF-2 expression
760 evaluator and location lists. */
761 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
762 loclist_read_variable
,
763 loclist_read_needs_frame
,
764 loclist_describe_location
,
765 loclist_tracepoint_var_ref