xref: /dflybsd-src/contrib/gdb-7/gdb/tramp-frame.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* Signal trampoline unwinder, for GDB the GNU Debugger.
25796c8dcSSimon Schubert 
3*c50c785cSJohn Marino    Copyright (C) 2004, 2007, 2008, 2009, 2010, 2011
4*c50c785cSJohn Marino    Free Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This file is part of GDB.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
95796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
105796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
115796c8dcSSimon Schubert    (at your option) any later version.
125796c8dcSSimon Schubert 
135796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
145796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
155796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165796c8dcSSimon Schubert    GNU General Public License for more details.
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
195796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "tramp-frame.h"
235796c8dcSSimon Schubert #include "frame-unwind.h"
245796c8dcSSimon Schubert #include "gdbcore.h"
255796c8dcSSimon Schubert #include "symtab.h"
265796c8dcSSimon Schubert #include "objfiles.h"
275796c8dcSSimon Schubert #include "target.h"
285796c8dcSSimon Schubert #include "trad-frame.h"
295796c8dcSSimon Schubert #include "frame-base.h"
305796c8dcSSimon Schubert #include "gdb_assert.h"
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert struct frame_data
335796c8dcSSimon Schubert {
345796c8dcSSimon Schubert   const struct tramp_frame *tramp_frame;
355796c8dcSSimon Schubert };
365796c8dcSSimon Schubert 
375796c8dcSSimon Schubert struct tramp_frame_cache
385796c8dcSSimon Schubert {
395796c8dcSSimon Schubert   CORE_ADDR func;
405796c8dcSSimon Schubert   const struct tramp_frame *tramp_frame;
415796c8dcSSimon Schubert   struct trad_frame_cache *trad_cache;
425796c8dcSSimon Schubert };
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert static struct trad_frame_cache *
455796c8dcSSimon Schubert tramp_frame_cache (struct frame_info *this_frame,
465796c8dcSSimon Schubert 		   void **this_cache)
475796c8dcSSimon Schubert {
485796c8dcSSimon Schubert   struct tramp_frame_cache *tramp_cache = (*this_cache);
49cf7f2e2dSJohn Marino 
505796c8dcSSimon Schubert   if (tramp_cache->trad_cache == NULL)
515796c8dcSSimon Schubert     {
525796c8dcSSimon Schubert       tramp_cache->trad_cache = trad_frame_cache_zalloc (this_frame);
535796c8dcSSimon Schubert       tramp_cache->tramp_frame->init (tramp_cache->tramp_frame,
545796c8dcSSimon Schubert 				      this_frame,
555796c8dcSSimon Schubert 				      tramp_cache->trad_cache,
565796c8dcSSimon Schubert 				      tramp_cache->func);
575796c8dcSSimon Schubert     }
585796c8dcSSimon Schubert   return tramp_cache->trad_cache;
595796c8dcSSimon Schubert }
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert static void
625796c8dcSSimon Schubert tramp_frame_this_id (struct frame_info *this_frame,
635796c8dcSSimon Schubert 		     void **this_cache,
645796c8dcSSimon Schubert 		     struct frame_id *this_id)
655796c8dcSSimon Schubert {
665796c8dcSSimon Schubert   struct trad_frame_cache *trad_cache
675796c8dcSSimon Schubert     = tramp_frame_cache (this_frame, this_cache);
68cf7f2e2dSJohn Marino 
695796c8dcSSimon Schubert   trad_frame_get_id (trad_cache, this_id);
705796c8dcSSimon Schubert }
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert static struct value *
735796c8dcSSimon Schubert tramp_frame_prev_register (struct frame_info *this_frame,
745796c8dcSSimon Schubert 			   void **this_cache,
755796c8dcSSimon Schubert 			   int prev_regnum)
765796c8dcSSimon Schubert {
775796c8dcSSimon Schubert   struct trad_frame_cache *trad_cache
785796c8dcSSimon Schubert     = tramp_frame_cache (this_frame, this_cache);
79cf7f2e2dSJohn Marino 
805796c8dcSSimon Schubert   return trad_frame_get_register (trad_cache, this_frame, prev_regnum);
815796c8dcSSimon Schubert }
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert static CORE_ADDR
845796c8dcSSimon Schubert tramp_frame_start (const struct tramp_frame *tramp,
855796c8dcSSimon Schubert 		   struct frame_info *this_frame, CORE_ADDR pc)
865796c8dcSSimon Schubert {
875796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
885796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
895796c8dcSSimon Schubert   int ti;
90cf7f2e2dSJohn Marino 
915796c8dcSSimon Schubert   /* Search through the trampoline for one that matches the
925796c8dcSSimon Schubert      instruction sequence around PC.  */
935796c8dcSSimon Schubert   for (ti = 0; tramp->insn[ti].bytes != TRAMP_SENTINEL_INSN; ti++)
945796c8dcSSimon Schubert     {
955796c8dcSSimon Schubert       CORE_ADDR func = pc - tramp->insn_size * ti;
965796c8dcSSimon Schubert       int i;
97cf7f2e2dSJohn Marino 
985796c8dcSSimon Schubert       for (i = 0; 1; i++)
995796c8dcSSimon Schubert 	{
1005796c8dcSSimon Schubert 	  gdb_byte buf[sizeof (tramp->insn[0])];
1015796c8dcSSimon Schubert 	  ULONGEST insn;
102cf7f2e2dSJohn Marino 
1035796c8dcSSimon Schubert 	  if (tramp->insn[i].bytes == TRAMP_SENTINEL_INSN)
1045796c8dcSSimon Schubert 	    return func;
1055796c8dcSSimon Schubert 	  if (!safe_frame_unwind_memory (this_frame,
1065796c8dcSSimon Schubert 					 func + i * tramp->insn_size,
1075796c8dcSSimon Schubert 					 buf, tramp->insn_size))
1085796c8dcSSimon Schubert 	    break;
1095796c8dcSSimon Schubert 	  insn = extract_unsigned_integer (buf, tramp->insn_size, byte_order);
1105796c8dcSSimon Schubert 	  if (tramp->insn[i].bytes != (insn & tramp->insn[i].mask))
1115796c8dcSSimon Schubert 	    break;
1125796c8dcSSimon Schubert 	}
1135796c8dcSSimon Schubert     }
1145796c8dcSSimon Schubert   /* Trampoline doesn't match.  */
1155796c8dcSSimon Schubert   return 0;
1165796c8dcSSimon Schubert }
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert static int
1195796c8dcSSimon Schubert tramp_frame_sniffer (const struct frame_unwind *self,
1205796c8dcSSimon Schubert 		     struct frame_info *this_frame,
1215796c8dcSSimon Schubert 		     void **this_cache)
1225796c8dcSSimon Schubert {
1235796c8dcSSimon Schubert   const struct tramp_frame *tramp = self->unwind_data->tramp_frame;
1245796c8dcSSimon Schubert   CORE_ADDR pc = get_frame_pc (this_frame);
1255796c8dcSSimon Schubert   CORE_ADDR func;
1265796c8dcSSimon Schubert   struct tramp_frame_cache *tramp_cache;
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert   /* tausq/2004-12-12: We used to assume if pc has a name or is in a valid
1295796c8dcSSimon Schubert      section, then this is not a trampoline.  However, this assumption is
1305796c8dcSSimon Schubert      false on HPUX which has a signal trampoline that has a name; it can
1315796c8dcSSimon Schubert      also be false when using an alternative signal stack.  */
1325796c8dcSSimon Schubert   func = tramp_frame_start (tramp, this_frame, pc);
1335796c8dcSSimon Schubert   if (func == 0)
1345796c8dcSSimon Schubert     return 0;
1355796c8dcSSimon Schubert   tramp_cache = FRAME_OBSTACK_ZALLOC (struct tramp_frame_cache);
1365796c8dcSSimon Schubert   tramp_cache->func = func;
1375796c8dcSSimon Schubert   tramp_cache->tramp_frame = tramp;
1385796c8dcSSimon Schubert   (*this_cache) = tramp_cache;
1395796c8dcSSimon Schubert   return 1;
1405796c8dcSSimon Schubert }
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert void
1435796c8dcSSimon Schubert tramp_frame_prepend_unwinder (struct gdbarch *gdbarch,
1445796c8dcSSimon Schubert 			      const struct tramp_frame *tramp_frame)
1455796c8dcSSimon Schubert {
1465796c8dcSSimon Schubert   struct frame_data *data;
1475796c8dcSSimon Schubert   struct frame_unwind *unwinder;
1485796c8dcSSimon Schubert   int i;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   /* Check that the instruction sequence contains a sentinel.  */
1515796c8dcSSimon Schubert   for (i = 0; i < ARRAY_SIZE (tramp_frame->insn); i++)
1525796c8dcSSimon Schubert     {
1535796c8dcSSimon Schubert       if (tramp_frame->insn[i].bytes == TRAMP_SENTINEL_INSN)
1545796c8dcSSimon Schubert 	break;
1555796c8dcSSimon Schubert     }
1565796c8dcSSimon Schubert   gdb_assert (i < ARRAY_SIZE (tramp_frame->insn));
1575796c8dcSSimon Schubert   gdb_assert (tramp_frame->insn_size <= sizeof (tramp_frame->insn[0].bytes));
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert   data = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_data);
1605796c8dcSSimon Schubert   unwinder = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind);
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert   data->tramp_frame = tramp_frame;
1635796c8dcSSimon Schubert   unwinder->type = tramp_frame->frame_type;
1645796c8dcSSimon Schubert   unwinder->unwind_data = data;
1655796c8dcSSimon Schubert   unwinder->sniffer = tramp_frame_sniffer;
166*c50c785cSJohn Marino   unwinder->stop_reason = default_frame_unwind_stop_reason;
1675796c8dcSSimon Schubert   unwinder->this_id = tramp_frame_this_id;
1685796c8dcSSimon Schubert   unwinder->prev_register = tramp_frame_prev_register;
1695796c8dcSSimon Schubert   frame_unwind_prepend_unwinder (gdbarch, unwinder);
1705796c8dcSSimon Schubert }
171