1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003-2015 Free Software Foundation, Inc.
5 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
35 #include "complaints.h"
37 #include "dwarf2expr.h"
38 #include "dwarf2loc.h"
39 #include "dwarf2-frame.h"
40 #include "compile/compile.h"
42 extern int dwarf_always_disassemble;
44 extern const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
46 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
47 struct frame_info *frame,
50 struct dwarf2_per_cu_data *per_cu,
53 /* Until these have formal names, we define these here.
54 ref: http://gcc.gnu.org/wiki/DebugFission
55 Each entry in .debug_loc.dwo begins with a byte that describes the entry,
56 and is then followed by data specific to that entry. */
60 /* Indicates the end of the list of entries. */
61 DEBUG_LOC_END_OF_LIST = 0,
63 /* This is followed by an unsigned LEB128 number that is an index into
64 .debug_addr and specifies the base address for all following entries. */
65 DEBUG_LOC_BASE_ADDRESS = 1,
67 /* This is followed by two unsigned LEB128 numbers that are indices into
68 .debug_addr and specify the beginning and ending addresses, and then
69 a normal location expression as in .debug_loc. */
70 DEBUG_LOC_START_END = 2,
72 /* This is followed by an unsigned LEB128 number that is an index into
73 .debug_addr and specifies the beginning address, and a 4 byte unsigned
74 number that specifies the length, and then a normal location expression
76 DEBUG_LOC_START_LENGTH = 3,
78 /* An internal value indicating there is insufficient data. */
79 DEBUG_LOC_BUFFER_OVERFLOW = -1,
81 /* An internal value indicating an invalid kind of entry was found. */
82 DEBUG_LOC_INVALID_ENTRY = -2
85 /* Helper function which throws an error if a synthetic pointer is
89 invalid_synthetic_pointer (void)
91 error (_("access outside bounds of object "
92 "referenced via synthetic pointer"));
95 /* Decode the addresses in a non-dwo .debug_loc entry.
96 A pointer to the next byte to examine is returned in *NEW_PTR.
97 The encoded low,high addresses are return in *LOW,*HIGH.
98 The result indicates the kind of entry found. */
100 static enum debug_loc_kind
101 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
102 const gdb_byte **new_ptr,
103 CORE_ADDR *low, CORE_ADDR *high,
104 enum bfd_endian byte_order,
105 unsigned int addr_size,
108 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
110 if (buf_end - loc_ptr < 2 * addr_size)
111 return DEBUG_LOC_BUFFER_OVERFLOW;
114 *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
116 *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
117 loc_ptr += addr_size;
120 *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
122 *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
123 loc_ptr += addr_size;
127 /* A base-address-selection entry. */
128 if ((*low & base_mask) == base_mask)
129 return DEBUG_LOC_BASE_ADDRESS;
131 /* An end-of-list entry. */
132 if (*low == 0 && *high == 0)
133 return DEBUG_LOC_END_OF_LIST;
135 return DEBUG_LOC_START_END;
138 /* Decode the addresses in .debug_loc.dwo entry.
139 A pointer to the next byte to examine is returned in *NEW_PTR.
140 The encoded low,high addresses are return in *LOW,*HIGH.
141 The result indicates the kind of entry found. */
143 static enum debug_loc_kind
144 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
145 const gdb_byte *loc_ptr,
146 const gdb_byte *buf_end,
147 const gdb_byte **new_ptr,
148 CORE_ADDR *low, CORE_ADDR *high,
149 enum bfd_endian byte_order)
151 uint64_t low_index, high_index;
153 if (loc_ptr == buf_end)
154 return DEBUG_LOC_BUFFER_OVERFLOW;
158 case DEBUG_LOC_END_OF_LIST:
160 return DEBUG_LOC_END_OF_LIST;
161 case DEBUG_LOC_BASE_ADDRESS:
163 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
165 return DEBUG_LOC_BUFFER_OVERFLOW;
166 *high = dwarf2_read_addr_index (per_cu, high_index);
168 return DEBUG_LOC_BASE_ADDRESS;
169 case DEBUG_LOC_START_END:
170 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
172 return DEBUG_LOC_BUFFER_OVERFLOW;
173 *low = dwarf2_read_addr_index (per_cu, low_index);
174 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
176 return DEBUG_LOC_BUFFER_OVERFLOW;
177 *high = dwarf2_read_addr_index (per_cu, high_index);
179 return DEBUG_LOC_START_END;
180 case DEBUG_LOC_START_LENGTH:
181 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
183 return DEBUG_LOC_BUFFER_OVERFLOW;
184 *low = dwarf2_read_addr_index (per_cu, low_index);
185 if (loc_ptr + 4 > buf_end)
186 return DEBUG_LOC_BUFFER_OVERFLOW;
188 *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
189 *new_ptr = loc_ptr + 4;
190 return DEBUG_LOC_START_LENGTH;
192 return DEBUG_LOC_INVALID_ENTRY;
196 /* A function for dealing with location lists. Given a
197 symbol baton (BATON) and a pc value (PC), find the appropriate
198 location expression, set *LOCEXPR_LENGTH, and return a pointer
199 to the beginning of the expression. Returns NULL on failure.
201 For now, only return the first matching location expression; there
202 can be more than one in the list. */
205 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
206 size_t *locexpr_length, CORE_ADDR pc)
208 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
209 struct gdbarch *gdbarch = get_objfile_arch (objfile);
210 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
211 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
212 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
213 /* Adjust base_address for relocatable objects. */
214 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
215 CORE_ADDR base_address = baton->base_address + base_offset;
216 const gdb_byte *loc_ptr, *buf_end;
218 loc_ptr = baton->data;
219 buf_end = baton->data + baton->size;
223 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
225 enum debug_loc_kind kind;
226 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
229 kind = decode_debug_loc_dwo_addresses (baton->per_cu,
230 loc_ptr, buf_end, &new_ptr,
231 &low, &high, byte_order);
233 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
235 byte_order, addr_size,
240 case DEBUG_LOC_END_OF_LIST:
243 case DEBUG_LOC_BASE_ADDRESS:
244 base_address = high + base_offset;
246 case DEBUG_LOC_START_END:
247 case DEBUG_LOC_START_LENGTH:
249 case DEBUG_LOC_BUFFER_OVERFLOW:
250 case DEBUG_LOC_INVALID_ENTRY:
251 error (_("dwarf2_find_location_expression: "
252 "Corrupted DWARF expression."));
254 gdb_assert_not_reached ("bad debug_loc_kind");
257 /* Otherwise, a location expression entry.
258 If the entry is from a DWO, don't add base address: the entry is
259 from .debug_addr which has absolute addresses. */
260 if (! baton->from_dwo)
263 high += base_address;
266 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
269 if (low == high && pc == low)
271 /* This is entry PC record present only at entry point
272 of a function. Verify it is really the function entry point. */
274 const struct block *pc_block = block_for_pc (pc);
275 struct symbol *pc_func = NULL;
278 pc_func = block_linkage_function (pc_block);
280 if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
282 *locexpr_length = length;
287 if (pc >= low && pc < high)
289 *locexpr_length = length;
297 /* This is the baton used when performing dwarf2 expression
299 struct dwarf_expr_baton
301 struct frame_info *frame;
302 struct dwarf2_per_cu_data *per_cu;
303 CORE_ADDR obj_address;
306 /* Helper functions for dwarf2_evaluate_loc_desc. */
308 /* Using the frame specified in BATON, return the value of register
309 REGNUM, treated as a pointer. */
311 dwarf_expr_read_addr_from_reg (void *baton, int dwarf_regnum)
313 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
314 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
315 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
317 return address_from_register (regnum, debaton->frame);
320 /* Implement struct dwarf_expr_context_funcs' "get_reg_value" callback. */
322 static struct value *
323 dwarf_expr_get_reg_value (void *baton, struct type *type, int dwarf_regnum)
325 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
326 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
327 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
329 return value_from_register (type, regnum, debaton->frame);
332 /* Read memory at ADDR (length LEN) into BUF. */
335 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
337 read_memory (addr, buf, len);
340 /* Using the frame specified in BATON, find the location expression
341 describing the frame base. Return a pointer to it in START and
342 its length in LENGTH. */
344 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
346 /* FIXME: cagney/2003-03-26: This code should be using
347 get_frame_base_address(), and then implement a dwarf2 specific
349 struct symbol *framefunc;
350 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
351 const struct block *bl = get_frame_block (debaton->frame, NULL);
354 error (_("frame address is not available."));
356 /* Use block_linkage_function, which returns a real (not inlined)
357 function, instead of get_frame_function, which may return an
359 framefunc = block_linkage_function (bl);
361 /* If we found a frame-relative symbol then it was certainly within
362 some function associated with a frame. If we can't find the frame,
363 something has gone wrong. */
364 gdb_assert (framefunc != NULL);
366 func_get_frame_base_dwarf_block (framefunc,
367 get_frame_address_in_block (debaton->frame),
371 /* Implement find_frame_base_location method for LOC_BLOCK functions using
372 DWARF expression for its DW_AT_frame_base. */
375 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
376 const gdb_byte **start, size_t *length)
378 struct dwarf2_locexpr_baton *symbaton
379 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
381 *length = symbaton->size;
382 *start = symbaton->data;
385 /* Implement the struct symbol_block_ops::get_frame_base method for
386 LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base. */
389 locexpr_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
391 struct gdbarch *gdbarch;
393 struct dwarf2_locexpr_baton *dlbaton;
394 const gdb_byte *start;
396 struct value *result;
398 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
399 Thus, it's supposed to provide the find_frame_base_location method as
401 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
403 gdbarch = get_frame_arch (frame);
404 type = builtin_type (gdbarch)->builtin_data_ptr;
405 dlbaton = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
407 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
408 (framefunc, get_frame_pc (frame), &start, &length);
409 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
412 /* The DW_AT_frame_base attribute contains a location description which
413 computes the base address itself. However, the call to
414 dwarf2_evaluate_loc_desc returns a value representing a variable at
415 that address. The frame base address is thus this variable's
417 return value_address (result);
420 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
421 function uses DWARF expression for its DW_AT_frame_base. */
423 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
425 locexpr_find_frame_base_location,
426 locexpr_get_frame_base
429 /* Implement find_frame_base_location method for LOC_BLOCK functions using
430 DWARF location list for its DW_AT_frame_base. */
433 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
434 const gdb_byte **start, size_t *length)
436 struct dwarf2_loclist_baton *symbaton
437 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
439 *start = dwarf2_find_location_expression (symbaton, length, pc);
442 /* Implement the struct symbol_block_ops::get_frame_base method for
443 LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base. */
446 loclist_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
448 struct gdbarch *gdbarch;
450 struct dwarf2_loclist_baton *dlbaton;
451 const gdb_byte *start;
453 struct value *result;
455 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
456 Thus, it's supposed to provide the find_frame_base_location method as
458 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
460 gdbarch = get_frame_arch (frame);
461 type = builtin_type (gdbarch)->builtin_data_ptr;
462 dlbaton = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
464 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
465 (framefunc, get_frame_pc (frame), &start, &length);
466 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
469 /* The DW_AT_frame_base attribute contains a location description which
470 computes the base address itself. However, the call to
471 dwarf2_evaluate_loc_desc returns a value representing a variable at
472 that address. The frame base address is thus this variable's
474 return value_address (result);
477 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
478 function uses DWARF location list for its DW_AT_frame_base. */
480 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
482 loclist_find_frame_base_location,
483 loclist_get_frame_base
486 /* See dwarf2loc.h. */
489 func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
490 const gdb_byte **start, size_t *length)
492 if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
494 const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
496 ops_block->find_frame_base_location (framefunc, pc, start, length);
502 error (_("Could not find the frame base for \"%s\"."),
503 SYMBOL_NATURAL_NAME (framefunc));
506 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
507 the frame in BATON. */
510 dwarf_expr_frame_cfa (void *baton)
512 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
514 return dwarf2_frame_cfa (debaton->frame);
517 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
518 the frame in BATON. */
521 dwarf_expr_frame_pc (void *baton)
523 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
525 return get_frame_address_in_block (debaton->frame);
528 /* Using the objfile specified in BATON, find the address for the
529 current thread's thread-local storage with offset OFFSET. */
531 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
533 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
534 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
536 return target_translate_tls_address (objfile, offset);
539 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
540 current CU (as is PER_CU). State of the CTX is not affected by the
544 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
545 struct dwarf2_per_cu_data *per_cu,
546 CORE_ADDR (*get_frame_pc) (void *baton),
549 struct dwarf2_locexpr_baton block;
551 block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);
553 /* DW_OP_call_ref is currently not supported. */
554 gdb_assert (block.per_cu == per_cu);
556 dwarf_expr_eval (ctx, block.data, block.size);
559 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
562 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
564 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) ctx->baton;
566 per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
567 ctx->funcs->get_frame_pc, ctx->baton);
570 /* Callback function for dwarf2_evaluate_loc_desc. */
573 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
574 cu_offset die_offset)
576 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) ctx->baton;
578 return dwarf2_get_die_type (die_offset, debaton->per_cu);
581 /* See dwarf2loc.h. */
583 unsigned int entry_values_debug = 0;
585 /* Helper to set entry_values_debug. */
588 show_entry_values_debug (struct ui_file *file, int from_tty,
589 struct cmd_list_element *c, const char *value)
591 fprintf_filtered (file,
592 _("Entry values and tail call frames debugging is %s.\n"),
596 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
597 CALLER_FRAME (for registers) can be NULL if it is not known. This function
598 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
601 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
602 struct call_site *call_site,
603 struct frame_info *caller_frame)
605 switch (FIELD_LOC_KIND (call_site->target))
607 case FIELD_LOC_KIND_DWARF_BLOCK:
609 struct dwarf2_locexpr_baton *dwarf_block;
611 struct type *caller_core_addr_type;
612 struct gdbarch *caller_arch;
614 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
615 if (dwarf_block == NULL)
617 struct bound_minimal_symbol msym;
619 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
620 throw_error (NO_ENTRY_VALUE_ERROR,
621 _("DW_AT_GNU_call_site_target is not specified "
623 paddress (call_site_gdbarch, call_site->pc),
624 (msym.minsym == NULL ? "???"
625 : MSYMBOL_PRINT_NAME (msym.minsym)));
628 if (caller_frame == NULL)
630 struct bound_minimal_symbol msym;
632 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
633 throw_error (NO_ENTRY_VALUE_ERROR,
634 _("DW_AT_GNU_call_site_target DWARF block resolving "
635 "requires known frame which is currently not "
636 "available at %s in %s"),
637 paddress (call_site_gdbarch, call_site->pc),
638 (msym.minsym == NULL ? "???"
639 : MSYMBOL_PRINT_NAME (msym.minsym)));
642 caller_arch = get_frame_arch (caller_frame);
643 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
644 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
645 dwarf_block->data, dwarf_block->size,
646 dwarf_block->per_cu);
647 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
649 if (VALUE_LVAL (val) == lval_memory)
650 return value_address (val);
652 return value_as_address (val);
655 case FIELD_LOC_KIND_PHYSNAME:
657 const char *physname;
658 struct bound_minimal_symbol msym;
660 physname = FIELD_STATIC_PHYSNAME (call_site->target);
662 /* Handle both the mangled and demangled PHYSNAME. */
663 msym = lookup_minimal_symbol (physname, NULL, NULL);
664 if (msym.minsym == NULL)
666 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
667 throw_error (NO_ENTRY_VALUE_ERROR,
668 _("Cannot find function \"%s\" for a call site target "
670 physname, paddress (call_site_gdbarch, call_site->pc),
671 (msym.minsym == NULL ? "???"
672 : MSYMBOL_PRINT_NAME (msym.minsym)));
675 return BMSYMBOL_VALUE_ADDRESS (msym);
678 case FIELD_LOC_KIND_PHYSADDR:
679 return FIELD_STATIC_PHYSADDR (call_site->target);
682 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
686 /* Convert function entry point exact address ADDR to the function which is
687 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
688 NO_ENTRY_VALUE_ERROR otherwise. */
690 static struct symbol *
691 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
693 struct symbol *sym = find_pc_function (addr);
696 if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
697 throw_error (NO_ENTRY_VALUE_ERROR,
698 _("DW_TAG_GNU_call_site resolving failed to find function "
699 "name for address %s"),
700 paddress (gdbarch, addr));
702 type = SYMBOL_TYPE (sym);
703 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
704 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
709 /* Verify function with entry point exact address ADDR can never call itself
710 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
711 can call itself via tail calls.
713 If a funtion can tail call itself its entry value based parameters are
714 unreliable. There is no verification whether the value of some/all
715 parameters is unchanged through the self tail call, we expect if there is
716 a self tail call all the parameters can be modified. */
719 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
721 struct obstack addr_obstack;
722 struct cleanup *old_chain;
725 /* Track here CORE_ADDRs which were already visited. */
728 /* The verification is completely unordered. Track here function addresses
729 which still need to be iterated. */
730 VEC (CORE_ADDR) *todo = NULL;
732 obstack_init (&addr_obstack);
733 old_chain = make_cleanup_obstack_free (&addr_obstack);
734 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
735 &addr_obstack, hashtab_obstack_allocate,
737 make_cleanup_htab_delete (addr_hash);
739 make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
741 VEC_safe_push (CORE_ADDR, todo, verify_addr);
742 while (!VEC_empty (CORE_ADDR, todo))
744 struct symbol *func_sym;
745 struct call_site *call_site;
747 addr = VEC_pop (CORE_ADDR, todo);
749 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
751 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
752 call_site; call_site = call_site->tail_call_next)
754 CORE_ADDR target_addr;
757 /* CALLER_FRAME with registers is not available for tail-call jumped
759 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
761 if (target_addr == verify_addr)
763 struct bound_minimal_symbol msym;
765 msym = lookup_minimal_symbol_by_pc (verify_addr);
766 throw_error (NO_ENTRY_VALUE_ERROR,
767 _("DW_OP_GNU_entry_value resolving has found "
768 "function \"%s\" at %s can call itself via tail "
770 (msym.minsym == NULL ? "???"
771 : MSYMBOL_PRINT_NAME (msym.minsym)),
772 paddress (gdbarch, verify_addr));
775 slot = htab_find_slot (addr_hash, &target_addr, INSERT);
778 *slot = obstack_copy (&addr_obstack, &target_addr,
779 sizeof (target_addr));
780 VEC_safe_push (CORE_ADDR, todo, target_addr);
785 do_cleanups (old_chain);
788 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
789 ENTRY_VALUES_DEBUG. */
792 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
794 CORE_ADDR addr = call_site->pc;
795 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
797 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
798 (msym.minsym == NULL ? "???"
799 : MSYMBOL_PRINT_NAME (msym.minsym)));
803 /* vec.h needs single word type name, typedef it. */
804 typedef struct call_site *call_sitep;
806 /* Define VEC (call_sitep) functions. */
807 DEF_VEC_P (call_sitep);
809 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
810 only top callers and bottom callees which are present in both. GDBARCH is
811 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
812 no remaining possibilities to provide unambiguous non-trivial result.
813 RESULTP should point to NULL on the first (initialization) call. Caller is
814 responsible for xfree of any RESULTP data. */
817 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
818 VEC (call_sitep) *chain)
820 struct call_site_chain *result = *resultp;
821 long length = VEC_length (call_sitep, chain);
822 int callers, callees, idx;
826 /* Create the initial chain containing all the passed PCs. */
828 result = ((struct call_site_chain *)
829 xmalloc (sizeof (*result)
830 + sizeof (*result->call_site) * (length - 1)));
831 result->length = length;
832 result->callers = result->callees = length;
833 if (!VEC_empty (call_sitep, chain))
834 memcpy (result->call_site, VEC_address (call_sitep, chain),
835 sizeof (*result->call_site) * length);
838 if (entry_values_debug)
840 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
841 for (idx = 0; idx < length; idx++)
842 tailcall_dump (gdbarch, result->call_site[idx]);
843 fputc_unfiltered ('\n', gdb_stdlog);
849 if (entry_values_debug)
851 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
852 for (idx = 0; idx < length; idx++)
853 tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
854 fputc_unfiltered ('\n', gdb_stdlog);
857 /* Intersect callers. */
859 callers = min (result->callers, length);
860 for (idx = 0; idx < callers; idx++)
861 if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
863 result->callers = idx;
867 /* Intersect callees. */
869 callees = min (result->callees, length);
870 for (idx = 0; idx < callees; idx++)
871 if (result->call_site[result->length - 1 - idx]
872 != VEC_index (call_sitep, chain, length - 1 - idx))
874 result->callees = idx;
878 if (entry_values_debug)
880 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
881 for (idx = 0; idx < result->callers; idx++)
882 tailcall_dump (gdbarch, result->call_site[idx]);
883 fputs_unfiltered (" |", gdb_stdlog);
884 for (idx = 0; idx < result->callees; idx++)
885 tailcall_dump (gdbarch, result->call_site[result->length
886 - result->callees + idx]);
887 fputc_unfiltered ('\n', gdb_stdlog);
890 if (result->callers == 0 && result->callees == 0)
892 /* There are no common callers or callees. It could be also a direct
893 call (which has length 0) with ambiguous possibility of an indirect
894 call - CALLERS == CALLEES == 0 is valid during the first allocation
895 but any subsequence processing of such entry means ambiguity. */
901 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
902 PC again. In such case there must be two different code paths to reach
903 it. CALLERS + CALLEES equal to LENGTH in the case of self tail-call. */
904 gdb_assert (result->callers + result->callees <= result->length);
907 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
908 assumed frames between them use GDBARCH. Use depth first search so we can
909 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
910 would have needless GDB stack overhead. Caller is responsible for xfree of
911 the returned result. Any unreliability results in thrown
912 NO_ENTRY_VALUE_ERROR. */
914 static struct call_site_chain *
915 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
918 CORE_ADDR save_callee_pc = callee_pc;
919 struct obstack addr_obstack;
920 struct cleanup *back_to_retval, *back_to_workdata;
921 struct call_site_chain *retval = NULL;
922 struct call_site *call_site;
924 /* Mark CALL_SITEs so we do not visit the same ones twice. */
927 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
928 call_site nor any possible call_site at CALLEE_PC's function is there.
929 Any CALL_SITE in CHAIN will be iterated to its siblings - via
930 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
931 VEC (call_sitep) *chain = NULL;
933 /* We are not interested in the specific PC inside the callee function. */
934 callee_pc = get_pc_function_start (callee_pc);
936 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
937 paddress (gdbarch, save_callee_pc));
939 back_to_retval = make_cleanup (free_current_contents, &retval);
941 obstack_init (&addr_obstack);
942 back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
943 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
944 &addr_obstack, hashtab_obstack_allocate,
946 make_cleanup_htab_delete (addr_hash);
948 make_cleanup (VEC_cleanup (call_sitep), &chain);
950 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
951 at the target's function. All the possible tail call sites in the
952 target's function will get iterated as already pushed into CHAIN via their
954 call_site = call_site_for_pc (gdbarch, caller_pc);
958 CORE_ADDR target_func_addr;
959 struct call_site *target_call_site;
961 /* CALLER_FRAME with registers is not available for tail-call jumped
963 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
965 if (target_func_addr == callee_pc)
967 chain_candidate (gdbarch, &retval, chain);
971 /* There is no way to reach CALLEE_PC again as we would prevent
972 entering it twice as being already marked in ADDR_HASH. */
973 target_call_site = NULL;
977 struct symbol *target_func;
979 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
980 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
985 /* Attempt to visit TARGET_CALL_SITE. */
987 if (target_call_site)
991 slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
994 /* Successfully entered TARGET_CALL_SITE. */
996 *slot = &target_call_site->pc;
997 VEC_safe_push (call_sitep, chain, target_call_site);
1002 /* Backtrack (without revisiting the originating call_site). Try the
1003 callers's sibling; if there isn't any try the callers's callers's
1006 target_call_site = NULL;
1007 while (!VEC_empty (call_sitep, chain))
1009 call_site = VEC_pop (call_sitep, chain);
1011 gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
1012 NO_INSERT) != NULL);
1013 htab_remove_elt (addr_hash, &call_site->pc);
1015 target_call_site = call_site->tail_call_next;
1016 if (target_call_site)
1020 while (target_call_site);
1022 if (VEC_empty (call_sitep, chain))
1025 call_site = VEC_last (call_sitep, chain);
1030 struct bound_minimal_symbol msym_caller, msym_callee;
1032 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
1033 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
1034 throw_error (NO_ENTRY_VALUE_ERROR,
1035 _("There are no unambiguously determinable intermediate "
1036 "callers or callees between caller function \"%s\" at %s "
1037 "and callee function \"%s\" at %s"),
1038 (msym_caller.minsym == NULL
1039 ? "???" : MSYMBOL_PRINT_NAME (msym_caller.minsym)),
1040 paddress (gdbarch, caller_pc),
1041 (msym_callee.minsym == NULL
1042 ? "???" : MSYMBOL_PRINT_NAME (msym_callee.minsym)),
1043 paddress (gdbarch, callee_pc));
1046 do_cleanups (back_to_workdata);
1047 discard_cleanups (back_to_retval);
1051 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1052 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
1053 constructed return NULL. Caller is responsible for xfree of the returned
1056 struct call_site_chain *
1057 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
1058 CORE_ADDR callee_pc)
1060 struct call_site_chain *retval = NULL;
1064 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
1066 CATCH (e, RETURN_MASK_ERROR)
1068 if (e.error == NO_ENTRY_VALUE_ERROR)
1070 if (entry_values_debug)
1071 exception_print (gdb_stdout, e);
1076 throw_exception (e);
1083 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
1086 call_site_parameter_matches (struct call_site_parameter *parameter,
1087 enum call_site_parameter_kind kind,
1088 union call_site_parameter_u kind_u)
1090 if (kind == parameter->kind)
1093 case CALL_SITE_PARAMETER_DWARF_REG:
1094 return kind_u.dwarf_reg == parameter->u.dwarf_reg;
1095 case CALL_SITE_PARAMETER_FB_OFFSET:
1096 return kind_u.fb_offset == parameter->u.fb_offset;
1097 case CALL_SITE_PARAMETER_PARAM_OFFSET:
1098 return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
1103 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1104 FRAME is for callee.
1106 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1109 static struct call_site_parameter *
1110 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
1111 enum call_site_parameter_kind kind,
1112 union call_site_parameter_u kind_u,
1113 struct dwarf2_per_cu_data **per_cu_return)
1115 CORE_ADDR func_addr, caller_pc;
1116 struct gdbarch *gdbarch;
1117 struct frame_info *caller_frame;
1118 struct call_site *call_site;
1120 /* Initialize it just to avoid a GCC false warning. */
1121 struct call_site_parameter *parameter = NULL;
1122 CORE_ADDR target_addr;
1124 while (get_frame_type (frame) == INLINE_FRAME)
1126 frame = get_prev_frame (frame);
1127 gdb_assert (frame != NULL);
1130 func_addr = get_frame_func (frame);
1131 gdbarch = get_frame_arch (frame);
1132 caller_frame = get_prev_frame (frame);
1133 if (gdbarch != frame_unwind_arch (frame))
1135 struct bound_minimal_symbol msym
1136 = lookup_minimal_symbol_by_pc (func_addr);
1137 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1139 throw_error (NO_ENTRY_VALUE_ERROR,
1140 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
1141 "(of %s (%s)) does not match caller gdbarch %s"),
1142 gdbarch_bfd_arch_info (gdbarch)->printable_name,
1143 paddress (gdbarch, func_addr),
1144 (msym.minsym == NULL ? "???"
1145 : MSYMBOL_PRINT_NAME (msym.minsym)),
1146 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1149 if (caller_frame == NULL)
1151 struct bound_minimal_symbol msym
1152 = lookup_minimal_symbol_by_pc (func_addr);
1154 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
1155 "requires caller of %s (%s)"),
1156 paddress (gdbarch, func_addr),
1157 (msym.minsym == NULL ? "???"
1158 : MSYMBOL_PRINT_NAME (msym.minsym)));
1160 caller_pc = get_frame_pc (caller_frame);
1161 call_site = call_site_for_pc (gdbarch, caller_pc);
1163 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1164 if (target_addr != func_addr)
1166 struct minimal_symbol *target_msym, *func_msym;
1168 target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
1169 func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
1170 throw_error (NO_ENTRY_VALUE_ERROR,
1171 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
1172 "but the called frame is for %s at %s"),
1173 (target_msym == NULL ? "???"
1174 : MSYMBOL_PRINT_NAME (target_msym)),
1175 paddress (gdbarch, target_addr),
1176 func_msym == NULL ? "???" : MSYMBOL_PRINT_NAME (func_msym),
1177 paddress (gdbarch, func_addr));
1180 /* No entry value based parameters would be reliable if this function can
1181 call itself via tail calls. */
1182 func_verify_no_selftailcall (gdbarch, func_addr);
1184 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1186 parameter = &call_site->parameter[iparams];
1187 if (call_site_parameter_matches (parameter, kind, kind_u))
1190 if (iparams == call_site->parameter_count)
1192 struct minimal_symbol *msym
1193 = lookup_minimal_symbol_by_pc (caller_pc).minsym;
1195 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1196 determine its value. */
1197 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1198 "at DW_TAG_GNU_call_site %s at %s"),
1199 paddress (gdbarch, caller_pc),
1200 msym == NULL ? "???" : MSYMBOL_PRINT_NAME (msym));
1203 *per_cu_return = call_site->per_cu;
1207 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1208 the normal DW_AT_GNU_call_site_value block. Otherwise return the
1209 DW_AT_GNU_call_site_data_value (dereferenced) block.
1211 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1214 Function always returns non-NULL, non-optimized out value. It throws
1215 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1217 static struct value *
1218 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1219 CORE_ADDR deref_size, struct type *type,
1220 struct frame_info *caller_frame,
1221 struct dwarf2_per_cu_data *per_cu)
1223 const gdb_byte *data_src;
1227 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1228 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1230 /* DEREF_SIZE size is not verified here. */
1231 if (data_src == NULL)
1232 throw_error (NO_ENTRY_VALUE_ERROR,
1233 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1235 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1236 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1238 data = (gdb_byte *) alloca (size + 1);
1239 memcpy (data, data_src, size);
1240 data[size] = DW_OP_stack_value;
1242 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1245 /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U.
1246 Choose DEREF_SIZE value of that parameter. Search caller of the CTX's
1247 frame. CTX must be of dwarf_expr_ctx_funcs kind.
1249 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
1250 can be more simple as it does not support cross-CU DWARF executions. */
1253 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1254 enum call_site_parameter_kind kind,
1255 union call_site_parameter_u kind_u,
1258 struct dwarf_expr_baton *debaton;
1259 struct frame_info *frame, *caller_frame;
1260 struct dwarf2_per_cu_data *caller_per_cu;
1261 struct dwarf_expr_baton baton_local;
1262 struct dwarf_expr_context saved_ctx;
1263 struct call_site_parameter *parameter;
1264 const gdb_byte *data_src;
1267 gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
1268 debaton = (struct dwarf_expr_baton *) ctx->baton;
1269 frame = debaton->frame;
1270 caller_frame = get_prev_frame (frame);
1272 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1274 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1275 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1277 /* DEREF_SIZE size is not verified here. */
1278 if (data_src == NULL)
1279 throw_error (NO_ENTRY_VALUE_ERROR,
1280 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1282 baton_local.frame = caller_frame;
1283 baton_local.per_cu = caller_per_cu;
1284 baton_local.obj_address = 0;
1286 saved_ctx.gdbarch = ctx->gdbarch;
1287 saved_ctx.addr_size = ctx->addr_size;
1288 saved_ctx.offset = ctx->offset;
1289 saved_ctx.baton = ctx->baton;
1290 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
1291 ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1292 ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1293 ctx->baton = &baton_local;
1295 dwarf_expr_eval (ctx, data_src, size);
1297 ctx->gdbarch = saved_ctx.gdbarch;
1298 ctx->addr_size = saved_ctx.addr_size;
1299 ctx->offset = saved_ctx.offset;
1300 ctx->baton = saved_ctx.baton;
1303 /* Callback function for dwarf2_evaluate_loc_desc.
1304 Fetch the address indexed by DW_OP_GNU_addr_index. */
1307 dwarf_expr_get_addr_index (void *baton, unsigned int index)
1309 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1311 return dwarf2_read_addr_index (debaton->per_cu, index);
1314 /* Callback function for get_object_address. Return the address of the VLA
1318 dwarf_expr_get_obj_addr (void *baton)
1320 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1322 gdb_assert (debaton != NULL);
1324 if (debaton->obj_address == 0)
1325 error (_("Location address is not set."));
1327 return debaton->obj_address;
1330 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1331 the indirect method on it, that is use its stored target value, the sole
1332 purpose of entry_data_value_funcs.. */
1334 static struct value *
1335 entry_data_value_coerce_ref (const struct value *value)
1337 struct type *checked_type = check_typedef (value_type (value));
1338 struct value *target_val;
1340 if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1343 target_val = (struct value *) value_computed_closure (value);
1344 value_incref (target_val);
1348 /* Implement copy_closure. */
1351 entry_data_value_copy_closure (const struct value *v)
1353 struct value *target_val = (struct value *) value_computed_closure (v);
1355 value_incref (target_val);
1359 /* Implement free_closure. */
1362 entry_data_value_free_closure (struct value *v)
1364 struct value *target_val = (struct value *) value_computed_closure (v);
1366 value_free (target_val);
1369 /* Vector for methods for an entry value reference where the referenced value
1370 is stored in the caller. On the first dereference use
1371 DW_AT_GNU_call_site_data_value in the caller. */
1373 static const struct lval_funcs entry_data_value_funcs =
1377 NULL, /* indirect */
1378 entry_data_value_coerce_ref,
1379 NULL, /* check_synthetic_pointer */
1380 entry_data_value_copy_closure,
1381 entry_data_value_free_closure
1384 /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U
1385 are used to match DW_AT_location at the caller's
1386 DW_TAG_GNU_call_site_parameter.
1388 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1389 cannot resolve the parameter for any reason. */
1391 static struct value *
1392 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1393 enum call_site_parameter_kind kind,
1394 union call_site_parameter_u kind_u)
1396 struct type *checked_type = check_typedef (type);
1397 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1398 struct frame_info *caller_frame = get_prev_frame (frame);
1399 struct value *outer_val, *target_val, *val;
1400 struct call_site_parameter *parameter;
1401 struct dwarf2_per_cu_data *caller_per_cu;
1403 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1406 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1410 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1411 used and it is not available do not fall back to OUTER_VAL - dereferencing
1412 TYPE_CODE_REF with non-entry data value would give current value - not the
1415 if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1416 || TYPE_TARGET_TYPE (checked_type) == NULL)
1419 target_val = dwarf_entry_parameter_to_value (parameter,
1420 TYPE_LENGTH (target_type),
1421 target_type, caller_frame,
1424 release_value (target_val);
1425 val = allocate_computed_value (type, &entry_data_value_funcs,
1426 target_val /* closure */);
1428 /* Copy the referencing pointer to the new computed value. */
1429 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1430 TYPE_LENGTH (checked_type));
1431 set_value_lazy (val, 0);
1436 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1437 SIZE are DWARF block used to match DW_AT_location at the caller's
1438 DW_TAG_GNU_call_site_parameter.
1440 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1441 cannot resolve the parameter for any reason. */
1443 static struct value *
1444 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1445 const gdb_byte *block, size_t block_len)
1447 union call_site_parameter_u kind_u;
1449 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1450 if (kind_u.dwarf_reg != -1)
1451 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1454 if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1455 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1458 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1459 suppressed during normal operation. The expression can be arbitrary if
1460 there is no caller-callee entry value binding expected. */
1461 throw_error (NO_ENTRY_VALUE_ERROR,
1462 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1463 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1466 struct piece_closure
1468 /* Reference count. */
1471 /* The CU from which this closure's expression came. */
1472 struct dwarf2_per_cu_data *per_cu;
1474 /* The number of pieces used to describe this variable. */
1477 /* The target address size, used only for DWARF_VALUE_STACK. */
1480 /* The pieces themselves. */
1481 struct dwarf_expr_piece *pieces;
1484 /* Allocate a closure for a value formed from separately-described
1487 static struct piece_closure *
1488 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1489 int n_pieces, struct dwarf_expr_piece *pieces,
1492 struct piece_closure *c = XCNEW (struct piece_closure);
1497 c->n_pieces = n_pieces;
1498 c->addr_size = addr_size;
1499 c->pieces = XCNEWVEC (struct dwarf_expr_piece, n_pieces);
1501 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1502 for (i = 0; i < n_pieces; ++i)
1503 if (c->pieces[i].location == DWARF_VALUE_STACK)
1504 value_incref (c->pieces[i].v.value);
1509 /* The lowest-level function to extract bits from a byte buffer.
1510 SOURCE is the buffer. It is updated if we read to the end of a
1512 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1513 updated to reflect the number of bits actually read.
1514 NBITS is the number of bits we want to read. It is updated to
1515 reflect the number of bits actually read. This function may read
1517 BITS_BIG_ENDIAN is taken directly from gdbarch.
1518 This function returns the extracted bits. */
1521 extract_bits_primitive (const gdb_byte **source,
1522 unsigned int *source_offset_bits,
1523 int *nbits, int bits_big_endian)
1525 unsigned int avail, mask, datum;
1527 gdb_assert (*source_offset_bits < 8);
1529 avail = 8 - *source_offset_bits;
1533 mask = (1 << avail) - 1;
1535 if (bits_big_endian)
1536 datum >>= 8 - (*source_offset_bits + *nbits);
1538 datum >>= *source_offset_bits;
1542 *source_offset_bits += avail;
1543 if (*source_offset_bits >= 8)
1545 *source_offset_bits -= 8;
1552 /* Extract some bits from a source buffer and move forward in the
1555 SOURCE is the source buffer. It is updated as bytes are read.
1556 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1558 NBITS is the number of bits to read.
1559 BITS_BIG_ENDIAN is taken directly from gdbarch.
1561 This function returns the bits that were read. */
1564 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1565 int nbits, int bits_big_endian)
1569 gdb_assert (nbits > 0 && nbits <= 8);
1571 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1577 more = extract_bits_primitive (source, source_offset_bits, &nbits,
1579 if (bits_big_endian)
1589 /* Write some bits into a buffer and move forward in the buffer.
1591 DATUM is the bits to write. The low-order bits of DATUM are used.
1592 DEST is the destination buffer. It is updated as bytes are
1594 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1596 NBITS is the number of valid bits in DATUM.
1597 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1600 insert_bits (unsigned int datum,
1601 gdb_byte *dest, unsigned int dest_offset_bits,
1602 int nbits, int bits_big_endian)
1606 gdb_assert (dest_offset_bits + nbits <= 8);
1608 mask = (1 << nbits) - 1;
1609 if (bits_big_endian)
1611 datum <<= 8 - (dest_offset_bits + nbits);
1612 mask <<= 8 - (dest_offset_bits + nbits);
1616 datum <<= dest_offset_bits;
1617 mask <<= dest_offset_bits;
1620 gdb_assert ((datum & ~mask) == 0);
1622 *dest = (*dest & ~mask) | datum;
1625 /* Copy bits from a source to a destination.
1627 DEST is where the bits should be written.
1628 DEST_OFFSET_BITS is the bit offset into DEST.
1629 SOURCE is the source of bits.
1630 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1631 BIT_COUNT is the number of bits to copy.
1632 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1635 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1636 const gdb_byte *source, unsigned int source_offset_bits,
1637 unsigned int bit_count,
1638 int bits_big_endian)
1640 unsigned int dest_avail;
1643 /* Reduce everything to byte-size pieces. */
1644 dest += dest_offset_bits / 8;
1645 dest_offset_bits %= 8;
1646 source += source_offset_bits / 8;
1647 source_offset_bits %= 8;
1649 dest_avail = 8 - dest_offset_bits % 8;
1651 /* See if we can fill the first destination byte. */
1652 if (dest_avail < bit_count)
1654 datum = extract_bits (&source, &source_offset_bits, dest_avail,
1656 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1658 dest_offset_bits = 0;
1659 bit_count -= dest_avail;
1662 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1663 than 8 bits remaining. */
1664 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1665 for (; bit_count >= 8; bit_count -= 8)
1667 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1668 *dest++ = (gdb_byte) datum;
1671 /* Finally, we may have a few leftover bits. */
1672 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1675 datum = extract_bits (&source, &source_offset_bits, bit_count,
1677 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1682 read_pieced_value (struct value *v)
1686 ULONGEST bits_to_skip;
1688 struct piece_closure *c
1689 = (struct piece_closure *) value_computed_closure (v);
1690 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1692 size_t buffer_size = 0;
1693 gdb_byte *buffer = NULL;
1694 struct cleanup *cleanup;
1696 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1698 if (value_type (v) != value_enclosing_type (v))
1699 internal_error (__FILE__, __LINE__,
1700 _("Should not be able to create a lazy value with "
1701 "an enclosing type"));
1703 cleanup = make_cleanup (free_current_contents, &buffer);
1705 contents = value_contents_raw (v);
1706 bits_to_skip = 8 * value_offset (v);
1707 if (value_bitsize (v))
1709 bits_to_skip += value_bitpos (v);
1710 type_len = value_bitsize (v);
1713 type_len = 8 * TYPE_LENGTH (value_type (v));
1715 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1717 struct dwarf_expr_piece *p = &c->pieces[i];
1718 size_t this_size, this_size_bits;
1719 long dest_offset_bits, source_offset_bits, source_offset;
1720 const gdb_byte *intermediate_buffer;
1722 /* Compute size, source, and destination offsets for copying, in
1724 this_size_bits = p->size;
1725 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1727 bits_to_skip -= this_size_bits;
1730 if (bits_to_skip > 0)
1732 dest_offset_bits = 0;
1733 source_offset_bits = bits_to_skip;
1734 this_size_bits -= bits_to_skip;
1739 dest_offset_bits = offset;
1740 source_offset_bits = 0;
1742 if (this_size_bits > type_len - offset)
1743 this_size_bits = type_len - offset;
1745 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1746 source_offset = source_offset_bits / 8;
1747 if (buffer_size < this_size)
1749 buffer_size = this_size;
1750 buffer = (gdb_byte *) xrealloc (buffer, buffer_size);
1752 intermediate_buffer = buffer;
1754 /* Copy from the source to DEST_BUFFER. */
1755 switch (p->location)
1757 case DWARF_VALUE_REGISTER:
1759 struct gdbarch *arch = get_frame_arch (frame);
1760 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1762 int reg_offset = source_offset;
1764 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1765 && this_size < register_size (arch, gdb_regnum))
1767 /* Big-endian, and we want less than full size. */
1768 reg_offset = register_size (arch, gdb_regnum) - this_size;
1769 /* We want the lower-order THIS_SIZE_BITS of the bytes
1770 we extract from the register. */
1771 source_offset_bits += 8 * this_size - this_size_bits;
1774 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1778 /* Just so garbage doesn't ever shine through. */
1779 memset (buffer, 0, this_size);
1782 mark_value_bits_optimized_out (v, offset, this_size_bits);
1784 mark_value_bits_unavailable (v, offset, this_size_bits);
1789 case DWARF_VALUE_MEMORY:
1790 read_value_memory (v, offset,
1791 p->v.mem.in_stack_memory,
1792 p->v.mem.addr + source_offset,
1796 case DWARF_VALUE_STACK:
1798 size_t n = this_size;
1800 if (n > c->addr_size - source_offset)
1801 n = (c->addr_size >= source_offset
1802 ? c->addr_size - source_offset
1810 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1812 intermediate_buffer = val_bytes + source_offset;
1817 case DWARF_VALUE_LITERAL:
1819 size_t n = this_size;
1821 if (n > p->v.literal.length - source_offset)
1822 n = (p->v.literal.length >= source_offset
1823 ? p->v.literal.length - source_offset
1826 intermediate_buffer = p->v.literal.data + source_offset;
1830 /* These bits show up as zeros -- but do not cause the value
1831 to be considered optimized-out. */
1832 case DWARF_VALUE_IMPLICIT_POINTER:
1835 case DWARF_VALUE_OPTIMIZED_OUT:
1836 mark_value_bits_optimized_out (v, offset, this_size_bits);
1840 internal_error (__FILE__, __LINE__, _("invalid location type"));
1843 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1844 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1845 copy_bitwise (contents, dest_offset_bits,
1846 intermediate_buffer, source_offset_bits % 8,
1847 this_size_bits, bits_big_endian);
1849 offset += this_size_bits;
1852 do_cleanups (cleanup);
1856 write_pieced_value (struct value *to, struct value *from)
1860 ULONGEST bits_to_skip;
1861 const gdb_byte *contents;
1862 struct piece_closure *c
1863 = (struct piece_closure *) value_computed_closure (to);
1864 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1866 size_t buffer_size = 0;
1867 gdb_byte *buffer = NULL;
1868 struct cleanup *cleanup;
1870 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1874 mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
1878 cleanup = make_cleanup (free_current_contents, &buffer);
1880 contents = value_contents (from);
1881 bits_to_skip = 8 * value_offset (to);
1882 if (value_bitsize (to))
1884 bits_to_skip += value_bitpos (to);
1885 type_len = value_bitsize (to);
1888 type_len = 8 * TYPE_LENGTH (value_type (to));
1890 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1892 struct dwarf_expr_piece *p = &c->pieces[i];
1893 size_t this_size_bits, this_size;
1894 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1896 const gdb_byte *source_buffer;
1898 this_size_bits = p->size;
1899 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1901 bits_to_skip -= this_size_bits;
1904 if (this_size_bits > type_len - offset)
1905 this_size_bits = type_len - offset;
1906 if (bits_to_skip > 0)
1908 dest_offset_bits = bits_to_skip;
1909 source_offset_bits = 0;
1910 this_size_bits -= bits_to_skip;
1915 dest_offset_bits = 0;
1916 source_offset_bits = offset;
1919 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1920 source_offset = source_offset_bits / 8;
1921 dest_offset = dest_offset_bits / 8;
1922 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1924 source_buffer = contents + source_offset;
1929 if (buffer_size < this_size)
1931 buffer_size = this_size;
1932 buffer = (gdb_byte *) xrealloc (buffer, buffer_size);
1934 source_buffer = buffer;
1938 switch (p->location)
1940 case DWARF_VALUE_REGISTER:
1942 struct gdbarch *arch = get_frame_arch (frame);
1943 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1944 int reg_offset = dest_offset;
1946 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1947 && this_size <= register_size (arch, gdb_regnum))
1949 /* Big-endian, and we want less than full size. */
1950 reg_offset = register_size (arch, gdb_regnum) - this_size;
1957 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1962 throw_error (OPTIMIZED_OUT_ERROR,
1963 _("Can't do read-modify-write to "
1964 "update bitfield; containing word "
1965 "has been optimized out"));
1967 throw_error (NOT_AVAILABLE_ERROR,
1968 _("Can't do read-modify-write to update "
1969 "bitfield; containing word "
1972 copy_bitwise (buffer, dest_offset_bits,
1973 contents, source_offset_bits,
1978 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
1979 this_size, source_buffer);
1982 case DWARF_VALUE_MEMORY:
1985 /* Only the first and last bytes can possibly have any
1987 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1988 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1989 buffer + this_size - 1, 1);
1990 copy_bitwise (buffer, dest_offset_bits,
1991 contents, source_offset_bits,
1996 write_memory (p->v.mem.addr + dest_offset,
1997 source_buffer, this_size);
2000 mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
2003 offset += this_size_bits;
2006 do_cleanups (cleanup);
2009 /* An implementation of an lval_funcs method to see whether a value is
2010 a synthetic pointer. */
2013 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
2016 struct piece_closure *c
2017 = (struct piece_closure *) value_computed_closure (value);
2020 bit_offset += 8 * value_offset (value);
2021 if (value_bitsize (value))
2022 bit_offset += value_bitpos (value);
2024 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2026 struct dwarf_expr_piece *p = &c->pieces[i];
2027 size_t this_size_bits = p->size;
2031 if (bit_offset >= this_size_bits)
2033 bit_offset -= this_size_bits;
2037 bit_length -= this_size_bits - bit_offset;
2041 bit_length -= this_size_bits;
2043 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2050 /* A wrapper function for get_frame_address_in_block. */
2053 get_frame_address_in_block_wrapper (void *baton)
2055 return get_frame_address_in_block ((struct frame_info *) baton);
2058 /* An implementation of an lval_funcs method to indirect through a
2059 pointer. This handles the synthetic pointer case when needed. */
2061 static struct value *
2062 indirect_pieced_value (struct value *value)
2064 struct piece_closure *c
2065 = (struct piece_closure *) value_computed_closure (value);
2067 struct frame_info *frame;
2068 struct dwarf2_locexpr_baton baton;
2069 int i, bit_offset, bit_length;
2070 struct dwarf_expr_piece *piece = NULL;
2071 LONGEST byte_offset;
2072 enum bfd_endian byte_order;
2074 type = check_typedef (value_type (value));
2075 if (TYPE_CODE (type) != TYPE_CODE_PTR)
2078 bit_length = 8 * TYPE_LENGTH (type);
2079 bit_offset = 8 * value_offset (value);
2080 if (value_bitsize (value))
2081 bit_offset += value_bitpos (value);
2083 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2085 struct dwarf_expr_piece *p = &c->pieces[i];
2086 size_t this_size_bits = p->size;
2090 if (bit_offset >= this_size_bits)
2092 bit_offset -= this_size_bits;
2096 bit_length -= this_size_bits - bit_offset;
2100 bit_length -= this_size_bits;
2102 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2105 if (bit_length != 0)
2106 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
2112 frame = get_selected_frame (_("No frame selected."));
2114 /* This is an offset requested by GDB, such as value subscripts.
2115 However, due to how synthetic pointers are implemented, this is
2116 always presented to us as a pointer type. This means we have to
2117 sign-extend it manually as appropriate. Use raw
2118 extract_signed_integer directly rather than value_as_address and
2119 sign extend afterwards on architectures that would need it
2120 (mostly everywhere except MIPS, which has signed addresses) as
2121 the later would go through gdbarch_pointer_to_address and thus
2122 return a CORE_ADDR with high bits set on architectures that
2123 encode address spaces and other things in CORE_ADDR. */
2124 byte_order = gdbarch_byte_order (get_frame_arch (frame));
2125 byte_offset = extract_signed_integer (value_contents (value),
2126 TYPE_LENGTH (type), byte_order);
2127 byte_offset += piece->v.ptr.offset;
2131 = dwarf2_fetch_die_loc_sect_off (piece->v.ptr.die, c->per_cu,
2132 get_frame_address_in_block_wrapper,
2135 if (baton.data != NULL)
2136 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
2137 baton.data, baton.size, baton.per_cu,
2141 struct obstack temp_obstack;
2142 struct cleanup *cleanup;
2143 const gdb_byte *bytes;
2145 struct value *result;
2147 obstack_init (&temp_obstack);
2148 cleanup = make_cleanup_obstack_free (&temp_obstack);
2150 bytes = dwarf2_fetch_constant_bytes (piece->v.ptr.die, c->per_cu,
2151 &temp_obstack, &len);
2153 result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
2157 || byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > len)
2158 invalid_synthetic_pointer ();
2159 bytes += byte_offset;
2160 result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
2163 do_cleanups (cleanup);
2169 copy_pieced_value_closure (const struct value *v)
2171 struct piece_closure *c
2172 = (struct piece_closure *) value_computed_closure (v);
2179 free_pieced_value_closure (struct value *v)
2181 struct piece_closure *c
2182 = (struct piece_closure *) value_computed_closure (v);
2189 for (i = 0; i < c->n_pieces; ++i)
2190 if (c->pieces[i].location == DWARF_VALUE_STACK)
2191 value_free (c->pieces[i].v.value);
2198 /* Functions for accessing a variable described by DW_OP_piece. */
2199 static const struct lval_funcs pieced_value_funcs = {
2202 indirect_pieced_value,
2203 NULL, /* coerce_ref */
2204 check_pieced_synthetic_pointer,
2205 copy_pieced_value_closure,
2206 free_pieced_value_closure
2209 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
2211 const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
2213 dwarf_expr_read_addr_from_reg,
2214 dwarf_expr_get_reg_value,
2215 dwarf_expr_read_mem,
2216 dwarf_expr_frame_base,
2217 dwarf_expr_frame_cfa,
2218 dwarf_expr_frame_pc,
2219 dwarf_expr_tls_address,
2220 dwarf_expr_dwarf_call,
2221 dwarf_expr_get_base_type,
2222 dwarf_expr_push_dwarf_reg_entry_value,
2223 dwarf_expr_get_addr_index,
2224 dwarf_expr_get_obj_addr
2227 /* Evaluate a location description, starting at DATA and with length
2228 SIZE, to find the current location of variable of TYPE in the
2229 context of FRAME. BYTE_OFFSET is applied after the contents are
2232 static struct value *
2233 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2234 const gdb_byte *data, size_t size,
2235 struct dwarf2_per_cu_data *per_cu,
2236 LONGEST byte_offset)
2238 struct value *retval;
2239 struct dwarf_expr_baton baton;
2240 struct dwarf_expr_context *ctx;
2241 struct cleanup *old_chain, *value_chain;
2242 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2244 if (byte_offset < 0)
2245 invalid_synthetic_pointer ();
2248 return allocate_optimized_out_value (type);
2250 baton.frame = frame;
2251 baton.per_cu = per_cu;
2252 baton.obj_address = 0;
2254 ctx = new_dwarf_expr_context ();
2255 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2256 value_chain = make_cleanup_value_free_to_mark (value_mark ());
2258 ctx->gdbarch = get_objfile_arch (objfile);
2259 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2260 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2261 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2262 ctx->baton = &baton;
2263 ctx->funcs = &dwarf_expr_ctx_funcs;
2267 dwarf_expr_eval (ctx, data, size);
2269 CATCH (ex, RETURN_MASK_ERROR)
2271 if (ex.error == NOT_AVAILABLE_ERROR)
2273 do_cleanups (old_chain);
2274 retval = allocate_value (type);
2275 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
2278 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2280 if (entry_values_debug)
2281 exception_print (gdb_stdout, ex);
2282 do_cleanups (old_chain);
2283 return allocate_optimized_out_value (type);
2286 throw_exception (ex);
2290 if (ctx->num_pieces > 0)
2292 struct piece_closure *c;
2293 struct frame_id frame_id = get_frame_id (frame);
2294 ULONGEST bit_size = 0;
2297 for (i = 0; i < ctx->num_pieces; ++i)
2298 bit_size += ctx->pieces[i].size;
2299 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2300 invalid_synthetic_pointer ();
2302 c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
2304 /* We must clean up the value chain after creating the piece
2305 closure but before allocating the result. */
2306 do_cleanups (value_chain);
2307 retval = allocate_computed_value (type, &pieced_value_funcs, c);
2308 VALUE_FRAME_ID (retval) = frame_id;
2309 set_value_offset (retval, byte_offset);
2313 switch (ctx->location)
2315 case DWARF_VALUE_REGISTER:
2317 struct gdbarch *arch = get_frame_arch (frame);
2319 = longest_to_int (value_as_long (dwarf_expr_fetch (ctx, 0)));
2320 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, dwarf_regnum);
2322 if (byte_offset != 0)
2323 error (_("cannot use offset on synthetic pointer to register"));
2324 do_cleanups (value_chain);
2325 retval = value_from_register (type, gdb_regnum, frame);
2326 if (value_optimized_out (retval))
2330 /* This means the register has undefined value / was
2331 not saved. As we're computing the location of some
2332 variable etc. in the program, not a value for
2333 inspecting a register ($pc, $sp, etc.), return a
2334 generic optimized out value instead, so that we show
2335 <optimized out> instead of <not saved>. */
2336 do_cleanups (value_chain);
2337 tmp = allocate_value (type);
2338 value_contents_copy (tmp, 0, retval, 0, TYPE_LENGTH (type));
2344 case DWARF_VALUE_MEMORY:
2346 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
2347 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
2349 do_cleanups (value_chain);
2350 retval = value_at_lazy (type, address + byte_offset);
2351 if (in_stack_memory)
2352 set_value_stack (retval, 1);
2356 case DWARF_VALUE_STACK:
2358 struct value *value = dwarf_expr_fetch (ctx, 0);
2360 const gdb_byte *val_bytes;
2361 size_t n = TYPE_LENGTH (value_type (value));
2363 if (byte_offset + TYPE_LENGTH (type) > n)
2364 invalid_synthetic_pointer ();
2366 val_bytes = value_contents_all (value);
2367 val_bytes += byte_offset;
2370 /* Preserve VALUE because we are going to free values back
2371 to the mark, but we still need the value contents
2373 value_incref (value);
2374 do_cleanups (value_chain);
2375 make_cleanup_value_free (value);
2377 retval = allocate_value (type);
2378 contents = value_contents_raw (retval);
2379 if (n > TYPE_LENGTH (type))
2381 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2383 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2384 val_bytes += n - TYPE_LENGTH (type);
2385 n = TYPE_LENGTH (type);
2387 memcpy (contents, val_bytes, n);
2391 case DWARF_VALUE_LITERAL:
2394 const bfd_byte *ldata;
2395 size_t n = ctx->len;
2397 if (byte_offset + TYPE_LENGTH (type) > n)
2398 invalid_synthetic_pointer ();
2400 do_cleanups (value_chain);
2401 retval = allocate_value (type);
2402 contents = value_contents_raw (retval);
2404 ldata = ctx->data + byte_offset;
2407 if (n > TYPE_LENGTH (type))
2409 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2411 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2412 ldata += n - TYPE_LENGTH (type);
2413 n = TYPE_LENGTH (type);
2415 memcpy (contents, ldata, n);
2419 case DWARF_VALUE_OPTIMIZED_OUT:
2420 do_cleanups (value_chain);
2421 retval = allocate_optimized_out_value (type);
2424 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2425 operation by execute_stack_op. */
2426 case DWARF_VALUE_IMPLICIT_POINTER:
2427 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2428 it can only be encountered when making a piece. */
2430 internal_error (__FILE__, __LINE__, _("invalid location type"));
2434 set_value_initialized (retval, ctx->initialized);
2436 do_cleanups (old_chain);
2441 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2442 passes 0 as the byte_offset. */
2445 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2446 const gdb_byte *data, size_t size,
2447 struct dwarf2_per_cu_data *per_cu)
2449 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2452 /* Evaluates a dwarf expression and stores the result in VAL, expecting
2453 that the dwarf expression only produces a single CORE_ADDR. FRAME is the
2454 frame in which the expression is evaluated. ADDR is a context (location of
2455 a variable) and might be needed to evaluate the location expression.
2456 Returns 1 on success, 0 otherwise. */
2459 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
2460 struct frame_info *frame,
2464 struct dwarf_expr_context *ctx;
2465 struct dwarf_expr_baton baton;
2466 struct objfile *objfile;
2467 struct cleanup *cleanup;
2469 if (dlbaton == NULL || dlbaton->size == 0)
2472 ctx = new_dwarf_expr_context ();
2473 cleanup = make_cleanup_free_dwarf_expr_context (ctx);
2475 baton.frame = frame;
2476 baton.per_cu = dlbaton->per_cu;
2477 baton.obj_address = addr;
2479 objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2481 ctx->gdbarch = get_objfile_arch (objfile);
2482 ctx->addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2483 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (dlbaton->per_cu);
2484 ctx->offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2485 ctx->funcs = &dwarf_expr_ctx_funcs;
2486 ctx->baton = &baton;
2488 dwarf_expr_eval (ctx, dlbaton->data, dlbaton->size);
2490 switch (ctx->location)
2492 case DWARF_VALUE_REGISTER:
2493 case DWARF_VALUE_MEMORY:
2494 case DWARF_VALUE_STACK:
2495 *valp = dwarf_expr_fetch_address (ctx, 0);
2496 if (ctx->location == DWARF_VALUE_REGISTER)
2497 *valp = dwarf_expr_read_addr_from_reg (&baton, *valp);
2498 do_cleanups (cleanup);
2500 case DWARF_VALUE_LITERAL:
2501 *valp = extract_signed_integer (ctx->data, ctx->len,
2502 gdbarch_byte_order (ctx->gdbarch));
2503 do_cleanups (cleanup);
2505 /* Unsupported dwarf values. */
2506 case DWARF_VALUE_OPTIMIZED_OUT:
2507 case DWARF_VALUE_IMPLICIT_POINTER:
2511 do_cleanups (cleanup);
2515 /* See dwarf2loc.h. */
2518 dwarf2_evaluate_property (const struct dynamic_prop *prop,
2519 struct frame_info *frame,
2520 struct property_addr_info *addr_stack,
2526 if (frame == NULL && has_stack_frames ())
2527 frame = get_selected_frame (NULL);
2533 const struct dwarf2_property_baton *baton
2534 = (const struct dwarf2_property_baton *) prop->data.baton;
2536 if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame,
2537 addr_stack ? addr_stack->addr : 0,
2540 if (baton->referenced_type)
2542 struct value *val = value_at (baton->referenced_type, *value);
2544 *value = value_as_address (val);
2553 struct dwarf2_property_baton *baton
2554 = (struct dwarf2_property_baton *) prop->data.baton;
2555 CORE_ADDR pc = get_frame_address_in_block (frame);
2556 const gdb_byte *data;
2560 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2563 val = dwarf2_evaluate_loc_desc (baton->referenced_type, frame, data,
2564 size, baton->loclist.per_cu);
2565 if (!value_optimized_out (val))
2567 *value = value_as_address (val);
2575 *value = prop->data.const_val;
2578 case PROP_ADDR_OFFSET:
2580 struct dwarf2_property_baton *baton
2581 = (struct dwarf2_property_baton *) prop->data.baton;
2582 struct property_addr_info *pinfo;
2585 for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
2586 if (pinfo->type == baton->referenced_type)
2589 error (_("cannot find reference address for offset property"));
2590 if (pinfo->valaddr != NULL)
2591 val = value_from_contents
2592 (baton->offset_info.type,
2593 pinfo->valaddr + baton->offset_info.offset);
2595 val = value_at (baton->offset_info.type,
2596 pinfo->addr + baton->offset_info.offset);
2597 *value = value_as_address (val);
2605 /* See dwarf2loc.h. */
2608 dwarf2_compile_property_to_c (struct ui_file *stream,
2609 const char *result_name,
2610 struct gdbarch *gdbarch,
2611 unsigned char *registers_used,
2612 const struct dynamic_prop *prop,
2616 struct dwarf2_property_baton *baton
2617 = (struct dwarf2_property_baton *) prop->data.baton;
2618 const gdb_byte *data;
2620 struct dwarf2_per_cu_data *per_cu;
2622 if (prop->kind == PROP_LOCEXPR)
2624 data = baton->locexpr.data;
2625 size = baton->locexpr.size;
2626 per_cu = baton->locexpr.per_cu;
2630 gdb_assert (prop->kind == PROP_LOCLIST);
2632 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2633 per_cu = baton->loclist.per_cu;
2636 compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2637 gdbarch, registers_used,
2638 dwarf2_per_cu_addr_size (per_cu),
2639 data, data + size, per_cu);
2643 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
2645 struct needs_frame_baton
2648 struct dwarf2_per_cu_data *per_cu;
2651 /* Reads from registers do require a frame. */
2653 needs_frame_read_addr_from_reg (void *baton, int regnum)
2655 struct needs_frame_baton *nf_baton = (struct needs_frame_baton *) baton;
2657 nf_baton->needs_frame = 1;
2661 /* struct dwarf_expr_context_funcs' "get_reg_value" callback:
2662 Reads from registers do require a frame. */
2664 static struct value *
2665 needs_frame_get_reg_value (void *baton, struct type *type, int regnum)
2667 struct needs_frame_baton *nf_baton = (struct needs_frame_baton *) baton;
2669 nf_baton->needs_frame = 1;
2670 return value_zero (type, not_lval);
2673 /* Reads from memory do not require a frame. */
2675 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2677 memset (buf, 0, len);
2680 /* Frame-relative accesses do require a frame. */
2682 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
2684 static gdb_byte lit0 = DW_OP_lit0;
2685 struct needs_frame_baton *nf_baton = (struct needs_frame_baton *) baton;
2690 nf_baton->needs_frame = 1;
2693 /* CFA accesses require a frame. */
2696 needs_frame_frame_cfa (void *baton)
2698 struct needs_frame_baton *nf_baton = (struct needs_frame_baton *) baton;
2700 nf_baton->needs_frame = 1;
2704 /* Thread-local accesses do require a frame. */
2706 needs_frame_tls_address (void *baton, CORE_ADDR offset)
2708 struct needs_frame_baton *nf_baton = (struct needs_frame_baton *) baton;
2710 nf_baton->needs_frame = 1;
2714 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
2717 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
2719 struct needs_frame_baton *nf_baton = (struct needs_frame_baton *) ctx->baton;
2721 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2722 ctx->funcs->get_frame_pc, ctx->baton);
2725 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2728 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2729 enum call_site_parameter_kind kind,
2730 union call_site_parameter_u kind_u, int deref_size)
2732 struct needs_frame_baton *nf_baton = (struct needs_frame_baton *) ctx->baton;
2734 nf_baton->needs_frame = 1;
2736 /* The expression may require some stub values on DWARF stack. */
2737 dwarf_expr_push_address (ctx, 0, 0);
2740 /* DW_OP_GNU_addr_index doesn't require a frame. */
2743 needs_get_addr_index (void *baton, unsigned int index)
2745 /* Nothing to do. */
2749 /* DW_OP_push_object_address has a frame already passed through. */
2752 needs_get_obj_addr (void *baton)
2754 /* Nothing to do. */
2758 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
2760 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
2762 needs_frame_read_addr_from_reg,
2763 needs_frame_get_reg_value,
2764 needs_frame_read_mem,
2765 needs_frame_frame_base,
2766 needs_frame_frame_cfa,
2767 needs_frame_frame_cfa, /* get_frame_pc */
2768 needs_frame_tls_address,
2769 needs_frame_dwarf_call,
2770 NULL, /* get_base_type */
2771 needs_dwarf_reg_entry_value,
2772 needs_get_addr_index,
2776 /* Return non-zero iff the location expression at DATA (length SIZE)
2777 requires a frame to evaluate. */
2780 dwarf2_loc_desc_needs_frame (const gdb_byte *data, size_t size,
2781 struct dwarf2_per_cu_data *per_cu)
2783 struct needs_frame_baton baton;
2784 struct dwarf_expr_context *ctx;
2786 struct cleanup *old_chain;
2787 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2789 baton.needs_frame = 0;
2790 baton.per_cu = per_cu;
2792 ctx = new_dwarf_expr_context ();
2793 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2794 make_cleanup_value_free_to_mark (value_mark ());
2796 ctx->gdbarch = get_objfile_arch (objfile);
2797 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2798 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2799 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2800 ctx->baton = &baton;
2801 ctx->funcs = &needs_frame_ctx_funcs;
2803 dwarf_expr_eval (ctx, data, size);
2805 in_reg = ctx->location == DWARF_VALUE_REGISTER;
2807 if (ctx->num_pieces > 0)
2811 /* If the location has several pieces, and any of them are in
2812 registers, then we will need a frame to fetch them from. */
2813 for (i = 0; i < ctx->num_pieces; i++)
2814 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
2818 do_cleanups (old_chain);
2820 return baton.needs_frame || in_reg;
2823 /* A helper function that throws an unimplemented error mentioning a
2824 given DWARF operator. */
2827 unimplemented (unsigned int op)
2829 const char *name = get_DW_OP_name (op);
2832 error (_("DWARF operator %s cannot be translated to an agent expression"),
2835 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2836 "to an agent expression"),
2842 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2843 can issue a complaint, which is better than having every target's
2844 implementation of dwarf2_reg_to_regnum do it. */
2847 dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
2849 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2853 complaint (&symfile_complaints,
2854 _("bad DWARF register number %d"), dwarf_reg);
2859 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2860 Throw an error because DWARF_REG is bad. */
2863 throw_bad_regnum_error (ULONGEST dwarf_reg)
2865 /* Still want to print -1 as "-1".
2866 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2867 but that's overkill for now. */
2868 if ((int) dwarf_reg == dwarf_reg)
2869 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2870 error (_("Unable to access DWARF register number %s"),
2871 pulongest (dwarf_reg));
2874 /* See dwarf2loc.h. */
2877 dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2881 if (dwarf_reg > INT_MAX)
2882 throw_bad_regnum_error (dwarf_reg);
2883 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2884 bad, but that's ok. */
2885 reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2887 throw_bad_regnum_error (dwarf_reg);
2891 /* A helper function that emits an access to memory. ARCH is the
2892 target architecture. EXPR is the expression which we are building.
2893 NBITS is the number of bits we want to read. This emits the
2894 opcodes needed to read the memory and then extract the desired
2898 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2900 ULONGEST nbytes = (nbits + 7) / 8;
2902 gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2905 ax_trace_quick (expr, nbytes);
2908 ax_simple (expr, aop_ref8);
2909 else if (nbits <= 16)
2910 ax_simple (expr, aop_ref16);
2911 else if (nbits <= 32)
2912 ax_simple (expr, aop_ref32);
2914 ax_simple (expr, aop_ref64);
2916 /* If we read exactly the number of bytes we wanted, we're done. */
2917 if (8 * nbytes == nbits)
2920 if (gdbarch_bits_big_endian (arch))
2922 /* On a bits-big-endian machine, we want the high-order
2924 ax_const_l (expr, 8 * nbytes - nbits);
2925 ax_simple (expr, aop_rsh_unsigned);
2929 /* On a bits-little-endian box, we want the low-order NBITS. */
2930 ax_zero_ext (expr, nbits);
2934 /* A helper function to return the frame's PC. */
2937 get_ax_pc (void *baton)
2939 struct agent_expr *expr = (struct agent_expr *) baton;
2944 /* Compile a DWARF location expression to an agent expression.
2946 EXPR is the agent expression we are building.
2947 LOC is the agent value we modify.
2948 ARCH is the architecture.
2949 ADDR_SIZE is the size of addresses, in bytes.
2950 OP_PTR is the start of the location expression.
2951 OP_END is one past the last byte of the location expression.
2953 This will throw an exception for various kinds of errors -- for
2954 example, if the expression cannot be compiled, or if the expression
2958 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2959 struct gdbarch *arch, unsigned int addr_size,
2960 const gdb_byte *op_ptr, const gdb_byte *op_end,
2961 struct dwarf2_per_cu_data *per_cu)
2963 struct cleanup *cleanups;
2965 VEC(int) *dw_labels = NULL, *patches = NULL;
2966 const gdb_byte * const base = op_ptr;
2967 const gdb_byte *previous_piece = op_ptr;
2968 enum bfd_endian byte_order = gdbarch_byte_order (arch);
2969 ULONGEST bits_collected = 0;
2970 unsigned int addr_size_bits = 8 * addr_size;
2971 int bits_big_endian = gdbarch_bits_big_endian (arch);
2973 offsets = XNEWVEC (int, op_end - op_ptr);
2974 cleanups = make_cleanup (xfree, offsets);
2976 for (i = 0; i < op_end - op_ptr; ++i)
2979 make_cleanup (VEC_cleanup (int), &dw_labels);
2980 make_cleanup (VEC_cleanup (int), &patches);
2982 /* By default we are making an address. */
2983 loc->kind = axs_lvalue_memory;
2985 while (op_ptr < op_end)
2987 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
2988 uint64_t uoffset, reg;
2992 offsets[op_ptr - base] = expr->len;
2995 /* Our basic approach to code generation is to map DWARF
2996 operations directly to AX operations. However, there are
2999 First, DWARF works on address-sized units, but AX always uses
3000 LONGEST. For most operations we simply ignore this
3001 difference; instead we generate sign extensions as needed
3002 before division and comparison operations. It would be nice
3003 to omit the sign extensions, but there is no way to determine
3004 the size of the target's LONGEST. (This code uses the size
3005 of the host LONGEST in some cases -- that is a bug but it is
3008 Second, some DWARF operations cannot be translated to AX.
3009 For these we simply fail. See
3010 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
3045 ax_const_l (expr, op - DW_OP_lit0);
3049 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3050 op_ptr += addr_size;
3051 /* Some versions of GCC emit DW_OP_addr before
3052 DW_OP_GNU_push_tls_address. In this case the value is an
3053 index, not an address. We don't support things like
3054 branching between the address and the TLS op. */
3055 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
3056 uoffset += dwarf2_per_cu_text_offset (per_cu);
3057 ax_const_l (expr, uoffset);
3061 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
3065 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
3069 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
3073 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
3077 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
3081 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
3085 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
3089 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
3093 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3094 ax_const_l (expr, uoffset);
3097 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3098 ax_const_l (expr, offset);
3133 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3134 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3135 loc->kind = axs_lvalue_register;
3139 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3140 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3141 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3142 loc->kind = axs_lvalue_register;
3145 case DW_OP_implicit_value:
3149 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3150 if (op_ptr + len > op_end)
3151 error (_("DW_OP_implicit_value: too few bytes available."));
3152 if (len > sizeof (ULONGEST))
3153 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3156 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3159 dwarf_expr_require_composition (op_ptr, op_end,
3160 "DW_OP_implicit_value");
3162 loc->kind = axs_rvalue;
3166 case DW_OP_stack_value:
3167 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3168 loc->kind = axs_rvalue;
3203 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3204 i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3208 ax_const_l (expr, offset);
3209 ax_simple (expr, aop_add);
3214 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3215 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3216 i = dwarf_reg_to_regnum_or_error (arch, reg);
3220 ax_const_l (expr, offset);
3221 ax_simple (expr, aop_add);
3227 const gdb_byte *datastart;
3229 const struct block *b;
3230 struct symbol *framefunc;
3232 b = block_for_pc (expr->scope);
3235 error (_("No block found for address"));
3237 framefunc = block_linkage_function (b);
3240 error (_("No function found for block"));
3242 func_get_frame_base_dwarf_block (framefunc, expr->scope,
3243 &datastart, &datalen);
3245 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3246 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
3247 datastart + datalen, per_cu);
3248 if (loc->kind == axs_lvalue_register)
3249 require_rvalue (expr, loc);
3253 ax_const_l (expr, offset);
3254 ax_simple (expr, aop_add);
3257 loc->kind = axs_lvalue_memory;
3262 ax_simple (expr, aop_dup);
3266 ax_simple (expr, aop_pop);
3271 ax_pick (expr, offset);
3275 ax_simple (expr, aop_swap);
3283 ax_simple (expr, aop_rot);
3287 case DW_OP_deref_size:
3291 if (op == DW_OP_deref_size)
3296 if (size != 1 && size != 2 && size != 4 && size != 8)
3297 error (_("Unsupported size %d in %s"),
3298 size, get_DW_OP_name (op));
3299 access_memory (arch, expr, size * TARGET_CHAR_BIT);
3304 /* Sign extend the operand. */
3305 ax_ext (expr, addr_size_bits);
3306 ax_simple (expr, aop_dup);
3307 ax_const_l (expr, 0);
3308 ax_simple (expr, aop_less_signed);
3309 ax_simple (expr, aop_log_not);
3310 i = ax_goto (expr, aop_if_goto);
3311 /* We have to emit 0 - X. */
3312 ax_const_l (expr, 0);
3313 ax_simple (expr, aop_swap);
3314 ax_simple (expr, aop_sub);
3315 ax_label (expr, i, expr->len);
3319 /* No need to sign extend here. */
3320 ax_const_l (expr, 0);
3321 ax_simple (expr, aop_swap);
3322 ax_simple (expr, aop_sub);
3326 /* Sign extend the operand. */
3327 ax_ext (expr, addr_size_bits);
3328 ax_simple (expr, aop_bit_not);
3331 case DW_OP_plus_uconst:
3332 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3333 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3334 but we micro-optimize anyhow. */
3337 ax_const_l (expr, reg);
3338 ax_simple (expr, aop_add);
3343 ax_simple (expr, aop_bit_and);
3347 /* Sign extend the operands. */
3348 ax_ext (expr, addr_size_bits);
3349 ax_simple (expr, aop_swap);
3350 ax_ext (expr, addr_size_bits);
3351 ax_simple (expr, aop_swap);
3352 ax_simple (expr, aop_div_signed);
3356 ax_simple (expr, aop_sub);
3360 ax_simple (expr, aop_rem_unsigned);
3364 ax_simple (expr, aop_mul);
3368 ax_simple (expr, aop_bit_or);
3372 ax_simple (expr, aop_add);
3376 ax_simple (expr, aop_lsh);
3380 ax_simple (expr, aop_rsh_unsigned);
3384 ax_simple (expr, aop_rsh_signed);
3388 ax_simple (expr, aop_bit_xor);
3392 /* Sign extend the operands. */
3393 ax_ext (expr, addr_size_bits);
3394 ax_simple (expr, aop_swap);
3395 ax_ext (expr, addr_size_bits);
3396 /* Note no swap here: A <= B is !(B < A). */
3397 ax_simple (expr, aop_less_signed);
3398 ax_simple (expr, aop_log_not);
3402 /* Sign extend the operands. */
3403 ax_ext (expr, addr_size_bits);
3404 ax_simple (expr, aop_swap);
3405 ax_ext (expr, addr_size_bits);
3406 ax_simple (expr, aop_swap);
3407 /* A >= B is !(A < B). */
3408 ax_simple (expr, aop_less_signed);
3409 ax_simple (expr, aop_log_not);
3413 /* Sign extend the operands. */
3414 ax_ext (expr, addr_size_bits);
3415 ax_simple (expr, aop_swap);
3416 ax_ext (expr, addr_size_bits);
3417 /* No need for a second swap here. */
3418 ax_simple (expr, aop_equal);
3422 /* Sign extend the operands. */
3423 ax_ext (expr, addr_size_bits);
3424 ax_simple (expr, aop_swap);
3425 ax_ext (expr, addr_size_bits);
3426 ax_simple (expr, aop_swap);
3427 ax_simple (expr, aop_less_signed);
3431 /* Sign extend the operands. */
3432 ax_ext (expr, addr_size_bits);
3433 ax_simple (expr, aop_swap);
3434 ax_ext (expr, addr_size_bits);
3435 /* Note no swap here: A > B is B < A. */
3436 ax_simple (expr, aop_less_signed);
3440 /* Sign extend the operands. */
3441 ax_ext (expr, addr_size_bits);
3442 ax_simple (expr, aop_swap);
3443 ax_ext (expr, addr_size_bits);
3444 /* No need for a swap here. */
3445 ax_simple (expr, aop_equal);
3446 ax_simple (expr, aop_log_not);
3449 case DW_OP_call_frame_cfa:
3452 CORE_ADDR text_offset;
3454 const gdb_byte *cfa_start, *cfa_end;
3456 if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3458 &text_offset, &cfa_start, &cfa_end))
3461 ax_reg (expr, regnum);
3464 ax_const_l (expr, off);
3465 ax_simple (expr, aop_add);
3470 /* Another expression. */
3471 ax_const_l (expr, text_offset);
3472 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3473 cfa_start, cfa_end, per_cu);
3476 loc->kind = axs_lvalue_memory;
3480 case DW_OP_GNU_push_tls_address:
3484 case DW_OP_push_object_address:
3489 offset = extract_signed_integer (op_ptr, 2, byte_order);
3491 i = ax_goto (expr, aop_goto);
3492 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3493 VEC_safe_push (int, patches, i);
3497 offset = extract_signed_integer (op_ptr, 2, byte_order);
3499 /* Zero extend the operand. */
3500 ax_zero_ext (expr, addr_size_bits);
3501 i = ax_goto (expr, aop_if_goto);
3502 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3503 VEC_safe_push (int, patches, i);
3510 case DW_OP_bit_piece:
3512 uint64_t size, offset;
3514 if (op_ptr - 1 == previous_piece)
3515 error (_("Cannot translate empty pieces to agent expressions"));
3516 previous_piece = op_ptr - 1;
3518 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3519 if (op == DW_OP_piece)
3525 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3527 if (bits_collected + size > 8 * sizeof (LONGEST))
3528 error (_("Expression pieces exceed word size"));
3530 /* Access the bits. */
3533 case axs_lvalue_register:
3534 ax_reg (expr, loc->u.reg);
3537 case axs_lvalue_memory:
3538 /* Offset the pointer, if needed. */
3541 ax_const_l (expr, offset / 8);
3542 ax_simple (expr, aop_add);
3545 access_memory (arch, expr, size);
3549 /* For a bits-big-endian target, shift up what we already
3550 have. For a bits-little-endian target, shift up the
3551 new data. Note that there is a potential bug here if
3552 the DWARF expression leaves multiple values on the
3554 if (bits_collected > 0)
3556 if (bits_big_endian)
3558 ax_simple (expr, aop_swap);
3559 ax_const_l (expr, size);
3560 ax_simple (expr, aop_lsh);
3561 /* We don't need a second swap here, because
3562 aop_bit_or is symmetric. */
3566 ax_const_l (expr, size);
3567 ax_simple (expr, aop_lsh);
3569 ax_simple (expr, aop_bit_or);
3572 bits_collected += size;
3573 loc->kind = axs_rvalue;
3577 case DW_OP_GNU_uninit:
3583 struct dwarf2_locexpr_baton block;
3584 int size = (op == DW_OP_call2 ? 2 : 4);
3587 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3590 offset.cu_off = uoffset;
3591 block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3594 /* DW_OP_call_ref is currently not supported. */
3595 gdb_assert (block.per_cu == per_cu);
3597 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3598 block.data, block.data + block.size,
3603 case DW_OP_call_ref:
3611 /* Patch all the branches we emitted. */
3612 for (i = 0; i < VEC_length (int, patches); ++i)
3614 int targ = offsets[VEC_index (int, dw_labels, i)];
3616 internal_error (__FILE__, __LINE__, _("invalid label"));
3617 ax_label (expr, VEC_index (int, patches, i), targ);
3620 do_cleanups (cleanups);
3624 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3625 evaluator to calculate the location. */
3626 static struct value *
3627 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3629 struct dwarf2_locexpr_baton *dlbaton
3630 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3633 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3634 dlbaton->size, dlbaton->per_cu);
3639 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3640 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3643 static struct value *
3644 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3646 struct dwarf2_locexpr_baton *dlbaton
3647 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3649 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3653 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3655 locexpr_read_needs_frame (struct symbol *symbol)
3657 struct dwarf2_locexpr_baton *dlbaton
3658 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3660 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
3664 /* Return true if DATA points to the end of a piece. END is one past
3665 the last byte in the expression. */
3668 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3670 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3673 /* Helper for locexpr_describe_location_piece that finds the name of a
3677 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3681 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3682 We'd rather print *something* here than throw an error. */
3683 regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3684 /* gdbarch_register_name may just return "", return something more
3685 descriptive for bad register numbers. */
3688 /* The text is output as "$bad_register_number".
3689 That is why we use the underscores. */
3690 return _("bad_register_number");
3692 return gdbarch_register_name (gdbarch, regnum);
3695 /* Nicely describe a single piece of a location, returning an updated
3696 position in the bytecode sequence. This function cannot recognize
3697 all locations; if a location is not recognized, it simply returns
3698 DATA. If there is an error during reading, e.g. we run off the end
3699 of the buffer, an error is thrown. */
3701 static const gdb_byte *
3702 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3703 CORE_ADDR addr, struct objfile *objfile,
3704 struct dwarf2_per_cu_data *per_cu,
3705 const gdb_byte *data, const gdb_byte *end,
3706 unsigned int addr_size)
3708 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3711 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3713 fprintf_filtered (stream, _("a variable in $%s"),
3714 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3717 else if (data[0] == DW_OP_regx)
3721 data = safe_read_uleb128 (data + 1, end, ®);
3722 fprintf_filtered (stream, _("a variable in $%s"),
3723 locexpr_regname (gdbarch, reg));
3725 else if (data[0] == DW_OP_fbreg)
3727 const struct block *b;
3728 struct symbol *framefunc;
3730 int64_t frame_offset;
3731 const gdb_byte *base_data, *new_data, *save_data = data;
3733 int64_t base_offset = 0;
3735 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3736 if (!piece_end_p (new_data, end))
3740 b = block_for_pc (addr);
3743 error (_("No block found for address for symbol \"%s\"."),
3744 SYMBOL_PRINT_NAME (symbol));
3746 framefunc = block_linkage_function (b);
3749 error (_("No function found for block for symbol \"%s\"."),
3750 SYMBOL_PRINT_NAME (symbol));
3752 func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
3754 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3756 const gdb_byte *buf_end;
3758 frame_reg = base_data[0] - DW_OP_breg0;
3759 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3761 if (buf_end != base_data + base_size)
3762 error (_("Unexpected opcode after "
3763 "DW_OP_breg%u for symbol \"%s\"."),
3764 frame_reg, SYMBOL_PRINT_NAME (symbol));
3766 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3768 /* The frame base is just the register, with no offset. */
3769 frame_reg = base_data[0] - DW_OP_reg0;
3774 /* We don't know what to do with the frame base expression,
3775 so we can't trace this variable; give up. */
3779 fprintf_filtered (stream,
3780 _("a variable at frame base reg $%s offset %s+%s"),
3781 locexpr_regname (gdbarch, frame_reg),
3782 plongest (base_offset), plongest (frame_offset));
3784 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3785 && piece_end_p (data, end))
3789 data = safe_read_sleb128 (data + 1, end, &offset);
3791 fprintf_filtered (stream,
3792 _("a variable at offset %s from base reg $%s"),
3794 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3797 /* The location expression for a TLS variable looks like this (on a
3800 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3801 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3803 0x3 is the encoding for DW_OP_addr, which has an operand as long
3804 as the size of an address on the target machine (here is 8
3805 bytes). Note that more recent version of GCC emit DW_OP_const4u
3806 or DW_OP_const8u, depending on address size, rather than
3807 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3808 The operand represents the offset at which the variable is within
3809 the thread local storage. */
3811 else if (data + 1 + addr_size < end
3812 && (data[0] == DW_OP_addr
3813 || (addr_size == 4 && data[0] == DW_OP_const4u)
3814 || (addr_size == 8 && data[0] == DW_OP_const8u))
3815 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3816 && piece_end_p (data + 2 + addr_size, end))
3819 offset = extract_unsigned_integer (data + 1, addr_size,
3820 gdbarch_byte_order (gdbarch));
3822 fprintf_filtered (stream,
3823 _("a thread-local variable at offset 0x%s "
3824 "in the thread-local storage for `%s'"),
3825 phex_nz (offset, addr_size), objfile_name (objfile));
3827 data += 1 + addr_size + 1;
3830 /* With -gsplit-dwarf a TLS variable can also look like this:
3831 DW_AT_location : 3 byte block: fc 4 e0
3832 (DW_OP_GNU_const_index: 4;
3833 DW_OP_GNU_push_tls_address) */
3834 else if (data + 3 <= end
3835 && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3836 && data[0] == DW_OP_GNU_const_index
3838 && data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3839 && piece_end_p (data + 2 + leb128_size, end))
3843 data = safe_read_uleb128 (data + 1, end, &offset);
3844 offset = dwarf2_read_addr_index (per_cu, offset);
3845 fprintf_filtered (stream,
3846 _("a thread-local variable at offset 0x%s "
3847 "in the thread-local storage for `%s'"),
3848 phex_nz (offset, addr_size), objfile_name (objfile));
3852 else if (data[0] >= DW_OP_lit0
3853 && data[0] <= DW_OP_lit31
3855 && data[1] == DW_OP_stack_value)
3857 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3864 /* Disassemble an expression, stopping at the end of a piece or at the
3865 end of the expression. Returns a pointer to the next unread byte
3866 in the input expression. If ALL is nonzero, then this function
3867 will keep going until it reaches the end of the expression.
3868 If there is an error during reading, e.g. we run off the end
3869 of the buffer, an error is thrown. */
3871 static const gdb_byte *
3872 disassemble_dwarf_expression (struct ui_file *stream,
3873 struct gdbarch *arch, unsigned int addr_size,
3874 int offset_size, const gdb_byte *start,
3875 const gdb_byte *data, const gdb_byte *end,
3876 int indent, int all,
3877 struct dwarf2_per_cu_data *per_cu)
3881 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3883 enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
3888 name = get_DW_OP_name (op);
3891 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3892 op, (long) (data - 1 - start));
3893 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3894 (long) (data - 1 - start), name);
3899 ul = extract_unsigned_integer (data, addr_size,
3900 gdbarch_byte_order (arch));
3902 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3906 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3908 fprintf_filtered (stream, " %s", pulongest (ul));
3911 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3913 fprintf_filtered (stream, " %s", plongest (l));
3916 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3918 fprintf_filtered (stream, " %s", pulongest (ul));
3921 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3923 fprintf_filtered (stream, " %s", plongest (l));
3926 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3928 fprintf_filtered (stream, " %s", pulongest (ul));
3931 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3933 fprintf_filtered (stream, " %s", plongest (l));
3936 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3938 fprintf_filtered (stream, " %s", pulongest (ul));
3941 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3943 fprintf_filtered (stream, " %s", plongest (l));
3946 data = safe_read_uleb128 (data, end, &ul);
3947 fprintf_filtered (stream, " %s", pulongest (ul));
3950 data = safe_read_sleb128 (data, end, &l);
3951 fprintf_filtered (stream, " %s", plongest (l));
3986 fprintf_filtered (stream, " [$%s]",
3987 locexpr_regname (arch, op - DW_OP_reg0));
3991 data = safe_read_uleb128 (data, end, &ul);
3992 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3993 locexpr_regname (arch, (int) ul));
3996 case DW_OP_implicit_value:
3997 data = safe_read_uleb128 (data, end, &ul);
3999 fprintf_filtered (stream, " %s", pulongest (ul));
4034 data = safe_read_sleb128 (data, end, &l);
4035 fprintf_filtered (stream, " %s [$%s]", plongest (l),
4036 locexpr_regname (arch, op - DW_OP_breg0));
4040 data = safe_read_uleb128 (data, end, &ul);
4041 data = safe_read_sleb128 (data, end, &l);
4042 fprintf_filtered (stream, " register %s [$%s] offset %s",
4044 locexpr_regname (arch, (int) ul),
4049 data = safe_read_sleb128 (data, end, &l);
4050 fprintf_filtered (stream, " %s", plongest (l));
4053 case DW_OP_xderef_size:
4054 case DW_OP_deref_size:
4056 fprintf_filtered (stream, " %d", *data);
4060 case DW_OP_plus_uconst:
4061 data = safe_read_uleb128 (data, end, &ul);
4062 fprintf_filtered (stream, " %s", pulongest (ul));
4066 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4068 fprintf_filtered (stream, " to %ld",
4069 (long) (data + l - start));
4073 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4075 fprintf_filtered (stream, " %ld",
4076 (long) (data + l - start));
4080 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4082 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
4086 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4088 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4091 case DW_OP_call_ref:
4092 ul = extract_unsigned_integer (data, offset_size,
4093 gdbarch_byte_order (arch));
4094 data += offset_size;
4095 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4099 data = safe_read_uleb128 (data, end, &ul);
4100 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
4103 case DW_OP_bit_piece:
4107 data = safe_read_uleb128 (data, end, &ul);
4108 data = safe_read_uleb128 (data, end, &offset);
4109 fprintf_filtered (stream, " size %s offset %s (bits)",
4110 pulongest (ul), pulongest (offset));
4114 case DW_OP_GNU_implicit_pointer:
4116 ul = extract_unsigned_integer (data, offset_size,
4117 gdbarch_byte_order (arch));
4118 data += offset_size;
4120 data = safe_read_sleb128 (data, end, &l);
4122 fprintf_filtered (stream, " DIE %s offset %s",
4123 phex_nz (ul, offset_size),
4128 case DW_OP_GNU_deref_type:
4130 int addr_size = *data++;
4134 data = safe_read_uleb128 (data, end, &ul);
4136 type = dwarf2_get_die_type (offset, per_cu);
4137 fprintf_filtered (stream, "<");
4138 type_print (type, "", stream, -1);
4139 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
4144 case DW_OP_GNU_const_type:
4149 data = safe_read_uleb128 (data, end, &ul);
4150 type_die.cu_off = ul;
4151 type = dwarf2_get_die_type (type_die, per_cu);
4152 fprintf_filtered (stream, "<");
4153 type_print (type, "", stream, -1);
4154 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
4158 case DW_OP_GNU_regval_type:
4164 data = safe_read_uleb128 (data, end, ®);
4165 data = safe_read_uleb128 (data, end, &ul);
4166 type_die.cu_off = ul;
4168 type = dwarf2_get_die_type (type_die, per_cu);
4169 fprintf_filtered (stream, "<");
4170 type_print (type, "", stream, -1);
4171 fprintf_filtered (stream, " [0x%s]> [$%s]",
4172 phex_nz (type_die.cu_off, 0),
4173 locexpr_regname (arch, reg));
4177 case DW_OP_GNU_convert:
4178 case DW_OP_GNU_reinterpret:
4182 data = safe_read_uleb128 (data, end, &ul);
4183 type_die.cu_off = ul;
4185 if (type_die.cu_off == 0)
4186 fprintf_filtered (stream, "<0>");
4191 type = dwarf2_get_die_type (type_die, per_cu);
4192 fprintf_filtered (stream, "<");
4193 type_print (type, "", stream, -1);
4194 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
4199 case DW_OP_GNU_entry_value:
4200 data = safe_read_uleb128 (data, end, &ul);
4201 fputc_filtered ('\n', stream);
4202 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4203 start, data, data + ul, indent + 2,
4208 case DW_OP_GNU_parameter_ref:
4209 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4211 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4214 case DW_OP_GNU_addr_index:
4215 data = safe_read_uleb128 (data, end, &ul);
4216 ul = dwarf2_read_addr_index (per_cu, ul);
4217 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4219 case DW_OP_GNU_const_index:
4220 data = safe_read_uleb128 (data, end, &ul);
4221 ul = dwarf2_read_addr_index (per_cu, ul);
4222 fprintf_filtered (stream, " %s", pulongest (ul));
4226 fprintf_filtered (stream, "\n");
4232 /* Describe a single location, which may in turn consist of multiple
4236 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
4237 struct ui_file *stream,
4238 const gdb_byte *data, size_t size,
4239 struct objfile *objfile, unsigned int addr_size,
4240 int offset_size, struct dwarf2_per_cu_data *per_cu)
4242 const gdb_byte *end = data + size;
4243 int first_piece = 1, bad = 0;
4247 const gdb_byte *here = data;
4248 int disassemble = 1;
4253 fprintf_filtered (stream, _(", and "));
4255 if (!dwarf_always_disassemble)
4257 data = locexpr_describe_location_piece (symbol, stream,
4258 addr, objfile, per_cu,
4259 data, end, addr_size);
4260 /* If we printed anything, or if we have an empty piece,
4261 then don't disassemble. */
4263 || data[0] == DW_OP_piece
4264 || data[0] == DW_OP_bit_piece)
4269 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4270 data = disassemble_dwarf_expression (stream,
4271 get_objfile_arch (objfile),
4272 addr_size, offset_size, data,
4274 dwarf_always_disassemble,
4280 int empty = data == here;
4283 fprintf_filtered (stream, " ");
4284 if (data[0] == DW_OP_piece)
4288 data = safe_read_uleb128 (data + 1, end, &bytes);
4291 fprintf_filtered (stream, _("an empty %s-byte piece"),
4294 fprintf_filtered (stream, _(" [%s-byte piece]"),
4297 else if (data[0] == DW_OP_bit_piece)
4299 uint64_t bits, offset;
4301 data = safe_read_uleb128 (data + 1, end, &bits);
4302 data = safe_read_uleb128 (data, end, &offset);
4305 fprintf_filtered (stream,
4306 _("an empty %s-bit piece"),
4309 fprintf_filtered (stream,
4310 _(" [%s-bit piece, offset %s bits]"),
4311 pulongest (bits), pulongest (offset));
4321 if (bad || data > end)
4322 error (_("Corrupted DWARF2 expression for \"%s\"."),
4323 SYMBOL_PRINT_NAME (symbol));
4326 /* Print a natural-language description of SYMBOL to STREAM. This
4327 version is for a symbol with a single location. */
4330 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4331 struct ui_file *stream)
4333 struct dwarf2_locexpr_baton *dlbaton
4334 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4335 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4336 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4337 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4339 locexpr_describe_location_1 (symbol, addr, stream,
4340 dlbaton->data, dlbaton->size,
4341 objfile, addr_size, offset_size,
4345 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4346 any necessary bytecode in AX. */
4349 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4350 struct agent_expr *ax, struct axs_value *value)
4352 struct dwarf2_locexpr_baton *dlbaton
4353 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4354 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4356 if (dlbaton->size == 0)
4357 value->optimized_out = 1;
4359 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
4360 dlbaton->data, dlbaton->data + dlbaton->size,
4364 /* symbol_computed_ops 'generate_c_location' method. */
4367 locexpr_generate_c_location (struct symbol *sym, struct ui_file *stream,
4368 struct gdbarch *gdbarch,
4369 unsigned char *registers_used,
4370 CORE_ADDR pc, const char *result_name)
4372 struct dwarf2_locexpr_baton *dlbaton
4373 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
4374 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4376 if (dlbaton->size == 0)
4377 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4379 compile_dwarf_expr_to_c (stream, result_name,
4380 sym, pc, gdbarch, registers_used, addr_size,
4381 dlbaton->data, dlbaton->data + dlbaton->size,
4385 /* The set of location functions used with the DWARF-2 expression
4387 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4388 locexpr_read_variable,
4389 locexpr_read_variable_at_entry,
4390 locexpr_read_needs_frame,
4391 locexpr_describe_location,
4392 0, /* location_has_loclist */
4393 locexpr_tracepoint_var_ref,
4394 locexpr_generate_c_location
4398 /* Wrapper functions for location lists. These generally find
4399 the appropriate location expression and call something above. */
4401 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4402 evaluator to calculate the location. */
4403 static struct value *
4404 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4406 struct dwarf2_loclist_baton *dlbaton
4407 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4409 const gdb_byte *data;
4411 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4413 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4414 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4420 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4421 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4424 Function always returns non-NULL value, it may be marked optimized out if
4425 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4426 if it cannot resolve the parameter for any reason. */
4428 static struct value *
4429 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4431 struct dwarf2_loclist_baton *dlbaton
4432 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4433 const gdb_byte *data;
4437 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4438 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4440 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4442 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4444 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4447 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
4449 loclist_read_needs_frame (struct symbol *symbol)
4451 /* If there's a location list, then assume we need to have a frame
4452 to choose the appropriate location expression. With tracking of
4453 global variables this is not necessarily true, but such tracking
4454 is disabled in GCC at the moment until we figure out how to
4460 /* Print a natural-language description of SYMBOL to STREAM. This
4461 version applies when there is a list of different locations, each
4462 with a specified address range. */
4465 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4466 struct ui_file *stream)
4468 struct dwarf2_loclist_baton *dlbaton
4469 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4470 const gdb_byte *loc_ptr, *buf_end;
4471 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4472 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4473 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4474 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4475 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4476 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4477 /* Adjust base_address for relocatable objects. */
4478 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4479 CORE_ADDR base_address = dlbaton->base_address + base_offset;
4482 loc_ptr = dlbaton->data;
4483 buf_end = dlbaton->data + dlbaton->size;
4485 fprintf_filtered (stream, _("multi-location:\n"));
4487 /* Iterate through locations until we run out. */
4490 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4492 enum debug_loc_kind kind;
4493 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4495 if (dlbaton->from_dwo)
4496 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4497 loc_ptr, buf_end, &new_ptr,
4498 &low, &high, byte_order);
4500 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4502 byte_order, addr_size,
4507 case DEBUG_LOC_END_OF_LIST:
4510 case DEBUG_LOC_BASE_ADDRESS:
4511 base_address = high + base_offset;
4512 fprintf_filtered (stream, _(" Base address %s"),
4513 paddress (gdbarch, base_address));
4515 case DEBUG_LOC_START_END:
4516 case DEBUG_LOC_START_LENGTH:
4518 case DEBUG_LOC_BUFFER_OVERFLOW:
4519 case DEBUG_LOC_INVALID_ENTRY:
4520 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4521 SYMBOL_PRINT_NAME (symbol));
4523 gdb_assert_not_reached ("bad debug_loc_kind");
4526 /* Otherwise, a location expression entry. */
4527 low += base_address;
4528 high += base_address;
4530 low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4531 high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4533 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4536 /* (It would improve readability to print only the minimum
4537 necessary digits of the second number of the range.) */
4538 fprintf_filtered (stream, _(" Range %s-%s: "),
4539 paddress (gdbarch, low), paddress (gdbarch, high));
4541 /* Now describe this particular location. */
4542 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4543 objfile, addr_size, offset_size,
4546 fprintf_filtered (stream, "\n");
4552 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4553 any necessary bytecode in AX. */
4555 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4556 struct agent_expr *ax, struct axs_value *value)
4558 struct dwarf2_loclist_baton *dlbaton
4559 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4560 const gdb_byte *data;
4562 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4564 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4566 value->optimized_out = 1;
4568 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4572 /* symbol_computed_ops 'generate_c_location' method. */
4575 loclist_generate_c_location (struct symbol *sym, struct ui_file *stream,
4576 struct gdbarch *gdbarch,
4577 unsigned char *registers_used,
4578 CORE_ADDR pc, const char *result_name)
4580 struct dwarf2_loclist_baton *dlbaton
4581 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
4582 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4583 const gdb_byte *data;
4586 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4588 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4590 compile_dwarf_expr_to_c (stream, result_name,
4591 sym, pc, gdbarch, registers_used, addr_size,
4596 /* The set of location functions used with the DWARF-2 expression
4597 evaluator and location lists. */
4598 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4599 loclist_read_variable,
4600 loclist_read_variable_at_entry,
4601 loclist_read_needs_frame,
4602 loclist_describe_location,
4603 1, /* location_has_loclist */
4604 loclist_tracepoint_var_ref,
4605 loclist_generate_c_location
4608 /* Provide a prototype to silence -Wmissing-prototypes. */
4609 extern initialize_file_ftype _initialize_dwarf2loc;
4612 _initialize_dwarf2loc (void)
4614 add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4615 &entry_values_debug,
4616 _("Set entry values and tail call frames "
4618 _("Show entry values and tail call frames "
4620 _("When non-zero, the process of determining "
4621 "parameter values from function entry point "
4622 "and tail call frames will be printed."),
4624 show_entry_values_debug,
4625 &setdebuglist, &showdebuglist);