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