.
[glibc/history.git] / nptl / unwind.c
blob56a423815837da9a5e7fe5ffc3efe962ac6f7fa1
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>
4 and Richard Henderson <rth@redhat.com>, 2003.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <setjmp.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include "pthreadP.h"
26 #include "jmpbuf-unwind.h"
28 #ifdef HAVE_FORCED_UNWIND
30 #ifdef _STACK_GROWS_DOWN
31 # define FRAME_LEFT(frame, other, adj) \
32 ((uintptr_t) frame - adj >= (uintptr_t) other - adj)
33 #elif _STACK_GROWS_UP
34 # define FRAME_LEFT(frame, other, adj) \
35 ((uintptr_t) frame - adj <= (uintptr_t) other - adj)
36 #else
37 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
38 #endif
40 static _Unwind_Reason_Code
41 unwind_stop (int version, _Unwind_Action actions,
42 _Unwind_Exception_Class exc_class,
43 struct _Unwind_Exception *exc_obj,
44 struct _Unwind_Context *context, void *stop_parameter)
46 struct pthread_unwind_buf *buf = stop_parameter;
47 struct pthread *self = THREAD_SELF;
48 struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
49 int do_longjump = 0;
51 /* Adjust all pointers used in comparisons, so that top of thread's
52 stack is at the top of address space. Without that, things break
53 if stack is allocated above the main stack. */
54 uintptr_t adj = (uintptr_t) self->stackblock + self->stackblock_size;
56 /* Do longjmp if we're at "end of stack", aka "end of unwind data".
57 We assume there are only C frame without unwind data in between
58 here and the jmp_buf target. Otherwise simply note that the CFA
59 of a function is NOT within it's stack frame; it's the SP of the
60 previous frame. */
61 if ((actions & _UA_END_OF_STACK)
62 || ! _JMPBUF_CFA_UNWINDS_ADJ (buf->cancel_jmp_buf[0].jmp_buf, context,
63 adj))
64 do_longjump = 1;
66 if (__builtin_expect (curp != NULL, 0))
68 /* Handle the compatibility stuff. Execute all handlers
69 registered with the old method which would be unwound by this
70 step. */
71 struct _pthread_cleanup_buffer *oldp = buf->priv.data.cleanup;
72 void *cfa = (void *) _Unwind_GetCFA (context);
74 if (curp != oldp && (do_longjump || FRAME_LEFT (cfa, curp, adj)))
78 /* Pointer to the next element. */
79 struct _pthread_cleanup_buffer *nextp = curp->__prev;
81 /* Call the handler. */
82 curp->__routine (curp->__arg);
84 /* To the next. */
85 curp = nextp;
87 while (curp != oldp
88 && (do_longjump || FRAME_LEFT (cfa, curp, adj)));
90 /* Mark the current element as handled. */
91 THREAD_SETMEM (self, cleanup, curp);
95 if (do_longjump)
96 __libc_unwind_longjmp ((struct __jmp_buf_tag *) buf->cancel_jmp_buf, 1);
98 return _URC_NO_REASON;
102 static void
103 unwind_cleanup (_Unwind_Reason_Code reason, struct _Unwind_Exception *exc)
105 /* When we get here a C++ catch block didn't rethrow the object. We
106 cannot handle this case and therefore abort. */
107 # define STR_N_LEN(str) str, strlen (str)
108 INTERNAL_SYSCALL_DECL (err);
109 INTERNAL_SYSCALL (write, err, 3, STDERR_FILENO,
110 STR_N_LEN ("FATAL: exception not rethrown\n"));
111 abort ();
114 #endif /* have forced unwind */
117 void
118 __cleanup_fct_attribute __attribute ((noreturn))
119 __pthread_unwind (__pthread_unwind_buf_t *buf)
121 struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
122 struct pthread *self = THREAD_SELF;
124 #ifdef HAVE_FORCED_UNWIND
125 /* This is not a catchable exception, so don't provide any details about
126 the exception type. We do need to initialize the field though. */
127 THREAD_SETMEM (self, exc.exception_class, 0);
128 THREAD_SETMEM (self, exc.exception_cleanup, unwind_cleanup);
130 _Unwind_ForcedUnwind (&self->exc, unwind_stop, ibuf);
131 #else
132 /* Handle the compatibility stuff first. Execute all handlers
133 registered with the old method. We don't execute them in order,
134 instead, they will run first. */
135 struct _pthread_cleanup_buffer *oldp = ibuf->priv.data.cleanup;
136 struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
138 if (curp != oldp)
142 /* Pointer to the next element. */
143 struct _pthread_cleanup_buffer *nextp = curp->__prev;
145 /* Call the handler. */
146 curp->__routine (curp->__arg);
148 /* To the next. */
149 curp = nextp;
151 while (curp != oldp);
153 /* Mark the current element as handled. */
154 THREAD_SETMEM (self, cleanup, curp);
157 /* We simply jump to the registered setjmp buffer. */
158 __libc_unwind_longjmp ((struct __jmp_buf_tag *) ibuf->cancel_jmp_buf, 1);
159 #endif
160 /* NOTREACHED */
162 /* We better do not get here. */
163 abort ();
165 hidden_def (__pthread_unwind)
168 void
169 __cleanup_fct_attribute __attribute ((noreturn))
170 __pthread_unwind_next (__pthread_unwind_buf_t *buf)
172 struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
174 __pthread_unwind ((__pthread_unwind_buf_t *) ibuf->priv.data.prev);
176 hidden_def (__pthread_unwind_next)