1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2003, 2005 Hewlett-Packard Co
3 	Contributed by David Mosberger-Tang <davidm@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 #ifndef unwind_internal_h
27 #define unwind_internal_h
28 
29 #define UNW_LOCAL_ONLY
30 
31 #include <unwind.h>
32 #include <stdlib.h>
33 #include <libunwind.h>
34 
35 #include "libunwind_i.h"
36 
37 /* The version of the _Unwind_*() interface implemented by this code.  */
38 #define _U_VERSION	1
39 
40 typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)
41 	(int, _Unwind_Action, uint64_t, struct _Unwind_Exception *,
42 	 struct _Unwind_Context *);
43 
44 struct _Unwind_Context {
45   unw_cursor_t cursor;
46   int end_of_stack;	/* set to 1 if the end of stack was reached */
47 };
48 
49 /* This must be a macro because unw_getcontext() must be invoked from
50    the callee, even if optimization (and hence inlining) is turned
51    off.  The macro arguments MUST NOT have any side-effects. */
52 #define _Unwind_InitContext(context, uc)				     \
53   ((context)->end_of_stack = 0,						     \
54    ((unw_getcontext (uc) < 0 || unw_init_local (&(context)->cursor, uc) < 0) \
55     ? -1 : 0))
56 
57 static _Unwind_Reason_Code ALWAYS_INLINE
_Unwind_Phase2(struct _Unwind_Exception * exception_object,struct _Unwind_Context * context,int * destroy_map)58 _Unwind_Phase2 (struct _Unwind_Exception *exception_object,
59 		struct _Unwind_Context *context,
60 /* ANDROID support update. */
61 		int *destroy_map)
62 /* End of ANDROID update. */
63 {
64   _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) exception_object->private_1;
65   uint64_t exception_class = exception_object->exception_class;
66   void *stop_parameter = (void *) exception_object->private_2;
67   _Unwind_Personality_Fn personality;
68   _Unwind_Reason_Code reason;
69   _Unwind_Action actions;
70   unw_proc_info_t pi;
71   unw_word_t ip;
72   int ret;
73 
74   /* ANDROID support update. */
75   *destroy_map = 1;
76   /* End of ANDROID update. */
77 
78   actions = _UA_CLEANUP_PHASE;
79   if (stop)
80     actions |= _UA_FORCE_UNWIND;
81 
82   while (1)
83     {
84       ret = unw_step (&context->cursor);
85       if (ret <= 0)
86 	{
87 	   /* ANDROID support update. */
88 	   /* Treat any stop as end of stack. */
89 	   actions |= _UA_END_OF_STACK;
90 	   context->end_of_stack = 1;
91 	   /* End of ANDROID support. */
92 	}
93 
94       if (stop)
95 	{
96 	  /* ANDROID support update. */
97 	  /* The stop function might not return, so free any local map. */
98 	  unw_map_local_destroy ();
99 	  /* End of ANDROID support. */
100 	  reason = (*stop) (_U_VERSION, actions, exception_class,
101 			    exception_object, context, stop_parameter);
102 	  if (reason != _URC_NO_REASON)
103 	    {
104 	      /* Stop function may return _URC_FATAL_PHASE2_ERROR if
105 	         it's unable to handle end-of-stack condition or
106 	         _URC_FATAL_PHASE2_ERROR if something is wrong.  Not
107 	         that it matters: the resulting state is indeterminate
108 	         anyhow so we must return _URC_FATAL_PHASE2_ERROR... */
109 	      *destroy_map = 0;
110 	      return _URC_FATAL_PHASE2_ERROR;
111 	    }
112 	  /* ANDROID support update. */
113 	  unw_map_local_create ();
114 	  /* End of ANDROID support. */
115 	}
116 
117       if (context->end_of_stack
118 	  || unw_get_proc_info (&context->cursor, &pi) < 0)
119 	return _URC_FATAL_PHASE2_ERROR;
120 
121       personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
122       if (personality)
123 	{
124 	  if (!stop)
125 	    {
126 	      if (unw_get_reg (&context->cursor, UNW_REG_IP, &ip) < 0)
127 		return _URC_FATAL_PHASE2_ERROR;
128 
129 	      if ((unsigned long) stop_parameter == ip)
130 		actions |= _UA_HANDLER_FRAME;
131 	    }
132 
133 	  reason = (*personality) (_U_VERSION, actions, exception_class,
134 				   exception_object, context);
135 	  if (reason != _URC_CONTINUE_UNWIND)
136 	    {
137 	      if (reason == _URC_INSTALL_CONTEXT)
138 		{
139 		  /* we may regain control via _Unwind_Resume() */
140 		  unw_resume (&context->cursor);
141 		  abort ();
142 		}
143 	      else
144 		return _URC_FATAL_PHASE2_ERROR;
145 	    }
146 	  if (actions & _UA_HANDLER_FRAME)
147 	    /* The personality routine for the handler-frame changed
148 	       it's mind; that's a no-no... */
149 	    abort ();
150 	}
151     }
152   return _URC_FATAL_PHASE2_ERROR;	/* shouldn't be reached */
153 }
154 
155 #endif /* unwind_internal_h */
156