]> git.saurik.com Git - apple/libplatform.git/blob - src/ucontext/generic/getmcontext.c
libplatform-220.tar.gz
[apple/libplatform.git] / src / ucontext / generic / getmcontext.c
1 /*
2 * Copyright (c) 2007, 2008, 2009 Apple Inc. All rights reserved.
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 #define _XOPEN_SOURCE 600L
25 #include <ucontext.h>
26 #include <errno.h>
27
28 #if defined(__x86_64__) || defined(__i386__)
29
30 #include <sys/resource.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <signal.h>
34
35 extern int __sigaltstack(const stack_t * __restrict, stack_t * __restrict);
36
37 #ifdef __DYNAMIC__
38 extern int __in_sigtramp;
39 #endif /* __DYNAMIC_ */
40
41 __attribute__((visibility("hidden")))
42 mcontext_t
43 getmcontext(ucontext_t *uctx, void *sp)
44 {
45 mcontext_t mctx = (mcontext_t)&uctx->__mcontext_data;
46 size_t stacksize = 0;
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) {
60 struct rlimit rlim;
61 if (0 == getrlimit(RLIMIT_STACK, &rlim))
62 stacksize = rlim.rlim_cur;
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*)(uintptr_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 #else
83
84 int
85 getcontext(ucontext_t *u)
86 {
87 errno = ENOTSUP;
88 return -1;
89 }
90
91 #endif