1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007-2012 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"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
47 extern int dwarf2_always_disassemble
;
49 static void dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
50 const gdb_byte
**start
, size_t *length
);
52 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs
;
54 static struct value
*dwarf2_evaluate_loc_desc_full (struct type
*type
,
55 struct frame_info
*frame
,
58 struct dwarf2_per_cu_data
*per_cu
,
61 /* A function for dealing with location lists. Given a
62 symbol baton (BATON) and a pc value (PC), find the appropriate
63 location expression, set *LOCEXPR_LENGTH, and return a pointer
64 to the beginning of the expression. Returns NULL on failure.
66 For now, only return the first matching location expression; there
67 can be more than one in the list. */
70 dwarf2_find_location_expression (struct dwarf2_loclist_baton
*baton
,
71 size_t *locexpr_length
, CORE_ADDR pc
)
74 const gdb_byte
*loc_ptr
, *buf_end
;
76 struct objfile
*objfile
= dwarf2_per_cu_objfile (baton
->per_cu
);
77 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
78 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
79 unsigned int addr_size
= dwarf2_per_cu_addr_size (baton
->per_cu
);
80 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
81 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
82 /* Adjust base_address for relocatable objects. */
83 CORE_ADDR base_offset
= dwarf2_per_cu_text_offset (baton
->per_cu
);
84 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
86 loc_ptr
= baton
->data
;
87 buf_end
= baton
->data
+ baton
->size
;
91 if (buf_end
- loc_ptr
< 2 * addr_size
)
92 error (_("dwarf2_find_location_expression: "
93 "Corrupted DWARF expression."));
96 low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
98 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
102 high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
104 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
105 loc_ptr
+= addr_size
;
107 /* A base-address-selection entry. */
108 if ((low
& base_mask
) == base_mask
)
110 base_address
= high
+ base_offset
;
114 /* An end-of-list entry. */
115 if (low
== 0 && high
== 0)
121 /* Otherwise, a location expression entry. */
123 high
+= base_address
;
125 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
128 if (low
== high
&& pc
== low
)
130 /* This is entry PC record present only at entry point
131 of a function. Verify it is really the function entry point. */
133 struct block
*pc_block
= block_for_pc (pc
);
134 struct symbol
*pc_func
= NULL
;
137 pc_func
= block_linkage_function (pc_block
);
139 if (pc_func
&& pc
== BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func
)))
141 *locexpr_length
= length
;
146 if (pc
>= low
&& pc
< high
)
148 *locexpr_length
= length
;
156 /* This is the baton used when performing dwarf2 expression
158 struct dwarf_expr_baton
160 struct frame_info
*frame
;
161 struct dwarf2_per_cu_data
*per_cu
;
164 /* Helper functions for dwarf2_evaluate_loc_desc. */
166 /* Using the frame specified in BATON, return the value of register
167 REGNUM, treated as a pointer. */
169 dwarf_expr_read_reg (void *baton
, int dwarf_regnum
)
171 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
172 struct gdbarch
*gdbarch
= get_frame_arch (debaton
->frame
);
176 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
177 result
= address_from_register (builtin_type (gdbarch
)->builtin_data_ptr
,
178 regnum
, debaton
->frame
);
182 /* Read memory at ADDR (length LEN) into BUF. */
185 dwarf_expr_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
187 read_memory (addr
, buf
, len
);
190 /* Using the frame specified in BATON, find the location expression
191 describing the frame base. Return a pointer to it in START and
192 its length in LENGTH. */
194 dwarf_expr_frame_base (void *baton
, const gdb_byte
**start
, size_t * length
)
196 /* FIXME: cagney/2003-03-26: This code should be using
197 get_frame_base_address(), and then implement a dwarf2 specific
199 struct symbol
*framefunc
;
200 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
202 /* Use block_linkage_function, which returns a real (not inlined)
203 function, instead of get_frame_function, which may return an
205 framefunc
= block_linkage_function (get_frame_block (debaton
->frame
, NULL
));
207 /* If we found a frame-relative symbol then it was certainly within
208 some function associated with a frame. If we can't find the frame,
209 something has gone wrong. */
210 gdb_assert (framefunc
!= NULL
);
212 dwarf_expr_frame_base_1 (framefunc
,
213 get_frame_address_in_block (debaton
->frame
),
218 dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
219 const gdb_byte
**start
, size_t *length
)
221 if (SYMBOL_LOCATION_BATON (framefunc
) == NULL
)
223 else if (SYMBOL_COMPUTED_OPS (framefunc
) == &dwarf2_loclist_funcs
)
225 struct dwarf2_loclist_baton
*symbaton
;
227 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
228 *start
= dwarf2_find_location_expression (symbaton
, length
, pc
);
232 struct dwarf2_locexpr_baton
*symbaton
;
234 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
235 if (symbaton
!= NULL
)
237 *length
= symbaton
->size
;
238 *start
= symbaton
->data
;
245 error (_("Could not find the frame base for \"%s\"."),
246 SYMBOL_NATURAL_NAME (framefunc
));
249 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
250 the frame in BATON. */
253 dwarf_expr_frame_cfa (void *baton
)
255 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
257 return dwarf2_frame_cfa (debaton
->frame
);
260 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
261 the frame in BATON. */
264 dwarf_expr_frame_pc (void *baton
)
266 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
268 return get_frame_address_in_block (debaton
->frame
);
271 /* Using the objfile specified in BATON, find the address for the
272 current thread's thread-local storage with offset OFFSET. */
274 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
276 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
277 struct objfile
*objfile
= dwarf2_per_cu_objfile (debaton
->per_cu
);
279 return target_translate_tls_address (objfile
, offset
);
282 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
283 current CU (as is PER_CU). State of the CTX is not affected by the
287 per_cu_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
,
288 struct dwarf2_per_cu_data
*per_cu
,
289 CORE_ADDR (*get_frame_pc
) (void *baton
),
292 struct dwarf2_locexpr_baton block
;
294 block
= dwarf2_fetch_die_location_block (die_offset
, per_cu
,
295 get_frame_pc
, baton
);
297 /* DW_OP_call_ref is currently not supported. */
298 gdb_assert (block
.per_cu
== per_cu
);
300 dwarf_expr_eval (ctx
, block
.data
, block
.size
);
303 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
306 dwarf_expr_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
)
308 struct dwarf_expr_baton
*debaton
= ctx
->baton
;
310 per_cu_dwarf_call (ctx
, die_offset
, debaton
->per_cu
,
311 ctx
->funcs
->get_frame_pc
, ctx
->baton
);
314 /* Callback function for dwarf2_evaluate_loc_desc. */
317 dwarf_expr_get_base_type (struct dwarf_expr_context
*ctx
, size_t die_offset
)
319 struct dwarf_expr_baton
*debaton
= ctx
->baton
;
321 return dwarf2_get_die_type (die_offset
, debaton
->per_cu
);
324 /* See dwarf2loc.h. */
326 int entry_values_debug
= 0;
328 /* Helper to set entry_values_debug. */
331 show_entry_values_debug (struct ui_file
*file
, int from_tty
,
332 struct cmd_list_element
*c
, const char *value
)
334 fprintf_filtered (file
,
335 _("Entry values and tail call frames debugging is %s.\n"),
339 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
340 CALLER_FRAME (for registers) can be NULL if it is not known. This function
341 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
344 call_site_to_target_addr (struct gdbarch
*call_site_gdbarch
,
345 struct call_site
*call_site
,
346 struct frame_info
*caller_frame
)
348 switch (FIELD_LOC_KIND (call_site
->target
))
350 case FIELD_LOC_KIND_DWARF_BLOCK
:
352 struct dwarf2_locexpr_baton
*dwarf_block
;
354 struct type
*caller_core_addr_type
;
355 struct gdbarch
*caller_arch
;
357 dwarf_block
= FIELD_DWARF_BLOCK (call_site
->target
);
358 if (dwarf_block
== NULL
)
360 struct minimal_symbol
*msym
;
362 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
363 throw_error (NO_ENTRY_VALUE_ERROR
,
364 _("DW_AT_GNU_call_site_target is not specified "
366 paddress (call_site_gdbarch
, call_site
->pc
),
367 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
370 if (caller_frame
== NULL
)
372 struct minimal_symbol
*msym
;
374 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
375 throw_error (NO_ENTRY_VALUE_ERROR
,
376 _("DW_AT_GNU_call_site_target DWARF block resolving "
377 "requires known frame which is currently not "
378 "available at %s in %s"),
379 paddress (call_site_gdbarch
, call_site
->pc
),
380 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
383 caller_arch
= get_frame_arch (caller_frame
);
384 caller_core_addr_type
= builtin_type (caller_arch
)->builtin_func_ptr
;
385 val
= dwarf2_evaluate_loc_desc (caller_core_addr_type
, caller_frame
,
386 dwarf_block
->data
, dwarf_block
->size
,
387 dwarf_block
->per_cu
);
388 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
390 if (VALUE_LVAL (val
) == lval_memory
)
391 return value_address (val
);
393 return value_as_address (val
);
396 case FIELD_LOC_KIND_PHYSNAME
:
398 const char *physname
;
399 struct minimal_symbol
*msym
;
401 physname
= FIELD_STATIC_PHYSNAME (call_site
->target
);
402 msym
= lookup_minimal_symbol_text (physname
, NULL
);
405 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
406 throw_error (NO_ENTRY_VALUE_ERROR
,
407 _("Cannot find function \"%s\" for a call site target "
409 physname
, paddress (call_site_gdbarch
, call_site
->pc
),
410 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
413 return SYMBOL_VALUE_ADDRESS (msym
);
416 case FIELD_LOC_KIND_PHYSADDR
:
417 return FIELD_STATIC_PHYSADDR (call_site
->target
);
420 internal_error (__FILE__
, __LINE__
, _("invalid call site target kind"));
424 /* Convert function entry point exact address ADDR to the function which is
425 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
426 NO_ENTRY_VALUE_ERROR otherwise. */
428 static struct symbol
*
429 func_addr_to_tail_call_list (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
431 struct symbol
*sym
= find_pc_function (addr
);
434 if (sym
== NULL
|| BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) != addr
)
435 throw_error (NO_ENTRY_VALUE_ERROR
,
436 _("DW_TAG_GNU_call_site resolving failed to find function "
437 "name for address %s"),
438 paddress (gdbarch
, addr
));
440 type
= SYMBOL_TYPE (sym
);
441 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FUNC
);
442 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
447 /* Verify function with entry point exact address ADDR can never call itself
448 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
449 can call itself via tail calls.
451 If a funtion can tail call itself its entry value based parameters are
452 unreliable. There is no verification whether the value of some/all
453 parameters is unchanged through the self tail call, we expect if there is
454 a self tail call all the parameters can be modified. */
457 func_verify_no_selftailcall (struct gdbarch
*gdbarch
, CORE_ADDR verify_addr
)
459 struct obstack addr_obstack
;
460 struct cleanup
*old_chain
;
463 /* Track here CORE_ADDRs which were already visited. */
466 /* The verification is completely unordered. Track here function addresses
467 which still need to be iterated. */
468 VEC (CORE_ADDR
) *todo
= NULL
;
470 obstack_init (&addr_obstack
);
471 old_chain
= make_cleanup_obstack_free (&addr_obstack
);
472 addr_hash
= htab_create_alloc_ex (64, core_addr_hash
, core_addr_eq
, NULL
,
473 &addr_obstack
, hashtab_obstack_allocate
,
475 make_cleanup_htab_delete (addr_hash
);
477 make_cleanup (VEC_cleanup (CORE_ADDR
), &todo
);
479 VEC_safe_push (CORE_ADDR
, todo
, verify_addr
);
480 while (!VEC_empty (CORE_ADDR
, todo
))
482 struct symbol
*func_sym
;
483 struct call_site
*call_site
;
485 addr
= VEC_pop (CORE_ADDR
, todo
);
487 func_sym
= func_addr_to_tail_call_list (gdbarch
, addr
);
489 for (call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym
));
490 call_site
; call_site
= call_site
->tail_call_next
)
492 CORE_ADDR target_addr
;
495 /* CALLER_FRAME with registers is not available for tail-call jumped
497 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
499 if (target_addr
== verify_addr
)
501 struct minimal_symbol
*msym
;
503 msym
= lookup_minimal_symbol_by_pc (verify_addr
);
504 throw_error (NO_ENTRY_VALUE_ERROR
,
505 _("DW_OP_GNU_entry_value resolving has found "
506 "function \"%s\" at %s can call itself via tail "
508 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
),
509 paddress (gdbarch
, verify_addr
));
512 slot
= htab_find_slot (addr_hash
, &target_addr
, INSERT
);
515 *slot
= obstack_copy (&addr_obstack
, &target_addr
,
516 sizeof (target_addr
));
517 VEC_safe_push (CORE_ADDR
, todo
, target_addr
);
522 do_cleanups (old_chain
);
525 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
526 ENTRY_VALUES_DEBUG. */
529 tailcall_dump (struct gdbarch
*gdbarch
, const struct call_site
*call_site
)
531 CORE_ADDR addr
= call_site
->pc
;
532 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (addr
- 1);
534 fprintf_unfiltered (gdb_stdlog
, " %s(%s)", paddress (gdbarch
, addr
),
535 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
539 /* vec.h needs single word type name, typedef it. */
540 typedef struct call_site
*call_sitep
;
542 /* Define VEC (call_sitep) functions. */
543 DEF_VEC_P (call_sitep
);
545 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
546 only top callers and bottom callees which are present in both. GDBARCH is
547 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
548 no remaining possibilities to provide unambiguous non-trivial result.
549 RESULTP should point to NULL on the first (initialization) call. Caller is
550 responsible for xfree of any RESULTP data. */
553 chain_candidate (struct gdbarch
*gdbarch
, struct call_site_chain
**resultp
,
554 VEC (call_sitep
) *chain
)
556 struct call_site_chain
*result
= *resultp
;
557 long length
= VEC_length (call_sitep
, chain
);
558 int callers
, callees
, idx
;
562 /* Create the initial chain containing all the passed PCs. */
564 result
= xmalloc (sizeof (*result
) + sizeof (*result
->call_site
)
566 result
->length
= length
;
567 result
->callers
= result
->callees
= length
;
568 memcpy (result
->call_site
, VEC_address (call_sitep
, chain
),
569 sizeof (*result
->call_site
) * length
);
572 if (entry_values_debug
)
574 fprintf_unfiltered (gdb_stdlog
, "tailcall: initial:");
575 for (idx
= 0; idx
< length
; idx
++)
576 tailcall_dump (gdbarch
, result
->call_site
[idx
]);
577 fputc_unfiltered ('\n', gdb_stdlog
);
583 if (entry_values_debug
)
585 fprintf_unfiltered (gdb_stdlog
, "tailcall: compare:");
586 for (idx
= 0; idx
< length
; idx
++)
587 tailcall_dump (gdbarch
, VEC_index (call_sitep
, chain
, idx
));
588 fputc_unfiltered ('\n', gdb_stdlog
);
591 /* Intersect callers. */
593 callers
= min (result
->callers
, length
);
594 for (idx
= 0; idx
< callers
; idx
++)
595 if (result
->call_site
[idx
] != VEC_index (call_sitep
, chain
, idx
))
597 result
->callers
= idx
;
601 /* Intersect callees. */
603 callees
= min (result
->callees
, length
);
604 for (idx
= 0; idx
< callees
; idx
++)
605 if (result
->call_site
[result
->length
- 1 - idx
]
606 != VEC_index (call_sitep
, chain
, length
- 1 - idx
))
608 result
->callees
= idx
;
612 if (entry_values_debug
)
614 fprintf_unfiltered (gdb_stdlog
, "tailcall: reduced:");
615 for (idx
= 0; idx
< result
->callers
; idx
++)
616 tailcall_dump (gdbarch
, result
->call_site
[idx
]);
617 fputs_unfiltered (" |", gdb_stdlog
);
618 for (idx
= 0; idx
< result
->callees
; idx
++)
619 tailcall_dump (gdbarch
, result
->call_site
[result
->length
620 - result
->callees
+ idx
]);
621 fputc_unfiltered ('\n', gdb_stdlog
);
624 if (result
->callers
== 0 && result
->callees
== 0)
626 /* There are no common callers or callees. It could be also a direct
627 call (which has length 0) with ambiguous possibility of an indirect
628 call - CALLERS == CALLEES == 0 is valid during the first allocation
629 but any subsequence processing of such entry means ambiguity. */
635 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
636 PC again. In such case there must be two different code paths to reach
637 it, therefore some of the former determined intermediate PCs must differ
638 and the unambiguous chain gets shortened. */
639 gdb_assert (result
->callers
+ result
->callees
< result
->length
);
642 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
643 assumed frames between them use GDBARCH. Use depth first search so we can
644 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
645 would have needless GDB stack overhead. Caller is responsible for xfree of
646 the returned result. Any unreliability results in thrown
647 NO_ENTRY_VALUE_ERROR. */
649 static struct call_site_chain
*
650 call_site_find_chain_1 (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
653 struct func_type
*func_specific
;
654 struct obstack addr_obstack
;
655 struct cleanup
*back_to_retval
, *back_to_workdata
;
656 struct call_site_chain
*retval
= NULL
;
657 struct call_site
*call_site
;
659 /* Mark CALL_SITEs so we do not visit the same ones twice. */
662 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
663 call_site nor any possible call_site at CALLEE_PC's function is there.
664 Any CALL_SITE in CHAIN will be iterated to its siblings - via
665 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
666 VEC (call_sitep
) *chain
= NULL
;
668 /* We are not interested in the specific PC inside the callee function. */
669 callee_pc
= get_pc_function_start (callee_pc
);
671 throw_error (NO_ENTRY_VALUE_ERROR
, _("Unable to find function for PC %s"),
672 paddress (gdbarch
, callee_pc
));
674 back_to_retval
= make_cleanup (free_current_contents
, &retval
);
676 obstack_init (&addr_obstack
);
677 back_to_workdata
= make_cleanup_obstack_free (&addr_obstack
);
678 addr_hash
= htab_create_alloc_ex (64, core_addr_hash
, core_addr_eq
, NULL
,
679 &addr_obstack
, hashtab_obstack_allocate
,
681 make_cleanup_htab_delete (addr_hash
);
683 make_cleanup (VEC_cleanup (call_sitep
), &chain
);
685 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
686 at the target's function. All the possible tail call sites in the
687 target's function will get iterated as already pushed into CHAIN via their
689 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
693 CORE_ADDR target_func_addr
;
694 struct call_site
*target_call_site
;
696 /* CALLER_FRAME with registers is not available for tail-call jumped
698 target_func_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
700 if (target_func_addr
== callee_pc
)
702 chain_candidate (gdbarch
, &retval
, chain
);
706 /* There is no way to reach CALLEE_PC again as we would prevent
707 entering it twice as being already marked in ADDR_HASH. */
708 target_call_site
= NULL
;
712 struct symbol
*target_func
;
714 target_func
= func_addr_to_tail_call_list (gdbarch
, target_func_addr
);
715 target_call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func
));
720 /* Attempt to visit TARGET_CALL_SITE. */
722 if (target_call_site
)
726 slot
= htab_find_slot (addr_hash
, &target_call_site
->pc
, INSERT
);
729 /* Successfully entered TARGET_CALL_SITE. */
731 *slot
= &target_call_site
->pc
;
732 VEC_safe_push (call_sitep
, chain
, target_call_site
);
737 /* Backtrack (without revisiting the originating call_site). Try the
738 callers's sibling; if there isn't any try the callers's callers's
741 target_call_site
= NULL
;
742 while (!VEC_empty (call_sitep
, chain
))
744 call_site
= VEC_pop (call_sitep
, chain
);
746 gdb_assert (htab_find_slot (addr_hash
, &call_site
->pc
,
748 htab_remove_elt (addr_hash
, &call_site
->pc
);
750 target_call_site
= call_site
->tail_call_next
;
751 if (target_call_site
)
755 while (target_call_site
);
757 if (VEC_empty (call_sitep
, chain
))
760 call_site
= VEC_last (call_sitep
, chain
);
765 struct minimal_symbol
*msym_caller
, *msym_callee
;
767 msym_caller
= lookup_minimal_symbol_by_pc (caller_pc
);
768 msym_callee
= lookup_minimal_symbol_by_pc (callee_pc
);
769 throw_error (NO_ENTRY_VALUE_ERROR
,
770 _("There are no unambiguously determinable intermediate "
771 "callers or callees between caller function \"%s\" at %s "
772 "and callee function \"%s\" at %s"),
774 ? "???" : SYMBOL_PRINT_NAME (msym_caller
)),
775 paddress (gdbarch
, caller_pc
),
777 ? "???" : SYMBOL_PRINT_NAME (msym_callee
)),
778 paddress (gdbarch
, callee_pc
));
781 do_cleanups (back_to_workdata
);
782 discard_cleanups (back_to_retval
);
786 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
787 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
788 constructed return NULL. Caller is responsible for xfree of the returned
791 struct call_site_chain
*
792 call_site_find_chain (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
795 volatile struct gdb_exception e
;
796 struct call_site_chain
*retval
= NULL
;
798 TRY_CATCH (e
, RETURN_MASK_ERROR
)
800 retval
= call_site_find_chain_1 (gdbarch
, caller_pc
, callee_pc
);
804 if (e
.error
== NO_ENTRY_VALUE_ERROR
)
806 if (entry_values_debug
)
807 exception_print (gdb_stdout
, e
);
817 /* Fetch call_site_parameter from caller matching the parameters. FRAME is for
818 callee. See DWARF_REG and FB_OFFSET description at struct
819 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
821 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
824 static struct call_site_parameter
*
825 dwarf_expr_reg_to_entry_parameter (struct frame_info
*frame
, int dwarf_reg
,
827 struct dwarf2_per_cu_data
**per_cu_return
)
829 CORE_ADDR func_addr
= get_frame_func (frame
);
831 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
832 struct frame_info
*caller_frame
= get_prev_frame (frame
);
833 struct call_site
*call_site
;
836 struct dwarf2_locexpr_baton
*dwarf_block
;
837 /* Initialize it just to avoid a GCC false warning. */
838 struct call_site_parameter
*parameter
= NULL
;
839 CORE_ADDR target_addr
;
841 if (gdbarch
!= frame_unwind_arch (frame
))
843 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (func_addr
);
844 struct gdbarch
*caller_gdbarch
= frame_unwind_arch (frame
);
846 throw_error (NO_ENTRY_VALUE_ERROR
,
847 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
848 "(of %s (%s)) does not match caller gdbarch %s"),
849 gdbarch_bfd_arch_info (gdbarch
)->printable_name
,
850 paddress (gdbarch
, func_addr
),
851 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
),
852 gdbarch_bfd_arch_info (caller_gdbarch
)->printable_name
);
855 if (caller_frame
== NULL
)
857 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (func_addr
);
859 throw_error (NO_ENTRY_VALUE_ERROR
, _("DW_OP_GNU_entry_value resolving "
860 "requires caller of %s (%s)"),
861 paddress (gdbarch
, func_addr
),
862 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
864 caller_pc
= get_frame_pc (caller_frame
);
865 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
867 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, caller_frame
);
868 if (target_addr
!= func_addr
)
870 struct minimal_symbol
*target_msym
, *func_msym
;
872 target_msym
= lookup_minimal_symbol_by_pc (target_addr
);
873 func_msym
= lookup_minimal_symbol_by_pc (func_addr
);
874 throw_error (NO_ENTRY_VALUE_ERROR
,
875 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
876 "but the called frame is for %s at %s"),
877 (target_msym
== NULL
? "???"
878 : SYMBOL_PRINT_NAME (target_msym
)),
879 paddress (gdbarch
, target_addr
),
880 func_msym
== NULL
? "???" : SYMBOL_PRINT_NAME (func_msym
),
881 paddress (gdbarch
, func_addr
));
884 /* No entry value based parameters would be reliable if this function can
885 call itself via tail calls. */
886 func_verify_no_selftailcall (gdbarch
, func_addr
);
888 for (iparams
= 0; iparams
< call_site
->parameter_count
; iparams
++)
890 parameter
= &call_site
->parameter
[iparams
];
891 if (parameter
->dwarf_reg
== -1 && dwarf_reg
== -1)
893 if (parameter
->fb_offset
== fb_offset
)
896 else if (parameter
->dwarf_reg
== dwarf_reg
)
899 if (iparams
== call_site
->parameter_count
)
901 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (caller_pc
);
903 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
904 determine its value. */
905 throw_error (NO_ENTRY_VALUE_ERROR
, _("Cannot find matching parameter "
906 "at DW_TAG_GNU_call_site %s at %s"),
907 paddress (gdbarch
, caller_pc
),
908 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
911 *per_cu_return
= call_site
->per_cu
;
915 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
916 the normal DW_AT_GNU_call_site_value block. Otherwise return the
917 DW_AT_GNU_call_site_data_value (dereferenced) block.
919 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
922 Function always returns non-NULL, non-optimized out value. It throws
923 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
925 static struct value
*
926 dwarf_entry_parameter_to_value (struct call_site_parameter
*parameter
,
927 CORE_ADDR deref_size
, struct type
*type
,
928 struct frame_info
*caller_frame
,
929 struct dwarf2_per_cu_data
*per_cu
)
931 const gdb_byte
*data_src
;
935 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
936 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
938 /* DEREF_SIZE size is not verified here. */
939 if (data_src
== NULL
)
940 throw_error (NO_ENTRY_VALUE_ERROR
,
941 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
943 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
944 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
946 data
= alloca (size
+ 1);
947 memcpy (data
, data_src
, size
);
948 data
[size
] = DW_OP_stack_value
;
950 return dwarf2_evaluate_loc_desc (type
, caller_frame
, data
, size
+ 1, per_cu
);
953 /* Execute call_site_parameter's DWARF block matching DEREF_SIZE for caller of
954 the CTX's frame. CTX must be of dwarf_expr_ctx_funcs kind. See DWARF_REG
955 and FB_OFFSET description at struct
956 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
958 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
959 can be more simple as it does not support cross-CU DWARF executions. */
962 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context
*ctx
,
963 int dwarf_reg
, CORE_ADDR fb_offset
,
966 struct dwarf_expr_baton
*debaton
;
967 struct frame_info
*frame
, *caller_frame
;
968 struct dwarf2_per_cu_data
*caller_per_cu
;
969 struct dwarf_expr_baton baton_local
;
970 struct dwarf_expr_context saved_ctx
;
971 struct call_site_parameter
*parameter
;
972 const gdb_byte
*data_src
;
975 gdb_assert (ctx
->funcs
== &dwarf_expr_ctx_funcs
);
976 debaton
= ctx
->baton
;
977 frame
= debaton
->frame
;
978 caller_frame
= get_prev_frame (frame
);
980 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, dwarf_reg
, fb_offset
,
982 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
983 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
985 /* DEREF_SIZE size is not verified here. */
986 if (data_src
== NULL
)
987 throw_error (NO_ENTRY_VALUE_ERROR
,
988 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
990 baton_local
.frame
= caller_frame
;
991 baton_local
.per_cu
= caller_per_cu
;
993 saved_ctx
.gdbarch
= ctx
->gdbarch
;
994 saved_ctx
.addr_size
= ctx
->addr_size
;
995 saved_ctx
.offset
= ctx
->offset
;
996 saved_ctx
.baton
= ctx
->baton
;
997 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (baton_local
.per_cu
));
998 ctx
->addr_size
= dwarf2_per_cu_addr_size (baton_local
.per_cu
);
999 ctx
->offset
= dwarf2_per_cu_text_offset (baton_local
.per_cu
);
1000 ctx
->baton
= &baton_local
;
1002 dwarf_expr_eval (ctx
, data_src
, size
);
1004 ctx
->gdbarch
= saved_ctx
.gdbarch
;
1005 ctx
->addr_size
= saved_ctx
.addr_size
;
1006 ctx
->offset
= saved_ctx
.offset
;
1007 ctx
->baton
= saved_ctx
.baton
;
1010 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1011 the indirect method on it, that is use its stored target value, the sole
1012 purpose of entry_data_value_funcs.. */
1014 static struct value
*
1015 entry_data_value_coerce_ref (const struct value
*value
)
1017 struct type
*checked_type
= check_typedef (value_type (value
));
1018 struct value
*target_val
;
1020 if (TYPE_CODE (checked_type
) != TYPE_CODE_REF
)
1023 target_val
= value_computed_closure (value
);
1024 value_incref (target_val
);
1028 /* Implement copy_closure. */
1031 entry_data_value_copy_closure (const struct value
*v
)
1033 struct value
*target_val
= value_computed_closure (v
);
1035 value_incref (target_val
);
1039 /* Implement free_closure. */
1042 entry_data_value_free_closure (struct value
*v
)
1044 struct value
*target_val
= value_computed_closure (v
);
1046 value_free (target_val
);
1049 /* Vector for methods for an entry value reference where the referenced value
1050 is stored in the caller. On the first dereference use
1051 DW_AT_GNU_call_site_data_value in the caller. */
1053 static const struct lval_funcs entry_data_value_funcs
=
1057 NULL
, /* check_validity */
1058 NULL
, /* check_any_valid */
1059 NULL
, /* indirect */
1060 entry_data_value_coerce_ref
,
1061 NULL
, /* check_synthetic_pointer */
1062 entry_data_value_copy_closure
,
1063 entry_data_value_free_closure
1066 /* Read parameter of TYPE at (callee) FRAME's function entry. DWARF_REG and
1067 FB_OFFSET are used to match DW_AT_location at the caller's
1068 DW_TAG_GNU_call_site_parameter. See DWARF_REG and FB_OFFSET description at
1069 struct dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
1071 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1072 cannot resolve the parameter for any reason. */
1074 static struct value
*
1075 value_of_dwarf_reg_entry (struct type
*type
, struct frame_info
*frame
,
1076 int dwarf_reg
, CORE_ADDR fb_offset
)
1078 struct type
*checked_type
= check_typedef (type
);
1079 struct type
*target_type
= TYPE_TARGET_TYPE (checked_type
);
1080 struct frame_info
*caller_frame
= get_prev_frame (frame
);
1081 struct value
*outer_val
, *target_val
, *val
;
1082 struct call_site_parameter
*parameter
;
1083 struct dwarf2_per_cu_data
*caller_per_cu
;
1086 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, dwarf_reg
, fb_offset
,
1089 outer_val
= dwarf_entry_parameter_to_value (parameter
, -1 /* deref_size */,
1093 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1094 used and it is not available do not fall back to OUTER_VAL - dereferencing
1095 TYPE_CODE_REF with non-entry data value would give current value - not the
1098 if (TYPE_CODE (checked_type
) != TYPE_CODE_REF
1099 || TYPE_TARGET_TYPE (checked_type
) == NULL
)
1102 target_val
= dwarf_entry_parameter_to_value (parameter
,
1103 TYPE_LENGTH (target_type
),
1104 target_type
, caller_frame
,
1107 /* value_as_address dereferences TYPE_CODE_REF. */
1108 addr
= extract_typed_address (value_contents (outer_val
), checked_type
);
1110 /* The target entry value has artificial address of the entry value
1112 VALUE_LVAL (target_val
) = lval_memory
;
1113 set_value_address (target_val
, addr
);
1115 release_value (target_val
);
1116 val
= allocate_computed_value (type
, &entry_data_value_funcs
,
1117 target_val
/* closure */);
1119 /* Copy the referencing pointer to the new computed value. */
1120 memcpy (value_contents_raw (val
), value_contents_raw (outer_val
),
1121 TYPE_LENGTH (checked_type
));
1122 set_value_lazy (val
, 0);
1127 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1128 SIZE are DWARF block used to match DW_AT_location at the caller's
1129 DW_TAG_GNU_call_site_parameter.
1131 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1132 cannot resolve the parameter for any reason. */
1134 static struct value
*
1135 value_of_dwarf_block_entry (struct type
*type
, struct frame_info
*frame
,
1136 const gdb_byte
*block
, size_t block_len
)
1139 CORE_ADDR fb_offset
;
1141 dwarf_reg
= dwarf_block_to_dwarf_reg (block
, block
+ block_len
);
1142 if (dwarf_reg
!= -1)
1143 return value_of_dwarf_reg_entry (type
, frame
, dwarf_reg
, 0 /* unused */);
1145 if (dwarf_block_to_fb_offset (block
, block
+ block_len
, &fb_offset
))
1146 return value_of_dwarf_reg_entry (type
, frame
, -1, fb_offset
);
1148 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1149 suppressed during normal operation. The expression can be arbitrary if
1150 there is no caller-callee entry value binding expected. */
1151 throw_error (NO_ENTRY_VALUE_ERROR
,
1152 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1153 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1156 struct piece_closure
1158 /* Reference count. */
1161 /* The CU from which this closure's expression came. */
1162 struct dwarf2_per_cu_data
*per_cu
;
1164 /* The number of pieces used to describe this variable. */
1167 /* The target address size, used only for DWARF_VALUE_STACK. */
1170 /* The pieces themselves. */
1171 struct dwarf_expr_piece
*pieces
;
1174 /* Allocate a closure for a value formed from separately-described
1177 static struct piece_closure
*
1178 allocate_piece_closure (struct dwarf2_per_cu_data
*per_cu
,
1179 int n_pieces
, struct dwarf_expr_piece
*pieces
,
1182 struct piece_closure
*c
= XZALLOC (struct piece_closure
);
1187 c
->n_pieces
= n_pieces
;
1188 c
->addr_size
= addr_size
;
1189 c
->pieces
= XCALLOC (n_pieces
, struct dwarf_expr_piece
);
1191 memcpy (c
->pieces
, pieces
, n_pieces
* sizeof (struct dwarf_expr_piece
));
1192 for (i
= 0; i
< n_pieces
; ++i
)
1193 if (c
->pieces
[i
].location
== DWARF_VALUE_STACK
)
1194 value_incref (c
->pieces
[i
].v
.value
);
1199 /* The lowest-level function to extract bits from a byte buffer.
1200 SOURCE is the buffer. It is updated if we read to the end of a
1202 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1203 updated to reflect the number of bits actually read.
1204 NBITS is the number of bits we want to read. It is updated to
1205 reflect the number of bits actually read. This function may read
1207 BITS_BIG_ENDIAN is taken directly from gdbarch.
1208 This function returns the extracted bits. */
1211 extract_bits_primitive (const gdb_byte
**source
,
1212 unsigned int *source_offset_bits
,
1213 int *nbits
, int bits_big_endian
)
1215 unsigned int avail
, mask
, datum
;
1217 gdb_assert (*source_offset_bits
< 8);
1219 avail
= 8 - *source_offset_bits
;
1223 mask
= (1 << avail
) - 1;
1225 if (bits_big_endian
)
1226 datum
>>= 8 - (*source_offset_bits
+ *nbits
);
1228 datum
>>= *source_offset_bits
;
1232 *source_offset_bits
+= avail
;
1233 if (*source_offset_bits
>= 8)
1235 *source_offset_bits
-= 8;
1242 /* Extract some bits from a source buffer and move forward in the
1245 SOURCE is the source buffer. It is updated as bytes are read.
1246 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1248 NBITS is the number of bits to read.
1249 BITS_BIG_ENDIAN is taken directly from gdbarch.
1251 This function returns the bits that were read. */
1254 extract_bits (const gdb_byte
**source
, unsigned int *source_offset_bits
,
1255 int nbits
, int bits_big_endian
)
1259 gdb_assert (nbits
> 0 && nbits
<= 8);
1261 datum
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
1267 more
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
1269 if (bits_big_endian
)
1279 /* Write some bits into a buffer and move forward in the buffer.
1281 DATUM is the bits to write. The low-order bits of DATUM are used.
1282 DEST is the destination buffer. It is updated as bytes are
1284 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1286 NBITS is the number of valid bits in DATUM.
1287 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1290 insert_bits (unsigned int datum
,
1291 gdb_byte
*dest
, unsigned int dest_offset_bits
,
1292 int nbits
, int bits_big_endian
)
1296 gdb_assert (dest_offset_bits
+ nbits
<= 8);
1298 mask
= (1 << nbits
) - 1;
1299 if (bits_big_endian
)
1301 datum
<<= 8 - (dest_offset_bits
+ nbits
);
1302 mask
<<= 8 - (dest_offset_bits
+ nbits
);
1306 datum
<<= dest_offset_bits
;
1307 mask
<<= dest_offset_bits
;
1310 gdb_assert ((datum
& ~mask
) == 0);
1312 *dest
= (*dest
& ~mask
) | datum
;
1315 /* Copy bits from a source to a destination.
1317 DEST is where the bits should be written.
1318 DEST_OFFSET_BITS is the bit offset into DEST.
1319 SOURCE is the source of bits.
1320 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1321 BIT_COUNT is the number of bits to copy.
1322 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1325 copy_bitwise (gdb_byte
*dest
, unsigned int dest_offset_bits
,
1326 const gdb_byte
*source
, unsigned int source_offset_bits
,
1327 unsigned int bit_count
,
1328 int bits_big_endian
)
1330 unsigned int dest_avail
;
1333 /* Reduce everything to byte-size pieces. */
1334 dest
+= dest_offset_bits
/ 8;
1335 dest_offset_bits
%= 8;
1336 source
+= source_offset_bits
/ 8;
1337 source_offset_bits
%= 8;
1339 dest_avail
= 8 - dest_offset_bits
% 8;
1341 /* See if we can fill the first destination byte. */
1342 if (dest_avail
< bit_count
)
1344 datum
= extract_bits (&source
, &source_offset_bits
, dest_avail
,
1346 insert_bits (datum
, dest
, dest_offset_bits
, dest_avail
, bits_big_endian
);
1348 dest_offset_bits
= 0;
1349 bit_count
-= dest_avail
;
1352 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1353 than 8 bits remaining. */
1354 gdb_assert (dest_offset_bits
% 8 == 0 || bit_count
< 8);
1355 for (; bit_count
>= 8; bit_count
-= 8)
1357 datum
= extract_bits (&source
, &source_offset_bits
, 8, bits_big_endian
);
1358 *dest
++ = (gdb_byte
) datum
;
1361 /* Finally, we may have a few leftover bits. */
1362 gdb_assert (bit_count
<= 8 - dest_offset_bits
% 8);
1365 datum
= extract_bits (&source
, &source_offset_bits
, bit_count
,
1367 insert_bits (datum
, dest
, dest_offset_bits
, bit_count
, bits_big_endian
);
1372 read_pieced_value (struct value
*v
)
1376 ULONGEST bits_to_skip
;
1378 struct piece_closure
*c
1379 = (struct piece_closure
*) value_computed_closure (v
);
1380 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (v
));
1382 size_t buffer_size
= 0;
1383 char *buffer
= NULL
;
1384 struct cleanup
*cleanup
;
1386 = gdbarch_bits_big_endian (get_type_arch (value_type (v
)));
1388 if (value_type (v
) != value_enclosing_type (v
))
1389 internal_error (__FILE__
, __LINE__
,
1390 _("Should not be able to create a lazy value with "
1391 "an enclosing type"));
1393 cleanup
= make_cleanup (free_current_contents
, &buffer
);
1395 contents
= value_contents_raw (v
);
1396 bits_to_skip
= 8 * value_offset (v
);
1397 if (value_bitsize (v
))
1399 bits_to_skip
+= value_bitpos (v
);
1400 type_len
= value_bitsize (v
);
1403 type_len
= 8 * TYPE_LENGTH (value_type (v
));
1405 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
1407 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1408 size_t this_size
, this_size_bits
;
1409 long dest_offset_bits
, source_offset_bits
, source_offset
;
1410 const gdb_byte
*intermediate_buffer
;
1412 /* Compute size, source, and destination offsets for copying, in
1414 this_size_bits
= p
->size
;
1415 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
1417 bits_to_skip
-= this_size_bits
;
1420 if (this_size_bits
> type_len
- offset
)
1421 this_size_bits
= type_len
- offset
;
1422 if (bits_to_skip
> 0)
1424 dest_offset_bits
= 0;
1425 source_offset_bits
= bits_to_skip
;
1426 this_size_bits
-= bits_to_skip
;
1431 dest_offset_bits
= offset
;
1432 source_offset_bits
= 0;
1435 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
1436 source_offset
= source_offset_bits
/ 8;
1437 if (buffer_size
< this_size
)
1439 buffer_size
= this_size
;
1440 buffer
= xrealloc (buffer
, buffer_size
);
1442 intermediate_buffer
= buffer
;
1444 /* Copy from the source to DEST_BUFFER. */
1445 switch (p
->location
)
1447 case DWARF_VALUE_REGISTER
:
1449 struct gdbarch
*arch
= get_frame_arch (frame
);
1450 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.regno
);
1451 int reg_offset
= source_offset
;
1453 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
1454 && this_size
< register_size (arch
, gdb_regnum
))
1456 /* Big-endian, and we want less than full size. */
1457 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
1458 /* We want the lower-order THIS_SIZE_BITS of the bytes
1459 we extract from the register. */
1460 source_offset_bits
+= 8 * this_size
- this_size_bits
;
1463 if (gdb_regnum
!= -1)
1467 if (!get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1471 /* Just so garbage doesn't ever shine through. */
1472 memset (buffer
, 0, this_size
);
1475 set_value_optimized_out (v
, 1);
1477 mark_value_bytes_unavailable (v
, offset
, this_size
);
1482 error (_("Unable to access DWARF register number %s"),
1483 paddress (arch
, p
->v
.regno
));
1488 case DWARF_VALUE_MEMORY
:
1489 read_value_memory (v
, offset
,
1490 p
->v
.mem
.in_stack_memory
,
1491 p
->v
.mem
.addr
+ source_offset
,
1495 case DWARF_VALUE_STACK
:
1497 size_t n
= this_size
;
1499 if (n
> c
->addr_size
- source_offset
)
1500 n
= (c
->addr_size
>= source_offset
1501 ? c
->addr_size
- source_offset
1509 const gdb_byte
*val_bytes
= value_contents_all (p
->v
.value
);
1511 intermediate_buffer
= val_bytes
+ source_offset
;
1516 case DWARF_VALUE_LITERAL
:
1518 size_t n
= this_size
;
1520 if (n
> p
->v
.literal
.length
- source_offset
)
1521 n
= (p
->v
.literal
.length
>= source_offset
1522 ? p
->v
.literal
.length
- source_offset
1525 intermediate_buffer
= p
->v
.literal
.data
+ source_offset
;
1529 /* These bits show up as zeros -- but do not cause the value
1530 to be considered optimized-out. */
1531 case DWARF_VALUE_IMPLICIT_POINTER
:
1534 case DWARF_VALUE_OPTIMIZED_OUT
:
1535 set_value_optimized_out (v
, 1);
1539 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
1542 if (p
->location
!= DWARF_VALUE_OPTIMIZED_OUT
1543 && p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1544 copy_bitwise (contents
, dest_offset_bits
,
1545 intermediate_buffer
, source_offset_bits
% 8,
1546 this_size_bits
, bits_big_endian
);
1548 offset
+= this_size_bits
;
1551 do_cleanups (cleanup
);
1555 write_pieced_value (struct value
*to
, struct value
*from
)
1559 ULONGEST bits_to_skip
;
1560 const gdb_byte
*contents
;
1561 struct piece_closure
*c
1562 = (struct piece_closure
*) value_computed_closure (to
);
1563 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (to
));
1565 size_t buffer_size
= 0;
1566 char *buffer
= NULL
;
1567 struct cleanup
*cleanup
;
1569 = gdbarch_bits_big_endian (get_type_arch (value_type (to
)));
1573 set_value_optimized_out (to
, 1);
1577 cleanup
= make_cleanup (free_current_contents
, &buffer
);
1579 contents
= value_contents (from
);
1580 bits_to_skip
= 8 * value_offset (to
);
1581 if (value_bitsize (to
))
1583 bits_to_skip
+= value_bitpos (to
);
1584 type_len
= value_bitsize (to
);
1587 type_len
= 8 * TYPE_LENGTH (value_type (to
));
1589 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
1591 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1592 size_t this_size_bits
, this_size
;
1593 long dest_offset_bits
, source_offset_bits
, dest_offset
, source_offset
;
1595 const gdb_byte
*source_buffer
;
1597 this_size_bits
= p
->size
;
1598 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
1600 bits_to_skip
-= this_size_bits
;
1603 if (this_size_bits
> type_len
- offset
)
1604 this_size_bits
= type_len
- offset
;
1605 if (bits_to_skip
> 0)
1607 dest_offset_bits
= bits_to_skip
;
1608 source_offset_bits
= 0;
1609 this_size_bits
-= bits_to_skip
;
1614 dest_offset_bits
= 0;
1615 source_offset_bits
= offset
;
1618 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
1619 source_offset
= source_offset_bits
/ 8;
1620 dest_offset
= dest_offset_bits
/ 8;
1621 if (dest_offset_bits
% 8 == 0 && source_offset_bits
% 8 == 0)
1623 source_buffer
= contents
+ source_offset
;
1628 if (buffer_size
< this_size
)
1630 buffer_size
= this_size
;
1631 buffer
= xrealloc (buffer
, buffer_size
);
1633 source_buffer
= buffer
;
1637 switch (p
->location
)
1639 case DWARF_VALUE_REGISTER
:
1641 struct gdbarch
*arch
= get_frame_arch (frame
);
1642 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.regno
);
1643 int reg_offset
= dest_offset
;
1645 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
1646 && this_size
<= register_size (arch
, gdb_regnum
))
1647 /* Big-endian, and we want less than full size. */
1648 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
1650 if (gdb_regnum
!= -1)
1656 if (!get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1661 error (_("Can't do read-modify-write to "
1662 "update bitfield; containing word has been "
1665 throw_error (NOT_AVAILABLE_ERROR
,
1666 _("Can't do read-modify-write to update "
1667 "bitfield; containing word "
1670 copy_bitwise (buffer
, dest_offset_bits
,
1671 contents
, source_offset_bits
,
1676 put_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1677 this_size
, source_buffer
);
1681 error (_("Unable to write to DWARF register number %s"),
1682 paddress (arch
, p
->v
.regno
));
1686 case DWARF_VALUE_MEMORY
:
1689 /* Only the first and last bytes can possibly have any
1691 read_memory (p
->v
.mem
.addr
+ dest_offset
, buffer
, 1);
1692 read_memory (p
->v
.mem
.addr
+ dest_offset
+ this_size
- 1,
1693 buffer
+ this_size
- 1, 1);
1694 copy_bitwise (buffer
, dest_offset_bits
,
1695 contents
, source_offset_bits
,
1700 write_memory (p
->v
.mem
.addr
+ dest_offset
,
1701 source_buffer
, this_size
);
1704 set_value_optimized_out (to
, 1);
1707 offset
+= this_size_bits
;
1710 do_cleanups (cleanup
);
1713 /* A helper function that checks bit validity in a pieced value.
1714 CHECK_FOR indicates the kind of validity checking.
1715 DWARF_VALUE_MEMORY means to check whether any bit is valid.
1716 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1718 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1719 implicit pointer. */
1722 check_pieced_value_bits (const struct value
*value
, int bit_offset
,
1724 enum dwarf_value_location check_for
)
1726 struct piece_closure
*c
1727 = (struct piece_closure
*) value_computed_closure (value
);
1729 int validity
= (check_for
== DWARF_VALUE_MEMORY
1730 || check_for
== DWARF_VALUE_IMPLICIT_POINTER
);
1732 bit_offset
+= 8 * value_offset (value
);
1733 if (value_bitsize (value
))
1734 bit_offset
+= value_bitpos (value
);
1736 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
1738 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1739 size_t this_size_bits
= p
->size
;
1743 if (bit_offset
>= this_size_bits
)
1745 bit_offset
-= this_size_bits
;
1749 bit_length
-= this_size_bits
- bit_offset
;
1753 bit_length
-= this_size_bits
;
1755 if (check_for
== DWARF_VALUE_IMPLICIT_POINTER
)
1757 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1760 else if (p
->location
== DWARF_VALUE_OPTIMIZED_OUT
1761 || p
->location
== DWARF_VALUE_IMPLICIT_POINTER
)
1777 check_pieced_value_validity (const struct value
*value
, int bit_offset
,
1780 return check_pieced_value_bits (value
, bit_offset
, bit_length
,
1781 DWARF_VALUE_MEMORY
);
1785 check_pieced_value_invalid (const struct value
*value
)
1787 return check_pieced_value_bits (value
, 0,
1788 8 * TYPE_LENGTH (value_type (value
)),
1789 DWARF_VALUE_OPTIMIZED_OUT
);
1792 /* An implementation of an lval_funcs method to see whether a value is
1793 a synthetic pointer. */
1796 check_pieced_synthetic_pointer (const struct value
*value
, int bit_offset
,
1799 return check_pieced_value_bits (value
, bit_offset
, bit_length
,
1800 DWARF_VALUE_IMPLICIT_POINTER
);
1803 /* A wrapper function for get_frame_address_in_block. */
1806 get_frame_address_in_block_wrapper (void *baton
)
1808 return get_frame_address_in_block (baton
);
1811 /* An implementation of an lval_funcs method to indirect through a
1812 pointer. This handles the synthetic pointer case when needed. */
1814 static struct value
*
1815 indirect_pieced_value (struct value
*value
)
1817 struct piece_closure
*c
1818 = (struct piece_closure
*) value_computed_closure (value
);
1820 struct frame_info
*frame
;
1821 struct dwarf2_locexpr_baton baton
;
1822 int i
, bit_offset
, bit_length
;
1823 struct dwarf_expr_piece
*piece
= NULL
;
1824 LONGEST byte_offset
;
1826 type
= check_typedef (value_type (value
));
1827 if (TYPE_CODE (type
) != TYPE_CODE_PTR
)
1830 bit_length
= 8 * TYPE_LENGTH (type
);
1831 bit_offset
= 8 * value_offset (value
);
1832 if (value_bitsize (value
))
1833 bit_offset
+= value_bitpos (value
);
1835 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
1837 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1838 size_t this_size_bits
= p
->size
;
1842 if (bit_offset
>= this_size_bits
)
1844 bit_offset
-= this_size_bits
;
1848 bit_length
-= this_size_bits
- bit_offset
;
1852 bit_length
-= this_size_bits
;
1854 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1857 if (bit_length
!= 0)
1858 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1864 frame
= get_selected_frame (_("No frame selected."));
1866 /* This is an offset requested by GDB, such as value subcripts. */
1867 byte_offset
= value_as_address (value
);
1870 baton
= dwarf2_fetch_die_location_block (piece
->v
.ptr
.die
, c
->per_cu
,
1871 get_frame_address_in_block_wrapper
,
1874 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type
), frame
,
1875 baton
.data
, baton
.size
, baton
.per_cu
,
1876 piece
->v
.ptr
.offset
+ byte_offset
);
1880 copy_pieced_value_closure (const struct value
*v
)
1882 struct piece_closure
*c
1883 = (struct piece_closure
*) value_computed_closure (v
);
1890 free_pieced_value_closure (struct value
*v
)
1892 struct piece_closure
*c
1893 = (struct piece_closure
*) value_computed_closure (v
);
1900 for (i
= 0; i
< c
->n_pieces
; ++i
)
1901 if (c
->pieces
[i
].location
== DWARF_VALUE_STACK
)
1902 value_free (c
->pieces
[i
].v
.value
);
1909 /* Functions for accessing a variable described by DW_OP_piece. */
1910 static const struct lval_funcs pieced_value_funcs
= {
1913 check_pieced_value_validity
,
1914 check_pieced_value_invalid
,
1915 indirect_pieced_value
,
1916 NULL
, /* coerce_ref */
1917 check_pieced_synthetic_pointer
,
1918 copy_pieced_value_closure
,
1919 free_pieced_value_closure
1922 /* Helper function which throws an error if a synthetic pointer is
1926 invalid_synthetic_pointer (void)
1928 error (_("access outside bounds of object "
1929 "referenced via synthetic pointer"));
1932 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
1934 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs
=
1936 dwarf_expr_read_reg
,
1937 dwarf_expr_read_mem
,
1938 dwarf_expr_frame_base
,
1939 dwarf_expr_frame_cfa
,
1940 dwarf_expr_frame_pc
,
1941 dwarf_expr_tls_address
,
1942 dwarf_expr_dwarf_call
,
1943 dwarf_expr_get_base_type
,
1944 dwarf_expr_push_dwarf_reg_entry_value
1947 /* Evaluate a location description, starting at DATA and with length
1948 SIZE, to find the current location of variable of TYPE in the
1949 context of FRAME. BYTE_OFFSET is applied after the contents are
1952 static struct value
*
1953 dwarf2_evaluate_loc_desc_full (struct type
*type
, struct frame_info
*frame
,
1954 const gdb_byte
*data
, unsigned short size
,
1955 struct dwarf2_per_cu_data
*per_cu
,
1956 LONGEST byte_offset
)
1958 struct value
*retval
;
1959 struct dwarf_expr_baton baton
;
1960 struct dwarf_expr_context
*ctx
;
1961 struct cleanup
*old_chain
, *value_chain
;
1962 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
1963 volatile struct gdb_exception ex
;
1965 if (byte_offset
< 0)
1966 invalid_synthetic_pointer ();
1969 return allocate_optimized_out_value (type
);
1971 baton
.frame
= frame
;
1972 baton
.per_cu
= per_cu
;
1974 ctx
= new_dwarf_expr_context ();
1975 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
1976 value_chain
= make_cleanup_value_free_to_mark (value_mark ());
1978 ctx
->gdbarch
= get_objfile_arch (objfile
);
1979 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
1980 ctx
->ref_addr_size
= dwarf2_per_cu_ref_addr_size (per_cu
);
1981 ctx
->offset
= dwarf2_per_cu_text_offset (per_cu
);
1982 ctx
->baton
= &baton
;
1983 ctx
->funcs
= &dwarf_expr_ctx_funcs
;
1985 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
1987 dwarf_expr_eval (ctx
, data
, size
);
1991 if (ex
.error
== NOT_AVAILABLE_ERROR
)
1993 do_cleanups (old_chain
);
1994 retval
= allocate_value (type
);
1995 mark_value_bytes_unavailable (retval
, 0, TYPE_LENGTH (type
));
1998 else if (ex
.error
== NO_ENTRY_VALUE_ERROR
)
2000 if (entry_values_debug
)
2001 exception_print (gdb_stdout
, ex
);
2002 do_cleanups (old_chain
);
2003 return allocate_optimized_out_value (type
);
2006 throw_exception (ex
);
2009 if (ctx
->num_pieces
> 0)
2011 struct piece_closure
*c
;
2012 struct frame_id frame_id
= get_frame_id (frame
);
2013 ULONGEST bit_size
= 0;
2016 for (i
= 0; i
< ctx
->num_pieces
; ++i
)
2017 bit_size
+= ctx
->pieces
[i
].size
;
2018 if (8 * (byte_offset
+ TYPE_LENGTH (type
)) > bit_size
)
2019 invalid_synthetic_pointer ();
2021 c
= allocate_piece_closure (per_cu
, ctx
->num_pieces
, ctx
->pieces
,
2023 /* We must clean up the value chain after creating the piece
2024 closure but before allocating the result. */
2025 do_cleanups (value_chain
);
2026 retval
= allocate_computed_value (type
, &pieced_value_funcs
, c
);
2027 VALUE_FRAME_ID (retval
) = frame_id
;
2028 set_value_offset (retval
, byte_offset
);
2032 switch (ctx
->location
)
2034 case DWARF_VALUE_REGISTER
:
2036 struct gdbarch
*arch
= get_frame_arch (frame
);
2037 ULONGEST dwarf_regnum
= value_as_long (dwarf_expr_fetch (ctx
, 0));
2038 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_regnum
);
2040 if (byte_offset
!= 0)
2041 error (_("cannot use offset on synthetic pointer to register"));
2042 do_cleanups (value_chain
);
2043 if (gdb_regnum
!= -1)
2044 retval
= value_from_register (type
, gdb_regnum
, frame
);
2046 error (_("Unable to access DWARF register number %s"),
2047 paddress (arch
, dwarf_regnum
));
2051 case DWARF_VALUE_MEMORY
:
2053 CORE_ADDR address
= dwarf_expr_fetch_address (ctx
, 0);
2054 int in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
2056 do_cleanups (value_chain
);
2057 retval
= allocate_value_lazy (type
);
2058 VALUE_LVAL (retval
) = lval_memory
;
2059 if (in_stack_memory
)
2060 set_value_stack (retval
, 1);
2061 set_value_address (retval
, address
+ byte_offset
);
2065 case DWARF_VALUE_STACK
:
2067 struct value
*value
= dwarf_expr_fetch (ctx
, 0);
2069 const gdb_byte
*val_bytes
;
2070 size_t n
= TYPE_LENGTH (value_type (value
));
2072 if (byte_offset
+ TYPE_LENGTH (type
) > n
)
2073 invalid_synthetic_pointer ();
2075 val_bytes
= value_contents_all (value
);
2076 val_bytes
+= byte_offset
;
2079 /* Preserve VALUE because we are going to free values back
2080 to the mark, but we still need the value contents
2082 value_incref (value
);
2083 do_cleanups (value_chain
);
2084 make_cleanup_value_free (value
);
2086 retval
= allocate_value (type
);
2087 contents
= value_contents_raw (retval
);
2088 if (n
> TYPE_LENGTH (type
))
2090 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
2092 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
2093 val_bytes
+= n
- TYPE_LENGTH (type
);
2094 n
= TYPE_LENGTH (type
);
2096 memcpy (contents
, val_bytes
, n
);
2100 case DWARF_VALUE_LITERAL
:
2103 const bfd_byte
*ldata
;
2104 size_t n
= ctx
->len
;
2106 if (byte_offset
+ TYPE_LENGTH (type
) > n
)
2107 invalid_synthetic_pointer ();
2109 do_cleanups (value_chain
);
2110 retval
= allocate_value (type
);
2111 contents
= value_contents_raw (retval
);
2113 ldata
= ctx
->data
+ byte_offset
;
2116 if (n
> TYPE_LENGTH (type
))
2118 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
2120 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
2121 ldata
+= n
- TYPE_LENGTH (type
);
2122 n
= TYPE_LENGTH (type
);
2124 memcpy (contents
, ldata
, n
);
2128 case DWARF_VALUE_OPTIMIZED_OUT
:
2129 do_cleanups (value_chain
);
2130 retval
= allocate_optimized_out_value (type
);
2133 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2134 operation by execute_stack_op. */
2135 case DWARF_VALUE_IMPLICIT_POINTER
:
2136 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2137 it can only be encountered when making a piece. */
2139 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
2143 set_value_initialized (retval
, ctx
->initialized
);
2145 do_cleanups (old_chain
);
2150 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2151 passes 0 as the byte_offset. */
2154 dwarf2_evaluate_loc_desc (struct type
*type
, struct frame_info
*frame
,
2155 const gdb_byte
*data
, unsigned short size
,
2156 struct dwarf2_per_cu_data
*per_cu
)
2158 return dwarf2_evaluate_loc_desc_full (type
, frame
, data
, size
, per_cu
, 0);
2162 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
2164 struct needs_frame_baton
2167 struct dwarf2_per_cu_data
*per_cu
;
2170 /* Reads from registers do require a frame. */
2172 needs_frame_read_reg (void *baton
, int regnum
)
2174 struct needs_frame_baton
*nf_baton
= baton
;
2176 nf_baton
->needs_frame
= 1;
2180 /* Reads from memory do not require a frame. */
2182 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
2184 memset (buf
, 0, len
);
2187 /* Frame-relative accesses do require a frame. */
2189 needs_frame_frame_base (void *baton
, const gdb_byte
**start
, size_t * length
)
2191 static gdb_byte lit0
= DW_OP_lit0
;
2192 struct needs_frame_baton
*nf_baton
= baton
;
2197 nf_baton
->needs_frame
= 1;
2200 /* CFA accesses require a frame. */
2203 needs_frame_frame_cfa (void *baton
)
2205 struct needs_frame_baton
*nf_baton
= baton
;
2207 nf_baton
->needs_frame
= 1;
2211 /* Thread-local accesses do require a frame. */
2213 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
2215 struct needs_frame_baton
*nf_baton
= baton
;
2217 nf_baton
->needs_frame
= 1;
2221 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
2224 needs_frame_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
)
2226 struct needs_frame_baton
*nf_baton
= ctx
->baton
;
2228 per_cu_dwarf_call (ctx
, die_offset
, nf_baton
->per_cu
,
2229 ctx
->funcs
->get_frame_pc
, ctx
->baton
);
2232 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2235 needs_dwarf_reg_entry_value (struct dwarf_expr_context
*ctx
,
2236 int dwarf_reg
, CORE_ADDR fb_offset
, int deref_size
)
2238 struct needs_frame_baton
*nf_baton
= ctx
->baton
;
2240 nf_baton
->needs_frame
= 1;
2243 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
2245 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs
=
2247 needs_frame_read_reg
,
2248 needs_frame_read_mem
,
2249 needs_frame_frame_base
,
2250 needs_frame_frame_cfa
,
2251 needs_frame_frame_cfa
, /* get_frame_pc */
2252 needs_frame_tls_address
,
2253 needs_frame_dwarf_call
,
2254 NULL
, /* get_base_type */
2255 needs_dwarf_reg_entry_value
2258 /* Return non-zero iff the location expression at DATA (length SIZE)
2259 requires a frame to evaluate. */
2262 dwarf2_loc_desc_needs_frame (const gdb_byte
*data
, unsigned short size
,
2263 struct dwarf2_per_cu_data
*per_cu
)
2265 struct needs_frame_baton baton
;
2266 struct dwarf_expr_context
*ctx
;
2268 struct cleanup
*old_chain
;
2269 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
2271 baton
.needs_frame
= 0;
2272 baton
.per_cu
= per_cu
;
2274 ctx
= new_dwarf_expr_context ();
2275 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
2276 make_cleanup_value_free_to_mark (value_mark ());
2278 ctx
->gdbarch
= get_objfile_arch (objfile
);
2279 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
2280 ctx
->ref_addr_size
= dwarf2_per_cu_ref_addr_size (per_cu
);
2281 ctx
->offset
= dwarf2_per_cu_text_offset (per_cu
);
2282 ctx
->baton
= &baton
;
2283 ctx
->funcs
= &needs_frame_ctx_funcs
;
2285 dwarf_expr_eval (ctx
, data
, size
);
2287 in_reg
= ctx
->location
== DWARF_VALUE_REGISTER
;
2289 if (ctx
->num_pieces
> 0)
2293 /* If the location has several pieces, and any of them are in
2294 registers, then we will need a frame to fetch them from. */
2295 for (i
= 0; i
< ctx
->num_pieces
; i
++)
2296 if (ctx
->pieces
[i
].location
== DWARF_VALUE_REGISTER
)
2300 do_cleanups (old_chain
);
2302 return baton
.needs_frame
|| in_reg
;
2305 /* A helper function that throws an unimplemented error mentioning a
2306 given DWARF operator. */
2309 unimplemented (unsigned int op
)
2311 const char *name
= dwarf_stack_op_name (op
);
2314 error (_("DWARF operator %s cannot be translated to an agent expression"),
2317 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2318 "to an agent expression"),
2322 /* A helper function to convert a DWARF register to an arch register.
2323 ARCH is the architecture.
2324 DWARF_REG is the register.
2325 This will throw an exception if the DWARF register cannot be
2326 translated to an architecture register. */
2329 translate_register (struct gdbarch
*arch
, int dwarf_reg
)
2331 int reg
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_reg
);
2333 error (_("Unable to access DWARF register number %d"), dwarf_reg
);
2337 /* A helper function that emits an access to memory. ARCH is the
2338 target architecture. EXPR is the expression which we are building.
2339 NBITS is the number of bits we want to read. This emits the
2340 opcodes needed to read the memory and then extract the desired
2344 access_memory (struct gdbarch
*arch
, struct agent_expr
*expr
, ULONGEST nbits
)
2346 ULONGEST nbytes
= (nbits
+ 7) / 8;
2348 gdb_assert (nbits
> 0 && nbits
<= sizeof (LONGEST
));
2351 ax_trace_quick (expr
, nbytes
);
2354 ax_simple (expr
, aop_ref8
);
2355 else if (nbits
<= 16)
2356 ax_simple (expr
, aop_ref16
);
2357 else if (nbits
<= 32)
2358 ax_simple (expr
, aop_ref32
);
2360 ax_simple (expr
, aop_ref64
);
2362 /* If we read exactly the number of bytes we wanted, we're done. */
2363 if (8 * nbytes
== nbits
)
2366 if (gdbarch_bits_big_endian (arch
))
2368 /* On a bits-big-endian machine, we want the high-order
2370 ax_const_l (expr
, 8 * nbytes
- nbits
);
2371 ax_simple (expr
, aop_rsh_unsigned
);
2375 /* On a bits-little-endian box, we want the low-order NBITS. */
2376 ax_zero_ext (expr
, nbits
);
2380 /* A helper function to return the frame's PC. */
2383 get_ax_pc (void *baton
)
2385 struct agent_expr
*expr
= baton
;
2390 /* Compile a DWARF location expression to an agent expression.
2392 EXPR is the agent expression we are building.
2393 LOC is the agent value we modify.
2394 ARCH is the architecture.
2395 ADDR_SIZE is the size of addresses, in bytes.
2396 OP_PTR is the start of the location expression.
2397 OP_END is one past the last byte of the location expression.
2399 This will throw an exception for various kinds of errors -- for
2400 example, if the expression cannot be compiled, or if the expression
2404 dwarf2_compile_expr_to_ax (struct agent_expr
*expr
, struct axs_value
*loc
,
2405 struct gdbarch
*arch
, unsigned int addr_size
,
2406 const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
2407 struct dwarf2_per_cu_data
*per_cu
)
2409 struct cleanup
*cleanups
;
2411 VEC(int) *dw_labels
= NULL
, *patches
= NULL
;
2412 const gdb_byte
* const base
= op_ptr
;
2413 const gdb_byte
*previous_piece
= op_ptr
;
2414 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
2415 ULONGEST bits_collected
= 0;
2416 unsigned int addr_size_bits
= 8 * addr_size
;
2417 int bits_big_endian
= gdbarch_bits_big_endian (arch
);
2419 offsets
= xmalloc ((op_end
- op_ptr
) * sizeof (int));
2420 cleanups
= make_cleanup (xfree
, offsets
);
2422 for (i
= 0; i
< op_end
- op_ptr
; ++i
)
2425 make_cleanup (VEC_cleanup (int), &dw_labels
);
2426 make_cleanup (VEC_cleanup (int), &patches
);
2428 /* By default we are making an address. */
2429 loc
->kind
= axs_lvalue_memory
;
2431 while (op_ptr
< op_end
)
2433 enum dwarf_location_atom op
= *op_ptr
;
2434 ULONGEST uoffset
, reg
;
2438 offsets
[op_ptr
- base
] = expr
->len
;
2441 /* Our basic approach to code generation is to map DWARF
2442 operations directly to AX operations. However, there are
2445 First, DWARF works on address-sized units, but AX always uses
2446 LONGEST. For most operations we simply ignore this
2447 difference; instead we generate sign extensions as needed
2448 before division and comparison operations. It would be nice
2449 to omit the sign extensions, but there is no way to determine
2450 the size of the target's LONGEST. (This code uses the size
2451 of the host LONGEST in some cases -- that is a bug but it is
2454 Second, some DWARF operations cannot be translated to AX.
2455 For these we simply fail. See
2456 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2491 ax_const_l (expr
, op
- DW_OP_lit0
);
2495 uoffset
= extract_unsigned_integer (op_ptr
, addr_size
, byte_order
);
2496 op_ptr
+= addr_size
;
2497 /* Some versions of GCC emit DW_OP_addr before
2498 DW_OP_GNU_push_tls_address. In this case the value is an
2499 index, not an address. We don't support things like
2500 branching between the address and the TLS op. */
2501 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
2502 uoffset
+= dwarf2_per_cu_text_offset (per_cu
);
2503 ax_const_l (expr
, uoffset
);
2507 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 1, byte_order
));
2511 ax_const_l (expr
, extract_signed_integer (op_ptr
, 1, byte_order
));
2515 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 2, byte_order
));
2519 ax_const_l (expr
, extract_signed_integer (op_ptr
, 2, byte_order
));
2523 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 4, byte_order
));
2527 ax_const_l (expr
, extract_signed_integer (op_ptr
, 4, byte_order
));
2531 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 8, byte_order
));
2535 ax_const_l (expr
, extract_signed_integer (op_ptr
, 8, byte_order
));
2539 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
2540 ax_const_l (expr
, uoffset
);
2543 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2544 ax_const_l (expr
, offset
);
2579 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
2580 loc
->u
.reg
= translate_register (arch
, op
- DW_OP_reg0
);
2581 loc
->kind
= axs_lvalue_register
;
2585 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
2586 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
2587 loc
->u
.reg
= translate_register (arch
, reg
);
2588 loc
->kind
= axs_lvalue_register
;
2591 case DW_OP_implicit_value
:
2595 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
2596 if (op_ptr
+ len
> op_end
)
2597 error (_("DW_OP_implicit_value: too few bytes available."));
2598 if (len
> sizeof (ULONGEST
))
2599 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2602 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, len
,
2605 dwarf_expr_require_composition (op_ptr
, op_end
,
2606 "DW_OP_implicit_value");
2608 loc
->kind
= axs_rvalue
;
2612 case DW_OP_stack_value
:
2613 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
2614 loc
->kind
= axs_rvalue
;
2649 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2650 i
= translate_register (arch
, op
- DW_OP_breg0
);
2654 ax_const_l (expr
, offset
);
2655 ax_simple (expr
, aop_add
);
2660 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
2661 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2662 i
= translate_register (arch
, reg
);
2666 ax_const_l (expr
, offset
);
2667 ax_simple (expr
, aop_add
);
2673 const gdb_byte
*datastart
;
2675 unsigned int before_stack_len
;
2677 struct symbol
*framefunc
;
2678 LONGEST base_offset
= 0;
2680 b
= block_for_pc (expr
->scope
);
2683 error (_("No block found for address"));
2685 framefunc
= block_linkage_function (b
);
2688 error (_("No function found for block"));
2690 dwarf_expr_frame_base_1 (framefunc
, expr
->scope
,
2691 &datastart
, &datalen
);
2693 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2694 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
, datastart
,
2695 datastart
+ datalen
, per_cu
);
2699 ax_const_l (expr
, offset
);
2700 ax_simple (expr
, aop_add
);
2703 loc
->kind
= axs_lvalue_memory
;
2708 ax_simple (expr
, aop_dup
);
2712 ax_simple (expr
, aop_pop
);
2717 ax_pick (expr
, offset
);
2721 ax_simple (expr
, aop_swap
);
2729 ax_simple (expr
, aop_rot
);
2733 case DW_OP_deref_size
:
2737 if (op
== DW_OP_deref_size
)
2745 ax_simple (expr
, aop_ref8
);
2748 ax_simple (expr
, aop_ref16
);
2751 ax_simple (expr
, aop_ref32
);
2754 ax_simple (expr
, aop_ref64
);
2757 /* Note that dwarf_stack_op_name will never return
2759 error (_("Unsupported size %d in %s"),
2760 size
, dwarf_stack_op_name (op
));
2766 /* Sign extend the operand. */
2767 ax_ext (expr
, addr_size_bits
);
2768 ax_simple (expr
, aop_dup
);
2769 ax_const_l (expr
, 0);
2770 ax_simple (expr
, aop_less_signed
);
2771 ax_simple (expr
, aop_log_not
);
2772 i
= ax_goto (expr
, aop_if_goto
);
2773 /* We have to emit 0 - X. */
2774 ax_const_l (expr
, 0);
2775 ax_simple (expr
, aop_swap
);
2776 ax_simple (expr
, aop_sub
);
2777 ax_label (expr
, i
, expr
->len
);
2781 /* No need to sign extend here. */
2782 ax_const_l (expr
, 0);
2783 ax_simple (expr
, aop_swap
);
2784 ax_simple (expr
, aop_sub
);
2788 /* Sign extend the operand. */
2789 ax_ext (expr
, addr_size_bits
);
2790 ax_simple (expr
, aop_bit_not
);
2793 case DW_OP_plus_uconst
:
2794 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
2795 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2796 but we micro-optimize anyhow. */
2799 ax_const_l (expr
, reg
);
2800 ax_simple (expr
, aop_add
);
2805 ax_simple (expr
, aop_bit_and
);
2809 /* Sign extend the operands. */
2810 ax_ext (expr
, addr_size_bits
);
2811 ax_simple (expr
, aop_swap
);
2812 ax_ext (expr
, addr_size_bits
);
2813 ax_simple (expr
, aop_swap
);
2814 ax_simple (expr
, aop_div_signed
);
2818 ax_simple (expr
, aop_sub
);
2822 ax_simple (expr
, aop_rem_unsigned
);
2826 ax_simple (expr
, aop_mul
);
2830 ax_simple (expr
, aop_bit_or
);
2834 ax_simple (expr
, aop_add
);
2838 ax_simple (expr
, aop_lsh
);
2842 ax_simple (expr
, aop_rsh_unsigned
);
2846 ax_simple (expr
, aop_rsh_signed
);
2850 ax_simple (expr
, aop_bit_xor
);
2854 /* Sign extend the operands. */
2855 ax_ext (expr
, addr_size_bits
);
2856 ax_simple (expr
, aop_swap
);
2857 ax_ext (expr
, addr_size_bits
);
2858 /* Note no swap here: A <= B is !(B < A). */
2859 ax_simple (expr
, aop_less_signed
);
2860 ax_simple (expr
, aop_log_not
);
2864 /* Sign extend the operands. */
2865 ax_ext (expr
, addr_size_bits
);
2866 ax_simple (expr
, aop_swap
);
2867 ax_ext (expr
, addr_size_bits
);
2868 ax_simple (expr
, aop_swap
);
2869 /* A >= B is !(A < B). */
2870 ax_simple (expr
, aop_less_signed
);
2871 ax_simple (expr
, aop_log_not
);
2875 /* Sign extend the operands. */
2876 ax_ext (expr
, addr_size_bits
);
2877 ax_simple (expr
, aop_swap
);
2878 ax_ext (expr
, addr_size_bits
);
2879 /* No need for a second swap here. */
2880 ax_simple (expr
, aop_equal
);
2884 /* Sign extend the operands. */
2885 ax_ext (expr
, addr_size_bits
);
2886 ax_simple (expr
, aop_swap
);
2887 ax_ext (expr
, addr_size_bits
);
2888 ax_simple (expr
, aop_swap
);
2889 ax_simple (expr
, aop_less_signed
);
2893 /* Sign extend the operands. */
2894 ax_ext (expr
, addr_size_bits
);
2895 ax_simple (expr
, aop_swap
);
2896 ax_ext (expr
, addr_size_bits
);
2897 /* Note no swap here: A > B is B < A. */
2898 ax_simple (expr
, aop_less_signed
);
2902 /* Sign extend the operands. */
2903 ax_ext (expr
, addr_size_bits
);
2904 ax_simple (expr
, aop_swap
);
2905 ax_ext (expr
, addr_size_bits
);
2906 /* No need for a swap here. */
2907 ax_simple (expr
, aop_equal
);
2908 ax_simple (expr
, aop_log_not
);
2911 case DW_OP_call_frame_cfa
:
2912 dwarf2_compile_cfa_to_ax (expr
, loc
, arch
, expr
->scope
, per_cu
);
2913 loc
->kind
= axs_lvalue_memory
;
2916 case DW_OP_GNU_push_tls_address
:
2921 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
2923 i
= ax_goto (expr
, aop_goto
);
2924 VEC_safe_push (int, dw_labels
, op_ptr
+ offset
- base
);
2925 VEC_safe_push (int, patches
, i
);
2929 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
2931 /* Zero extend the operand. */
2932 ax_zero_ext (expr
, addr_size_bits
);
2933 i
= ax_goto (expr
, aop_if_goto
);
2934 VEC_safe_push (int, dw_labels
, op_ptr
+ offset
- base
);
2935 VEC_safe_push (int, patches
, i
);
2942 case DW_OP_bit_piece
:
2944 ULONGEST size
, offset
;
2946 if (op_ptr
- 1 == previous_piece
)
2947 error (_("Cannot translate empty pieces to agent expressions"));
2948 previous_piece
= op_ptr
- 1;
2950 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
2951 if (op
== DW_OP_piece
)
2957 op_ptr
= read_uleb128 (op_ptr
, op_end
, &offset
);
2959 if (bits_collected
+ size
> 8 * sizeof (LONGEST
))
2960 error (_("Expression pieces exceed word size"));
2962 /* Access the bits. */
2965 case axs_lvalue_register
:
2966 ax_reg (expr
, loc
->u
.reg
);
2969 case axs_lvalue_memory
:
2970 /* Offset the pointer, if needed. */
2973 ax_const_l (expr
, offset
/ 8);
2974 ax_simple (expr
, aop_add
);
2977 access_memory (arch
, expr
, size
);
2981 /* For a bits-big-endian target, shift up what we already
2982 have. For a bits-little-endian target, shift up the
2983 new data. Note that there is a potential bug here if
2984 the DWARF expression leaves multiple values on the
2986 if (bits_collected
> 0)
2988 if (bits_big_endian
)
2990 ax_simple (expr
, aop_swap
);
2991 ax_const_l (expr
, size
);
2992 ax_simple (expr
, aop_lsh
);
2993 /* We don't need a second swap here, because
2994 aop_bit_or is symmetric. */
2998 ax_const_l (expr
, size
);
2999 ax_simple (expr
, aop_lsh
);
3001 ax_simple (expr
, aop_bit_or
);
3004 bits_collected
+= size
;
3005 loc
->kind
= axs_rvalue
;
3009 case DW_OP_GNU_uninit
:
3015 struct dwarf2_locexpr_baton block
;
3016 int size
= (op
== DW_OP_call2
? 2 : 4);
3018 uoffset
= extract_unsigned_integer (op_ptr
, size
, byte_order
);
3021 block
= dwarf2_fetch_die_location_block (uoffset
, per_cu
,
3024 /* DW_OP_call_ref is currently not supported. */
3025 gdb_assert (block
.per_cu
== per_cu
);
3027 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
,
3028 block
.data
, block
.data
+ block
.size
,
3033 case DW_OP_call_ref
:
3041 /* Patch all the branches we emitted. */
3042 for (i
= 0; i
< VEC_length (int, patches
); ++i
)
3044 int targ
= offsets
[VEC_index (int, dw_labels
, i
)];
3046 internal_error (__FILE__
, __LINE__
, _("invalid label"));
3047 ax_label (expr
, VEC_index (int, patches
, i
), targ
);
3050 do_cleanups (cleanups
);
3054 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3055 evaluator to calculate the location. */
3056 static struct value
*
3057 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
3059 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3062 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3063 dlbaton
->size
, dlbaton
->per_cu
);
3068 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3069 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3072 static struct value
*
3073 locexpr_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
3075 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3077 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3081 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3083 locexpr_read_needs_frame (struct symbol
*symbol
)
3085 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3087 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
3091 /* Return true if DATA points to the end of a piece. END is one past
3092 the last byte in the expression. */
3095 piece_end_p (const gdb_byte
*data
, const gdb_byte
*end
)
3097 return data
== end
|| data
[0] == DW_OP_piece
|| data
[0] == DW_OP_bit_piece
;
3100 /* Helper for locexpr_describe_location_piece that finds the name of a
3104 locexpr_regname (struct gdbarch
*gdbarch
, int dwarf_regnum
)
3108 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
3109 return gdbarch_register_name (gdbarch
, regnum
);
3112 /* Nicely describe a single piece of a location, returning an updated
3113 position in the bytecode sequence. This function cannot recognize
3114 all locations; if a location is not recognized, it simply returns
3117 static const gdb_byte
*
3118 locexpr_describe_location_piece (struct symbol
*symbol
, struct ui_file
*stream
,
3119 CORE_ADDR addr
, struct objfile
*objfile
,
3120 const gdb_byte
*data
, const gdb_byte
*end
,
3121 unsigned int addr_size
)
3123 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3125 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
3127 fprintf_filtered (stream
, _("a variable in $%s"),
3128 locexpr_regname (gdbarch
, data
[0] - DW_OP_reg0
));
3131 else if (data
[0] == DW_OP_regx
)
3135 data
= read_uleb128 (data
+ 1, end
, ®
);
3136 fprintf_filtered (stream
, _("a variable in $%s"),
3137 locexpr_regname (gdbarch
, reg
));
3139 else if (data
[0] == DW_OP_fbreg
)
3142 struct symbol
*framefunc
;
3144 LONGEST frame_offset
;
3145 const gdb_byte
*base_data
, *new_data
, *save_data
= data
;
3147 LONGEST base_offset
= 0;
3149 new_data
= read_sleb128 (data
+ 1, end
, &frame_offset
);
3150 if (!piece_end_p (new_data
, end
))
3154 b
= block_for_pc (addr
);
3157 error (_("No block found for address for symbol \"%s\"."),
3158 SYMBOL_PRINT_NAME (symbol
));
3160 framefunc
= block_linkage_function (b
);
3163 error (_("No function found for block for symbol \"%s\"."),
3164 SYMBOL_PRINT_NAME (symbol
));
3166 dwarf_expr_frame_base_1 (framefunc
, addr
, &base_data
, &base_size
);
3168 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
3170 const gdb_byte
*buf_end
;
3172 frame_reg
= base_data
[0] - DW_OP_breg0
;
3173 buf_end
= read_sleb128 (base_data
+ 1,
3174 base_data
+ base_size
, &base_offset
);
3175 if (buf_end
!= base_data
+ base_size
)
3176 error (_("Unexpected opcode after "
3177 "DW_OP_breg%u for symbol \"%s\"."),
3178 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
3180 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
3182 /* The frame base is just the register, with no offset. */
3183 frame_reg
= base_data
[0] - DW_OP_reg0
;
3188 /* We don't know what to do with the frame base expression,
3189 so we can't trace this variable; give up. */
3193 fprintf_filtered (stream
,
3194 _("a variable at frame base reg $%s offset %s+%s"),
3195 locexpr_regname (gdbarch
, frame_reg
),
3196 plongest (base_offset
), plongest (frame_offset
));
3198 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
3199 && piece_end_p (data
, end
))
3203 data
= read_sleb128 (data
+ 1, end
, &offset
);
3205 fprintf_filtered (stream
,
3206 _("a variable at offset %s from base reg $%s"),
3208 locexpr_regname (gdbarch
, data
[0] - DW_OP_breg0
));
3211 /* The location expression for a TLS variable looks like this (on a
3214 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3215 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3217 0x3 is the encoding for DW_OP_addr, which has an operand as long
3218 as the size of an address on the target machine (here is 8
3219 bytes). Note that more recent version of GCC emit DW_OP_const4u
3220 or DW_OP_const8u, depending on address size, rather than
3221 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3222 The operand represents the offset at which the variable is within
3223 the thread local storage. */
3225 else if (data
+ 1 + addr_size
< end
3226 && (data
[0] == DW_OP_addr
3227 || (addr_size
== 4 && data
[0] == DW_OP_const4u
)
3228 || (addr_size
== 8 && data
[0] == DW_OP_const8u
))
3229 && data
[1 + addr_size
] == DW_OP_GNU_push_tls_address
3230 && piece_end_p (data
+ 2 + addr_size
, end
))
3233 offset
= extract_unsigned_integer (data
+ 1, addr_size
,
3234 gdbarch_byte_order (gdbarch
));
3236 fprintf_filtered (stream
,
3237 _("a thread-local variable at offset 0x%s "
3238 "in the thread-local storage for `%s'"),
3239 phex_nz (offset
, addr_size
), objfile
->name
);
3241 data
+= 1 + addr_size
+ 1;
3243 else if (data
[0] >= DW_OP_lit0
3244 && data
[0] <= DW_OP_lit31
3246 && data
[1] == DW_OP_stack_value
)
3248 fprintf_filtered (stream
, _("the constant %d"), data
[0] - DW_OP_lit0
);
3255 /* Disassemble an expression, stopping at the end of a piece or at the
3256 end of the expression. Returns a pointer to the next unread byte
3257 in the input expression. If ALL is nonzero, then this function
3258 will keep going until it reaches the end of the expression. */
3260 static const gdb_byte
*
3261 disassemble_dwarf_expression (struct ui_file
*stream
,
3262 struct gdbarch
*arch
, unsigned int addr_size
,
3263 int offset_size
, const gdb_byte
*start
,
3264 const gdb_byte
*data
, const gdb_byte
*end
,
3265 int indent
, int all
,
3266 struct dwarf2_per_cu_data
*per_cu
)
3270 || (data
[0] != DW_OP_piece
&& data
[0] != DW_OP_bit_piece
)))
3272 enum dwarf_location_atom op
= *data
++;
3277 name
= dwarf_stack_op_name (op
);
3280 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3281 op
, (long) (data
- 1 - start
));
3282 fprintf_filtered (stream
, " %*ld: %s", indent
+ 4,
3283 (long) (data
- 1 - start
), name
);
3288 ul
= extract_unsigned_integer (data
, addr_size
,
3289 gdbarch_byte_order (arch
));
3291 fprintf_filtered (stream
, " 0x%s", phex_nz (ul
, addr_size
));
3295 ul
= extract_unsigned_integer (data
, 1, gdbarch_byte_order (arch
));
3297 fprintf_filtered (stream
, " %s", pulongest (ul
));
3300 l
= extract_signed_integer (data
, 1, gdbarch_byte_order (arch
));
3302 fprintf_filtered (stream
, " %s", plongest (l
));
3305 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
3307 fprintf_filtered (stream
, " %s", pulongest (ul
));
3310 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3312 fprintf_filtered (stream
, " %s", plongest (l
));
3315 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3317 fprintf_filtered (stream
, " %s", pulongest (ul
));
3320 l
= extract_signed_integer (data
, 4, gdbarch_byte_order (arch
));
3322 fprintf_filtered (stream
, " %s", plongest (l
));
3325 ul
= extract_unsigned_integer (data
, 8, gdbarch_byte_order (arch
));
3327 fprintf_filtered (stream
, " %s", pulongest (ul
));
3330 l
= extract_signed_integer (data
, 8, gdbarch_byte_order (arch
));
3332 fprintf_filtered (stream
, " %s", plongest (l
));
3335 data
= read_uleb128 (data
, end
, &ul
);
3336 fprintf_filtered (stream
, " %s", pulongest (ul
));
3339 data
= read_sleb128 (data
, end
, &l
);
3340 fprintf_filtered (stream
, " %s", plongest (l
));
3375 fprintf_filtered (stream
, " [$%s]",
3376 locexpr_regname (arch
, op
- DW_OP_reg0
));
3380 data
= read_uleb128 (data
, end
, &ul
);
3381 fprintf_filtered (stream
, " %s [$%s]", pulongest (ul
),
3382 locexpr_regname (arch
, (int) ul
));
3385 case DW_OP_implicit_value
:
3386 data
= read_uleb128 (data
, end
, &ul
);
3388 fprintf_filtered (stream
, " %s", pulongest (ul
));
3423 data
= read_sleb128 (data
, end
, &l
);
3424 fprintf_filtered (stream
, " %s [$%s]", plongest (l
),
3425 locexpr_regname (arch
, op
- DW_OP_breg0
));
3429 data
= read_uleb128 (data
, end
, &ul
);
3430 data
= read_sleb128 (data
, end
, &l
);
3431 fprintf_filtered (stream
, " register %s [$%s] offset %s",
3433 locexpr_regname (arch
, (int) ul
),
3438 data
= read_sleb128 (data
, end
, &l
);
3439 fprintf_filtered (stream
, " %s", plongest (l
));
3442 case DW_OP_xderef_size
:
3443 case DW_OP_deref_size
:
3445 fprintf_filtered (stream
, " %d", *data
);
3449 case DW_OP_plus_uconst
:
3450 data
= read_uleb128 (data
, end
, &ul
);
3451 fprintf_filtered (stream
, " %s", pulongest (ul
));
3455 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3457 fprintf_filtered (stream
, " to %ld",
3458 (long) (data
+ l
- start
));
3462 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3464 fprintf_filtered (stream
, " %ld",
3465 (long) (data
+ l
- start
));
3469 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
3471 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 2));
3475 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3477 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
3480 case DW_OP_call_ref
:
3481 ul
= extract_unsigned_integer (data
, offset_size
,
3482 gdbarch_byte_order (arch
));
3483 data
+= offset_size
;
3484 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, offset_size
));
3488 data
= read_uleb128 (data
, end
, &ul
);
3489 fprintf_filtered (stream
, " %s (bytes)", pulongest (ul
));
3492 case DW_OP_bit_piece
:
3496 data
= read_uleb128 (data
, end
, &ul
);
3497 data
= read_uleb128 (data
, end
, &offset
);
3498 fprintf_filtered (stream
, " size %s offset %s (bits)",
3499 pulongest (ul
), pulongest (offset
));
3503 case DW_OP_GNU_implicit_pointer
:
3505 ul
= extract_unsigned_integer (data
, offset_size
,
3506 gdbarch_byte_order (arch
));
3507 data
+= offset_size
;
3509 data
= read_sleb128 (data
, end
, &l
);
3511 fprintf_filtered (stream
, " DIE %s offset %s",
3512 phex_nz (ul
, offset_size
),
3517 case DW_OP_GNU_deref_type
:
3519 int addr_size
= *data
++;
3523 data
= read_uleb128 (data
, end
, &offset
);
3524 type
= dwarf2_get_die_type (offset
, per_cu
);
3525 fprintf_filtered (stream
, "<");
3526 type_print (type
, "", stream
, -1);
3527 fprintf_filtered (stream
, " [0x%s]> %d", phex_nz (offset
, 0),
3532 case DW_OP_GNU_const_type
:
3537 data
= read_uleb128 (data
, end
, &type_die
);
3538 type
= dwarf2_get_die_type (type_die
, per_cu
);
3539 fprintf_filtered (stream
, "<");
3540 type_print (type
, "", stream
, -1);
3541 fprintf_filtered (stream
, " [0x%s]>", phex_nz (type_die
, 0));
3545 case DW_OP_GNU_regval_type
:
3547 ULONGEST type_die
, reg
;
3550 data
= read_uleb128 (data
, end
, ®
);
3551 data
= read_uleb128 (data
, end
, &type_die
);
3553 type
= dwarf2_get_die_type (type_die
, per_cu
);
3554 fprintf_filtered (stream
, "<");
3555 type_print (type
, "", stream
, -1);
3556 fprintf_filtered (stream
, " [0x%s]> [$%s]", phex_nz (type_die
, 0),
3557 locexpr_regname (arch
, reg
));
3561 case DW_OP_GNU_convert
:
3562 case DW_OP_GNU_reinterpret
:
3566 data
= read_uleb128 (data
, end
, &type_die
);
3569 fprintf_filtered (stream
, "<0>");
3574 type
= dwarf2_get_die_type (type_die
, per_cu
);
3575 fprintf_filtered (stream
, "<");
3576 type_print (type
, "", stream
, -1);
3577 fprintf_filtered (stream
, " [0x%s]>", phex_nz (type_die
, 0));
3582 case DW_OP_GNU_entry_value
:
3583 data
= read_uleb128 (data
, end
, &ul
);
3584 fputc_filtered ('\n', stream
);
3585 disassemble_dwarf_expression (stream
, arch
, addr_size
, offset_size
,
3586 start
, data
, data
+ ul
, indent
+ 2,
3592 fprintf_filtered (stream
, "\n");
3598 /* Describe a single location, which may in turn consist of multiple
3602 locexpr_describe_location_1 (struct symbol
*symbol
, CORE_ADDR addr
,
3603 struct ui_file
*stream
,
3604 const gdb_byte
*data
, int size
,
3605 struct objfile
*objfile
, unsigned int addr_size
,
3606 int offset_size
, struct dwarf2_per_cu_data
*per_cu
)
3608 const gdb_byte
*end
= data
+ size
;
3609 int first_piece
= 1, bad
= 0;
3613 const gdb_byte
*here
= data
;
3614 int disassemble
= 1;
3619 fprintf_filtered (stream
, _(", and "));
3621 if (!dwarf2_always_disassemble
)
3623 data
= locexpr_describe_location_piece (symbol
, stream
,
3625 data
, end
, addr_size
);
3626 /* If we printed anything, or if we have an empty piece,
3627 then don't disassemble. */
3629 || data
[0] == DW_OP_piece
3630 || data
[0] == DW_OP_bit_piece
)
3635 fprintf_filtered (stream
, _("a complex DWARF expression:\n"));
3636 data
= disassemble_dwarf_expression (stream
,
3637 get_objfile_arch (objfile
),
3638 addr_size
, offset_size
, data
,
3640 dwarf2_always_disassemble
,
3646 int empty
= data
== here
;
3649 fprintf_filtered (stream
, " ");
3650 if (data
[0] == DW_OP_piece
)
3654 data
= read_uleb128 (data
+ 1, end
, &bytes
);
3657 fprintf_filtered (stream
, _("an empty %s-byte piece"),
3660 fprintf_filtered (stream
, _(" [%s-byte piece]"),
3663 else if (data
[0] == DW_OP_bit_piece
)
3665 ULONGEST bits
, offset
;
3667 data
= read_uleb128 (data
+ 1, end
, &bits
);
3668 data
= read_uleb128 (data
, end
, &offset
);
3671 fprintf_filtered (stream
,
3672 _("an empty %s-bit piece"),
3675 fprintf_filtered (stream
,
3676 _(" [%s-bit piece, offset %s bits]"),
3677 pulongest (bits
), pulongest (offset
));
3687 if (bad
|| data
> end
)
3688 error (_("Corrupted DWARF2 expression for \"%s\"."),
3689 SYMBOL_PRINT_NAME (symbol
));
3692 /* Print a natural-language description of SYMBOL to STREAM. This
3693 version is for a symbol with a single location. */
3696 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
3697 struct ui_file
*stream
)
3699 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3700 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
3701 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3702 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
3704 locexpr_describe_location_1 (symbol
, addr
, stream
,
3705 dlbaton
->data
, dlbaton
->size
,
3706 objfile
, addr_size
, offset_size
,
3710 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3711 any necessary bytecode in AX. */
3714 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
3715 struct agent_expr
*ax
, struct axs_value
*value
)
3717 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3718 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3720 if (dlbaton
->size
== 0)
3721 value
->optimized_out
= 1;
3723 dwarf2_compile_expr_to_ax (ax
, value
, gdbarch
, addr_size
,
3724 dlbaton
->data
, dlbaton
->data
+ dlbaton
->size
,
3728 /* The set of location functions used with the DWARF-2 expression
3730 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
3731 locexpr_read_variable
,
3732 locexpr_read_variable_at_entry
,
3733 locexpr_read_needs_frame
,
3734 locexpr_describe_location
,
3735 locexpr_tracepoint_var_ref
3739 /* Wrapper functions for location lists. These generally find
3740 the appropriate location expression and call something above. */
3742 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3743 evaluator to calculate the location. */
3744 static struct value
*
3745 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
3747 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3749 const gdb_byte
*data
;
3751 CORE_ADDR pc
= frame
? get_frame_address_in_block (frame
) : 0;
3753 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
3754 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, data
, size
,
3760 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
3761 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3764 Function always returns non-NULL value, it may be marked optimized out if
3765 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
3766 if it cannot resolve the parameter for any reason. */
3768 static struct value
*
3769 loclist_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
3771 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3772 const gdb_byte
*data
;
3776 if (frame
== NULL
|| !get_frame_func_if_available (frame
, &pc
))
3777 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
3779 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
3781 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
3783 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, data
, size
);
3786 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3788 loclist_read_needs_frame (struct symbol
*symbol
)
3790 /* If there's a location list, then assume we need to have a frame
3791 to choose the appropriate location expression. With tracking of
3792 global variables this is not necessarily true, but such tracking
3793 is disabled in GCC at the moment until we figure out how to
3799 /* Print a natural-language description of SYMBOL to STREAM. This
3800 version applies when there is a list of different locations, each
3801 with a specified address range. */
3804 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
3805 struct ui_file
*stream
)
3807 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3808 CORE_ADDR low
, high
;
3809 const gdb_byte
*loc_ptr
, *buf_end
;
3810 int length
, first
= 1;
3811 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
3812 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3813 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
3814 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3815 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
3816 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
3817 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
3818 /* Adjust base_address for relocatable objects. */
3819 CORE_ADDR base_offset
= dwarf2_per_cu_text_offset (dlbaton
->per_cu
);
3820 CORE_ADDR base_address
= dlbaton
->base_address
+ base_offset
;
3822 loc_ptr
= dlbaton
->data
;
3823 buf_end
= dlbaton
->data
+ dlbaton
->size
;
3825 fprintf_filtered (stream
, _("multi-location:\n"));
3827 /* Iterate through locations until we run out. */
3830 if (buf_end
- loc_ptr
< 2 * addr_size
)
3831 error (_("Corrupted DWARF expression for symbol \"%s\"."),
3832 SYMBOL_PRINT_NAME (symbol
));
3835 low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
3837 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
3838 loc_ptr
+= addr_size
;
3841 high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
3843 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
3844 loc_ptr
+= addr_size
;
3846 /* A base-address-selection entry. */
3847 if ((low
& base_mask
) == base_mask
)
3849 base_address
= high
+ base_offset
;
3850 fprintf_filtered (stream
, _(" Base address %s"),
3851 paddress (gdbarch
, base_address
));
3855 /* An end-of-list entry. */
3856 if (low
== 0 && high
== 0)
3859 /* Otherwise, a location expression entry. */
3860 low
+= base_address
;
3861 high
+= base_address
;
3863 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
3866 /* (It would improve readability to print only the minimum
3867 necessary digits of the second number of the range.) */
3868 fprintf_filtered (stream
, _(" Range %s-%s: "),
3869 paddress (gdbarch
, low
), paddress (gdbarch
, high
));
3871 /* Now describe this particular location. */
3872 locexpr_describe_location_1 (symbol
, low
, stream
, loc_ptr
, length
,
3873 objfile
, addr_size
, offset_size
,
3876 fprintf_filtered (stream
, "\n");
3882 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3883 any necessary bytecode in AX. */
3885 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
3886 struct agent_expr
*ax
, struct axs_value
*value
)
3888 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3889 const gdb_byte
*data
;
3891 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3893 data
= dwarf2_find_location_expression (dlbaton
, &size
, ax
->scope
);
3895 value
->optimized_out
= 1;
3897 dwarf2_compile_expr_to_ax (ax
, value
, gdbarch
, addr_size
, data
, data
+ size
,
3901 /* The set of location functions used with the DWARF-2 expression
3902 evaluator and location lists. */
3903 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
3904 loclist_read_variable
,
3905 loclist_read_variable_at_entry
,
3906 loclist_read_needs_frame
,
3907 loclist_describe_location
,
3908 loclist_tracepoint_var_ref
3911 /* Provide a prototype to silence -Wmissing-prototypes. */
3912 extern initialize_file_ftype _initialize_dwarf2loc
;
3915 _initialize_dwarf2loc (void)
3917 add_setshow_zinteger_cmd ("entry-values", class_maintenance
,
3918 &entry_values_debug
,
3919 _("Set entry values and tail call frames "
3921 _("Show entry values and tail call frames "
3923 _("When non-zero, the process of determining "
3924 "parameter values from function entry point "
3925 "and tail call frames will be printed."),
3927 show_entry_values_debug
,
3928 &setdebuglist
, &showdebuglist
);