1 GETCONTEXT(2) System Calls GETCONTEXT(2) 2 3 NAME 4 getcontext, getcontext_extd, setcontext - get and set current user 5 context 6 7 SYNOPSIS 8 #include <ucontext.h> 9 10 int 11 getcontext(ucontext_t *ucp); 12 13 int 14 getcontext_extd(ucontext_t *ucp, uint32_t flags); 15 16 int 17 setcontext(const ucontext_t *ucp); 18 19 DESCRIPTION 20 The getcontext() function initializes the structure pointed to by ucp to 21 the current user context of the calling process. The ucontext_t type 22 that ucp points to defines the user context and includes the contents of 23 the calling process' machine registers, the signal mask, and the current 24 execution stack. 25 26 The ucontext_t structure is a part of the system ABI. However, most 27 architectures have added additional register states such as the extended 28 vector and floating point registers that are not part of that. To 29 facilitate getting that state (such as the x86 xsave area) the 30 getcontext_extd() function exists. Once called, the context will be 31 initialized and is suitable for use in other context operations just as 32 though one had called getcontext(). 33 34 Unlike the getcontext() function, getcontext_extd() assumes that callers 35 have previously initialized ucp and thus it treats additional members 36 (such as the uc_xsave member on x86) as potentially valid. To allow for 37 all extended states to be copied out, ucp must be allocated with 38 ucontext_alloc(3C). Otherwise whether it is declared on the stack, as 39 global data, allocated dynamically, or part of a structure, ucp must be 40 zeroed through a call to bzero(3C) or memset(3C) prior to calling 41 getcontext_extd(). Improper initialization can lead to memory safety 42 bugs, making it critical that this is done. 43 44 The flags member must be zero and is present to allow for what is copied 45 out to change in the future. This indicates that the system should 46 attempt to copy out all extended states, though if the ucontext_t was not 47 allocated with ucontext_alloc(3C), some extended states may not be. 48 49 The setcontext() function restores the user context pointed to by ucp. A 50 successful call to setcontext() does not return; program execution 51 resumes at the point specified by the ucp argument passed to 52 setcontext(). The ucp argument should be created either by a prior call 53 to getcontext(), or by being passed as an argument to a signal handler. 54 If the ucp argument was created with getcontext(), program execution 55 continues as if the corresponding call of getcontext() had just returned. 56 If the ucp argument was created with makecontext(3C), program execution 57 continues with the function passed to makecontext(3C). When that 58 function returns, the process continues as if after a call to 59 setcontext() with the ucp argument that was input to makecontext(3C). If 60 the ucp argument was passed to a signal handler, program execution 61 continues with the program instruction following the instruction 62 interrupted by the signal. If the uc_link member of the ucontext_t 63 structure pointed to by the ucp argument is NULL, then this context is 64 the main context, and the process will exit when this context returns. 65 The effects of passing a ucp argument obtained from any other source are 66 unspecified. 67 68 RETURN VALUES 69 On successful completion, setcontext() does not return and getcontext() 70 and getcontext_extd() returns 0. Otherwise, -1 is returned. 71 72 ERRORS 73 No errors are defined for getcontext() or setcontext(). 74 75 The getcontext_extd() function only sets errno in some circumstances when 76 it fails. The function may fail if: 77 78 EINVAL flags had invalid values. 79 80 USAGE 81 When a signal handler is executed, the current user context is saved and 82 a new context is created. If the thread leaves the signal handler via 83 longjmp(3C), then it is unspecified whether the context at the time of 84 the corresponding setjmp(3C) call is restored and thus whether future 85 calls to getcontext() will provide an accurate representation of the 86 current context, since the context restored by longjmp(3C) may not 87 contain all the information that setcontext() requires. Signal handlers 88 should use siglongjmp(3C) instead. 89 90 Portable applications should not modify or access the uc_mcontext member 91 of ucontext_t. A portable application cannot assume that context 92 includes any process-wide static data, possibly including errno. Users 93 manipulating contexts should take care to handle these explicitly when 94 required. 95 96 INTERFACE STABILITY 97 Committed 98 99 SEE ALSO 100 sigaction(2), sigaltstack(2), sigprocmask(2), bsd_signal(3C), 101 makecontext(3C), setjmp(3C), sigsetjmp(3C), ucontext_alloc(3C), 102 ucontext.h(3HEAD), attributes(7), standards(7) 103 104 illumos January 24, 2022 illumos