Home | History | Annotate | Download | only in x86
      1 /* libunwind - a platform-independent unwind library
      2    Copyright (C) 2002-2004 Hewlett-Packard Co
      3 	Contributed by David Mosberger-Tang <davidm (at) hpl.hp.com>
      4 
      5 This file is part of libunwind.
      6 
      7 Permission is hereby granted, free of charge, to any person obtaining
      8 a copy of this software and associated documentation files (the
      9 "Software"), to deal in the Software without restriction, including
     10 without limitation the rights to use, copy, modify, merge, publish,
     11 distribute, sublicense, and/or sell copies of the Software, and to
     12 permit persons to whom the Software is furnished to do so, subject to
     13 the following conditions:
     14 
     15 The above copyright notice and this permission notice shall be
     16 included in all copies or substantial portions of the Software.
     17 
     18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
     25 
     26 #include "unwind_i.h"
     27 #include "offsets.h"
     28 
     29 PROTECTED int
     30 unw_step (unw_cursor_t *cursor)
     31 {
     32   struct cursor *c = (struct cursor *) cursor;
     33   int ret, i;
     34 
     35   Debug (1, "(cursor=%p, ip=0x%08x)\n", c, (unsigned) c->dwarf.ip);
     36 
     37   /* ANDROID support update. */
     38   /* Save the current ip/cfa to prevent looping if the decode yields
     39      the same ip/cfa as before. */
     40   unw_word_t old_ip = c->dwarf.ip;
     41   unw_word_t old_cfa = c->dwarf.cfa;
     42   /* End of ANDROID update. */
     43 
     44   /* Try DWARF-based unwinding... */
     45   ret = dwarf_step (&c->dwarf);
     46 
     47   if (ret < 0)
     48     {
     49       /* DWARF failed, let's see if we can follow the frame-chain
     50 	 or skip over the signal trampoline.  */
     51       struct dwarf_loc ebp_loc, eip_loc;
     52 
     53       /* We could get here because of missing/bad unwind information.
     54          Validate all addresses before dereferencing. */
     55       c->validate = 1;
     56 
     57       Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
     58 
     59       if (unw_is_signal_frame (cursor))
     60         {
     61           ret = unw_handle_signal_frame(cursor);
     62 	  if (ret < 0)
     63 	    {
     64 	      Debug (2, "returning 0\n");
     65 	      return 0;
     66 	    }
     67         }
     68       else
     69 	{
     70 	  ret = dwarf_get (&c->dwarf, c->dwarf.loc[EBP], &c->dwarf.cfa);
     71 	  if (ret < 0)
     72 	    {
     73 	      Debug (2, "returning %d\n", ret);
     74 	      return ret;
     75 	    }
     76 
     77 	  Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]),
     78 		 c->dwarf.cfa);
     79 
     80 	  ebp_loc = DWARF_LOC (c->dwarf.cfa, 0);
     81 	  eip_loc = DWARF_LOC (c->dwarf.cfa + 4, 0);
     82 	  c->dwarf.cfa += 8;
     83 
     84 	  /* Mark all registers unsaved, since we don't know where
     85 	     they are saved (if at all), except for the EBP and
     86 	     EIP.  */
     87 	  for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
     88 	    c->dwarf.loc[i] = DWARF_NULL_LOC;
     89 
     90           c->dwarf.loc[EBP] = ebp_loc;
     91           c->dwarf.loc[EIP] = eip_loc;
     92 	}
     93       c->dwarf.ret_addr_column = EIP;
     94 
     95       if (!DWARF_IS_NULL_LOC (c->dwarf.loc[EBP]))
     96 	{
     97 	  ret = dwarf_get (&c->dwarf, c->dwarf.loc[EIP], &c->dwarf.ip);
     98 	  if (ret < 0)
     99 	    {
    100 	      Debug (13, "dwarf_get([EIP=0x%x]) failed\n", DWARF_GET_LOC (c->dwarf.loc[EIP]));
    101 	      Debug (2, "returning %d\n", ret);
    102 	      return ret;
    103 	    }
    104 	  else
    105 	    {
    106 	      Debug (13, "[EIP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EIP]),
    107 		c->dwarf.ip);
    108 	    }
    109 	}
    110       else
    111 	c->dwarf.ip = 0;
    112     }
    113 
    114   /* ANDROID support update. */
    115   if (ret >= 0)
    116     {
    117       if (c->dwarf.ip)
    118         {
    119           /* Adjust the pc to the instruction before. */
    120           c->dwarf.ip--;
    121         }
    122       /* If the decode yields the exact same ip/cfa as before, then indicate
    123          the unwind is complete. */
    124       if (old_ip == c->dwarf.ip && old_cfa == c->dwarf.cfa)
    125         return 0;
    126       c->dwarf.frame++;
    127     }
    128   /* End of ANDROID update. */
    129   ret = (c->dwarf.ip == 0) ? 0 : 1;
    130   Debug (2, "returning %d\n", ret);
    131   return ret;
    132 }
    133