findvar.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /* Find a variable's value in memory, for GDB, the GNU debugger.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "frame.h"
  18. #include "value.h"
  19. #include "gdbcore.h"
  20. #include "inferior.h"
  21. #include "target.h"
  22. #include "symfile.h" /* for overlay functions */
  23. #include "regcache.h"
  24. #include "user-regs.h"
  25. #include "block.h"
  26. #include "objfiles.h"
  27. #include "language.h"
  28. #include "dwarf2/loc.h"
  29. #include "gdbsupport/selftest.h"
  30. /* Basic byte-swapping routines. All 'extract' functions return a
  31. host-format integer from a target-format integer at ADDR which is
  32. LEN bytes long. */
  33. #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
  34. /* 8 bit characters are a pretty safe assumption these days, so we
  35. assume it throughout all these swapping routines. If we had to deal with
  36. 9 bit characters, we would need to make len be in bits and would have
  37. to re-write these routines... */
  38. you lose
  39. #endif
  40. template<typename T, typename>
  41. T
  42. extract_integer (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order)
  43. {
  44. typename std::make_unsigned<T>::type retval = 0;
  45. if (buf.size () > (int) sizeof (T))
  46. error (_("\
  47. That operation is not available on integers of more than %d bytes."),
  48. (int) sizeof (T));
  49. /* Start at the most significant end of the integer, and work towards
  50. the least significant. */
  51. if (byte_order == BFD_ENDIAN_BIG)
  52. {
  53. size_t i = 0;
  54. if (std::is_signed<T>::value)
  55. {
  56. /* Do the sign extension once at the start. */
  57. retval = ((LONGEST) buf[i] ^ 0x80) - 0x80;
  58. ++i;
  59. }
  60. for (; i < buf.size (); ++i)
  61. retval = (retval << 8) | buf[i];
  62. }
  63. else
  64. {
  65. ssize_t i = buf.size () - 1;
  66. if (std::is_signed<T>::value)
  67. {
  68. /* Do the sign extension once at the start. */
  69. retval = ((LONGEST) buf[i] ^ 0x80) - 0x80;
  70. --i;
  71. }
  72. for (; i >= 0; --i)
  73. retval = (retval << 8) | buf[i];
  74. }
  75. return retval;
  76. }
  77. /* Explicit instantiations. */
  78. template LONGEST extract_integer<LONGEST> (gdb::array_view<const gdb_byte> buf,
  79. enum bfd_endian byte_order);
  80. template ULONGEST extract_integer<ULONGEST>
  81. (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order);
  82. /* Sometimes a long long unsigned integer can be extracted as a
  83. LONGEST value. This is done so that we can print these values
  84. better. If this integer can be converted to a LONGEST, this
  85. function returns 1 and sets *PVAL. Otherwise it returns 0. */
  86. int
  87. extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
  88. enum bfd_endian byte_order, LONGEST *pval)
  89. {
  90. const gdb_byte *p;
  91. const gdb_byte *first_addr;
  92. int len;
  93. len = orig_len;
  94. if (byte_order == BFD_ENDIAN_BIG)
  95. {
  96. for (p = addr;
  97. len > (int) sizeof (LONGEST) && p < addr + orig_len;
  98. p++)
  99. {
  100. if (*p == 0)
  101. len--;
  102. else
  103. break;
  104. }
  105. first_addr = p;
  106. }
  107. else
  108. {
  109. first_addr = addr;
  110. for (p = addr + orig_len - 1;
  111. len > (int) sizeof (LONGEST) && p >= addr;
  112. p--)
  113. {
  114. if (*p == 0)
  115. len--;
  116. else
  117. break;
  118. }
  119. }
  120. if (len <= (int) sizeof (LONGEST))
  121. {
  122. *pval = (LONGEST) extract_unsigned_integer (first_addr,
  123. sizeof (LONGEST),
  124. byte_order);
  125. return 1;
  126. }
  127. return 0;
  128. }
  129. /* Treat the bytes at BUF as a pointer of type TYPE, and return the
  130. address it represents. */
  131. CORE_ADDR
  132. extract_typed_address (const gdb_byte *buf, struct type *type)
  133. {
  134. if (!type->is_pointer_or_reference ())
  135. internal_error (__FILE__, __LINE__,
  136. _("extract_typed_address: "
  137. "type is not a pointer or reference"));
  138. return gdbarch_pointer_to_address (type->arch (), type, buf);
  139. }
  140. /* All 'store' functions accept a host-format integer and store a
  141. target-format integer at ADDR which is LEN bytes long. */
  142. template<typename T, typename>
  143. void
  144. store_integer (gdb_byte *addr, int len, enum bfd_endian byte_order,
  145. T val)
  146. {
  147. gdb_byte *p;
  148. gdb_byte *startaddr = addr;
  149. gdb_byte *endaddr = startaddr + len;
  150. /* Start at the least significant end of the integer, and work towards
  151. the most significant. */
  152. if (byte_order == BFD_ENDIAN_BIG)
  153. {
  154. for (p = endaddr - 1; p >= startaddr; --p)
  155. {
  156. *p = val & 0xff;
  157. val >>= 8;
  158. }
  159. }
  160. else
  161. {
  162. for (p = startaddr; p < endaddr; ++p)
  163. {
  164. *p = val & 0xff;
  165. val >>= 8;
  166. }
  167. }
  168. }
  169. /* Explicit instantiations. */
  170. template void store_integer (gdb_byte *addr, int len,
  171. enum bfd_endian byte_order,
  172. LONGEST val);
  173. template void store_integer (gdb_byte *addr, int len,
  174. enum bfd_endian byte_order,
  175. ULONGEST val);
  176. /* Store the address ADDR as a pointer of type TYPE at BUF, in target
  177. form. */
  178. void
  179. store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
  180. {
  181. if (!type->is_pointer_or_reference ())
  182. internal_error (__FILE__, __LINE__,
  183. _("store_typed_address: "
  184. "type is not a pointer or reference"));
  185. gdbarch_address_to_pointer (type->arch (), type, buf, addr);
  186. }
  187. /* Copy a value from SOURCE of size SOURCE_SIZE bytes to DEST of size DEST_SIZE
  188. bytes. If SOURCE_SIZE is greater than DEST_SIZE, then truncate the most
  189. significant bytes. If SOURCE_SIZE is less than DEST_SIZE then either sign
  190. or zero extended according to IS_SIGNED. Values are stored in memory with
  191. endianness BYTE_ORDER. */
  192. void
  193. copy_integer_to_size (gdb_byte *dest, int dest_size, const gdb_byte *source,
  194. int source_size, bool is_signed,
  195. enum bfd_endian byte_order)
  196. {
  197. signed int size_diff = dest_size - source_size;
  198. /* Copy across everything from SOURCE that can fit into DEST. */
  199. if (byte_order == BFD_ENDIAN_BIG && size_diff > 0)
  200. memcpy (dest + size_diff, source, source_size);
  201. else if (byte_order == BFD_ENDIAN_BIG && size_diff < 0)
  202. memcpy (dest, source - size_diff, dest_size);
  203. else
  204. memcpy (dest, source, std::min (source_size, dest_size));
  205. /* Fill the remaining space in DEST by either zero extending or sign
  206. extending. */
  207. if (size_diff > 0)
  208. {
  209. gdb_byte extension = 0;
  210. if (is_signed
  211. && ((byte_order != BFD_ENDIAN_BIG && source[source_size - 1] & 0x80)
  212. || (byte_order == BFD_ENDIAN_BIG && source[0] & 0x80)))
  213. extension = 0xff;
  214. /* Extend into MSBs of SOURCE. */
  215. if (byte_order == BFD_ENDIAN_BIG)
  216. memset (dest, extension, size_diff);
  217. else
  218. memset (dest + source_size, extension, size_diff);
  219. }
  220. }
  221. /* Return a `value' with the contents of (virtual or cooked) register
  222. REGNUM as found in the specified FRAME. The register's type is
  223. determined by register_type (). */
  224. struct value *
  225. value_of_register (int regnum, struct frame_info *frame)
  226. {
  227. struct gdbarch *gdbarch = get_frame_arch (frame);
  228. struct value *reg_val;
  229. /* User registers lie completely outside of the range of normal
  230. registers. Catch them early so that the target never sees them. */
  231. if (regnum >= gdbarch_num_cooked_regs (gdbarch))
  232. return value_of_user_reg (regnum, frame);
  233. reg_val = value_of_register_lazy (frame, regnum);
  234. value_fetch_lazy (reg_val);
  235. return reg_val;
  236. }
  237. /* Return a `value' with the contents of (virtual or cooked) register
  238. REGNUM as found in the specified FRAME. The register's type is
  239. determined by register_type (). The value is not fetched. */
  240. struct value *
  241. value_of_register_lazy (struct frame_info *frame, int regnum)
  242. {
  243. struct gdbarch *gdbarch = get_frame_arch (frame);
  244. struct value *reg_val;
  245. struct frame_info *next_frame;
  246. gdb_assert (regnum < gdbarch_num_cooked_regs (gdbarch));
  247. gdb_assert (frame != NULL);
  248. next_frame = get_next_frame_sentinel_okay (frame);
  249. /* In some cases NEXT_FRAME may not have a valid frame-id yet. This can
  250. happen if we end up trying to unwind a register as part of the frame
  251. sniffer. The only time that we get here without a valid frame-id is
  252. if NEXT_FRAME is an inline frame. If this is the case then we can
  253. avoid getting into trouble here by skipping past the inline frames. */
  254. while (get_frame_type (next_frame) == INLINE_FRAME)
  255. next_frame = get_next_frame_sentinel_okay (next_frame);
  256. /* We should have a valid next frame. */
  257. gdb_assert (frame_id_p (get_frame_id (next_frame)));
  258. reg_val = allocate_value_lazy (register_type (gdbarch, regnum));
  259. VALUE_LVAL (reg_val) = lval_register;
  260. VALUE_REGNUM (reg_val) = regnum;
  261. VALUE_NEXT_FRAME_ID (reg_val) = get_frame_id (next_frame);
  262. return reg_val;
  263. }
  264. /* Given a pointer of type TYPE in target form in BUF, return the
  265. address it represents. */
  266. CORE_ADDR
  267. unsigned_pointer_to_address (struct gdbarch *gdbarch,
  268. struct type *type, const gdb_byte *buf)
  269. {
  270. enum bfd_endian byte_order = type_byte_order (type);
  271. return extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
  272. }
  273. CORE_ADDR
  274. signed_pointer_to_address (struct gdbarch *gdbarch,
  275. struct type *type, const gdb_byte *buf)
  276. {
  277. enum bfd_endian byte_order = type_byte_order (type);
  278. return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
  279. }
  280. /* Given an address, store it as a pointer of type TYPE in target
  281. format in BUF. */
  282. void
  283. unsigned_address_to_pointer (struct gdbarch *gdbarch, struct type *type,
  284. gdb_byte *buf, CORE_ADDR addr)
  285. {
  286. enum bfd_endian byte_order = type_byte_order (type);
  287. store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  288. }
  289. void
  290. address_to_signed_pointer (struct gdbarch *gdbarch, struct type *type,
  291. gdb_byte *buf, CORE_ADDR addr)
  292. {
  293. enum bfd_endian byte_order = type_byte_order (type);
  294. store_signed_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  295. }
  296. /* See value.h. */
  297. enum symbol_needs_kind
  298. symbol_read_needs (struct symbol *sym)
  299. {
  300. if (SYMBOL_COMPUTED_OPS (sym) != NULL)
  301. return SYMBOL_COMPUTED_OPS (sym)->get_symbol_read_needs (sym);
  302. switch (sym->aclass ())
  303. {
  304. /* All cases listed explicitly so that gcc -Wall will detect it if
  305. we failed to consider one. */
  306. case LOC_COMPUTED:
  307. gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
  308. case LOC_REGISTER:
  309. case LOC_ARG:
  310. case LOC_REF_ARG:
  311. case LOC_REGPARM_ADDR:
  312. case LOC_LOCAL:
  313. return SYMBOL_NEEDS_FRAME;
  314. case LOC_UNDEF:
  315. case LOC_CONST:
  316. case LOC_STATIC:
  317. case LOC_TYPEDEF:
  318. case LOC_LABEL:
  319. /* Getting the address of a label can be done independently of the block,
  320. even if some *uses* of that address wouldn't work so well without
  321. the right frame. */
  322. case LOC_BLOCK:
  323. case LOC_CONST_BYTES:
  324. case LOC_UNRESOLVED:
  325. case LOC_OPTIMIZED_OUT:
  326. return SYMBOL_NEEDS_NONE;
  327. }
  328. return SYMBOL_NEEDS_FRAME;
  329. }
  330. /* See value.h. */
  331. int
  332. symbol_read_needs_frame (struct symbol *sym)
  333. {
  334. return symbol_read_needs (sym) == SYMBOL_NEEDS_FRAME;
  335. }
  336. /* Private data to be used with minsym_lookup_iterator_cb. */
  337. struct minsym_lookup_data
  338. {
  339. /* The name of the minimal symbol we are searching for. */
  340. const char *name = nullptr;
  341. /* The field where the callback should store the minimal symbol
  342. if found. It should be initialized to NULL before the search
  343. is started. */
  344. struct bound_minimal_symbol result;
  345. };
  346. /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
  347. It searches by name for a minimal symbol within the given OBJFILE.
  348. The arguments are passed via CB_DATA, which in reality is a pointer
  349. to struct minsym_lookup_data. */
  350. static int
  351. minsym_lookup_iterator_cb (struct objfile *objfile, void *cb_data)
  352. {
  353. struct minsym_lookup_data *data = (struct minsym_lookup_data *) cb_data;
  354. gdb_assert (data->result.minsym == NULL);
  355. data->result = lookup_minimal_symbol (data->name, NULL, objfile);
  356. /* The iterator should stop iff a match was found. */
  357. return (data->result.minsym != NULL);
  358. }
  359. /* Given static link expression and the frame it lives in, look for the frame
  360. the static links points to and return it. Return NULL if we could not find
  361. such a frame. */
  362. static struct frame_info *
  363. follow_static_link (struct frame_info *frame,
  364. const struct dynamic_prop *static_link)
  365. {
  366. CORE_ADDR upper_frame_base;
  367. if (!dwarf2_evaluate_property (static_link, frame, NULL, &upper_frame_base))
  368. return NULL;
  369. /* Now climb up the stack frame until we reach the frame we are interested
  370. in. */
  371. for (; frame != NULL; frame = get_prev_frame (frame))
  372. {
  373. struct symbol *framefunc = get_frame_function (frame);
  374. /* Stacks can be quite deep: give the user a chance to stop this. */
  375. QUIT;
  376. /* If we don't know how to compute FRAME's base address, don't give up:
  377. maybe the frame we are looking for is upper in the stack frame. */
  378. if (framefunc != NULL
  379. && SYMBOL_BLOCK_OPS (framefunc) != NULL
  380. && SYMBOL_BLOCK_OPS (framefunc)->get_frame_base != NULL
  381. && (SYMBOL_BLOCK_OPS (framefunc)->get_frame_base (framefunc, frame)
  382. == upper_frame_base))
  383. break;
  384. }
  385. return frame;
  386. }
  387. /* Assuming VAR is a symbol that can be reached from FRAME thanks to lexical
  388. rules, look for the frame that is actually hosting VAR and return it. If,
  389. for some reason, we found no such frame, return NULL.
  390. This kind of computation is necessary to correctly handle lexically nested
  391. functions.
  392. Note that in some cases, we know what scope VAR comes from but we cannot
  393. reach the specific frame that hosts the instance of VAR we are looking for.
  394. For backward compatibility purposes (with old compilers), we then look for
  395. the first frame that can host it. */
  396. static struct frame_info *
  397. get_hosting_frame (struct symbol *var, const struct block *var_block,
  398. struct frame_info *frame)
  399. {
  400. const struct block *frame_block = NULL;
  401. if (!symbol_read_needs_frame (var))
  402. return NULL;
  403. /* Some symbols for local variables have no block: this happens when they are
  404. not produced by a debug information reader, for instance when GDB creates
  405. synthetic symbols. Without block information, we must assume they are
  406. local to FRAME. In this case, there is nothing to do. */
  407. else if (var_block == NULL)
  408. return frame;
  409. /* We currently assume that all symbols with a location list need a frame.
  410. This is true in practice because selecting the location description
  411. requires to compute the CFA, hence requires a frame. However we have
  412. tests that embed global/static symbols with null location lists.
  413. We want to get <optimized out> instead of <frame required> when evaluating
  414. them so return a frame instead of raising an error. */
  415. else if (var_block == block_global_block (var_block)
  416. || var_block == block_static_block (var_block))
  417. return frame;
  418. /* We have to handle the "my_func::my_local_var" notation. This requires us
  419. to look for upper frames when we find no block for the current frame: here
  420. and below, handle when frame_block == NULL. */
  421. if (frame != NULL)
  422. frame_block = get_frame_block (frame, NULL);
  423. /* Climb up the call stack until reaching the frame we are looking for. */
  424. while (frame != NULL && frame_block != var_block)
  425. {
  426. /* Stacks can be quite deep: give the user a chance to stop this. */
  427. QUIT;
  428. if (frame_block == NULL)
  429. {
  430. frame = get_prev_frame (frame);
  431. if (frame == NULL)
  432. break;
  433. frame_block = get_frame_block (frame, NULL);
  434. }
  435. /* If we failed to find the proper frame, fallback to the heuristic
  436. method below. */
  437. else if (frame_block == block_global_block (frame_block))
  438. {
  439. frame = NULL;
  440. break;
  441. }
  442. /* Assuming we have a block for this frame: if we are at the function
  443. level, the immediate upper lexical block is in an outer function:
  444. follow the static link. */
  445. else if (BLOCK_FUNCTION (frame_block))
  446. {
  447. const struct dynamic_prop *static_link
  448. = block_static_link (frame_block);
  449. int could_climb_up = 0;
  450. if (static_link != NULL)
  451. {
  452. frame = follow_static_link (frame, static_link);
  453. if (frame != NULL)
  454. {
  455. frame_block = get_frame_block (frame, NULL);
  456. could_climb_up = frame_block != NULL;
  457. }
  458. }
  459. if (!could_climb_up)
  460. {
  461. frame = NULL;
  462. break;
  463. }
  464. }
  465. else
  466. /* We must be in some function nested lexical block. Just get the
  467. outer block: both must share the same frame. */
  468. frame_block = BLOCK_SUPERBLOCK (frame_block);
  469. }
  470. /* Old compilers may not provide a static link, or they may provide an
  471. invalid one. For such cases, fallback on the old way to evaluate
  472. non-local references: just climb up the call stack and pick the first
  473. frame that contains the variable we are looking for. */
  474. if (frame == NULL)
  475. {
  476. frame = block_innermost_frame (var_block);
  477. if (frame == NULL)
  478. {
  479. if (BLOCK_FUNCTION (var_block)
  480. && !block_inlined_p (var_block)
  481. && BLOCK_FUNCTION (var_block)->print_name ())
  482. error (_("No frame is currently executing in block %s."),
  483. BLOCK_FUNCTION (var_block)->print_name ());
  484. else
  485. error (_("No frame is currently executing in specified"
  486. " block"));
  487. }
  488. }
  489. return frame;
  490. }
  491. /* See language.h. */
  492. struct value *
  493. language_defn::read_var_value (struct symbol *var,
  494. const struct block *var_block,
  495. struct frame_info *frame) const
  496. {
  497. struct value *v;
  498. struct type *type = var->type ();
  499. CORE_ADDR addr;
  500. enum symbol_needs_kind sym_need;
  501. /* Call check_typedef on our type to make sure that, if TYPE is
  502. a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
  503. instead of zero. However, we do not replace the typedef type by the
  504. target type, because we want to keep the typedef in order to be able to
  505. set the returned value type description correctly. */
  506. check_typedef (type);
  507. sym_need = symbol_read_needs (var);
  508. if (sym_need == SYMBOL_NEEDS_FRAME)
  509. gdb_assert (frame != NULL);
  510. else if (sym_need == SYMBOL_NEEDS_REGISTERS && !target_has_registers ())
  511. error (_("Cannot read `%s' without registers"), var->print_name ());
  512. if (frame != NULL)
  513. frame = get_hosting_frame (var, var_block, frame);
  514. if (SYMBOL_COMPUTED_OPS (var) != NULL)
  515. return SYMBOL_COMPUTED_OPS (var)->read_variable (var, frame);
  516. switch (var->aclass ())
  517. {
  518. case LOC_CONST:
  519. if (is_dynamic_type (type))
  520. {
  521. /* Value is a constant byte-sequence and needs no memory access. */
  522. type = resolve_dynamic_type (type, {}, /* Unused address. */ 0);
  523. }
  524. /* Put the constant back in target format. */
  525. v = allocate_value (type);
  526. store_signed_integer (value_contents_raw (v).data (), TYPE_LENGTH (type),
  527. type_byte_order (type),
  528. (LONGEST) SYMBOL_VALUE (var));
  529. VALUE_LVAL (v) = not_lval;
  530. return v;
  531. case LOC_LABEL:
  532. /* Put the constant back in target format. */
  533. v = allocate_value (type);
  534. if (overlay_debugging)
  535. {
  536. struct objfile *var_objfile = symbol_objfile (var);
  537. addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
  538. var->obj_section (var_objfile));
  539. store_typed_address (value_contents_raw (v).data (), type, addr);
  540. }
  541. else
  542. store_typed_address (value_contents_raw (v).data (), type,
  543. SYMBOL_VALUE_ADDRESS (var));
  544. VALUE_LVAL (v) = not_lval;
  545. return v;
  546. case LOC_CONST_BYTES:
  547. if (is_dynamic_type (type))
  548. {
  549. /* Value is a constant byte-sequence and needs no memory access. */
  550. type = resolve_dynamic_type (type, {}, /* Unused address. */ 0);
  551. }
  552. v = allocate_value (type);
  553. memcpy (value_contents_raw (v).data (), SYMBOL_VALUE_BYTES (var),
  554. TYPE_LENGTH (type));
  555. VALUE_LVAL (v) = not_lval;
  556. return v;
  557. case LOC_STATIC:
  558. if (overlay_debugging)
  559. addr
  560. = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
  561. var->obj_section (symbol_objfile (var)));
  562. else
  563. addr = SYMBOL_VALUE_ADDRESS (var);
  564. break;
  565. case LOC_ARG:
  566. addr = get_frame_args_address (frame);
  567. if (!addr)
  568. error (_("Unknown argument list address for `%s'."),
  569. var->print_name ());
  570. addr += SYMBOL_VALUE (var);
  571. break;
  572. case LOC_REF_ARG:
  573. {
  574. struct value *ref;
  575. CORE_ADDR argref;
  576. argref = get_frame_args_address (frame);
  577. if (!argref)
  578. error (_("Unknown argument list address for `%s'."),
  579. var->print_name ());
  580. argref += SYMBOL_VALUE (var);
  581. ref = value_at (lookup_pointer_type (type), argref);
  582. addr = value_as_address (ref);
  583. break;
  584. }
  585. case LOC_LOCAL:
  586. addr = get_frame_locals_address (frame);
  587. addr += SYMBOL_VALUE (var);
  588. break;
  589. case LOC_TYPEDEF:
  590. error (_("Cannot look up value of a typedef `%s'."),
  591. var->print_name ());
  592. break;
  593. case LOC_BLOCK:
  594. if (overlay_debugging)
  595. addr = symbol_overlayed_address
  596. (BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (var)),
  597. var->obj_section (symbol_objfile (var)));
  598. else
  599. addr = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (var));
  600. break;
  601. case LOC_REGISTER:
  602. case LOC_REGPARM_ADDR:
  603. {
  604. int regno = SYMBOL_REGISTER_OPS (var)
  605. ->register_number (var, get_frame_arch (frame));
  606. struct value *regval;
  607. if (var->aclass () == LOC_REGPARM_ADDR)
  608. {
  609. regval = value_from_register (lookup_pointer_type (type),
  610. regno,
  611. frame);
  612. if (regval == NULL)
  613. error (_("Value of register variable not available for `%s'."),
  614. var->print_name ());
  615. addr = value_as_address (regval);
  616. }
  617. else
  618. {
  619. regval = value_from_register (type, regno, frame);
  620. if (regval == NULL)
  621. error (_("Value of register variable not available for `%s'."),
  622. var->print_name ());
  623. return regval;
  624. }
  625. }
  626. break;
  627. case LOC_COMPUTED:
  628. gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
  629. case LOC_UNRESOLVED:
  630. {
  631. struct minsym_lookup_data lookup_data;
  632. struct minimal_symbol *msym;
  633. struct obj_section *obj_section;
  634. lookup_data.name = var->linkage_name ();
  635. gdbarch_iterate_over_objfiles_in_search_order
  636. (symbol_arch (var),
  637. minsym_lookup_iterator_cb, &lookup_data,
  638. symbol_objfile (var));
  639. msym = lookup_data.result.minsym;
  640. /* If we can't find the minsym there's a problem in the symbol info.
  641. The symbol exists in the debug info, but it's missing in the minsym
  642. table. */
  643. if (msym == NULL)
  644. {
  645. const char *flavour_name
  646. = objfile_flavour_name (symbol_objfile (var));
  647. /* We can't get here unless we've opened the file, so flavour_name
  648. can't be NULL. */
  649. gdb_assert (flavour_name != NULL);
  650. error (_("Missing %s symbol \"%s\"."),
  651. flavour_name, var->linkage_name ());
  652. }
  653. obj_section = msym->obj_section (lookup_data.result.objfile);
  654. /* Relocate address, unless there is no section or the variable is
  655. a TLS variable. */
  656. if (obj_section == NULL
  657. || (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
  658. addr = MSYMBOL_VALUE_RAW_ADDRESS (msym);
  659. else
  660. addr = BMSYMBOL_VALUE_ADDRESS (lookup_data.result);
  661. if (overlay_debugging)
  662. addr = symbol_overlayed_address (addr, obj_section);
  663. /* Determine address of TLS variable. */
  664. if (obj_section
  665. && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
  666. addr = target_translate_tls_address (obj_section->objfile, addr);
  667. }
  668. break;
  669. case LOC_OPTIMIZED_OUT:
  670. if (is_dynamic_type (type))
  671. type = resolve_dynamic_type (type, {}, /* Unused address. */ 0);
  672. return allocate_optimized_out_value (type);
  673. default:
  674. error (_("Cannot look up value of a botched symbol `%s'."),
  675. var->print_name ());
  676. break;
  677. }
  678. v = value_at_lazy (type, addr);
  679. return v;
  680. }
  681. /* Calls VAR's language read_var_value hook with the given arguments. */
  682. struct value *
  683. read_var_value (struct symbol *var, const struct block *var_block,
  684. struct frame_info *frame)
  685. {
  686. const struct language_defn *lang = language_def (var->language ());
  687. gdb_assert (lang != NULL);
  688. return lang->read_var_value (var, var_block, frame);
  689. }
  690. /* Install default attributes for register values. */
  691. struct value *
  692. default_value_from_register (struct gdbarch *gdbarch, struct type *type,
  693. int regnum, struct frame_id frame_id)
  694. {
  695. int len = TYPE_LENGTH (type);
  696. struct value *value = allocate_value (type);
  697. struct frame_info *frame;
  698. VALUE_LVAL (value) = lval_register;
  699. frame = frame_find_by_id (frame_id);
  700. if (frame == NULL)
  701. frame_id = null_frame_id;
  702. else
  703. frame_id = get_frame_id (get_next_frame_sentinel_okay (frame));
  704. VALUE_NEXT_FRAME_ID (value) = frame_id;
  705. VALUE_REGNUM (value) = regnum;
  706. /* Any structure stored in more than one register will always be
  707. an integral number of registers. Otherwise, you need to do
  708. some fiddling with the last register copied here for little
  709. endian machines. */
  710. if (type_byte_order (type) == BFD_ENDIAN_BIG
  711. && len < register_size (gdbarch, regnum))
  712. /* Big-endian, and we want less than full size. */
  713. set_value_offset (value, register_size (gdbarch, regnum) - len);
  714. else
  715. set_value_offset (value, 0);
  716. return value;
  717. }
  718. /* VALUE must be an lval_register value. If regnum is the value's
  719. associated register number, and len the length of the values type,
  720. read one or more registers in FRAME, starting with register REGNUM,
  721. until we've read LEN bytes.
  722. If any of the registers we try to read are optimized out, then mark the
  723. complete resulting value as optimized out. */
  724. void
  725. read_frame_register_value (struct value *value, struct frame_info *frame)
  726. {
  727. struct gdbarch *gdbarch = get_frame_arch (frame);
  728. LONGEST offset = 0;
  729. LONGEST reg_offset = value_offset (value);
  730. int regnum = VALUE_REGNUM (value);
  731. int len = type_length_units (check_typedef (value_type (value)));
  732. gdb_assert (VALUE_LVAL (value) == lval_register);
  733. /* Skip registers wholly inside of REG_OFFSET. */
  734. while (reg_offset >= register_size (gdbarch, regnum))
  735. {
  736. reg_offset -= register_size (gdbarch, regnum);
  737. regnum++;
  738. }
  739. /* Copy the data. */
  740. while (len > 0)
  741. {
  742. struct value *regval = get_frame_register_value (frame, regnum);
  743. int reg_len = type_length_units (value_type (regval)) - reg_offset;
  744. /* If the register length is larger than the number of bytes
  745. remaining to copy, then only copy the appropriate bytes. */
  746. if (reg_len > len)
  747. reg_len = len;
  748. value_contents_copy (value, offset, regval, reg_offset, reg_len);
  749. offset += reg_len;
  750. len -= reg_len;
  751. reg_offset = 0;
  752. regnum++;
  753. }
  754. }
  755. /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
  756. struct value *
  757. value_from_register (struct type *type, int regnum, struct frame_info *frame)
  758. {
  759. struct gdbarch *gdbarch = get_frame_arch (frame);
  760. struct type *type1 = check_typedef (type);
  761. struct value *v;
  762. if (gdbarch_convert_register_p (gdbarch, regnum, type1))
  763. {
  764. int optim, unavail, ok;
  765. /* The ISA/ABI need to something weird when obtaining the
  766. specified value from this register. It might need to
  767. re-order non-adjacent, starting with REGNUM (see MIPS and
  768. i386). It might need to convert the [float] register into
  769. the corresponding [integer] type (see Alpha). The assumption
  770. is that gdbarch_register_to_value populates the entire value
  771. including the location. */
  772. v = allocate_value (type);
  773. VALUE_LVAL (v) = lval_register;
  774. VALUE_NEXT_FRAME_ID (v) = get_frame_id (get_next_frame_sentinel_okay (frame));
  775. VALUE_REGNUM (v) = regnum;
  776. ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
  777. value_contents_raw (v).data (), &optim,
  778. &unavail);
  779. if (!ok)
  780. {
  781. if (optim)
  782. mark_value_bytes_optimized_out (v, 0, TYPE_LENGTH (type));
  783. if (unavail)
  784. mark_value_bytes_unavailable (v, 0, TYPE_LENGTH (type));
  785. }
  786. }
  787. else
  788. {
  789. /* Construct the value. */
  790. v = gdbarch_value_from_register (gdbarch, type,
  791. regnum, get_frame_id (frame));
  792. /* Get the data. */
  793. read_frame_register_value (v, frame);
  794. }
  795. return v;
  796. }
  797. /* Return contents of register REGNUM in frame FRAME as address.
  798. Will abort if register value is not available. */
  799. CORE_ADDR
  800. address_from_register (int regnum, struct frame_info *frame)
  801. {
  802. struct gdbarch *gdbarch = get_frame_arch (frame);
  803. struct type *type = builtin_type (gdbarch)->builtin_data_ptr;
  804. struct value *value;
  805. CORE_ADDR result;
  806. int regnum_max_excl = gdbarch_num_cooked_regs (gdbarch);
  807. if (regnum < 0 || regnum >= regnum_max_excl)
  808. error (_("Invalid register #%d, expecting 0 <= # < %d"), regnum,
  809. regnum_max_excl);
  810. /* This routine may be called during early unwinding, at a time
  811. where the ID of FRAME is not yet known. Calling value_from_register
  812. would therefore abort in get_frame_id. However, since we only need
  813. a temporary value that is never used as lvalue, we actually do not
  814. really need to set its VALUE_NEXT_FRAME_ID. Therefore, we re-implement
  815. the core of value_from_register, but use the null_frame_id. */
  816. /* Some targets require a special conversion routine even for plain
  817. pointer types. Avoid constructing a value object in those cases. */
  818. if (gdbarch_convert_register_p (gdbarch, regnum, type))
  819. {
  820. gdb_byte *buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
  821. int optim, unavail, ok;
  822. ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
  823. buf, &optim, &unavail);
  824. if (!ok)
  825. {
  826. /* This function is used while computing a location expression.
  827. Complain about the value being optimized out, rather than
  828. letting value_as_address complain about some random register
  829. the expression depends on not being saved. */
  830. error_value_optimized_out ();
  831. }
  832. return unpack_long (type, buf);
  833. }
  834. value = gdbarch_value_from_register (gdbarch, type, regnum, null_frame_id);
  835. read_frame_register_value (value, frame);
  836. if (value_optimized_out (value))
  837. {
  838. /* This function is used while computing a location expression.
  839. Complain about the value being optimized out, rather than
  840. letting value_as_address complain about some random register
  841. the expression depends on not being saved. */
  842. error_value_optimized_out ();
  843. }
  844. result = value_as_address (value);
  845. release_value (value);
  846. return result;
  847. }
  848. #if GDB_SELF_TEST
  849. namespace selftests {
  850. namespace findvar_tests {
  851. /* Function to test copy_integer_to_size. Store SOURCE_VAL with size
  852. SOURCE_SIZE to a buffer, making sure no sign extending happens at this
  853. stage. Copy buffer to a new buffer using copy_integer_to_size. Extract
  854. copied value and compare to DEST_VALU. Copy again with a signed
  855. copy_integer_to_size and compare to DEST_VALS. Do everything for both
  856. LITTLE and BIG target endians. Use unsigned values throughout to make
  857. sure there are no implicit sign extensions. */
  858. static void
  859. do_cint_test (ULONGEST dest_valu, ULONGEST dest_vals, int dest_size,
  860. ULONGEST src_val, int src_size)
  861. {
  862. for (int i = 0; i < 2 ; i++)
  863. {
  864. gdb_byte srcbuf[sizeof (ULONGEST)] = {};
  865. gdb_byte destbuf[sizeof (ULONGEST)] = {};
  866. enum bfd_endian byte_order = i ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
  867. /* Fill the src buffer (and later the dest buffer) with non-zero junk,
  868. to ensure zero extensions aren't hidden. */
  869. memset (srcbuf, 0xaa, sizeof (srcbuf));
  870. /* Store (and later extract) using unsigned to ensure there are no sign
  871. extensions. */
  872. store_unsigned_integer (srcbuf, src_size, byte_order, src_val);
  873. /* Test unsigned. */
  874. memset (destbuf, 0xaa, sizeof (destbuf));
  875. copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, false,
  876. byte_order);
  877. SELF_CHECK (dest_valu == extract_unsigned_integer (destbuf, dest_size,
  878. byte_order));
  879. /* Test signed. */
  880. memset (destbuf, 0xaa, sizeof (destbuf));
  881. copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, true,
  882. byte_order);
  883. SELF_CHECK (dest_vals == extract_unsigned_integer (destbuf, dest_size,
  884. byte_order));
  885. }
  886. }
  887. static void
  888. copy_integer_to_size_test ()
  889. {
  890. /* Destination is bigger than the source, which has the signed bit unset. */
  891. do_cint_test (0x12345678, 0x12345678, 8, 0x12345678, 4);
  892. do_cint_test (0x345678, 0x345678, 8, 0x12345678, 3);
  893. /* Destination is bigger than the source, which has the signed bit set. */
  894. do_cint_test (0xdeadbeef, 0xffffffffdeadbeef, 8, 0xdeadbeef, 4);
  895. do_cint_test (0xadbeef, 0xffffffffffadbeef, 8, 0xdeadbeef, 3);
  896. /* Destination is smaller than the source. */
  897. do_cint_test (0x5678, 0x5678, 2, 0x12345678, 3);
  898. do_cint_test (0xbeef, 0xbeef, 2, 0xdeadbeef, 3);
  899. /* Destination and source are the same size. */
  900. do_cint_test (0x8765432112345678, 0x8765432112345678, 8, 0x8765432112345678,
  901. 8);
  902. do_cint_test (0x432112345678, 0x432112345678, 6, 0x8765432112345678, 6);
  903. do_cint_test (0xfeedbeaddeadbeef, 0xfeedbeaddeadbeef, 8, 0xfeedbeaddeadbeef,
  904. 8);
  905. do_cint_test (0xbeaddeadbeef, 0xbeaddeadbeef, 6, 0xfeedbeaddeadbeef, 6);
  906. /* Destination is bigger than the source. Source is bigger than 32bits. */
  907. do_cint_test (0x3412345678, 0x3412345678, 8, 0x3412345678, 6);
  908. do_cint_test (0xff12345678, 0xff12345678, 8, 0xff12345678, 6);
  909. do_cint_test (0x432112345678, 0x432112345678, 8, 0x8765432112345678, 6);
  910. do_cint_test (0xff2112345678, 0xffffff2112345678, 8, 0xffffff2112345678, 6);
  911. }
  912. } // namespace findvar_test
  913. } // namespace selftests
  914. #endif
  915. void _initialize_findvar ();
  916. void
  917. _initialize_findvar ()
  918. {
  919. #if GDB_SELF_TEST
  920. selftests::register_test
  921. ("copy_integer_to_size",
  922. selftests::findvar_tests::copy_integer_to_size_test);
  923. #endif
  924. }