setjmp(2) [plan9 man page]
SETJMP(2) System Calls Manual SETJMP(2) NAME
setjmp, longjmp, notejmp - non-local goto SYNOPSIS
#include <u.h> #include <libc.h> int setjmp(jmp_buf env) void longjmp(jmp_buf env, int val) void notejmp(void *uregs, jump_buf env, int val) DESCRIPTION
These routines are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. Setjmp saves its stack environment in env for later use by longjmp. It returns value 0. Longjmp restores the environment saved by the last call of setjmp. It then causes execution to continue as if the call of setjmp had just returned with value val. The invoker of setjmp must not itself have returned in the interim. All accessible data have values as of the time longjmp was called. Notejmp is the same as longjmp except that it is to be called from within a note handler (see notify(2)). The uregs argument should be the first argument passed to the note handler. Setjmp and longjmp can also be used to switch stacks. Defined in </$objtype/u.h> are several macros that can be used to build jmp_bufs by hand. The following code establishes a jmp_buf that may be called by longjmp to begin execution in a function f with 1024 bytes of stack: #include <u.h> #include <libc.h> jmp_buf label; #define NSTACK 1024 char stack[NSTACK]; void setlabel(void) { label[JMPBUFPC] = ((ulong)f+JMPBUFDPC); /* -2 leaves room for old pc and new pc in frame */ label[JMPBUFSP = (ulong)(&stack[NSTACK-2*sizeof(ulong*)]); } SOURCE
/sys/src/libc/$objtype/setjmp.s /sys/src/libc/$objtype/notejmp.c SEE ALSO
notify(2) BUGS
Notejmp cannot recover from an address trap or bus error (page fault) on the 680x0 architectures. SETJMP(2)
Check Out this Related Man Page
setjmp(3) Library Functions Manual setjmp(3) Name setjmp, longjmp - non-local goto Syntax #include <setjmp.h> int setjmp (env) jmp_buf env; void longjmp (env, val) jmp_buf env; int val; Description The and functions help deal with errors and interrupts encountered in a low-level subroutine of a program. The function saves its stack environment in env (whose type, jmp_buf, is defined in the <setjmp.h> header file) for later use by It returns the value 0. The function restores the environment saved by the last call of with the corresponding env argument. After finishes, program execution continues as if the corresponding call of (which must not itself have returned in the interim) had just returned the value val. The func- tion cannot cause to return the value 0. If is invoked with a second argument of 0, returns 1. At the time of the second return from all accessible data have values as of the time is called. However, global variables have the expected values. For example, those as of the time of the Examples #include <setjmp.h> jmp_buf env; int i = 0; main () { void exit(); if(setjmp(env) != 0) { (void) printf("value of i on 2nd return from setjmp: %d0, i); exit(0); } (void) printf("value of i on 1st return from setjmp: %d0, i); i = 1; g(); /*NOTREACHED*/ } g() { longjmp(env, 1); /*NOTREACHED*/ } If the a.out resulting from this C language code is run, the output is as follows: value of i on 1st return from setjmp:0 value of i on 2nd return from setjmp:1 Unexpected behavior occurs if is called without a previous call to or when the last such call was in a function which has since returned. Restrictions The values of the registers on the second return from are register values at the time of the first call to not those of the Thus, variables in a given function can produce unexpected results in the presence of depending on whether they are register or stack variables. See Also signal(2). RISC setjmp(3)