xref: /freebsd/lib/libc/gen/getcontext.3 (revision 3cbb4cc200f8a0ad7ed08233425ea54524a21f1c)
1.\" Copyright (c) 2002 Packet Design, LLC.
2.\" All rights reserved.
3.\"
4.\" Subject to the following obligations and disclaimer of warranty,
5.\" use and redistribution of this software, in source or object code
6.\" forms, with or without modifications are expressly permitted by
7.\" Packet Design; provided, however, that:
8.\"
9.\"    (i)  Any and all reproductions of the source or object code
10.\"         must include the copyright notice above and the following
11.\"         disclaimer of warranties; and
12.\"    (ii) No rights are granted, in any manner or form, to use
13.\"         Packet Design trademarks, including the mark "PACKET DESIGN"
14.\"         on advertising, endorsements, or otherwise except as such
15.\"         appears in the above copyright notice or in the software.
16.\"
17.\" THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
18.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
19.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
20.\" THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
21.\" WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
22.\" OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
23.\" OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
24.\" OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
25.\" RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
26.\" LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
27.\" OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
28.\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
29.\" DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
30.\" USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
31.\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
33.\" THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
34.\" THE POSSIBILITY OF SUCH DAMAGE.
35.\"
36.\" $FreeBSD$
37.\"
38.Dd March 23, 2020
39.Dt GETCONTEXT 3
40.Os
41.Sh NAME
42.Nm getcontext , getcontextx , setcontext
43.Nd get and set user thread context
44.Sh LIBRARY
45.Lb libc
46.Sh SYNOPSIS
47.In ucontext.h
48.Ft int
49.Fn getcontext "ucontext_t *ucp"
50.Ft ucontext_t *
51.Fn getcontextx "void"
52.Ft int
53.Fn setcontext "const ucontext_t *ucp"
54.Sh DESCRIPTION
55The
56.Fn getcontext
57function
58saves the current thread's execution context in the structure pointed to by
59.Fa ucp .
60This saved context may then later be restored by calling
61.Fn setcontext .
62.Pp
63The
64.Fn getcontextx
65function saves the current execution context in the newly allocated structure
66.Vt ucontext_t ,
67which is returned on success.
68If architecture defines additional CPU states that can be stored in extended
69blocks referenced from the
70.Vt ucontext_t ,
71the memory for them may be allocated and their context also stored.
72Memory returned by
73.Fn getcontextx
74function shall be freed using
75.Fn free 3 .
76.Pp
77The
78.Fn setcontext
79function
80makes a previously saved thread context the current thread context, i.e.,
81the current context is lost and
82.Fn setcontext
83does not return.
84Instead, execution continues in the context specified by
85.Fa ucp ,
86which must have been previously initialized by a call to
87.Fn getcontext ,
88.Xr makecontext 3 ,
89or by being passed as an argument to a signal handler (see
90.Xr sigaction 2 ) .
91.Pp
92If
93.Fa ucp
94was initialized by
95.Fn getcontext ,
96then execution continues as if the original
97.Fn getcontext
98call had just returned (again).
99.Pp
100If
101.Fa ucp
102was initialized by
103.Xr makecontext 3 ,
104execution continues with the invocation of the function specified to
105.Xr makecontext 3 .
106When that function returns,
107.Fa "ucp->uc_link"
108determines what happens next:
109if
110.Fa "ucp->uc_link"
111is
112.Dv NULL ,
113the process exits;
114otherwise,
115.Fn setcontext "ucp->uc_link"
116is implicitly invoked.
117.Pp
118If
119.Fa ucp
120was initialized by the invocation of a signal handler, execution continues
121at the point the thread was interrupted by the signal.
122.Sh RETURN VALUES
123If successful,
124.Fn getcontext
125returns zero and
126.Fn setcontext
127does not return; otherwise \-1 is returned.
128The
129.Fn getcontextx
130returns pointer to the allocated and initialized context on success, and
131.Va NULL
132on failure.
133.Sh ERRORS
134No errors are defined for
135.Fn getcontext
136or
137.Fn setcontext .
138The
139.Fn getcontextx
140may return the following errors in
141.Va errno :
142.Bl -tag -width Er
143.It Bq Er ENOMEM
144No memory was available to allocate for the context or some extended state.
145.El
146.Sh SEE ALSO
147.Xr sigaction 2 ,
148.Xr sigaltstack 2 ,
149.Xr makecontext 3 ,
150.Xr ucontext 3
151.Sh STANDARDS
152The
153.Fn getcontext
154and
155.Fn setcontext
156functions conform to
157.St -xsh5
158and
159.St -p1003.1-2001 .
160The
161.Va errno
162indications are an extension to the standard.
163.Pp
164The
165.St -p1003.1-2004
166revision marked the functions
167.Fn getcontext
168and
169.Fn setcontext
170as obsolete, citing portability issues and recommending the use of
171.Tn POSIX
172threads instead.
173The
174.St -p1003.1-2008
175revision removed the functions from the specification.
176.Sh HISTORY
177The
178.Fn getcontext
179and
180.Fn setcontext
181functions first appeared in
182.At V.4 .
183