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