1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
34 #include "exceptions.h"
39 #include "dwarf2expr.h"
40 #include "dwarf2loc.h"
41 #include "dwarf2-frame.h"
43 #include "gdb_string.h"
44 #include "gdb_assert.h"
46 extern int dwarf2_always_disassemble;
48 static void dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
49 const gdb_byte **start, size_t *length);
51 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
53 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
54 struct frame_info *frame,
57 struct dwarf2_per_cu_data *per_cu,
60 /* A function for dealing with location lists. Given a
61 symbol baton (BATON) and a pc value (PC), find the appropriate
62 location expression, set *LOCEXPR_LENGTH, and return a pointer
63 to the beginning of the expression. Returns NULL on failure.
65 For now, only return the first matching location expression; there
66 can be more than one in the list. */
69 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
70 size_t *locexpr_length, CORE_ADDR pc)
73 const gdb_byte *loc_ptr, *buf_end;
75 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
76 struct gdbarch *gdbarch = get_objfile_arch (objfile);
77 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
78 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
79 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
80 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
81 /* Adjust base_address for relocatable objects. */
82 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
83 CORE_ADDR base_address = baton->base_address + base_offset;
85 loc_ptr = baton->data;
86 buf_end = baton->data + baton->size;
90 if (buf_end - loc_ptr < 2 * addr_size)
91 error (_("dwarf2_find_location_expression: "
92 "Corrupted DWARF expression."));
95 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
97 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
101 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
103 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
104 loc_ptr += addr_size;
106 /* A base-address-selection entry. */
107 if ((low & base_mask) == base_mask)
109 base_address = high + base_offset;
113 /* An end-of-list entry. */
114 if (low == 0 && high == 0)
117 /* Otherwise, a location expression entry. */
119 high += base_address;
121 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
124 if (low == high && pc == low)
126 /* This is entry PC record present only at entry point
127 of a function. Verify it is really the function entry point. */
129 struct block *pc_block = block_for_pc (pc);
130 struct symbol *pc_func = NULL;
133 pc_func = block_linkage_function (pc_block);
135 if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
137 *locexpr_length = length;
142 if (pc >= low && pc < high)
144 *locexpr_length = length;
152 /* This is the baton used when performing dwarf2 expression
154 struct dwarf_expr_baton
156 struct frame_info *frame;
157 struct dwarf2_per_cu_data *per_cu;
160 /* Helper functions for dwarf2_evaluate_loc_desc. */
162 /* Using the frame specified in BATON, return the value of register
163 REGNUM, treated as a pointer. */
165 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
167 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
168 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
172 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
173 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
174 regnum, debaton->frame);
178 /* Read memory at ADDR (length LEN) into BUF. */
181 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
183 read_memory (addr, buf, len);
186 /* Using the frame specified in BATON, find the location expression
187 describing the frame base. Return a pointer to it in START and
188 its length in LENGTH. */
190 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
192 /* FIXME: cagney/2003-03-26: This code should be using
193 get_frame_base_address(), and then implement a dwarf2 specific
195 struct symbol *framefunc;
196 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
198 /* Use block_linkage_function, which returns a real (not inlined)
199 function, instead of get_frame_function, which may return an
201 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
203 /* If we found a frame-relative symbol then it was certainly within
204 some function associated with a frame. If we can't find the frame,
205 something has gone wrong. */
206 gdb_assert (framefunc != NULL);
208 dwarf_expr_frame_base_1 (framefunc,
209 get_frame_address_in_block (debaton->frame),
214 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
215 const gdb_byte **start, size_t *length)
217 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
219 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
221 struct dwarf2_loclist_baton *symbaton;
223 symbaton = SYMBOL_LOCATION_BATON (framefunc);
224 *start = dwarf2_find_location_expression (symbaton, length, pc);
228 struct dwarf2_locexpr_baton *symbaton;
230 symbaton = SYMBOL_LOCATION_BATON (framefunc);
231 if (symbaton != NULL)
233 *length = symbaton->size;
234 *start = symbaton->data;
241 error (_("Could not find the frame base for \"%s\"."),
242 SYMBOL_NATURAL_NAME (framefunc));
245 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
246 the frame in BATON. */
249 dwarf_expr_frame_cfa (void *baton)
251 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
253 return dwarf2_frame_cfa (debaton->frame);
256 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
257 the frame in BATON. */
260 dwarf_expr_frame_pc (void *baton)
262 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
264 return get_frame_address_in_block (debaton->frame);
267 /* Using the objfile specified in BATON, find the address for the
268 current thread's thread-local storage with offset OFFSET. */
270 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
272 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
273 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
275 return target_translate_tls_address (objfile, offset);
278 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
279 current CU (as is PER_CU). State of the CTX is not affected by the
283 per_cu_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset,
284 struct dwarf2_per_cu_data *per_cu,
285 CORE_ADDR (*get_frame_pc) (void *baton),
288 struct dwarf2_locexpr_baton block;
290 block = dwarf2_fetch_die_location_block (die_offset, per_cu,
291 get_frame_pc, baton);
293 /* DW_OP_call_ref is currently not supported. */
294 gdb_assert (block.per_cu == per_cu);
296 dwarf_expr_eval (ctx, block.data, block.size);
299 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
302 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
304 struct dwarf_expr_baton *debaton = ctx->baton;
306 per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
307 ctx->funcs->get_frame_pc, ctx->baton);
310 /* Callback function for dwarf2_evaluate_loc_desc. */
313 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx, size_t die_offset)
315 struct dwarf_expr_baton *debaton = ctx->baton;
317 return dwarf2_get_die_type (die_offset, debaton->per_cu);
320 /* See dwarf2loc.h. */
322 int entry_values_debug = 0;
324 /* Helper to set entry_values_debug. */
327 show_entry_values_debug (struct ui_file *file, int from_tty,
328 struct cmd_list_element *c, const char *value)
330 fprintf_filtered (file,
331 _("Entry values and tail call frames debugging is %s.\n"),
335 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
336 CALLER_FRAME (for registers) can be NULL if it is not known. This function
337 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
340 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
341 struct call_site *call_site,
342 struct frame_info *caller_frame)
344 switch (FIELD_LOC_KIND (call_site->target))
346 case FIELD_LOC_KIND_DWARF_BLOCK:
348 struct dwarf2_locexpr_baton *dwarf_block;
350 struct type *caller_core_addr_type;
351 struct gdbarch *caller_arch;
353 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
354 if (dwarf_block == NULL)
356 struct minimal_symbol *msym;
358 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
359 throw_error (NO_ENTRY_VALUE_ERROR,
360 _("DW_AT_GNU_call_site_target is not specified "
362 paddress (call_site_gdbarch, call_site->pc),
363 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
366 if (caller_frame == NULL)
368 struct minimal_symbol *msym;
370 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
371 throw_error (NO_ENTRY_VALUE_ERROR,
372 _("DW_AT_GNU_call_site_target DWARF block resolving "
373 "requires known frame which is currently not "
374 "available at %s in %s"),
375 paddress (call_site_gdbarch, call_site->pc),
376 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
379 caller_arch = get_frame_arch (caller_frame);
380 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
381 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
382 dwarf_block->data, dwarf_block->size,
383 dwarf_block->per_cu);
384 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
386 if (VALUE_LVAL (val) == lval_memory)
387 return value_address (val);
389 return value_as_address (val);
392 case FIELD_LOC_KIND_PHYSNAME:
394 const char *physname;
395 struct minimal_symbol *msym;
397 physname = FIELD_STATIC_PHYSNAME (call_site->target);
398 msym = lookup_minimal_symbol_text (physname, NULL);
401 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
402 throw_error (NO_ENTRY_VALUE_ERROR,
403 _("Cannot find function \"%s\" for a call site target "
405 physname, paddress (call_site_gdbarch, call_site->pc),
406 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
409 return SYMBOL_VALUE_ADDRESS (msym);
412 case FIELD_LOC_KIND_PHYSADDR:
413 return FIELD_STATIC_PHYSADDR (call_site->target);
416 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
420 /* Convert function entry point exact address ADDR to the function which is
421 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
422 NO_ENTRY_VALUE_ERROR otherwise. */
424 static struct symbol *
425 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
427 struct symbol *sym = find_pc_function (addr);
430 if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
431 throw_error (NO_ENTRY_VALUE_ERROR,
432 _("DW_TAG_GNU_call_site resolving failed to find function "
433 "name for address %s"),
434 paddress (gdbarch, addr));
436 type = SYMBOL_TYPE (sym);
437 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
438 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
443 /* Define VEC (CORE_ADDR) functions. */
444 DEF_VEC_I (CORE_ADDR);
446 /* Verify function with entry point exact address ADDR can never call itself
447 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
448 can call itself via tail calls.
450 If a funtion can tail call itself its entry value based parameters are
451 unreliable. There is no verification whether the value of some/all
452 parameters is unchanged through the self tail call, we expect if there is
453 a self tail call all the parameters can be modified. */
456 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
458 struct obstack addr_obstack;
459 struct cleanup *old_chain;
462 /* Track here CORE_ADDRs which were already visited. */
465 /* The verification is completely unordered. Track here function addresses
466 which still need to be iterated. */
467 VEC (CORE_ADDR) *todo = NULL;
469 obstack_init (&addr_obstack);
470 old_chain = make_cleanup_obstack_free (&addr_obstack);
471 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
472 &addr_obstack, hashtab_obstack_allocate,
474 make_cleanup_htab_delete (addr_hash);
476 make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
478 VEC_safe_push (CORE_ADDR, todo, verify_addr);
479 while (!VEC_empty (CORE_ADDR, todo))
481 struct symbol *func_sym;
482 struct call_site *call_site;
484 addr = VEC_pop (CORE_ADDR, todo);
486 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
488 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
489 call_site; call_site = call_site->tail_call_next)
491 CORE_ADDR target_addr;
494 /* CALLER_FRAME with registers is not available for tail-call jumped
496 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
498 if (target_addr == verify_addr)
500 struct minimal_symbol *msym;
502 msym = lookup_minimal_symbol_by_pc (verify_addr);
503 throw_error (NO_ENTRY_VALUE_ERROR,
504 _("DW_OP_GNU_entry_value resolving has found "
505 "function \"%s\" at %s can call itself via tail "
507 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
508 paddress (gdbarch, verify_addr));
511 slot = htab_find_slot (addr_hash, &target_addr, INSERT);
514 *slot = obstack_copy (&addr_obstack, &target_addr,
515 sizeof (target_addr));
516 VEC_safe_push (CORE_ADDR, todo, target_addr);
521 do_cleanups (old_chain);
524 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
525 ENTRY_VALUES_DEBUG. */
528 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
530 CORE_ADDR addr = call_site->pc;
531 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (addr - 1);
533 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
534 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
538 /* vec.h needs single word type name, typedef it. */
539 typedef struct call_site *call_sitep;
541 /* Define VEC (call_sitep) functions. */
542 DEF_VEC_P (call_sitep);
544 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
545 only top callers and bottom callees which are present in both. GDBARCH is
546 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
547 no remaining possibilities to provide unambiguous non-trivial result.
548 RESULTP should point to NULL on the first (initialization) call. Caller is
549 responsible for xfree of any RESULTP data. */
552 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
553 VEC (call_sitep) *chain)
555 struct call_site_chain *result = *resultp;
556 long length = VEC_length (call_sitep, chain);
557 int callers, callees, idx;
561 /* Create the initial chain containing all the passed PCs. */
563 result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
565 result->length = length;
566 result->callers = result->callees = length;
567 memcpy (result->call_site, VEC_address (call_sitep, chain),
568 sizeof (*result->call_site) * length);
571 if (entry_values_debug)
573 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
574 for (idx = 0; idx < length; idx++)
575 tailcall_dump (gdbarch, result->call_site[idx]);
576 fputc_unfiltered ('\n', gdb_stdlog);
582 if (entry_values_debug)
584 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
585 for (idx = 0; idx < length; idx++)
586 tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
587 fputc_unfiltered ('\n', gdb_stdlog);
590 /* Intersect callers. */
592 callers = min (result->callers, length);
593 for (idx = 0; idx < callers; idx++)
594 if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
596 result->callers = idx;
600 /* Intersect callees. */
602 callees = min (result->callees, length);
603 for (idx = 0; idx < callees; idx++)
604 if (result->call_site[result->length - 1 - idx]
605 != VEC_index (call_sitep, chain, length - 1 - idx))
607 result->callees = idx;
611 if (entry_values_debug)
613 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
614 for (idx = 0; idx < result->callers; idx++)
615 tailcall_dump (gdbarch, result->call_site[idx]);
616 fputs_unfiltered (" |", gdb_stdlog);
617 for (idx = 0; idx < result->callees; idx++)
618 tailcall_dump (gdbarch, result->call_site[result->length
619 - result->callees + idx]);
620 fputc_unfiltered ('\n', gdb_stdlog);
623 if (result->callers == 0 && result->callees == 0)
625 /* There are no common callers or callees. It could be also a direct
626 call (which has length 0) with ambiguous possibility of an indirect
627 call - CALLERS == CALLEES == 0 is valid during the first allocation
628 but any subsequence processing of such entry means ambiguity. */
634 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
635 PC again. In such case there must be two different code paths to reach
636 it, therefore some of the former determined intermediate PCs must differ
637 and the unambiguous chain gets shortened. */
638 gdb_assert (result->callers + result->callees < result->length);
641 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
642 assumed frames between them use GDBARCH. Use depth first search so we can
643 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
644 would have needless GDB stack overhead. Caller is responsible for xfree of
645 the returned result. Any unreliability results in thrown
646 NO_ENTRY_VALUE_ERROR. */
648 static struct call_site_chain *
649 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
652 struct func_type *func_specific;
653 struct obstack addr_obstack;
654 struct cleanup *back_to_retval, *back_to_workdata;
655 struct call_site_chain *retval = NULL;
656 struct call_site *call_site;
658 /* Mark CALL_SITEs so we do not visit the same ones twice. */
661 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
662 call_site nor any possible call_site at CALLEE_PC's function is there.
663 Any CALL_SITE in CHAIN will be iterated to its siblings - via
664 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
665 VEC (call_sitep) *chain = NULL;
667 /* We are not interested in the specific PC inside the callee function. */
668 callee_pc = get_pc_function_start (callee_pc);
670 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
671 paddress (gdbarch, callee_pc));
673 back_to_retval = make_cleanup (free_current_contents, &retval);
675 obstack_init (&addr_obstack);
676 back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
677 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
678 &addr_obstack, hashtab_obstack_allocate,
680 make_cleanup_htab_delete (addr_hash);
682 make_cleanup (VEC_cleanup (call_sitep), &chain);
684 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
685 at the target's function. All the possible tail call sites in the
686 target's function will get iterated as already pushed into CHAIN via their
688 call_site = call_site_for_pc (gdbarch, caller_pc);
692 CORE_ADDR target_func_addr;
693 struct call_site *target_call_site;
695 /* CALLER_FRAME with registers is not available for tail-call jumped
697 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
699 if (target_func_addr == callee_pc)
701 chain_candidate (gdbarch, &retval, chain);
705 /* There is no way to reach CALLEE_PC again as we would prevent
706 entering it twice as being already marked in ADDR_HASH. */
707 target_call_site = NULL;
711 struct symbol *target_func;
713 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
714 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
719 /* Attempt to visit TARGET_CALL_SITE. */
721 if (target_call_site)
725 slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
728 /* Successfully entered TARGET_CALL_SITE. */
730 *slot = &target_call_site->pc;
731 VEC_safe_push (call_sitep, chain, target_call_site);
736 /* Backtrack (without revisiting the originating call_site). Try the
737 callers's sibling; if there isn't any try the callers's callers's
740 target_call_site = NULL;
741 while (!VEC_empty (call_sitep, chain))
743 call_site = VEC_pop (call_sitep, chain);
745 gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
747 htab_remove_elt (addr_hash, &call_site->pc);
749 target_call_site = call_site->tail_call_next;
750 if (target_call_site)
754 while (target_call_site);
756 if (VEC_empty (call_sitep, chain))
759 call_site = VEC_last (call_sitep, chain);
764 struct minimal_symbol *msym_caller, *msym_callee;
766 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
767 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
768 throw_error (NO_ENTRY_VALUE_ERROR,
769 _("There are no unambiguously determinable intermediate "
770 "callers or callees between caller function \"%s\" at %s "
771 "and callee function \"%s\" at %s"),
773 ? "???" : SYMBOL_PRINT_NAME (msym_caller)),
774 paddress (gdbarch, caller_pc),
776 ? "???" : SYMBOL_PRINT_NAME (msym_callee)),
777 paddress (gdbarch, callee_pc));
780 do_cleanups (back_to_workdata);
781 discard_cleanups (back_to_retval);
785 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
786 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
787 constructed return NULL. Caller is responsible for xfree of the returned
790 struct call_site_chain *
791 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
794 volatile struct gdb_exception e;
795 struct call_site_chain *retval = NULL;
797 TRY_CATCH (e, RETURN_MASK_ERROR)
799 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
803 if (e.error == NO_ENTRY_VALUE_ERROR)
805 if (entry_values_debug)
806 exception_print (gdb_stdout, e);
816 /* Fetch call_site_parameter from caller matching the parameters. FRAME is for
817 callee. See DWARF_REG and FB_OFFSET description at struct
818 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
820 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
823 static struct call_site_parameter *
824 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame, int dwarf_reg,
826 struct dwarf2_per_cu_data **per_cu_return)
828 CORE_ADDR func_addr = get_frame_func (frame);
830 struct gdbarch *gdbarch = get_frame_arch (frame);
831 struct frame_info *caller_frame = get_prev_frame (frame);
832 struct call_site *call_site;
835 struct dwarf2_locexpr_baton *dwarf_block;
836 struct call_site_parameter *parameter;
837 CORE_ADDR target_addr;
839 if (gdbarch != frame_unwind_arch (frame))
841 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
842 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
844 throw_error (NO_ENTRY_VALUE_ERROR,
845 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
846 "(of %s (%s)) does not match caller gdbarch %s"),
847 gdbarch_bfd_arch_info (gdbarch)->printable_name,
848 paddress (gdbarch, func_addr),
849 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
850 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
853 if (caller_frame == NULL)
855 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
857 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
858 "requires caller of %s (%s)"),
859 paddress (gdbarch, func_addr),
860 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
862 caller_pc = get_frame_pc (caller_frame);
863 call_site = call_site_for_pc (gdbarch, caller_pc);
865 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
866 if (target_addr != func_addr)
868 struct minimal_symbol *target_msym, *func_msym;
870 target_msym = lookup_minimal_symbol_by_pc (target_addr);
871 func_msym = lookup_minimal_symbol_by_pc (func_addr);
872 throw_error (NO_ENTRY_VALUE_ERROR,
873 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
874 "but the called frame is for %s at %s"),
875 (target_msym == NULL ? "???"
876 : SYMBOL_PRINT_NAME (target_msym)),
877 paddress (gdbarch, target_addr),
878 func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym),
879 paddress (gdbarch, func_addr));
882 /* No entry value based parameters would be reliable if this function can
883 call itself via tail calls. */
884 func_verify_no_selftailcall (gdbarch, func_addr);
886 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
888 parameter = &call_site->parameter[iparams];
889 if (parameter->dwarf_reg == -1 && dwarf_reg == -1)
891 if (parameter->fb_offset == fb_offset)
894 else if (parameter->dwarf_reg == dwarf_reg)
897 if (iparams == call_site->parameter_count)
899 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (caller_pc);
901 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
902 determine its value. */
903 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
904 "at DW_TAG_GNU_call_site %s at %s"),
905 paddress (gdbarch, caller_pc),
906 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
909 *per_cu_return = call_site->per_cu;
913 /* Return value for PARAMETER for DW_AT_GNU_call_site_value.
915 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
918 Function always returns non-NULL, non-optimized out value. It throws
919 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
921 static struct value *
922 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
924 struct frame_info *caller_frame,
925 struct dwarf2_per_cu_data *per_cu)
929 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
930 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
932 data = alloca (parameter->value_size + 1);
933 memcpy (data, parameter->value, parameter->value_size);
934 data[parameter->value_size] = DW_OP_stack_value;
936 return dwarf2_evaluate_loc_desc (type, caller_frame, data,
937 parameter->value_size + 1, per_cu);
940 /* Execute call_site_parameter's DWARF block for caller of the CTX's frame.
941 CTX must be of dwarf_expr_ctx_funcs kind. See DWARF_REG and FB_OFFSET
942 description at struct dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
944 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
945 can be more simple as it does not support cross-CU DWARF executions. */
948 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
949 int dwarf_reg, CORE_ADDR fb_offset)
951 struct dwarf_expr_baton *debaton;
952 struct frame_info *frame, *caller_frame;
953 struct dwarf2_per_cu_data *caller_per_cu;
954 struct dwarf_expr_baton baton_local;
955 struct dwarf_expr_context saved_ctx;
956 struct call_site_parameter *parameter;
957 const gdb_byte *data_src;
960 gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
961 debaton = ctx->baton;
962 frame = debaton->frame;
963 caller_frame = get_prev_frame (frame);
965 parameter = dwarf_expr_reg_to_entry_parameter (frame, dwarf_reg, fb_offset,
967 data_src = parameter->value;
968 size = parameter->value_size;
970 baton_local.frame = caller_frame;
971 baton_local.per_cu = caller_per_cu;
973 saved_ctx.gdbarch = ctx->gdbarch;
974 saved_ctx.addr_size = ctx->addr_size;
975 saved_ctx.offset = ctx->offset;
976 saved_ctx.baton = ctx->baton;
977 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
978 ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
979 ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
980 ctx->baton = &baton_local;
982 dwarf_expr_eval (ctx, data_src, size);
984 ctx->gdbarch = saved_ctx.gdbarch;
985 ctx->addr_size = saved_ctx.addr_size;
986 ctx->offset = saved_ctx.offset;
987 ctx->baton = saved_ctx.baton;
990 /* Read parameter of TYPE at (callee) FRAME's function entry. DWARF_REG and
991 FB_OFFSET are used to match DW_AT_location at the caller's
992 DW_TAG_GNU_call_site_parameter. See DWARF_REG and FB_OFFSET description at
993 struct dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
995 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
996 cannot resolve the parameter for any reason. */
998 static struct value *
999 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1000 int dwarf_reg, CORE_ADDR fb_offset)
1002 struct frame_info *caller_frame = get_prev_frame (frame);
1003 struct call_site_parameter *parameter;
1004 struct dwarf2_per_cu_data *caller_per_cu;
1006 parameter = dwarf_expr_reg_to_entry_parameter (frame, dwarf_reg, fb_offset,
1009 return dwarf_entry_parameter_to_value (parameter, type, caller_frame,
1013 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1014 SIZE are DWARF block used to match DW_AT_location at the caller's
1015 DW_TAG_GNU_call_site_parameter.
1017 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1018 cannot resolve the parameter for any reason. */
1020 static struct value *
1021 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1022 const gdb_byte *block, size_t block_len)
1025 CORE_ADDR fb_offset;
1027 dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1028 if (dwarf_reg != -1)
1029 return value_of_dwarf_reg_entry (type, frame, dwarf_reg, 0 /* unused */);
1031 if (dwarf_block_to_fb_offset (block, block + block_len, &fb_offset))
1032 return value_of_dwarf_reg_entry (type, frame, -1, fb_offset);
1034 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1035 suppressed during normal operation. The expression can be arbitrary if
1036 there is no caller-callee entry value binding expected. */
1037 throw_error (NO_ENTRY_VALUE_ERROR,
1038 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1039 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1042 struct piece_closure
1044 /* Reference count. */
1047 /* The CU from which this closure's expression came. */
1048 struct dwarf2_per_cu_data *per_cu;
1050 /* The number of pieces used to describe this variable. */
1053 /* The target address size, used only for DWARF_VALUE_STACK. */
1056 /* The pieces themselves. */
1057 struct dwarf_expr_piece *pieces;
1060 /* Allocate a closure for a value formed from separately-described
1063 static struct piece_closure *
1064 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1065 int n_pieces, struct dwarf_expr_piece *pieces,
1068 struct piece_closure *c = XZALLOC (struct piece_closure);
1073 c->n_pieces = n_pieces;
1074 c->addr_size = addr_size;
1075 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
1077 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1078 for (i = 0; i < n_pieces; ++i)
1079 if (c->pieces[i].location == DWARF_VALUE_STACK)
1080 value_incref (c->pieces[i].v.value);
1085 /* The lowest-level function to extract bits from a byte buffer.
1086 SOURCE is the buffer. It is updated if we read to the end of a
1088 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1089 updated to reflect the number of bits actually read.
1090 NBITS is the number of bits we want to read. It is updated to
1091 reflect the number of bits actually read. This function may read
1093 BITS_BIG_ENDIAN is taken directly from gdbarch.
1094 This function returns the extracted bits. */
1097 extract_bits_primitive (const gdb_byte **source,
1098 unsigned int *source_offset_bits,
1099 int *nbits, int bits_big_endian)
1101 unsigned int avail, mask, datum;
1103 gdb_assert (*source_offset_bits < 8);
1105 avail = 8 - *source_offset_bits;
1109 mask = (1 << avail) - 1;
1111 if (bits_big_endian)
1112 datum >>= 8 - (*source_offset_bits + *nbits);
1114 datum >>= *source_offset_bits;
1118 *source_offset_bits += avail;
1119 if (*source_offset_bits >= 8)
1121 *source_offset_bits -= 8;
1128 /* Extract some bits from a source buffer and move forward in the
1131 SOURCE is the source buffer. It is updated as bytes are read.
1132 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1134 NBITS is the number of bits to read.
1135 BITS_BIG_ENDIAN is taken directly from gdbarch.
1137 This function returns the bits that were read. */
1140 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1141 int nbits, int bits_big_endian)
1145 gdb_assert (nbits > 0 && nbits <= 8);
1147 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1153 more = extract_bits_primitive (source, source_offset_bits, &nbits,
1155 if (bits_big_endian)
1165 /* Write some bits into a buffer and move forward in the buffer.
1167 DATUM is the bits to write. The low-order bits of DATUM are used.
1168 DEST is the destination buffer. It is updated as bytes are
1170 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1172 NBITS is the number of valid bits in DATUM.
1173 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1176 insert_bits (unsigned int datum,
1177 gdb_byte *dest, unsigned int dest_offset_bits,
1178 int nbits, int bits_big_endian)
1182 gdb_assert (dest_offset_bits + nbits <= 8);
1184 mask = (1 << nbits) - 1;
1185 if (bits_big_endian)
1187 datum <<= 8 - (dest_offset_bits + nbits);
1188 mask <<= 8 - (dest_offset_bits + nbits);
1192 datum <<= dest_offset_bits;
1193 mask <<= dest_offset_bits;
1196 gdb_assert ((datum & ~mask) == 0);
1198 *dest = (*dest & ~mask) | datum;
1201 /* Copy bits from a source to a destination.
1203 DEST is where the bits should be written.
1204 DEST_OFFSET_BITS is the bit offset into DEST.
1205 SOURCE is the source of bits.
1206 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1207 BIT_COUNT is the number of bits to copy.
1208 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1211 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1212 const gdb_byte *source, unsigned int source_offset_bits,
1213 unsigned int bit_count,
1214 int bits_big_endian)
1216 unsigned int dest_avail;
1219 /* Reduce everything to byte-size pieces. */
1220 dest += dest_offset_bits / 8;
1221 dest_offset_bits %= 8;
1222 source += source_offset_bits / 8;
1223 source_offset_bits %= 8;
1225 dest_avail = 8 - dest_offset_bits % 8;
1227 /* See if we can fill the first destination byte. */
1228 if (dest_avail < bit_count)
1230 datum = extract_bits (&source, &source_offset_bits, dest_avail,
1232 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1234 dest_offset_bits = 0;
1235 bit_count -= dest_avail;
1238 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1239 than 8 bits remaining. */
1240 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1241 for (; bit_count >= 8; bit_count -= 8)
1243 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1244 *dest++ = (gdb_byte) datum;
1247 /* Finally, we may have a few leftover bits. */
1248 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1251 datum = extract_bits (&source, &source_offset_bits, bit_count,
1253 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1258 read_pieced_value (struct value *v)
1262 ULONGEST bits_to_skip;
1264 struct piece_closure *c
1265 = (struct piece_closure *) value_computed_closure (v);
1266 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1268 size_t buffer_size = 0;
1269 char *buffer = NULL;
1270 struct cleanup *cleanup;
1272 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1274 if (value_type (v) != value_enclosing_type (v))
1275 internal_error (__FILE__, __LINE__,
1276 _("Should not be able to create a lazy value with "
1277 "an enclosing type"));
1279 cleanup = make_cleanup (free_current_contents, &buffer);
1281 contents = value_contents_raw (v);
1282 bits_to_skip = 8 * value_offset (v);
1283 if (value_bitsize (v))
1285 bits_to_skip += value_bitpos (v);
1286 type_len = value_bitsize (v);
1289 type_len = 8 * TYPE_LENGTH (value_type (v));
1291 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1293 struct dwarf_expr_piece *p = &c->pieces[i];
1294 size_t this_size, this_size_bits;
1295 long dest_offset_bits, source_offset_bits, source_offset;
1296 const gdb_byte *intermediate_buffer;
1298 /* Compute size, source, and destination offsets for copying, in
1300 this_size_bits = p->size;
1301 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1303 bits_to_skip -= this_size_bits;
1306 if (this_size_bits > type_len - offset)
1307 this_size_bits = type_len - offset;
1308 if (bits_to_skip > 0)
1310 dest_offset_bits = 0;
1311 source_offset_bits = bits_to_skip;
1312 this_size_bits -= bits_to_skip;
1317 dest_offset_bits = offset;
1318 source_offset_bits = 0;
1321 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1322 source_offset = source_offset_bits / 8;
1323 if (buffer_size < this_size)
1325 buffer_size = this_size;
1326 buffer = xrealloc (buffer, buffer_size);
1328 intermediate_buffer = buffer;
1330 /* Copy from the source to DEST_BUFFER. */
1331 switch (p->location)
1333 case DWARF_VALUE_REGISTER:
1335 struct gdbarch *arch = get_frame_arch (frame);
1336 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1337 int reg_offset = source_offset;
1339 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1340 && this_size < register_size (arch, gdb_regnum))
1342 /* Big-endian, and we want less than full size. */
1343 reg_offset = register_size (arch, gdb_regnum) - this_size;
1344 /* We want the lower-order THIS_SIZE_BITS of the bytes
1345 we extract from the register. */
1346 source_offset_bits += 8 * this_size - this_size_bits;
1349 if (gdb_regnum != -1)
1353 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1357 /* Just so garbage doesn't ever shine through. */
1358 memset (buffer, 0, this_size);
1361 set_value_optimized_out (v, 1);
1363 mark_value_bytes_unavailable (v, offset, this_size);
1368 error (_("Unable to access DWARF register number %s"),
1369 paddress (arch, p->v.regno));
1374 case DWARF_VALUE_MEMORY:
1375 read_value_memory (v, offset,
1376 p->v.mem.in_stack_memory,
1377 p->v.mem.addr + source_offset,
1381 case DWARF_VALUE_STACK:
1383 size_t n = this_size;
1385 if (n > c->addr_size - source_offset)
1386 n = (c->addr_size >= source_offset
1387 ? c->addr_size - source_offset
1395 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1397 intermediate_buffer = val_bytes + source_offset;
1402 case DWARF_VALUE_LITERAL:
1404 size_t n = this_size;
1406 if (n > p->v.literal.length - source_offset)
1407 n = (p->v.literal.length >= source_offset
1408 ? p->v.literal.length - source_offset
1411 intermediate_buffer = p->v.literal.data + source_offset;
1415 /* These bits show up as zeros -- but do not cause the value
1416 to be considered optimized-out. */
1417 case DWARF_VALUE_IMPLICIT_POINTER:
1420 case DWARF_VALUE_OPTIMIZED_OUT:
1421 set_value_optimized_out (v, 1);
1425 internal_error (__FILE__, __LINE__, _("invalid location type"));
1428 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1429 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1430 copy_bitwise (contents, dest_offset_bits,
1431 intermediate_buffer, source_offset_bits % 8,
1432 this_size_bits, bits_big_endian);
1434 offset += this_size_bits;
1437 do_cleanups (cleanup);
1441 write_pieced_value (struct value *to, struct value *from)
1445 ULONGEST bits_to_skip;
1446 const gdb_byte *contents;
1447 struct piece_closure *c
1448 = (struct piece_closure *) value_computed_closure (to);
1449 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1451 size_t buffer_size = 0;
1452 char *buffer = NULL;
1453 struct cleanup *cleanup;
1455 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1459 set_value_optimized_out (to, 1);
1463 cleanup = make_cleanup (free_current_contents, &buffer);
1465 contents = value_contents (from);
1466 bits_to_skip = 8 * value_offset (to);
1467 if (value_bitsize (to))
1469 bits_to_skip += value_bitpos (to);
1470 type_len = value_bitsize (to);
1473 type_len = 8 * TYPE_LENGTH (value_type (to));
1475 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1477 struct dwarf_expr_piece *p = &c->pieces[i];
1478 size_t this_size_bits, this_size;
1479 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1481 const gdb_byte *source_buffer;
1483 this_size_bits = p->size;
1484 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1486 bits_to_skip -= this_size_bits;
1489 if (this_size_bits > type_len - offset)
1490 this_size_bits = type_len - offset;
1491 if (bits_to_skip > 0)
1493 dest_offset_bits = bits_to_skip;
1494 source_offset_bits = 0;
1495 this_size_bits -= bits_to_skip;
1500 dest_offset_bits = 0;
1501 source_offset_bits = offset;
1504 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1505 source_offset = source_offset_bits / 8;
1506 dest_offset = dest_offset_bits / 8;
1507 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1509 source_buffer = contents + source_offset;
1514 if (buffer_size < this_size)
1516 buffer_size = this_size;
1517 buffer = xrealloc (buffer, buffer_size);
1519 source_buffer = buffer;
1523 switch (p->location)
1525 case DWARF_VALUE_REGISTER:
1527 struct gdbarch *arch = get_frame_arch (frame);
1528 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1529 int reg_offset = dest_offset;
1531 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1532 && this_size <= register_size (arch, gdb_regnum))
1533 /* Big-endian, and we want less than full size. */
1534 reg_offset = register_size (arch, gdb_regnum) - this_size;
1536 if (gdb_regnum != -1)
1542 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1547 error (_("Can't do read-modify-write to "
1548 "update bitfield; containing word has been "
1551 throw_error (NOT_AVAILABLE_ERROR,
1552 _("Can't do read-modify-write to update "
1553 "bitfield; containing word "
1556 copy_bitwise (buffer, dest_offset_bits,
1557 contents, source_offset_bits,
1562 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
1563 this_size, source_buffer);
1567 error (_("Unable to write to DWARF register number %s"),
1568 paddress (arch, p->v.regno));
1572 case DWARF_VALUE_MEMORY:
1575 /* Only the first and last bytes can possibly have any
1577 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1578 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1579 buffer + this_size - 1, 1);
1580 copy_bitwise (buffer, dest_offset_bits,
1581 contents, source_offset_bits,
1586 write_memory (p->v.mem.addr + dest_offset,
1587 source_buffer, this_size);
1590 set_value_optimized_out (to, 1);
1593 offset += this_size_bits;
1596 do_cleanups (cleanup);
1599 /* A helper function that checks bit validity in a pieced value.
1600 CHECK_FOR indicates the kind of validity checking.
1601 DWARF_VALUE_MEMORY means to check whether any bit is valid.
1602 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1604 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1605 implicit pointer. */
1608 check_pieced_value_bits (const struct value *value, int bit_offset,
1610 enum dwarf_value_location check_for)
1612 struct piece_closure *c
1613 = (struct piece_closure *) value_computed_closure (value);
1615 int validity = (check_for == DWARF_VALUE_MEMORY
1616 || check_for == DWARF_VALUE_IMPLICIT_POINTER);
1618 bit_offset += 8 * value_offset (value);
1619 if (value_bitsize (value))
1620 bit_offset += value_bitpos (value);
1622 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1624 struct dwarf_expr_piece *p = &c->pieces[i];
1625 size_t this_size_bits = p->size;
1629 if (bit_offset >= this_size_bits)
1631 bit_offset -= this_size_bits;
1635 bit_length -= this_size_bits - bit_offset;
1639 bit_length -= this_size_bits;
1641 if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
1643 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1646 else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
1647 || p->location == DWARF_VALUE_IMPLICIT_POINTER)
1663 check_pieced_value_validity (const struct value *value, int bit_offset,
1666 return check_pieced_value_bits (value, bit_offset, bit_length,
1667 DWARF_VALUE_MEMORY);
1671 check_pieced_value_invalid (const struct value *value)
1673 return check_pieced_value_bits (value, 0,
1674 8 * TYPE_LENGTH (value_type (value)),
1675 DWARF_VALUE_OPTIMIZED_OUT);
1678 /* An implementation of an lval_funcs method to see whether a value is
1679 a synthetic pointer. */
1682 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
1685 return check_pieced_value_bits (value, bit_offset, bit_length,
1686 DWARF_VALUE_IMPLICIT_POINTER);
1689 /* A wrapper function for get_frame_address_in_block. */
1692 get_frame_address_in_block_wrapper (void *baton)
1694 return get_frame_address_in_block (baton);
1697 /* An implementation of an lval_funcs method to indirect through a
1698 pointer. This handles the synthetic pointer case when needed. */
1700 static struct value *
1701 indirect_pieced_value (struct value *value)
1703 struct piece_closure *c
1704 = (struct piece_closure *) value_computed_closure (value);
1706 struct frame_info *frame;
1707 struct dwarf2_locexpr_baton baton;
1708 int i, bit_offset, bit_length;
1709 struct dwarf_expr_piece *piece = NULL;
1710 LONGEST byte_offset;
1712 type = check_typedef (value_type (value));
1713 if (TYPE_CODE (type) != TYPE_CODE_PTR)
1716 bit_length = 8 * TYPE_LENGTH (type);
1717 bit_offset = 8 * value_offset (value);
1718 if (value_bitsize (value))
1719 bit_offset += value_bitpos (value);
1721 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1723 struct dwarf_expr_piece *p = &c->pieces[i];
1724 size_t this_size_bits = p->size;
1728 if (bit_offset >= this_size_bits)
1730 bit_offset -= this_size_bits;
1734 bit_length -= this_size_bits - bit_offset;
1738 bit_length -= this_size_bits;
1740 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1743 if (bit_length != 0)
1744 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1750 frame = get_selected_frame (_("No frame selected."));
1752 /* This is an offset requested by GDB, such as value subcripts. */
1753 byte_offset = value_as_address (value);
1756 baton = dwarf2_fetch_die_location_block (piece->v.ptr.die, c->per_cu,
1757 get_frame_address_in_block_wrapper,
1760 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
1761 baton.data, baton.size, baton.per_cu,
1762 piece->v.ptr.offset + byte_offset);
1766 copy_pieced_value_closure (const struct value *v)
1768 struct piece_closure *c
1769 = (struct piece_closure *) value_computed_closure (v);
1776 free_pieced_value_closure (struct value *v)
1778 struct piece_closure *c
1779 = (struct piece_closure *) value_computed_closure (v);
1786 for (i = 0; i < c->n_pieces; ++i)
1787 if (c->pieces[i].location == DWARF_VALUE_STACK)
1788 value_free (c->pieces[i].v.value);
1795 /* Functions for accessing a variable described by DW_OP_piece. */
1796 static const struct lval_funcs pieced_value_funcs = {
1799 check_pieced_value_validity,
1800 check_pieced_value_invalid,
1801 indirect_pieced_value,
1802 check_pieced_synthetic_pointer,
1803 copy_pieced_value_closure,
1804 free_pieced_value_closure
1807 /* Helper function which throws an error if a synthetic pointer is
1811 invalid_synthetic_pointer (void)
1813 error (_("access outside bounds of object "
1814 "referenced via synthetic pointer"));
1817 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
1819 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
1821 dwarf_expr_read_reg,
1822 dwarf_expr_read_mem,
1823 dwarf_expr_frame_base,
1824 dwarf_expr_frame_cfa,
1825 dwarf_expr_frame_pc,
1826 dwarf_expr_tls_address,
1827 dwarf_expr_dwarf_call,
1828 dwarf_expr_get_base_type,
1829 dwarf_expr_push_dwarf_reg_entry_value
1832 /* Evaluate a location description, starting at DATA and with length
1833 SIZE, to find the current location of variable of TYPE in the
1834 context of FRAME. BYTE_OFFSET is applied after the contents are
1837 static struct value *
1838 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1839 const gdb_byte *data, unsigned short size,
1840 struct dwarf2_per_cu_data *per_cu,
1841 LONGEST byte_offset)
1843 struct value *retval;
1844 struct dwarf_expr_baton baton;
1845 struct dwarf_expr_context *ctx;
1846 struct cleanup *old_chain, *value_chain;
1847 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1848 volatile struct gdb_exception ex;
1850 if (byte_offset < 0)
1851 invalid_synthetic_pointer ();
1854 return allocate_optimized_out_value (type);
1856 baton.frame = frame;
1857 baton.per_cu = per_cu;
1859 ctx = new_dwarf_expr_context ();
1860 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1861 value_chain = make_cleanup_value_free_to_mark (value_mark ());
1863 ctx->gdbarch = get_objfile_arch (objfile);
1864 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1865 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
1866 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1867 ctx->baton = &baton;
1868 ctx->funcs = &dwarf_expr_ctx_funcs;
1870 TRY_CATCH (ex, RETURN_MASK_ERROR)
1872 dwarf_expr_eval (ctx, data, size);
1876 if (ex.error == NOT_AVAILABLE_ERROR)
1878 do_cleanups (old_chain);
1879 retval = allocate_value (type);
1880 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
1883 else if (ex.error == NO_ENTRY_VALUE_ERROR)
1885 if (entry_values_debug)
1886 exception_print (gdb_stdout, ex);
1887 do_cleanups (old_chain);
1888 return allocate_optimized_out_value (type);
1891 throw_exception (ex);
1894 if (ctx->num_pieces > 0)
1896 struct piece_closure *c;
1897 struct frame_id frame_id = get_frame_id (frame);
1898 ULONGEST bit_size = 0;
1901 for (i = 0; i < ctx->num_pieces; ++i)
1902 bit_size += ctx->pieces[i].size;
1903 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
1904 invalid_synthetic_pointer ();
1906 c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
1908 /* We must clean up the value chain after creating the piece
1909 closure but before allocating the result. */
1910 do_cleanups (value_chain);
1911 retval = allocate_computed_value (type, &pieced_value_funcs, c);
1912 VALUE_FRAME_ID (retval) = frame_id;
1913 set_value_offset (retval, byte_offset);
1917 switch (ctx->location)
1919 case DWARF_VALUE_REGISTER:
1921 struct gdbarch *arch = get_frame_arch (frame);
1922 ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
1923 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
1925 if (byte_offset != 0)
1926 error (_("cannot use offset on synthetic pointer to register"));
1927 do_cleanups (value_chain);
1928 if (gdb_regnum != -1)
1929 retval = value_from_register (type, gdb_regnum, frame);
1931 error (_("Unable to access DWARF register number %s"),
1932 paddress (arch, dwarf_regnum));
1936 case DWARF_VALUE_MEMORY:
1938 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
1939 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
1941 do_cleanups (value_chain);
1942 retval = allocate_value_lazy (type);
1943 VALUE_LVAL (retval) = lval_memory;
1944 if (in_stack_memory)
1945 set_value_stack (retval, 1);
1946 set_value_address (retval, address + byte_offset);
1950 case DWARF_VALUE_STACK:
1952 struct value *value = dwarf_expr_fetch (ctx, 0);
1954 const gdb_byte *val_bytes;
1955 size_t n = TYPE_LENGTH (value_type (value));
1957 if (byte_offset + TYPE_LENGTH (type) > n)
1958 invalid_synthetic_pointer ();
1960 val_bytes = value_contents_all (value);
1961 val_bytes += byte_offset;
1964 /* Preserve VALUE because we are going to free values back
1965 to the mark, but we still need the value contents
1967 value_incref (value);
1968 do_cleanups (value_chain);
1969 make_cleanup_value_free (value);
1971 retval = allocate_value (type);
1972 contents = value_contents_raw (retval);
1973 if (n > TYPE_LENGTH (type))
1975 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
1977 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
1978 val_bytes += n - TYPE_LENGTH (type);
1979 n = TYPE_LENGTH (type);
1981 memcpy (contents, val_bytes, n);
1985 case DWARF_VALUE_LITERAL:
1988 const bfd_byte *ldata;
1989 size_t n = ctx->len;
1991 if (byte_offset + TYPE_LENGTH (type) > n)
1992 invalid_synthetic_pointer ();
1994 do_cleanups (value_chain);
1995 retval = allocate_value (type);
1996 contents = value_contents_raw (retval);
1998 ldata = ctx->data + byte_offset;
2001 if (n > TYPE_LENGTH (type))
2003 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2005 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2006 ldata += n - TYPE_LENGTH (type);
2007 n = TYPE_LENGTH (type);
2009 memcpy (contents, ldata, n);
2013 case DWARF_VALUE_OPTIMIZED_OUT:
2014 do_cleanups (value_chain);
2015 retval = allocate_optimized_out_value (type);
2018 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2019 operation by execute_stack_op. */
2020 case DWARF_VALUE_IMPLICIT_POINTER:
2021 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2022 it can only be encountered when making a piece. */
2024 internal_error (__FILE__, __LINE__, _("invalid location type"));
2028 set_value_initialized (retval, ctx->initialized);
2030 do_cleanups (old_chain);
2035 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2036 passes 0 as the byte_offset. */
2039 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2040 const gdb_byte *data, unsigned short size,
2041 struct dwarf2_per_cu_data *per_cu)
2043 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2047 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
2049 struct needs_frame_baton
2052 struct dwarf2_per_cu_data *per_cu;
2055 /* Reads from registers do require a frame. */
2057 needs_frame_read_reg (void *baton, int regnum)
2059 struct needs_frame_baton *nf_baton = baton;
2061 nf_baton->needs_frame = 1;
2065 /* Reads from memory do not require a frame. */
2067 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2069 memset (buf, 0, len);
2072 /* Frame-relative accesses do require a frame. */
2074 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
2076 static gdb_byte lit0 = DW_OP_lit0;
2077 struct needs_frame_baton *nf_baton = baton;
2082 nf_baton->needs_frame = 1;
2085 /* CFA accesses require a frame. */
2088 needs_frame_frame_cfa (void *baton)
2090 struct needs_frame_baton *nf_baton = baton;
2092 nf_baton->needs_frame = 1;
2096 /* Thread-local accesses do require a frame. */
2098 needs_frame_tls_address (void *baton, CORE_ADDR offset)
2100 struct needs_frame_baton *nf_baton = baton;
2102 nf_baton->needs_frame = 1;
2106 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
2109 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
2111 struct needs_frame_baton *nf_baton = ctx->baton;
2113 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2114 ctx->funcs->get_frame_pc, ctx->baton);
2117 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2120 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2121 int dwarf_reg, CORE_ADDR fb_offset)
2123 struct needs_frame_baton *nf_baton = ctx->baton;
2125 nf_baton->needs_frame = 1;
2128 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
2130 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
2132 needs_frame_read_reg,
2133 needs_frame_read_mem,
2134 needs_frame_frame_base,
2135 needs_frame_frame_cfa,
2136 needs_frame_frame_cfa, /* get_frame_pc */
2137 needs_frame_tls_address,
2138 needs_frame_dwarf_call,
2139 NULL, /* get_base_type */
2140 needs_dwarf_reg_entry_value
2143 /* Return non-zero iff the location expression at DATA (length SIZE)
2144 requires a frame to evaluate. */
2147 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
2148 struct dwarf2_per_cu_data *per_cu)
2150 struct needs_frame_baton baton;
2151 struct dwarf_expr_context *ctx;
2153 struct cleanup *old_chain;
2154 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2156 baton.needs_frame = 0;
2157 baton.per_cu = per_cu;
2159 ctx = new_dwarf_expr_context ();
2160 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2161 make_cleanup_value_free_to_mark (value_mark ());
2163 ctx->gdbarch = get_objfile_arch (objfile);
2164 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2165 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2166 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2167 ctx->baton = &baton;
2168 ctx->funcs = &needs_frame_ctx_funcs;
2170 dwarf_expr_eval (ctx, data, size);
2172 in_reg = ctx->location == DWARF_VALUE_REGISTER;
2174 if (ctx->num_pieces > 0)
2178 /* If the location has several pieces, and any of them are in
2179 registers, then we will need a frame to fetch them from. */
2180 for (i = 0; i < ctx->num_pieces; i++)
2181 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
2185 do_cleanups (old_chain);
2187 return baton.needs_frame || in_reg;
2190 /* A helper function that throws an unimplemented error mentioning a
2191 given DWARF operator. */
2194 unimplemented (unsigned int op)
2196 const char *name = dwarf_stack_op_name (op);
2199 error (_("DWARF operator %s cannot be translated to an agent expression"),
2202 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2203 "to an agent expression"),
2207 /* A helper function to convert a DWARF register to an arch register.
2208 ARCH is the architecture.
2209 DWARF_REG is the register.
2210 This will throw an exception if the DWARF register cannot be
2211 translated to an architecture register. */
2214 translate_register (struct gdbarch *arch, int dwarf_reg)
2216 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2218 error (_("Unable to access DWARF register number %d"), dwarf_reg);
2222 /* A helper function that emits an access to memory. ARCH is the
2223 target architecture. EXPR is the expression which we are building.
2224 NBITS is the number of bits we want to read. This emits the
2225 opcodes needed to read the memory and then extract the desired
2229 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2231 ULONGEST nbytes = (nbits + 7) / 8;
2233 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
2236 ax_trace_quick (expr, nbytes);
2239 ax_simple (expr, aop_ref8);
2240 else if (nbits <= 16)
2241 ax_simple (expr, aop_ref16);
2242 else if (nbits <= 32)
2243 ax_simple (expr, aop_ref32);
2245 ax_simple (expr, aop_ref64);
2247 /* If we read exactly the number of bytes we wanted, we're done. */
2248 if (8 * nbytes == nbits)
2251 if (gdbarch_bits_big_endian (arch))
2253 /* On a bits-big-endian machine, we want the high-order
2255 ax_const_l (expr, 8 * nbytes - nbits);
2256 ax_simple (expr, aop_rsh_unsigned);
2260 /* On a bits-little-endian box, we want the low-order NBITS. */
2261 ax_zero_ext (expr, nbits);
2265 /* A helper function to return the frame's PC. */
2268 get_ax_pc (void *baton)
2270 struct agent_expr *expr = baton;
2275 /* Compile a DWARF location expression to an agent expression.
2277 EXPR is the agent expression we are building.
2278 LOC is the agent value we modify.
2279 ARCH is the architecture.
2280 ADDR_SIZE is the size of addresses, in bytes.
2281 OP_PTR is the start of the location expression.
2282 OP_END is one past the last byte of the location expression.
2284 This will throw an exception for various kinds of errors -- for
2285 example, if the expression cannot be compiled, or if the expression
2289 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2290 struct gdbarch *arch, unsigned int addr_size,
2291 const gdb_byte *op_ptr, const gdb_byte *op_end,
2292 struct dwarf2_per_cu_data *per_cu)
2294 struct cleanup *cleanups;
2296 VEC(int) *dw_labels = NULL, *patches = NULL;
2297 const gdb_byte * const base = op_ptr;
2298 const gdb_byte *previous_piece = op_ptr;
2299 enum bfd_endian byte_order = gdbarch_byte_order (arch);
2300 ULONGEST bits_collected = 0;
2301 unsigned int addr_size_bits = 8 * addr_size;
2302 int bits_big_endian = gdbarch_bits_big_endian (arch);
2304 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
2305 cleanups = make_cleanup (xfree, offsets);
2307 for (i = 0; i < op_end - op_ptr; ++i)
2310 make_cleanup (VEC_cleanup (int), &dw_labels);
2311 make_cleanup (VEC_cleanup (int), &patches);
2313 /* By default we are making an address. */
2314 loc->kind = axs_lvalue_memory;
2316 while (op_ptr < op_end)
2318 enum dwarf_location_atom op = *op_ptr;
2319 ULONGEST uoffset, reg;
2323 offsets[op_ptr - base] = expr->len;
2326 /* Our basic approach to code generation is to map DWARF
2327 operations directly to AX operations. However, there are
2330 First, DWARF works on address-sized units, but AX always uses
2331 LONGEST. For most operations we simply ignore this
2332 difference; instead we generate sign extensions as needed
2333 before division and comparison operations. It would be nice
2334 to omit the sign extensions, but there is no way to determine
2335 the size of the target's LONGEST. (This code uses the size
2336 of the host LONGEST in some cases -- that is a bug but it is
2339 Second, some DWARF operations cannot be translated to AX.
2340 For these we simply fail. See
2341 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2376 ax_const_l (expr, op - DW_OP_lit0);
2380 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
2381 op_ptr += addr_size;
2382 /* Some versions of GCC emit DW_OP_addr before
2383 DW_OP_GNU_push_tls_address. In this case the value is an
2384 index, not an address. We don't support things like
2385 branching between the address and the TLS op. */
2386 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
2387 uoffset += dwarf2_per_cu_text_offset (per_cu);
2388 ax_const_l (expr, uoffset);
2392 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
2396 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
2400 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
2404 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
2408 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
2412 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
2416 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
2420 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
2424 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
2425 ax_const_l (expr, uoffset);
2428 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2429 ax_const_l (expr, offset);
2464 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2465 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
2466 loc->kind = axs_lvalue_register;
2470 op_ptr = read_uleb128 (op_ptr, op_end, ®);
2471 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2472 loc->u.reg = translate_register (arch, reg);
2473 loc->kind = axs_lvalue_register;
2476 case DW_OP_implicit_value:
2480 op_ptr = read_uleb128 (op_ptr, op_end, &len);
2481 if (op_ptr + len > op_end)
2482 error (_("DW_OP_implicit_value: too few bytes available."));
2483 if (len > sizeof (ULONGEST))
2484 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2487 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
2490 dwarf_expr_require_composition (op_ptr, op_end,
2491 "DW_OP_implicit_value");
2493 loc->kind = axs_rvalue;
2497 case DW_OP_stack_value:
2498 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
2499 loc->kind = axs_rvalue;
2534 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2535 i = translate_register (arch, op - DW_OP_breg0);
2539 ax_const_l (expr, offset);
2540 ax_simple (expr, aop_add);
2545 op_ptr = read_uleb128 (op_ptr, op_end, ®);
2546 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2547 i = translate_register (arch, reg);
2551 ax_const_l (expr, offset);
2552 ax_simple (expr, aop_add);
2558 const gdb_byte *datastart;
2560 unsigned int before_stack_len;
2562 struct symbol *framefunc;
2563 LONGEST base_offset = 0;
2565 b = block_for_pc (expr->scope);
2568 error (_("No block found for address"));
2570 framefunc = block_linkage_function (b);
2573 error (_("No function found for block"));
2575 dwarf_expr_frame_base_1 (framefunc, expr->scope,
2576 &datastart, &datalen);
2578 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2579 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
2580 datastart + datalen, per_cu);
2584 ax_const_l (expr, offset);
2585 ax_simple (expr, aop_add);
2588 loc->kind = axs_lvalue_memory;
2593 ax_simple (expr, aop_dup);
2597 ax_simple (expr, aop_pop);
2602 ax_pick (expr, offset);
2606 ax_simple (expr, aop_swap);
2614 ax_simple (expr, aop_rot);
2618 case DW_OP_deref_size:
2622 if (op == DW_OP_deref_size)
2630 ax_simple (expr, aop_ref8);
2633 ax_simple (expr, aop_ref16);
2636 ax_simple (expr, aop_ref32);
2639 ax_simple (expr, aop_ref64);
2642 /* Note that dwarf_stack_op_name will never return
2644 error (_("Unsupported size %d in %s"),
2645 size, dwarf_stack_op_name (op));
2651 /* Sign extend the operand. */
2652 ax_ext (expr, addr_size_bits);
2653 ax_simple (expr, aop_dup);
2654 ax_const_l (expr, 0);
2655 ax_simple (expr, aop_less_signed);
2656 ax_simple (expr, aop_log_not);
2657 i = ax_goto (expr, aop_if_goto);
2658 /* We have to emit 0 - X. */
2659 ax_const_l (expr, 0);
2660 ax_simple (expr, aop_swap);
2661 ax_simple (expr, aop_sub);
2662 ax_label (expr, i, expr->len);
2666 /* No need to sign extend here. */
2667 ax_const_l (expr, 0);
2668 ax_simple (expr, aop_swap);
2669 ax_simple (expr, aop_sub);
2673 /* Sign extend the operand. */
2674 ax_ext (expr, addr_size_bits);
2675 ax_simple (expr, aop_bit_not);
2678 case DW_OP_plus_uconst:
2679 op_ptr = read_uleb128 (op_ptr, op_end, ®);
2680 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2681 but we micro-optimize anyhow. */
2684 ax_const_l (expr, reg);
2685 ax_simple (expr, aop_add);
2690 ax_simple (expr, aop_bit_and);
2694 /* Sign extend the operands. */
2695 ax_ext (expr, addr_size_bits);
2696 ax_simple (expr, aop_swap);
2697 ax_ext (expr, addr_size_bits);
2698 ax_simple (expr, aop_swap);
2699 ax_simple (expr, aop_div_signed);
2703 ax_simple (expr, aop_sub);
2707 ax_simple (expr, aop_rem_unsigned);
2711 ax_simple (expr, aop_mul);
2715 ax_simple (expr, aop_bit_or);
2719 ax_simple (expr, aop_add);
2723 ax_simple (expr, aop_lsh);
2727 ax_simple (expr, aop_rsh_unsigned);
2731 ax_simple (expr, aop_rsh_signed);
2735 ax_simple (expr, aop_bit_xor);
2739 /* Sign extend the operands. */
2740 ax_ext (expr, addr_size_bits);
2741 ax_simple (expr, aop_swap);
2742 ax_ext (expr, addr_size_bits);
2743 /* Note no swap here: A <= B is !(B < A). */
2744 ax_simple (expr, aop_less_signed);
2745 ax_simple (expr, aop_log_not);
2749 /* Sign extend the operands. */
2750 ax_ext (expr, addr_size_bits);
2751 ax_simple (expr, aop_swap);
2752 ax_ext (expr, addr_size_bits);
2753 ax_simple (expr, aop_swap);
2754 /* A >= B is !(A < B). */
2755 ax_simple (expr, aop_less_signed);
2756 ax_simple (expr, aop_log_not);
2760 /* Sign extend the operands. */
2761 ax_ext (expr, addr_size_bits);
2762 ax_simple (expr, aop_swap);
2763 ax_ext (expr, addr_size_bits);
2764 /* No need for a second swap here. */
2765 ax_simple (expr, aop_equal);
2769 /* Sign extend the operands. */
2770 ax_ext (expr, addr_size_bits);
2771 ax_simple (expr, aop_swap);
2772 ax_ext (expr, addr_size_bits);
2773 ax_simple (expr, aop_swap);
2774 ax_simple (expr, aop_less_signed);
2778 /* Sign extend the operands. */
2779 ax_ext (expr, addr_size_bits);
2780 ax_simple (expr, aop_swap);
2781 ax_ext (expr, addr_size_bits);
2782 /* Note no swap here: A > B is B < A. */
2783 ax_simple (expr, aop_less_signed);
2787 /* Sign extend the operands. */
2788 ax_ext (expr, addr_size_bits);
2789 ax_simple (expr, aop_swap);
2790 ax_ext (expr, addr_size_bits);
2791 /* No need for a swap here. */
2792 ax_simple (expr, aop_equal);
2793 ax_simple (expr, aop_log_not);
2796 case DW_OP_call_frame_cfa:
2797 dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
2798 loc->kind = axs_lvalue_memory;
2801 case DW_OP_GNU_push_tls_address:
2806 offset = extract_signed_integer (op_ptr, 2, byte_order);
2808 i = ax_goto (expr, aop_goto);
2809 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2810 VEC_safe_push (int, patches, i);
2814 offset = extract_signed_integer (op_ptr, 2, byte_order);
2816 /* Zero extend the operand. */
2817 ax_zero_ext (expr, addr_size_bits);
2818 i = ax_goto (expr, aop_if_goto);
2819 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2820 VEC_safe_push (int, patches, i);
2827 case DW_OP_bit_piece:
2829 ULONGEST size, offset;
2831 if (op_ptr - 1 == previous_piece)
2832 error (_("Cannot translate empty pieces to agent expressions"));
2833 previous_piece = op_ptr - 1;
2835 op_ptr = read_uleb128 (op_ptr, op_end, &size);
2836 if (op == DW_OP_piece)
2842 op_ptr = read_uleb128 (op_ptr, op_end, &offset);
2844 if (bits_collected + size > 8 * sizeof (LONGEST))
2845 error (_("Expression pieces exceed word size"));
2847 /* Access the bits. */
2850 case axs_lvalue_register:
2851 ax_reg (expr, loc->u.reg);
2854 case axs_lvalue_memory:
2855 /* Offset the pointer, if needed. */
2858 ax_const_l (expr, offset / 8);
2859 ax_simple (expr, aop_add);
2862 access_memory (arch, expr, size);
2866 /* For a bits-big-endian target, shift up what we already
2867 have. For a bits-little-endian target, shift up the
2868 new data. Note that there is a potential bug here if
2869 the DWARF expression leaves multiple values on the
2871 if (bits_collected > 0)
2873 if (bits_big_endian)
2875 ax_simple (expr, aop_swap);
2876 ax_const_l (expr, size);
2877 ax_simple (expr, aop_lsh);
2878 /* We don't need a second swap here, because
2879 aop_bit_or is symmetric. */
2883 ax_const_l (expr, size);
2884 ax_simple (expr, aop_lsh);
2886 ax_simple (expr, aop_bit_or);
2889 bits_collected += size;
2890 loc->kind = axs_rvalue;
2894 case DW_OP_GNU_uninit:
2900 struct dwarf2_locexpr_baton block;
2901 int size = (op == DW_OP_call2 ? 2 : 4);
2903 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
2906 block = dwarf2_fetch_die_location_block (uoffset, per_cu,
2909 /* DW_OP_call_ref is currently not supported. */
2910 gdb_assert (block.per_cu == per_cu);
2912 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
2913 block.data, block.data + block.size,
2918 case DW_OP_call_ref:
2926 /* Patch all the branches we emitted. */
2927 for (i = 0; i < VEC_length (int, patches); ++i)
2929 int targ = offsets[VEC_index (int, dw_labels, i)];
2931 internal_error (__FILE__, __LINE__, _("invalid label"));
2932 ax_label (expr, VEC_index (int, patches, i), targ);
2935 do_cleanups (cleanups);
2939 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2940 evaluator to calculate the location. */
2941 static struct value *
2942 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
2944 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2947 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
2948 dlbaton->size, dlbaton->per_cu);
2953 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
2954 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
2957 static struct value *
2958 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
2960 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2962 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
2966 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
2968 locexpr_read_needs_frame (struct symbol *symbol)
2970 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2972 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
2976 /* Return true if DATA points to the end of a piece. END is one past
2977 the last byte in the expression. */
2980 piece_end_p (const gdb_byte *data, const gdb_byte *end)
2982 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
2985 /* Helper for locexpr_describe_location_piece that finds the name of a
2989 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
2993 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
2994 return gdbarch_register_name (gdbarch, regnum);
2997 /* Nicely describe a single piece of a location, returning an updated
2998 position in the bytecode sequence. This function cannot recognize
2999 all locations; if a location is not recognized, it simply returns
3002 static const gdb_byte *
3003 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3004 CORE_ADDR addr, struct objfile *objfile,
3005 const gdb_byte *data, const gdb_byte *end,
3006 unsigned int addr_size)
3008 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3010 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3012 fprintf_filtered (stream, _("a variable in $%s"),
3013 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3016 else if (data[0] == DW_OP_regx)
3020 data = read_uleb128 (data + 1, end, ®);
3021 fprintf_filtered (stream, _("a variable in $%s"),
3022 locexpr_regname (gdbarch, reg));
3024 else if (data[0] == DW_OP_fbreg)
3027 struct symbol *framefunc;
3029 LONGEST frame_offset;
3030 const gdb_byte *base_data, *new_data, *save_data = data;
3032 LONGEST base_offset = 0;
3034 new_data = read_sleb128 (data + 1, end, &frame_offset);
3035 if (!piece_end_p (new_data, end))
3039 b = block_for_pc (addr);
3042 error (_("No block found for address for symbol \"%s\"."),
3043 SYMBOL_PRINT_NAME (symbol));
3045 framefunc = block_linkage_function (b);
3048 error (_("No function found for block for symbol \"%s\"."),
3049 SYMBOL_PRINT_NAME (symbol));
3051 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
3053 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3055 const gdb_byte *buf_end;
3057 frame_reg = base_data[0] - DW_OP_breg0;
3058 buf_end = read_sleb128 (base_data + 1,
3059 base_data + base_size, &base_offset);
3060 if (buf_end != base_data + base_size)
3061 error (_("Unexpected opcode after "
3062 "DW_OP_breg%u for symbol \"%s\"."),
3063 frame_reg, SYMBOL_PRINT_NAME (symbol));
3065 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3067 /* The frame base is just the register, with no offset. */
3068 frame_reg = base_data[0] - DW_OP_reg0;
3073 /* We don't know what to do with the frame base expression,
3074 so we can't trace this variable; give up. */
3078 fprintf_filtered (stream,
3079 _("a variable at frame base reg $%s offset %s+%s"),
3080 locexpr_regname (gdbarch, frame_reg),
3081 plongest (base_offset), plongest (frame_offset));
3083 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3084 && piece_end_p (data, end))
3088 data = read_sleb128 (data + 1, end, &offset);
3090 fprintf_filtered (stream,
3091 _("a variable at offset %s from base reg $%s"),
3093 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3096 /* The location expression for a TLS variable looks like this (on a
3099 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3100 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3102 0x3 is the encoding for DW_OP_addr, which has an operand as long
3103 as the size of an address on the target machine (here is 8
3104 bytes). Note that more recent version of GCC emit DW_OP_const4u
3105 or DW_OP_const8u, depending on address size, rather than
3106 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3107 The operand represents the offset at which the variable is within
3108 the thread local storage. */
3110 else if (data + 1 + addr_size < end
3111 && (data[0] == DW_OP_addr
3112 || (addr_size == 4 && data[0] == DW_OP_const4u)
3113 || (addr_size == 8 && data[0] == DW_OP_const8u))
3114 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3115 && piece_end_p (data + 2 + addr_size, end))
3118 offset = extract_unsigned_integer (data + 1, addr_size,
3119 gdbarch_byte_order (gdbarch));
3121 fprintf_filtered (stream,
3122 _("a thread-local variable at offset 0x%s "
3123 "in the thread-local storage for `%s'"),
3124 phex_nz (offset, addr_size), objfile->name);
3126 data += 1 + addr_size + 1;
3128 else if (data[0] >= DW_OP_lit0
3129 && data[0] <= DW_OP_lit31
3131 && data[1] == DW_OP_stack_value)
3133 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3140 /* Disassemble an expression, stopping at the end of a piece or at the
3141 end of the expression. Returns a pointer to the next unread byte
3142 in the input expression. If ALL is nonzero, then this function
3143 will keep going until it reaches the end of the expression. */
3145 static const gdb_byte *
3146 disassemble_dwarf_expression (struct ui_file *stream,
3147 struct gdbarch *arch, unsigned int addr_size,
3149 const gdb_byte *data, const gdb_byte *end,
3151 struct dwarf2_per_cu_data *per_cu)
3153 const gdb_byte *start = data;
3155 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
3159 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3161 enum dwarf_location_atom op = *data++;
3166 name = dwarf_stack_op_name (op);
3169 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3170 op, (long) (data - 1 - start));
3171 fprintf_filtered (stream, " % 4ld: %s", (long) (data - 1 - start), name);
3176 ul = extract_unsigned_integer (data, addr_size,
3177 gdbarch_byte_order (arch));
3179 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3183 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3185 fprintf_filtered (stream, " %s", pulongest (ul));
3188 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3190 fprintf_filtered (stream, " %s", plongest (l));
3193 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3195 fprintf_filtered (stream, " %s", pulongest (ul));
3198 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3200 fprintf_filtered (stream, " %s", plongest (l));
3203 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3205 fprintf_filtered (stream, " %s", pulongest (ul));
3208 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3210 fprintf_filtered (stream, " %s", plongest (l));
3213 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3215 fprintf_filtered (stream, " %s", pulongest (ul));
3218 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3220 fprintf_filtered (stream, " %s", plongest (l));
3223 data = read_uleb128 (data, end, &ul);
3224 fprintf_filtered (stream, " %s", pulongest (ul));
3227 data = read_sleb128 (data, end, &l);
3228 fprintf_filtered (stream, " %s", plongest (l));
3263 fprintf_filtered (stream, " [$%s]",
3264 locexpr_regname (arch, op - DW_OP_reg0));
3268 data = read_uleb128 (data, end, &ul);
3269 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3270 locexpr_regname (arch, (int) ul));
3273 case DW_OP_implicit_value:
3274 data = read_uleb128 (data, end, &ul);
3276 fprintf_filtered (stream, " %s", pulongest (ul));
3311 data = read_sleb128 (data, end, &l);
3312 fprintf_filtered (stream, " %s [$%s]", plongest (l),
3313 locexpr_regname (arch, op - DW_OP_breg0));
3317 data = read_uleb128 (data, end, &ul);
3318 data = read_sleb128 (data, end, &l);
3319 fprintf_filtered (stream, " register %s [$%s] offset %s",
3321 locexpr_regname (arch, (int) ul),
3326 data = read_sleb128 (data, end, &l);
3327 fprintf_filtered (stream, " %s", plongest (l));
3330 case DW_OP_xderef_size:
3331 case DW_OP_deref_size:
3333 fprintf_filtered (stream, " %d", *data);
3337 case DW_OP_plus_uconst:
3338 data = read_uleb128 (data, end, &ul);
3339 fprintf_filtered (stream, " %s", pulongest (ul));
3343 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3345 fprintf_filtered (stream, " to %ld",
3346 (long) (data + l - start));
3350 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3352 fprintf_filtered (stream, " %ld",
3353 (long) (data + l - start));
3357 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3359 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3363 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3365 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3368 case DW_OP_call_ref:
3369 ul = extract_unsigned_integer (data, offset_size,
3370 gdbarch_byte_order (arch));
3371 data += offset_size;
3372 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3376 data = read_uleb128 (data, end, &ul);
3377 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3380 case DW_OP_bit_piece:
3384 data = read_uleb128 (data, end, &ul);
3385 data = read_uleb128 (data, end, &offset);
3386 fprintf_filtered (stream, " size %s offset %s (bits)",
3387 pulongest (ul), pulongest (offset));
3391 case DW_OP_GNU_implicit_pointer:
3393 ul = extract_unsigned_integer (data, offset_size,
3394 gdbarch_byte_order (arch));
3395 data += offset_size;
3397 data = read_sleb128 (data, end, &l);
3399 fprintf_filtered (stream, " DIE %s offset %s",
3400 phex_nz (ul, offset_size),
3405 case DW_OP_GNU_deref_type:
3407 int addr_size = *data++;
3411 data = read_uleb128 (data, end, &offset);
3412 type = dwarf2_get_die_type (offset, per_cu);
3413 fprintf_filtered (stream, "<");
3414 type_print (type, "", stream, -1);
3415 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset, 0),
3420 case DW_OP_GNU_const_type:
3425 data = read_uleb128 (data, end, &type_die);
3426 type = dwarf2_get_die_type (type_die, per_cu);
3427 fprintf_filtered (stream, "<");
3428 type_print (type, "", stream, -1);
3429 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die, 0));
3433 case DW_OP_GNU_regval_type:
3435 ULONGEST type_die, reg;
3438 data = read_uleb128 (data, end, ®);
3439 data = read_uleb128 (data, end, &type_die);
3441 type = dwarf2_get_die_type (type_die, per_cu);
3442 fprintf_filtered (stream, "<");
3443 type_print (type, "", stream, -1);
3444 fprintf_filtered (stream, " [0x%s]> [$%s]", phex_nz (type_die, 0),
3445 locexpr_regname (arch, reg));
3449 case DW_OP_GNU_convert:
3450 case DW_OP_GNU_reinterpret:
3454 data = read_uleb128 (data, end, &type_die);
3457 fprintf_filtered (stream, "<0>");
3462 type = dwarf2_get_die_type (type_die, per_cu);
3463 fprintf_filtered (stream, "<");
3464 type_print (type, "", stream, -1);
3465 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die, 0));
3471 fprintf_filtered (stream, "\n");
3477 /* Describe a single location, which may in turn consist of multiple
3481 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
3482 struct ui_file *stream,
3483 const gdb_byte *data, int size,
3484 struct objfile *objfile, unsigned int addr_size,
3485 int offset_size, struct dwarf2_per_cu_data *per_cu)
3487 const gdb_byte *end = data + size;
3488 int first_piece = 1, bad = 0;
3492 const gdb_byte *here = data;
3493 int disassemble = 1;
3498 fprintf_filtered (stream, _(", and "));
3500 if (!dwarf2_always_disassemble)
3502 data = locexpr_describe_location_piece (symbol, stream,
3504 data, end, addr_size);
3505 /* If we printed anything, or if we have an empty piece,
3506 then don't disassemble. */
3508 || data[0] == DW_OP_piece
3509 || data[0] == DW_OP_bit_piece)
3513 data = disassemble_dwarf_expression (stream,
3514 get_objfile_arch (objfile),
3515 addr_size, offset_size, data, end,
3516 dwarf2_always_disassemble,
3521 int empty = data == here;
3524 fprintf_filtered (stream, " ");
3525 if (data[0] == DW_OP_piece)
3529 data = read_uleb128 (data + 1, end, &bytes);
3532 fprintf_filtered (stream, _("an empty %s-byte piece"),
3535 fprintf_filtered (stream, _(" [%s-byte piece]"),
3538 else if (data[0] == DW_OP_bit_piece)
3540 ULONGEST bits, offset;
3542 data = read_uleb128 (data + 1, end, &bits);
3543 data = read_uleb128 (data, end, &offset);
3546 fprintf_filtered (stream,
3547 _("an empty %s-bit piece"),
3550 fprintf_filtered (stream,
3551 _(" [%s-bit piece, offset %s bits]"),
3552 pulongest (bits), pulongest (offset));
3562 if (bad || data > end)
3563 error (_("Corrupted DWARF2 expression for \"%s\"."),
3564 SYMBOL_PRINT_NAME (symbol));
3567 /* Print a natural-language description of SYMBOL to STREAM. This
3568 version is for a symbol with a single location. */
3571 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
3572 struct ui_file *stream)
3574 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3575 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3576 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3577 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3579 locexpr_describe_location_1 (symbol, addr, stream,
3580 dlbaton->data, dlbaton->size,
3581 objfile, addr_size, offset_size,
3585 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3586 any necessary bytecode in AX. */
3589 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3590 struct agent_expr *ax, struct axs_value *value)
3592 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3593 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3595 if (dlbaton->data == NULL || dlbaton->size == 0)
3596 value->optimized_out = 1;
3598 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
3599 dlbaton->data, dlbaton->data + dlbaton->size,
3603 /* The set of location functions used with the DWARF-2 expression
3605 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
3606 locexpr_read_variable,
3607 locexpr_read_variable_at_entry,
3608 locexpr_read_needs_frame,
3609 locexpr_describe_location,
3610 locexpr_tracepoint_var_ref
3614 /* Wrapper functions for location lists. These generally find
3615 the appropriate location expression and call something above. */
3617 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3618 evaluator to calculate the location. */
3619 static struct value *
3620 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
3622 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3624 const gdb_byte *data;
3626 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
3628 data = dwarf2_find_location_expression (dlbaton, &size, pc);
3630 val = allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3632 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
3638 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
3639 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3642 Function always returns non-NULL value, it may be marked optimized out if
3643 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
3644 if it cannot resolve the parameter for any reason. */
3646 static struct value *
3647 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3649 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3650 const gdb_byte *data;
3654 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
3655 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3657 data = dwarf2_find_location_expression (dlbaton, &size, pc);
3659 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3661 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
3664 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3666 loclist_read_needs_frame (struct symbol *symbol)
3668 /* If there's a location list, then assume we need to have a frame
3669 to choose the appropriate location expression. With tracking of
3670 global variables this is not necessarily true, but such tracking
3671 is disabled in GCC at the moment until we figure out how to
3677 /* Print a natural-language description of SYMBOL to STREAM. This
3678 version applies when there is a list of different locations, each
3679 with a specified address range. */
3682 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
3683 struct ui_file *stream)
3685 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3686 CORE_ADDR low, high;
3687 const gdb_byte *loc_ptr, *buf_end;
3688 int length, first = 1;
3689 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3691 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3692 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3693 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3694 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
3695 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3696 /* Adjust base_address for relocatable objects. */
3697 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
3698 CORE_ADDR base_address = dlbaton->base_address + base_offset;
3700 loc_ptr = dlbaton->data;
3701 buf_end = dlbaton->data + dlbaton->size;
3703 fprintf_filtered (stream, _("multi-location:\n"));
3705 /* Iterate through locations until we run out. */
3708 if (buf_end - loc_ptr < 2 * addr_size)
3709 error (_("Corrupted DWARF expression for symbol \"%s\"."),
3710 SYMBOL_PRINT_NAME (symbol));
3713 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
3715 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
3716 loc_ptr += addr_size;
3719 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
3721 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
3722 loc_ptr += addr_size;
3724 /* A base-address-selection entry. */
3725 if ((low & base_mask) == base_mask)
3727 base_address = high + base_offset;
3728 fprintf_filtered (stream, _(" Base address %s"),
3729 paddress (gdbarch, base_address));
3733 /* An end-of-list entry. */
3734 if (low == 0 && high == 0)
3737 /* Otherwise, a location expression entry. */
3738 low += base_address;
3739 high += base_address;
3741 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
3744 /* (It would improve readability to print only the minimum
3745 necessary digits of the second number of the range.) */
3746 fprintf_filtered (stream, _(" Range %s-%s: "),
3747 paddress (gdbarch, low), paddress (gdbarch, high));
3749 /* Now describe this particular location. */
3750 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
3751 objfile, addr_size, offset_size,
3754 fprintf_filtered (stream, "\n");
3760 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3761 any necessary bytecode in AX. */
3763 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3764 struct agent_expr *ax, struct axs_value *value)
3766 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3767 const gdb_byte *data;
3769 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3771 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
3772 if (data == NULL || size == 0)
3773 value->optimized_out = 1;
3775 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
3779 /* The set of location functions used with the DWARF-2 expression
3780 evaluator and location lists. */
3781 const struct symbol_computed_ops dwarf2_loclist_funcs = {
3782 loclist_read_variable,
3783 loclist_read_variable_at_entry,
3784 loclist_read_needs_frame,
3785 loclist_describe_location,
3786 loclist_tracepoint_var_ref
3790 _initialize_dwarf2loc (void)
3792 add_setshow_zinteger_cmd ("entry-values", class_maintenance,
3793 &entry_values_debug,
3794 _("Set entry values and tail call frames "
3796 _("Show entry values and tail call frames "
3798 _("When non-zero, the process of determining "
3799 "parameter values from function entry point "
3800 "and tail call frames will be printed."),
3802 show_entry_values_debug,
3803 &setdebuglist, &showdebuglist);