alpha-mdebug-tdep.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* Target-dependent mdebug code for the ALPHA architecture.
  2. Copyright (C) 1993-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 "frame.h"
  16. #include "frame-unwind.h"
  17. #include "frame-base.h"
  18. #include "symtab.h"
  19. #include "gdbcore.h"
  20. #include "block.h"
  21. #include "trad-frame.h"
  22. #include "alpha-tdep.h"
  23. #include "mdebugread.h"
  24. #include "gdbarch.h"
  25. /* FIXME: Some of this code should perhaps be merged with mips. */
  26. /* *INDENT-OFF* */
  27. /* Layout of a stack frame on the alpha:
  28. | |
  29. pdr members: | 7th ... nth arg, |
  30. | `pushed' by caller. |
  31. | |
  32. ----------------|-------------------------------|<-- old_sp == vfp
  33. ^ ^ ^ ^ | |
  34. | | | | | |
  35. | |localoff | Copies of 1st .. 6th |
  36. | | | | | argument if necessary. |
  37. | | | v | |
  38. | | | --- |-------------------------------|<-- LOCALS_ADDRESS
  39. | | | | |
  40. | | | | Locals and temporaries. |
  41. | | | | |
  42. | | | |-------------------------------|
  43. | | | | |
  44. |-fregoffset | Saved float registers. |
  45. | | | | F9 |
  46. | | | | . |
  47. | | | | . |
  48. | | | | F2 |
  49. | | v | |
  50. | | -------|-------------------------------|
  51. | | | |
  52. | | | Saved registers. |
  53. | | | S6 |
  54. |-regoffset | . |
  55. | | | . |
  56. | | | S0 |
  57. | | | pdr.pcreg |
  58. | v | |
  59. | ----------|-------------------------------|
  60. | | |
  61. frameoffset | Argument build area, gets |
  62. | | 7th ... nth arg for any |
  63. | | called procedure. |
  64. v | |
  65. -------------|-------------------------------|<-- sp
  66. | |
  67. */
  68. /* *INDENT-ON* */
  69. #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr)
  70. #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
  71. #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
  72. #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
  73. #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
  74. #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
  75. #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
  76. #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
  77. #define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
  78. /* Locate the mdebug PDR for the given PC. Return null if one can't
  79. be found; you'll have to fall back to other methods in that case. */
  80. static struct mdebug_extra_func_info *
  81. find_proc_desc (CORE_ADDR pc)
  82. {
  83. const struct block *b = block_for_pc (pc);
  84. struct mdebug_extra_func_info *proc_desc = NULL;
  85. struct symbol *sym = NULL;
  86. const char *sh_name = NULL;
  87. if (b)
  88. {
  89. CORE_ADDR startaddr;
  90. find_pc_partial_function (pc, &sh_name, &startaddr, NULL);
  91. if (startaddr > BLOCK_START (b))
  92. /* This is the "pathological" case referred to in a comment in
  93. print_frame_info. It might be better to move this check into
  94. symbol reading. */
  95. sym = NULL;
  96. else
  97. sym = lookup_symbol (MDEBUG_EFI_SYMBOL_NAME, b, LABEL_DOMAIN,
  98. 0).symbol;
  99. }
  100. if (sym)
  101. {
  102. proc_desc = (struct mdebug_extra_func_info *) SYMBOL_VALUE_BYTES (sym);
  103. /* Correct incorrect setjmp procedure descriptor from the library
  104. to make backtrace through setjmp work. */
  105. if (proc_desc->pdr.pcreg == 0
  106. && strcmp (sh_name, "setjmp") == 0)
  107. {
  108. proc_desc->pdr.pcreg = ALPHA_RA_REGNUM;
  109. proc_desc->pdr.regmask = 0x80000000;
  110. proc_desc->pdr.regoffset = -4;
  111. }
  112. /* If we never found a PDR for this function in symbol reading,
  113. then examine prologues to find the information. */
  114. if (proc_desc->pdr.framereg == -1)
  115. proc_desc = NULL;
  116. }
  117. return proc_desc;
  118. }
  119. /* Return a non-zero result if the function is frameless; zero otherwise. */
  120. static int
  121. alpha_mdebug_frameless (struct mdebug_extra_func_info *proc_desc)
  122. {
  123. return (PROC_FRAME_REG (proc_desc) == ALPHA_SP_REGNUM
  124. && PROC_FRAME_OFFSET (proc_desc) == 0);
  125. }
  126. /* This returns the PC of the first inst after the prologue. If we can't
  127. find the prologue, then return 0. */
  128. static CORE_ADDR
  129. alpha_mdebug_after_prologue (CORE_ADDR pc,
  130. struct mdebug_extra_func_info *proc_desc)
  131. {
  132. if (proc_desc)
  133. {
  134. /* If function is frameless, then we need to do it the hard way. I
  135. strongly suspect that frameless always means prologueless... */
  136. if (alpha_mdebug_frameless (proc_desc))
  137. return 0;
  138. }
  139. return alpha_after_prologue (pc);
  140. }
  141. /* Return non-zero if we *might* be in a function prologue. Return zero
  142. if we are definitively *not* in a function prologue. */
  143. static int
  144. alpha_mdebug_in_prologue (CORE_ADDR pc,
  145. struct mdebug_extra_func_info *proc_desc)
  146. {
  147. CORE_ADDR after_prologue_pc = alpha_mdebug_after_prologue (pc, proc_desc);
  148. return (after_prologue_pc == 0 || pc < after_prologue_pc);
  149. }
  150. /* Frame unwinder that reads mdebug PDRs. */
  151. struct alpha_mdebug_unwind_cache
  152. {
  153. struct mdebug_extra_func_info *proc_desc;
  154. CORE_ADDR vfp;
  155. trad_frame_saved_reg *saved_regs;
  156. };
  157. /* Extract all of the information about the frame from PROC_DESC
  158. and store the resulting register save locations in the structure. */
  159. static struct alpha_mdebug_unwind_cache *
  160. alpha_mdebug_frame_unwind_cache (struct frame_info *this_frame,
  161. void **this_prologue_cache)
  162. {
  163. struct alpha_mdebug_unwind_cache *info;
  164. struct mdebug_extra_func_info *proc_desc;
  165. ULONGEST vfp;
  166. CORE_ADDR pc, reg_position;
  167. unsigned long mask;
  168. int ireg, returnreg;
  169. if (*this_prologue_cache)
  170. return (struct alpha_mdebug_unwind_cache *) *this_prologue_cache;
  171. info = FRAME_OBSTACK_ZALLOC (struct alpha_mdebug_unwind_cache);
  172. *this_prologue_cache = info;
  173. pc = get_frame_address_in_block (this_frame);
  174. /* ??? We don't seem to be able to cache the lookup of the PDR
  175. from alpha_mdebug_frame_p. It'd be nice if we could change
  176. the arguments to that function. Oh well. */
  177. proc_desc = find_proc_desc (pc);
  178. info->proc_desc = proc_desc;
  179. gdb_assert (proc_desc != NULL);
  180. info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  181. /* The VFP of the frame is at FRAME_REG+FRAME_OFFSET. */
  182. vfp = get_frame_register_unsigned (this_frame, PROC_FRAME_REG (proc_desc));
  183. vfp += PROC_FRAME_OFFSET (info->proc_desc);
  184. info->vfp = vfp;
  185. /* Fill in the offsets for the registers which gen_mask says were saved. */
  186. reg_position = vfp + PROC_REG_OFFSET (proc_desc);
  187. mask = PROC_REG_MASK (proc_desc);
  188. returnreg = PROC_PC_REG (proc_desc);
  189. /* Note that RA is always saved first, regardless of its actual
  190. register number. */
  191. if (mask & (1 << returnreg))
  192. {
  193. /* Clear bit for RA so we don't save it again later. */
  194. mask &= ~(1 << returnreg);
  195. info->saved_regs[returnreg].set_addr (reg_position);
  196. reg_position += 8;
  197. }
  198. for (ireg = 0; ireg <= 31; ++ireg)
  199. if (mask & (1 << ireg))
  200. {
  201. info->saved_regs[ireg].set_addr (reg_position);
  202. reg_position += 8;
  203. }
  204. reg_position = vfp + PROC_FREG_OFFSET (proc_desc);
  205. mask = PROC_FREG_MASK (proc_desc);
  206. for (ireg = 0; ireg <= 31; ++ireg)
  207. if (mask & (1 << ireg))
  208. {
  209. info->saved_regs[ALPHA_FP0_REGNUM + ireg].set_addr (reg_position);
  210. reg_position += 8;
  211. }
  212. /* The stack pointer of the previous frame is computed by popping
  213. the current stack frame. */
  214. if (!info->saved_regs[ALPHA_SP_REGNUM].is_addr ())
  215. info->saved_regs[ALPHA_SP_REGNUM].set_value (vfp);
  216. return info;
  217. }
  218. /* Given a GDB frame, determine the address of the calling function's
  219. frame. This will be used to create a new GDB frame struct. */
  220. static void
  221. alpha_mdebug_frame_this_id (struct frame_info *this_frame,
  222. void **this_prologue_cache,
  223. struct frame_id *this_id)
  224. {
  225. struct alpha_mdebug_unwind_cache *info
  226. = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
  227. *this_id = frame_id_build (info->vfp, get_frame_func (this_frame));
  228. }
  229. /* Retrieve the value of REGNUM in FRAME. Don't give up! */
  230. static struct value *
  231. alpha_mdebug_frame_prev_register (struct frame_info *this_frame,
  232. void **this_prologue_cache, int regnum)
  233. {
  234. struct alpha_mdebug_unwind_cache *info
  235. = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
  236. /* The PC of the previous frame is stored in the link register of
  237. the current frame. Frob regnum so that we pull the value from
  238. the correct place. */
  239. if (regnum == ALPHA_PC_REGNUM)
  240. regnum = PROC_PC_REG (info->proc_desc);
  241. return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  242. }
  243. /* Return a non-zero result if the size of the stack frame exceeds the
  244. maximum debuggable frame size (512 Kbytes); zero otherwise. */
  245. static int
  246. alpha_mdebug_max_frame_size_exceeded (struct mdebug_extra_func_info *proc_desc)
  247. {
  248. /* If frame offset is null, we can be in two cases: either the
  249. function is frameless (the stack frame is null) or its
  250. frame exceeds the maximum debuggable frame size (512 Kbytes). */
  251. return (PROC_FRAME_OFFSET (proc_desc) == 0
  252. && !alpha_mdebug_frameless (proc_desc));
  253. }
  254. static int
  255. alpha_mdebug_frame_sniffer (const struct frame_unwind *self,
  256. struct frame_info *this_frame,
  257. void **this_cache)
  258. {
  259. CORE_ADDR pc = get_frame_address_in_block (this_frame);
  260. struct mdebug_extra_func_info *proc_desc;
  261. /* If this PC does not map to a PDR, then clearly this isn't an
  262. mdebug frame. */
  263. proc_desc = find_proc_desc (pc);
  264. if (proc_desc == NULL)
  265. return 0;
  266. /* If we're in the prologue, the PDR for this frame is not yet valid.
  267. Say no here and we'll fall back on the heuristic unwinder. */
  268. if (alpha_mdebug_in_prologue (pc, proc_desc))
  269. return 0;
  270. /* If the maximum debuggable frame size has been exceeded, the
  271. proc desc is bogus. Fall back on the heuristic unwinder. */
  272. if (alpha_mdebug_max_frame_size_exceeded (proc_desc))
  273. return 0;
  274. return 1;
  275. }
  276. static const struct frame_unwind alpha_mdebug_frame_unwind =
  277. {
  278. "alpha mdebug",
  279. NORMAL_FRAME,
  280. default_frame_unwind_stop_reason,
  281. alpha_mdebug_frame_this_id,
  282. alpha_mdebug_frame_prev_register,
  283. NULL,
  284. alpha_mdebug_frame_sniffer
  285. };
  286. static CORE_ADDR
  287. alpha_mdebug_frame_base_address (struct frame_info *this_frame,
  288. void **this_prologue_cache)
  289. {
  290. struct alpha_mdebug_unwind_cache *info
  291. = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
  292. return info->vfp;
  293. }
  294. static CORE_ADDR
  295. alpha_mdebug_frame_locals_address (struct frame_info *this_frame,
  296. void **this_prologue_cache)
  297. {
  298. struct alpha_mdebug_unwind_cache *info
  299. = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
  300. return info->vfp - PROC_LOCALOFF (info->proc_desc);
  301. }
  302. static CORE_ADDR
  303. alpha_mdebug_frame_args_address (struct frame_info *this_frame,
  304. void **this_prologue_cache)
  305. {
  306. struct alpha_mdebug_unwind_cache *info
  307. = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
  308. return info->vfp - ALPHA_NUM_ARG_REGS * 8;
  309. }
  310. static const struct frame_base alpha_mdebug_frame_base = {
  311. &alpha_mdebug_frame_unwind,
  312. alpha_mdebug_frame_base_address,
  313. alpha_mdebug_frame_locals_address,
  314. alpha_mdebug_frame_args_address
  315. };
  316. static const struct frame_base *
  317. alpha_mdebug_frame_base_sniffer (struct frame_info *this_frame)
  318. {
  319. CORE_ADDR pc = get_frame_address_in_block (this_frame);
  320. struct mdebug_extra_func_info *proc_desc;
  321. /* If this PC does not map to a PDR, then clearly this isn't an
  322. mdebug frame. */
  323. proc_desc = find_proc_desc (pc);
  324. if (proc_desc == NULL)
  325. return NULL;
  326. /* If the maximum debuggable frame size has been exceeded, the
  327. proc desc is bogus. Fall back on the heuristic unwinder. */
  328. if (alpha_mdebug_max_frame_size_exceeded (proc_desc))
  329. return 0;
  330. return &alpha_mdebug_frame_base;
  331. }
  332. void
  333. alpha_mdebug_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  334. {
  335. frame_unwind_append_unwinder (gdbarch, &alpha_mdebug_frame_unwind);
  336. frame_base_append_sniffer (gdbarch, alpha_mdebug_frame_base_sniffer);
  337. }