summaryrefslogtreecommitdiff
path: root/src/unwind/unwind-internal.h
blob: c68fc3c5ed34adf815a854aac06385217c05a490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* libunwind - a platform-independent unwind library
   Copyright (C) 2003, 2005 Hewlett-Packard Co
        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#ifndef unwind_internal_h
#define unwind_internal_h

#define UNW_LOCAL_ONLY

#include <unwind.h>
#include <stdlib.h>
#include <libunwind.h>

#include "libunwind_i.h"

/* The version of the _Unwind_*() interface implemented by this code.  */
#define _U_VERSION      1

typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)
        (int, _Unwind_Action, uint64_t, struct _Unwind_Exception *,
         struct _Unwind_Context *);

struct _Unwind_Context {
  unw_cursor_t cursor;
  int end_of_stack;     /* set to 1 if the end of stack was reached */
};

/* This must be a macro because unw_getcontext() must be invoked from
   the callee, even if optimization (and hence inlining) is turned
   off.  The macro arguments MUST NOT have any side-effects. */
#define _Unwind_InitContext(context, uc)                                     \
  ((context)->end_of_stack = 0,                                              \
   ((unw_getcontext (uc) < 0 || unw_init_local (&(context)->cursor, uc) < 0) \
    ? -1 : 0))

static _Unwind_Reason_Code ALWAYS_INLINE
_Unwind_Phase2 (struct _Unwind_Exception *exception_object,
                struct _Unwind_Context *context)
{
  _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) exception_object->private_1;
  uint64_t exception_class = exception_object->exception_class;
  void *stop_parameter = (void *) exception_object->private_2;
  _Unwind_Personality_Fn personality;
  _Unwind_Reason_Code reason;
  _Unwind_Action actions;
  unw_proc_info_t pi;
  unw_word_t ip;
  int ret;

  actions = _UA_CLEANUP_PHASE;
  if (stop)
    actions |= _UA_FORCE_UNWIND;

  while (1)
    {
      ret = unw_step (&context->cursor);
      if (ret <= 0)
        {
          if (ret == 0)
            {
              actions |= _UA_END_OF_STACK;
              context->end_of_stack = 1;
            }
          else
            return _URC_FATAL_PHASE2_ERROR;
        }

      if (stop)
        {
          reason = (*stop) (_U_VERSION, actions, exception_class,
                            exception_object, context, stop_parameter);
          if (reason != _URC_NO_REASON)
            /* Stop function may return _URC_FATAL_PHASE2_ERROR if
               it's unable to handle end-of-stack condition or
               _URC_FATAL_PHASE2_ERROR if something is wrong.  Not
               that it matters: the resulting state is indeterminate
               anyhow so we must return _URC_FATAL_PHASE2_ERROR... */
            return _URC_FATAL_PHASE2_ERROR;
        }

      if (context->end_of_stack
          || unw_get_proc_info (&context->cursor, &pi) < 0)
        return _URC_FATAL_PHASE2_ERROR;

      personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
      if (personality)
        {
          if (!stop)
            {
              if (unw_get_reg (&context->cursor, UNW_REG_IP, &ip) < 0)
                return _URC_FATAL_PHASE2_ERROR;

              if ((unsigned long) stop_parameter == ip)
                actions |= _UA_HANDLER_FRAME;
            }

          reason = (*personality) (_U_VERSION, actions, exception_class,
                                   exception_object, context);
          if (reason != _URC_CONTINUE_UNWIND)
            {
              if (reason == _URC_INSTALL_CONTEXT)
                {
                  /* we may regain control via _Unwind_Resume() */
                  unw_resume (&context->cursor);
                  abort ();
                }
              else
                return _URC_FATAL_PHASE2_ERROR;
            }
          if (actions & _UA_HANDLER_FRAME)
            /* The personality routine for the handler-frame changed
               it's mind; that's a no-no... */
            abort ();
        }
    }
  return _URC_FATAL_PHASE2_ERROR;       /* shouldn't be reached */
}

#endif /* unwind_internal_h */