2003-04-12 00:58:26 +00:00
|
|
|
/* Copyright (C) 2003 Free Software Foundation, Inc.
|
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Ulrich Drepper <drepper@redhat.com>
|
|
|
|
and Richard Henderson <rth@redhat.com>, 2003.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA. */
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdlib.h>
|
2003-07-31 19:26:38 +00:00
|
|
|
#include <string.h>
|
2003-05-16 17:40:49 +00:00
|
|
|
#include <unistd.h>
|
2003-04-12 00:58:26 +00:00
|
|
|
#include "pthreadP.h"
|
2003-09-04 05:57:06 +00:00
|
|
|
#include "jmpbuf-unwind.h"
|
2003-04-12 00:58:26 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FORCED_UNWIND
|
|
|
|
|
2003-11-06 04:29:42 +00:00
|
|
|
#ifdef _STACK_GROWS_DOWN
|
|
|
|
# define FRAME_LEFT(frame, other) ((char *) frame >= (char *) other)
|
|
|
|
#elif _STACK_GROWS_UP
|
|
|
|
# define FRAME_LEFT(frame, other) ((char *) frame <= (char *) other)
|
|
|
|
#else
|
|
|
|
# error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
|
|
|
|
#endif
|
|
|
|
|
2003-04-12 00:58:26 +00:00
|
|
|
static _Unwind_Reason_Code
|
|
|
|
unwind_stop (int version, _Unwind_Action actions,
|
|
|
|
_Unwind_Exception_Class exc_class,
|
|
|
|
struct _Unwind_Exception *exc_obj,
|
|
|
|
struct _Unwind_Context *context, void *stop_parameter)
|
|
|
|
{
|
|
|
|
struct pthread_unwind_buf *buf = stop_parameter;
|
2003-11-06 04:29:42 +00:00
|
|
|
struct pthread *self = THREAD_SELF;
|
|
|
|
struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
|
|
|
|
int do_longjump = 0;
|
2003-04-12 00:58:26 +00:00
|
|
|
|
|
|
|
/* Do longjmp if we're at "end of stack", aka "end of unwind data".
|
|
|
|
We assume there are only C frame without unwind data in between
|
|
|
|
here and the jmp_buf target. Otherwise simply note that the CFA
|
|
|
|
of a function is NOT within it's stack frame; it's the SP of the
|
|
|
|
previous frame. */
|
|
|
|
if ((actions & _UA_END_OF_STACK)
|
2003-09-04 05:57:06 +00:00
|
|
|
|| ! _JMPBUF_CFA_UNWINDS (buf->cancel_jmp_buf[0].jmp_buf, context))
|
2003-11-06 04:29:42 +00:00
|
|
|
do_longjump = 1;
|
|
|
|
|
|
|
|
if (__builtin_expect (curp != NULL, 0))
|
|
|
|
{
|
|
|
|
/* Handle the compatibility stuff. Execute all handlers
|
|
|
|
registered with the old method which would be unwound by this
|
|
|
|
step. */
|
|
|
|
struct _pthread_cleanup_buffer *oldp = buf->priv.data.cleanup;
|
|
|
|
void *cfa = (void *) _Unwind_GetCFA (context);
|
|
|
|
|
|
|
|
if (curp != oldp && (do_longjump || FRAME_LEFT (cfa, curp)))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Pointer to the next element. */
|
|
|
|
struct _pthread_cleanup_buffer *nextp = curp->__prev;
|
|
|
|
|
|
|
|
/* Call the handler. */
|
|
|
|
curp->__routine (curp->__arg);
|
|
|
|
|
|
|
|
/* To the next. */
|
|
|
|
curp = nextp;
|
|
|
|
}
|
|
|
|
while (curp != oldp && (do_longjump || FRAME_LEFT (cfa, curp)));
|
|
|
|
|
|
|
|
/* Mark the current element as handled. */
|
|
|
|
THREAD_SETMEM (self, cleanup, curp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_longjump)
|
2003-04-12 00:58:26 +00:00
|
|
|
__libc_longjmp ((struct __jmp_buf_tag *) buf->cancel_jmp_buf, 1);
|
|
|
|
|
|
|
|
return _URC_NO_REASON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
unwind_cleanup (_Unwind_Reason_Code reason, struct _Unwind_Exception *exc)
|
|
|
|
{
|
2003-05-09 03:00:52 +00:00
|
|
|
/* When we get here a C++ catch block didn't rethrow the object. We
|
|
|
|
cannot handle this case and therefore abort. */
|
|
|
|
# define STR_N_LEN(str) str, strlen (str)
|
|
|
|
INTERNAL_SYSCALL_DECL (err);
|
|
|
|
INTERNAL_SYSCALL (write, err, 3, STDERR_FILENO,
|
|
|
|
STR_N_LEN ("FATAL: exception not rethrown\n"));
|
|
|
|
abort ();
|
2003-04-12 00:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* have forced unwind */
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
__cleanup_fct_attribute __attribute ((noreturn))
|
|
|
|
__pthread_unwind (__pthread_unwind_buf_t *buf)
|
|
|
|
{
|
|
|
|
struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
|
|
|
|
struct pthread *self = THREAD_SELF;
|
|
|
|
|
2003-11-06 04:29:42 +00:00
|
|
|
#ifdef HAVE_FORCED_UNWIND
|
|
|
|
/* This is not a catchable exception, so don't provide any details about
|
|
|
|
the exception type. We do need to initialize the field though. */
|
|
|
|
THREAD_SETMEM (self, exc.exception_class, 0);
|
|
|
|
THREAD_SETMEM (self, exc.exception_cleanup, unwind_cleanup);
|
|
|
|
|
|
|
|
_Unwind_ForcedUnwind (&self->exc, unwind_stop, ibuf);
|
|
|
|
#else
|
2003-04-12 00:58:26 +00:00
|
|
|
/* Handle the compatibility stuff first. Execute all handlers
|
|
|
|
registered with the old method. We don't execute them in order,
|
|
|
|
instead, they will run first. */
|
|
|
|
struct _pthread_cleanup_buffer *oldp = ibuf->priv.data.cleanup;
|
|
|
|
struct _pthread_cleanup_buffer *curp = THREAD_GETMEM (self, cleanup);
|
|
|
|
|
2003-04-12 08:38:14 +00:00
|
|
|
if (curp != oldp)
|
2003-04-12 00:58:26 +00:00
|
|
|
{
|
2003-04-12 08:38:14 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Pointer to the next element. */
|
|
|
|
struct _pthread_cleanup_buffer *nextp = curp->__prev;
|
2003-04-12 00:58:26 +00:00
|
|
|
|
2003-04-12 08:38:14 +00:00
|
|
|
/* Call the handler. */
|
|
|
|
curp->__routine (curp->__arg);
|
2003-04-12 00:58:26 +00:00
|
|
|
|
2003-04-12 08:38:14 +00:00
|
|
|
/* To the next. */
|
|
|
|
curp = nextp;
|
|
|
|
}
|
|
|
|
while (curp != oldp);
|
2003-04-12 00:58:26 +00:00
|
|
|
|
2003-04-12 08:38:14 +00:00
|
|
|
/* Mark the current element as handled. */
|
|
|
|
THREAD_SETMEM (self, cleanup, curp);
|
|
|
|
}
|
2003-04-12 00:58:26 +00:00
|
|
|
|
|
|
|
/* We simply jump to the registered setjmp buffer. */
|
|
|
|
__libc_longjmp ((struct __jmp_buf_tag *) ibuf->cancel_jmp_buf, 1);
|
|
|
|
#endif
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
/* We better do not get here. */
|
|
|
|
abort ();
|
|
|
|
}
|
2003-07-03 07:37:59 +00:00
|
|
|
hidden_def (__pthread_unwind)
|
2003-04-12 00:58:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
__cleanup_fct_attribute __attribute ((noreturn))
|
|
|
|
__pthread_unwind_next (__pthread_unwind_buf_t *buf)
|
|
|
|
{
|
|
|
|
struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
|
|
|
|
|
2003-05-31 07:50:12 +00:00
|
|
|
__pthread_unwind ((__pthread_unwind_buf_t *) ibuf->priv.data.prev);
|
2003-04-12 00:58:26 +00:00
|
|
|
}
|
2003-07-08 21:08:13 +00:00
|
|
|
hidden_def (__pthread_unwind_next)
|