blockframe.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* Get info from stack frames;
  2. convert between frames, blocks, functions and pc values.
  3. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  4. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY. No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the GDB General Public License for full details.
  9. Everyone is granted permission to copy, modify and redistribute GDB,
  10. but only under the conditions described in the GDB General Public
  11. License. A copy of this license is supposed to have been given to you
  12. along with GDB so you can know your rights and responsibilities. It
  13. should be in a file named COPYING. Among other things, the copyright
  14. notice and this notice must be preserved on all copies.
  15. In other words, go ahead and share GDB, but don't try to stop
  16. anyone else from sharing it farther. Help stamp out software hoarding!
  17. */
  18. #include "defs.h"
  19. #include "initialize.h"
  20. #include "param.h"
  21. #include "symtab.h"
  22. #include "frame.h"
  23. /* Address of end of first object file.
  24. This file is assumed to be a startup file
  25. and frames with pc's inside it
  26. are treated as nonexistent. */
  27. CORE_ADDR first_object_file_end;
  28. /* Address of innermost stack frame (contents of FP register) */
  29. static FRAME current_frame;
  30. struct block *block_for_pc ();
  31. CORE_ADDR get_pc_function_start ();
  32. START_FILE
  33. /* Return the innermost (currently executing) stack frame. */
  34. FRAME
  35. get_current_frame ()
  36. {
  37. /* We assume its address is kept in a general register;
  38. param.h says which register. */
  39. return current_frame;
  40. }
  41. void
  42. set_current_frame (frame)
  43. FRAME frame;
  44. {
  45. current_frame = frame;
  46. }
  47. /* Return the frame that called FRAME.
  48. If FRAME is the original frame (it has no caller), return 0. */
  49. FRAME
  50. get_prev_frame (frame)
  51. FRAME frame;
  52. {
  53. CORE_ADDR pointer;
  54. /* The caller of "no frame" is the innermost frame. */
  55. if (frame == 0)
  56. return get_current_frame ();
  57. /* Two macros defined in param.h specify the machine-dependent
  58. actions to be performed here. */
  59. /* First, get the frame's chain-pointer.
  60. If that is zero, the frame is the outermost frame. */
  61. pointer = FRAME_CHAIN (frame);
  62. if (!FRAME_CHAIN_VALID (pointer, frame))
  63. return 0;
  64. /* If frame has a caller, combine the chain pointer and the frame's own
  65. address to get the address of the caller. */
  66. return FRAME_CHAIN_COMBINE (pointer, frame);
  67. }
  68. /* Return a structure containing various interesting information
  69. about a specified stack frame. */
  70. struct frame_info
  71. get_frame_info (frame)
  72. FRAME frame;
  73. {
  74. struct frame_info val;
  75. FRAME current = get_current_frame ();
  76. register FRAME frame1;
  77. val.frame = frame;
  78. if (frame == current)
  79. {
  80. val.pc = read_pc ();
  81. val.next_frame = 0;
  82. }
  83. else
  84. {
  85. for (frame1 = current; frame1; frame1 = get_prev_frame (frame1))
  86. {
  87. QUIT;
  88. if (frame1 == frame)
  89. break;
  90. val.pc = FRAME_SAVED_PC (frame1);
  91. val.next_frame = frame1;
  92. }
  93. }
  94. return val;
  95. }
  96. /* Return a structure containing various interesting information
  97. about the frame that called FRAME.
  98. This is much faster than get_frame_info (get_prev_frame (FRAME))
  99. because it does not need to search the entire stack
  100. to find the frame called by the one being described -- that is FRAME. */
  101. struct frame_info
  102. get_prev_frame_info (next_frame)
  103. FRAME next_frame;
  104. {
  105. struct frame_info val;
  106. register FRAME frame = get_prev_frame (next_frame);
  107. val.frame = frame;
  108. val.next_frame = next_frame;
  109. if (next_frame == 0)
  110. {
  111. val.pc = read_pc ();
  112. }
  113. else
  114. {
  115. val.pc = FRAME_SAVED_PC (next_frame);
  116. }
  117. return val;
  118. }
  119. CORE_ADDR
  120. get_frame_pc (frame)
  121. FRAME frame;
  122. {
  123. struct frame_info fi;
  124. fi = get_frame_info (frame);
  125. return fi.pc;
  126. }
  127. /* Find the addresses in which registers are saved in FRAME. */
  128. void
  129. get_frame_saved_regs (frame_info_addr, saved_regs_addr)
  130. struct frame_info *frame_info_addr;
  131. struct frame_saved_regs *saved_regs_addr;
  132. {
  133. FRAME_FIND_SAVED_REGS (*frame_info_addr, *saved_regs_addr);
  134. }
  135. /* Return the innermost lexical block in execution
  136. in a specified stack frame. The frame address is assumed valid. */
  137. struct block *
  138. get_frame_block (frame)
  139. FRAME frame;
  140. {
  141. struct frame_info fi;
  142. fi = get_frame_info (frame);
  143. return block_for_pc (fi.pc);
  144. }
  145. struct block *
  146. get_current_block ()
  147. {
  148. return block_for_pc (read_pc ());
  149. }
  150. CORE_ADDR
  151. get_pc_function_start (pc)
  152. CORE_ADDR pc;
  153. {
  154. register struct block *bl = block_for_pc (pc);
  155. register struct symbol *symbol;
  156. if (bl == 0)
  157. {
  158. register int misc_index = find_pc_misc_function (pc);
  159. if (misc_index >= 0)
  160. return misc_function_vector[misc_index].address;
  161. return 0;
  162. }
  163. symbol = block_function (bl);
  164. bl = SYMBOL_BLOCK_VALUE (symbol);
  165. return BLOCK_START (bl);
  166. }
  167. /* Return the symbol for the function executing in frame FRAME. */
  168. struct symbol *
  169. get_frame_function (frame)
  170. FRAME frame;
  171. {
  172. register struct block *bl = get_frame_block (frame);
  173. if (bl == 0)
  174. return 0;
  175. return block_function (bl);
  176. }
  177. /* Return the innermost lexical block containing the specified pc value,
  178. or 0 if there is none. */
  179. struct block *
  180. block_for_pc (pc)
  181. register CORE_ADDR pc;
  182. {
  183. register struct block *b;
  184. register int bot, top, half;
  185. register struct symtab *s;
  186. struct blockvector *bl;
  187. /* First search all symtabs for one whose file contains our pc */
  188. for (s = symtab_list; s; s = s->next)
  189. {
  190. bl = BLOCKVECTOR (s);
  191. b = BLOCKVECTOR_BLOCK (bl, 0);
  192. if (BLOCK_START (b) <= pc
  193. && BLOCK_END (b) > pc)
  194. break;
  195. }
  196. if (s == 0)
  197. return 0;
  198. /* Then search that symtab for the smallest block that wins. */
  199. /* Use binary search to find the last block that starts before PC. */
  200. bot = 0;
  201. top = BLOCKVECTOR_NBLOCKS (bl);
  202. while (top - bot > 1)
  203. {
  204. half = (top - bot + 1) >> 1;
  205. b = BLOCKVECTOR_BLOCK (bl, bot + half);
  206. if (BLOCK_START (b) <= pc)
  207. bot += half;
  208. else
  209. top = bot + half;
  210. }
  211. /* Now search backward for a block that ends after PC. */
  212. while (bot >= 0)
  213. {
  214. b = BLOCKVECTOR_BLOCK (bl, bot);
  215. if (BLOCK_END (b) > pc)
  216. return b;
  217. bot--;
  218. }
  219. return 0;
  220. }
  221. /* Return the function containing pc value PC.
  222. Returns 0 if function is not known. */
  223. struct symbol *
  224. find_pc_function (pc)
  225. CORE_ADDR pc;
  226. {
  227. register struct block *b = block_for_pc (pc);
  228. if (b == 0)
  229. return 0;
  230. return block_function (b);
  231. }
  232. /* Find the misc function whose address is the largest
  233. while being less than PC. Return its index in misc_function_vector.
  234. Returns -1 if PC is not in suitable range. */
  235. int
  236. find_pc_misc_function (pc)
  237. CORE_ADDR pc;
  238. {
  239. register int i;
  240. /* Note that the last thing in the vector is always _etext. */
  241. for (i = 0; i < misc_function_count; i++)
  242. {
  243. if (pc < misc_function_vector[i].address)
  244. return i - 1;
  245. }
  246. return -1;
  247. }
  248. /* Return the innermost stack frame executing inside of the specified block,
  249. or zero if there is no such frame. */
  250. FRAME
  251. block_innermost_frame (block)
  252. struct block *block;
  253. {
  254. struct frame_info fi;
  255. register FRAME frame;
  256. register CORE_ADDR start = BLOCK_START (block);
  257. register CORE_ADDR end = BLOCK_END (block);
  258. frame = 0;
  259. while (1)
  260. {
  261. fi = get_prev_frame_info (frame);
  262. frame = fi.frame;
  263. if (frame == 0)
  264. return 0;
  265. if (fi.pc >= start && fi.pc < end)
  266. return frame;
  267. }
  268. }
  269. static
  270. initialize ()
  271. {
  272. }
  273. END_FILE