]> git.saurik.com Git - apple/libc.git/blame - i386/gen/getmcontext.c
Libc-763.11.tar.gz
[apple/libc.git] / i386 / gen / getmcontext.c
CommitLineData
224c7076 1/*
1f2f436a 2 * Copyright (c) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
224c7076
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#if defined(__i386__)
25
26#define _XOPEN_SOURCE 600L
27
28#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
31#include <pthread.h>
32#include <signal.h>
33#include <ucontext.h>
34
35extern size_t pthread_get_stacksize_np(pthread_t);
36extern void *pthread_get_stackaddr_np(pthread_t);
37#ifdef __DYNAMIC__
38extern int __in_sigtramp;
39#endif /* __DYNAMIC_ */
40
41__private_extern__ mcontext_t
42getmcontext(ucontext_t *uctx, void *sp)
43{
44 pthread_t self = pthread_self();
45 mcontext_t mctx = (mcontext_t)&uctx->__mcontext_data;
46 size_t stacksize = pthread_get_stacksize_np(self);
47 stack_t stack;
48
49 uctx->uc_stack.ss_sp = sp;
50 uctx->uc_stack.ss_flags = 0;
51
52 if (0 == sigaltstack(NULL, &stack)) {
53 if (stack.ss_flags & SS_ONSTACK) {
54 uctx->uc_stack = stack;
55 stacksize = stack.ss_size;
56 }
57 }
58
59 if (stacksize == 0) { /* main thread doesn't have pthread stack size */
34e8f829 60 struct rlimit rlim;
224c7076 61 if (0 == getrlimit(RLIMIT_STACK, &rlim))
34e8f829 62 stacksize = rlim.rlim_cur;
224c7076
A
63 }
64
65 uctx->uc_stack.ss_size = stacksize;
66
67 if (uctx->uc_mcontext != mctx) {
68 uctx->uc_mcontext = mctx;
69
70#ifdef __DYNAMIC__
71 uctx->uc_link = (ucontext_t*)__in_sigtramp; /* non-zero if in signal handler */
72#else /* !__DYNAMIC__ */
73 uctx->uc_link = 0;
74#endif /* __DYNAMIC__ */
75
76 }
77
78 sigprocmask(0, NULL, &uctx->uc_sigmask);
79 return mctx;
80}
81
82#endif /* __i386__ */