Subject: Re: jmp_buf
To: Stuart Carter <StuartC@omnibus.co.uk>
From: Roger Brooks <R.S.Brooks@liverpool.ac.uk>
List: netbsd-help
Date: 01/07/2000 15:24:22
On Fri, 7 Jan 2000, Stuart Carter wrote:

>Pardon me if I'm being a bit clueless, but I'm currently
>groping around in the dark, trying to find out the format
>of the jmp_buf as used in setjmp().
>
>I know that it's 13 long-words by looking in the header,
>(I'm using NetBSD/i386 1.4), but I'm not sure what goes
>in them.
>
>I understand this is machine specific, but is there somewhere
>I can look?

You should only set the contents of jmp_buf by calling setjmp() thus:

	i = setjmp( jmp_buf );

whereupon setjmp() will return a value of 0.

Later on if you call longjmp() thus:

	longjmp( jmp_buf, n );

longjmp() never returns.  Instead, the call to setjmp which initiallised
jmp_buf returns a second time, but this time it returns the value of n
from the call to longjmp() (which should be non-zero).  Normally you would
do something like:

	switch( setjmp( jmp_buf ) )
	{
		case 0:
			break;

		default:
			/*
			 *  We've returned from the depths of the program, so
			 *  do any necessary clearing up or error recovery here.
			 */

			break;
	}

If you have more than one call to longjmp(), these can have different values
of n, and the switch on the return value of setjmp() can then tell where
you have longjmp()ed from.

I suppose you might call setjmp() and then look at the contents of jmp_buf
as a way of examining the CPU registers at that point in the code.
However, modifying the contents of jmp_buf yourself and then calling longjmp()
isn't likely to do much apart from sending you a long way up sh*t cr**k
without the paddle.
	


Roger

------------------------------------------------------------------------------
Roger Brooks (Systems Programmer),          |  Email: R.S.Brooks@liv.ac.uk
Computing Services Dept,                    |  Tel:   +44 151 794 4441
The University of Liverpool,                |  Fax:   +44 151 794 4442
PO Box 147, Liverpool L69 3BX, UK           | 
------------------------------------------------------------------------------