I've read that setjmp "saves the program state" in the passed-in jmp_buf variable, but I haven't found any description of exactly what that entails. Does it make a copy of all the application's memory? Just the registers? The stack?
4 Answers
The following is from C in a Nutshell by Peter Prinz and Tony Crawford:
The
setjmp()macro saves the current environment at the time of the call in a buffer specified by its argument. The environment includes the stack, and with it all variables that have automatic storage duration.
Here is what ISO/IEC 9899:TC2 has to say in section 7.13:
The environment of a call to the
setjmpmacro consists of information sufficient for a call to thelongjmpfunction to return execution to the correct block and invocation of that block, were it called recursively. It does not include the state of the floating-point status flags, of open files, or of any other component of the abstract machine.
Here is an interesting reference by P.J. Plauger in his book, The Standard C Library:
One of the dangers [of implementing setjmp] lies in expression evaluation. A typical computer has some number of registers that it uses to hold intermediate results while evaluating an expression. Write a sufficiently complex expression, however, and you may exhaust the available registers...
setjmpmust guess how much "calling context" to store in thejmp_bufdata object. It is a safe bet that certain registers must be saved.
And finally, from Expert C Programming by Peter Van Der Linden.
Setjmp saves a copy of the program counter and the current pointer to the top of the stack.
Based on the above information, it looks to me like the "current environment" is left up to the implementation.
The setjmp() function saves the contents of most of the general purpose registers, in the same way as they would be saved on any function entry. It also saves the stack pointer and the return address. All these are placed in the buffer. It then arranges for the function to return zero.
The longjmp() function restores the general purpose registers and the stack pointer and then does a jump to the previously saved return address. In practice it may do this explicitly, or by setting up the stack and executing a normal function return. On this occasion the function returns a non-zero value.
The principle is the same, but the detail has varied quite a bit across the many different CPUs I have encountered.
From the setjmp manpage
setjmp() and longjmp(3) are useful for dealing with errors and interrupts encountered in a
low-level subroutine of a program. setjmp() saves the stack context/environment in env for
later use by longjmp(3). The stack context will be invalidated if the function which called
setjmp() returns.
Essentially, it remembers the current stack location and register state. When you call longjmp, you jump back to the same program counter and stack location with some additional registers are restored.
These are often referred to as "non-local gotos". They are not like a fork that would copy memory state or anything like that.
setjmp()does whatever needs to be done for a subsequentlongjmp()to be successfull. Note: the standard avoids the termstack, because an implementation does not need to have a stack. That is why the rather vague termprogram stateis used. - joop