findvar.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* Find a variable's value in memory, for GDB, the GNU debugger.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include "defs.h"
  18. #include "initialize.h"
  19. #include "param.h"
  20. #include "symtab.h"
  21. #include "frame.h"
  22. #include "value.h"
  23. CORE_ADDR read_register ();
  24. START_FILE
  25. /* Return the address in which frame FRAME's value of register REGNUM
  26. has been saved in memory. Or return zero if it has not been saved.
  27. If REGNUM specifies the SP, the value we return is actually
  28. the SP value, not an address where it was saved. */
  29. static CORE_ADDR
  30. find_saved_register (frame, regnum)
  31. FRAME frame;
  32. int regnum;
  33. {
  34. struct frame_info fi;
  35. struct frame_saved_regs saved_regs;
  36. register FRAME frame1 = 0;
  37. register CORE_ADDR addr = 0;
  38. while (1)
  39. {
  40. QUIT;
  41. fi = get_prev_frame_info (frame1);
  42. if (fi.frame == 0 || fi.frame == frame)
  43. break;
  44. get_frame_saved_regs (&fi, &saved_regs);
  45. if (saved_regs.regs[regnum])
  46. addr = saved_regs.regs[regnum];
  47. frame1 = fi.frame;
  48. }
  49. return addr;
  50. }
  51. /* Copy the bytes of register REGNUM, relative to the current stack frame,
  52. into our memory at MYADDR.
  53. The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). */
  54. void
  55. read_relative_register_raw_bytes (regnum, myaddr)
  56. int regnum;
  57. char *myaddr;
  58. {
  59. register CORE_ADDR addr;
  60. if (regnum == FP_REGNUM)
  61. {
  62. bcopy (&selected_frame, myaddr, sizeof (CORE_ADDR));
  63. return;
  64. }
  65. addr = find_saved_register (selected_frame, regnum);
  66. if (addr)
  67. {
  68. if (regnum == SP_REGNUM)
  69. {
  70. CORE_ADDR buffer = addr;
  71. bcopy (&buffer, myaddr, sizeof (CORE_ADDR));
  72. }
  73. else
  74. read_memory (addr, myaddr, REGISTER_RAW_SIZE (regnum));
  75. return;
  76. }
  77. read_register_bytes (REGISTER_BYTE (regnum),
  78. myaddr, REGISTER_RAW_SIZE (regnum));
  79. }
  80. /* Return a `value' with the contents of register REGNUM
  81. in its virtual format, with the type specified by
  82. REGISTER_VIRTUAL_TYPE. */
  83. value
  84. value_of_register (regnum)
  85. int regnum;
  86. {
  87. register CORE_ADDR addr = find_saved_register (selected_frame, regnum);
  88. register value val;
  89. char raw_buffer[MAX_REGISTER_RAW_SIZE];
  90. char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  91. if (addr)
  92. {
  93. if (regnum == SP_REGNUM)
  94. return value_from_long (builtin_type_int, addr);
  95. read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
  96. }
  97. else
  98. read_register_bytes (REGISTER_BYTE (regnum), raw_buffer,
  99. REGISTER_RAW_SIZE (regnum));
  100. REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
  101. val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
  102. bcopy (virtual_buffer, VALUE_CONTENTS (val), REGISTER_VIRTUAL_SIZE (regnum));
  103. VALUE_LVAL (val) = addr ? lval_memory : lval_register;
  104. VALUE_ADDRESS (val) = addr ? addr : REGISTER_BYTE (regnum);
  105. VALUE_REGNO (val) = regnum;
  106. return val;
  107. }
  108. /* Low level examining and depositing of registers.
  109. Note that you must call `fetch_registers' once
  110. before examining or depositing any registers. */
  111. char registers[REGISTER_BYTES];
  112. /* Copy LEN bytes of consecutive data from registers
  113. starting with the REGBYTE'th byte of register data
  114. into memory at MYADDR. */
  115. read_register_bytes (regbyte, myaddr, len)
  116. int regbyte;
  117. char *myaddr;
  118. int len;
  119. {
  120. bcopy (&registers[regbyte], myaddr, len);
  121. }
  122. /* Copy LEN bytes of consecutive data from memory at MYADDR
  123. into registers starting with the REGBYTE'th byte of register data. */
  124. write_register_bytes (regbyte, myaddr, len)
  125. int regbyte;
  126. char *myaddr;
  127. int len;
  128. {
  129. bcopy (myaddr, &registers[regbyte], len);
  130. if (have_inferior_p ())
  131. store_inferior_registers (0);
  132. }
  133. /* Return the contents of register REGNO,
  134. regarding it as an integer. */
  135. CORE_ADDR
  136. read_register (regno)
  137. int regno;
  138. {
  139. return *(int *) &registers[REGISTER_BYTE (regno)];
  140. }
  141. /* Store VALUE in the register number REGNO, regarded as an integer. */
  142. void
  143. write_register (regno, value)
  144. int regno, value;
  145. {
  146. *(int *) &registers[REGISTER_BYTE (regno)] = value;
  147. if (have_inferior_p ())
  148. store_inferior_registers (regno);
  149. }
  150. /* Record that register REGNO contains VALUE.
  151. This is used when the value is obtained from the inferior or core dump,
  152. so there is no need to store the value there. */
  153. void
  154. supply_register (regno, value)
  155. int regno, value;
  156. {
  157. *(int *) &registers[REGISTER_BYTE (regno)] = value;
  158. }
  159. /* Given a struct symbol for a variable,
  160. and a stack frame address, read the value of the variable
  161. and return a (pointer to a) struct value containing the value. */
  162. value
  163. read_var_value (var, frame)
  164. register struct symbol *var;
  165. FRAME frame;
  166. {
  167. register value v;
  168. struct frame_info fi;
  169. struct type *type = SYMBOL_TYPE (var);
  170. register CORE_ADDR addr = 0;
  171. int val = SYMBOL_VALUE (var);
  172. register int len;
  173. if (SYMBOL_CLASS (var) == LOC_BLOCK)
  174. type = lookup_function_type (type);
  175. v = allocate_value (type);
  176. VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
  177. len = TYPE_LENGTH (type);
  178. if (frame == 0) frame = selected_frame;
  179. switch (SYMBOL_CLASS (var))
  180. {
  181. case LOC_CONST:
  182. case LOC_LABEL:
  183. bcopy (&val, VALUE_CONTENTS (v), len);
  184. VALUE_LVAL (v) = not_lval;
  185. return v;
  186. case LOC_STATIC:
  187. addr = val;
  188. break;
  189. case LOC_ARG:
  190. fi = get_frame_info (frame);
  191. addr = val + FRAME_ARGS_ADDRESS (fi);
  192. break;
  193. case LOC_LOCAL:
  194. fi = get_frame_info (frame);
  195. addr = val + FRAME_LOCALS_ADDRESS (fi);
  196. break;
  197. case LOC_TYPEDEF:
  198. error ("Cannot look up value of a typedef");
  199. case LOC_BLOCK:
  200. VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
  201. return v;
  202. case LOC_REGISTER:
  203. {
  204. char raw_buffer[MAX_REGISTER_RAW_SIZE];
  205. char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  206. addr = find_saved_register (frame, val);
  207. if (addr == 0)
  208. {
  209. VALUE_LVAL (v) = lval_register;
  210. VALUE_ADDRESS (v) = REGISTER_BYTE (val);
  211. if (REGISTER_CONVERTIBLE (val))
  212. {
  213. read_register_bytes (REGISTER_BYTE (val),
  214. raw_buffer, REGISTER_RAW_SIZE (val));
  215. REGISTER_CONVERT_TO_VIRTUAL (val, raw_buffer, virtual_buffer);
  216. bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
  217. }
  218. else
  219. {
  220. union { int i; char c; } test;
  221. /* If we want less than the full size, we need to
  222. test for a big-endian or little-endian machine. */
  223. test.c = 1;
  224. if (test.i != 1 && len < REGISTER_RAW_SIZE (val))
  225. /* Big-endian, and we want less than full size. */
  226. read_register_bytes (REGISTER_BYTE (val)
  227. + REGISTER_RAW_SIZE (val) - len,
  228. VALUE_CONTENTS (v),
  229. len);
  230. else
  231. read_register_bytes (REGISTER_BYTE (val),
  232. VALUE_CONTENTS (v), len);
  233. }
  234. VALUE_REGNO (v) = val;
  235. return v;
  236. }
  237. else if (REGISTER_CONVERTIBLE (val))
  238. {
  239. read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (val));
  240. REGISTER_CONVERT_TO_VIRTUAL (val, raw_buffer, virtual_buffer);
  241. bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
  242. VALUE_ADDRESS (v) = addr;
  243. VALUE_REGNO (v) = val;
  244. return v;
  245. }
  246. }
  247. }
  248. read_memory (addr, VALUE_CONTENTS (v), len);
  249. VALUE_ADDRESS (v) = addr;
  250. return v;
  251. }
  252. /* Given a struct symbol for a variable,
  253. and a stack frame address,
  254. return a (pointer to a) struct value containing the variable's address. */
  255. value
  256. locate_var_value (var, frame)
  257. register struct symbol *var;
  258. FRAME frame;
  259. {
  260. register CORE_ADDR addr = 0;
  261. int val = SYMBOL_VALUE (var);
  262. struct frame_info fi;
  263. if (frame == 0) frame = selected_frame;
  264. switch (SYMBOL_CLASS (var))
  265. {
  266. case LOC_CONST:
  267. error ("Address requested for identifier \"%s\" which is a constant.",
  268. SYMBOL_NAME (var));
  269. case LOC_REGISTER:
  270. addr = find_saved_register (frame, val);
  271. if (addr != 0)
  272. break;
  273. error ("Address requested for identifier \"%s\" which is in a register.",
  274. SYMBOL_NAME (var));
  275. case LOC_STATIC:
  276. case LOC_LABEL:
  277. addr = val;
  278. break;
  279. case LOC_ARG:
  280. fi = get_frame_info (frame);
  281. addr = val + FRAME_ARGS_ADDRESS (fi);
  282. break;
  283. case LOC_LOCAL:
  284. fi = get_frame_info (frame);
  285. addr = val + FRAME_LOCALS_ADDRESS (fi);
  286. break;
  287. case LOC_TYPEDEF:
  288. error ("Address requested for identifier \"%s\" which is a typedef.",
  289. SYMBOL_NAME (var));
  290. case LOC_BLOCK:
  291. addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
  292. break;
  293. }
  294. return value_cast (lookup_pointer_type (SYMBOL_TYPE (var)),
  295. value_from_long (builtin_type_long, addr));
  296. }
  297. static
  298. initialize ()
  299. {}
  300. END_FILE