• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)58 _Unwind_Phase2 (struct _Unwind_Exception *exception_object,
59                 struct _Unwind_Context *context)
60 {
61   _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) exception_object->private_1;
62   uint64_t exception_class = exception_object->exception_class;
63   void *stop_parameter = (void *) exception_object->private_2;
64   _Unwind_Personality_Fn personality;
65   _Unwind_Reason_Code reason;
66   _Unwind_Action actions;
67   unw_proc_info_t pi;
68   unw_word_t ip;
69   int ret;
70 
71   actions = _UA_CLEANUP_PHASE;
72   if (stop)
73     actions |= _UA_FORCE_UNWIND;
74 
75   while (1)
76     {
77       ret = unw_step (&context->cursor);
78       if (ret <= 0)
79         {
80           if (ret == 0)
81             {
82               actions |= _UA_END_OF_STACK;
83               context->end_of_stack = 1;
84             }
85           else
86             return _URC_FATAL_PHASE2_ERROR;
87         }
88 
89       if (stop)
90         {
91           reason = (*stop) (_U_VERSION, actions, exception_class,
92                             exception_object, context, stop_parameter);
93           if (reason != _URC_NO_REASON)
94             /* Stop function may return _URC_FATAL_PHASE2_ERROR if
95                it's unable to handle end-of-stack condition or
96                _URC_FATAL_PHASE2_ERROR if something is wrong.  Not
97                that it matters: the resulting state is indeterminate
98                anyhow so we must return _URC_FATAL_PHASE2_ERROR... */
99             return _URC_FATAL_PHASE2_ERROR;
100         }
101 
102       if (context->end_of_stack
103           || unw_get_proc_info (&context->cursor, &pi) < 0)
104         return _URC_FATAL_PHASE2_ERROR;
105 
106       personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
107       if (personality)
108         {
109           if (!stop)
110             {
111               if (unw_get_reg (&context->cursor, UNW_REG_IP, &ip) < 0)
112                 return _URC_FATAL_PHASE2_ERROR;
113 
114               if ((unsigned long) stop_parameter == ip)
115                 actions |= _UA_HANDLER_FRAME;
116             }
117 
118           reason = (*personality) (_U_VERSION, actions, exception_class,
119                                    exception_object, context);
120           if (reason != _URC_CONTINUE_UNWIND)
121             {
122               if (reason == _URC_INSTALL_CONTEXT)
123                 {
124                   /* we may regain control via _Unwind_Resume() */
125                   unw_resume (&context->cursor);
126                   abort ();
127                 }
128               else
129                 return _URC_FATAL_PHASE2_ERROR;
130             }
131           if (actions & _UA_HANDLER_FRAME)
132             /* The personality routine for the handler-frame changed
133                it's mind; that's a no-no... */
134             abort ();
135         }
136     }
137   return _URC_FATAL_PHASE2_ERROR;       /* shouldn't be reached */
138 }
139 
140 #endif /* unwind_internal_h */
141