PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Setjmp, longjmp aus der C Stanardlib?



Import-Script
16.09.2002, 16:30
Hallo Leute, <BR> <BR>ich bin in der C Stanard-Library in der Header-Datei setjmp.h auf die Funktionen setjmp und longjmp gestossen, mit der man z. B. in die Lage versetzt wird, aktuelle Variablen etc auf einen Stack zu sichern und zu einem gegebenen Zeitpunkt mit longjmp wiederherzustellen. <BR>Weiss jemand, wie Compiler/Prozessorabhängig diese Befehle implementiert sind, und wie die Variablen ausgesucht werden, die gesichert werden? <BR>Es gibt ja im Netz eine ganze Menge Beispiele zur Benutzung dieser Befehle, eine Implementierung oder weitere Dokumentation habe ich jedoch nicht gefunden. <BR>Vielleicht weiss ja jemand was? <BR> <BR>Thorsten

Import-Script
16.09.2002, 19:20
Aus der µsoft Visual C&#43;&#43; 5-Hilfe: <BR> <BR>setjmp <BR>Saves the current state of the program. <BR> <BR>int setjmp&#40; jmp_buf env &#41;; <BR> <BR>Routine <BR> Required Header <BR> Compatibility <BR> <BR> <BR> <BR>setjmp <BR> &#60;setjmp.h&#62; <BR> ANSI, Win 95, Win NT <BR> <BR> <BR> <BR> <BR> <BR>For additional compatibility information, see Compatibility in the Introduction. <BR> <BR>Libraries <BR> <BR> <BR> <BR>LIBC.LIB <BR> Single thread static library, retail version <BR> <BR>LIBCMT.LIB <BR> Multithread static library, retail version <BR> <BR>MSVCRT.LIB <BR> Import library for MSVCRT.DLL, retail version <BR> <BR> <BR> <BR> <BR> <BR>Return Value <BR> <BR>setjmp returns 0 after saving the stack environment. If setjmp returns as a result of a longjmp call, it returns the value argument of longjmp, or if the value argument of longjmp is 0, setjmp returns 1. There is no error return. <BR> <BR>Parameter <BR> <BR>env Variable in which environment is stored <BR> <BR>Remarks <BR> <BR>The setjmp function saves a stack environment, which you can subsequently restore using longjmp. When used together, setjmp and longjmp provide a way to execute a &#34;non-local goto.&#34; They are typically used to pass execution control to error-handling or recovery code in a previously called routine without using the normal calling or return conventions. <BR> <BR>A call to setjmp saves the current stack environment in env. A subsequent call to longjmp restores the saved environment and returns control to the point just after the corresponding setjmp call. All variables &#40;except register variables&#41; accessible to the routine receiving control contain the values they had when longjmp was called. <BR> <BR>setjmp and longjmp do not support C&#43;&#43; object semantics. In C&#43;&#43; programs, use the C&#43;&#43; exception-handling mechanism. <BR> <BR>Example <BR> <BR> <BR> <BR> <BR> <BR>/&#42; FPRESET.C: This program uses signal to set up a <BR> &#42; routine for handling floating-point errors. <BR> &#42;/ <BR> <BR>#include &#60;stdio.h&#62; <BR>#include &#60;signal.h&#62; <BR>#include &#60;setjmp.h&#62; <BR>#include &#60;stdlib.h&#62; <BR>#include &#60;float.h&#62; <BR>#include &#60;math.h&#62; <BR>#include &#60;string.h&#62; <BR> <BR>jmp_buf mark; /&#42; Address for long jump to jump to &#42;/ <BR>int fperr; /&#42; Global error number &#42;/ <BR> <BR>void __cdecl fphandler&#40; int sig, int num &#41;; /&#42; Prototypes &#42;/ <BR>void fpcheck&#40; void &#41;; <BR> <BR>void main&#40; void &#41; <BR>{ <BR> double n1, n2, r; <BR> int jmpret; <BR> /&#42; Unmask all floating-point exceptions. &#42;/ <BR> _control87&#40; 0, _MCW_EM &#41;; <BR> /&#42; Set up floating-point error handler. The compiler <BR> &#42; will generate a warning because it expects <BR> &#42; signal-handling functions to take only one argument. <BR> &#42;/ <BR> if&#40; signal&#40; SIGFPE, fphandler &#41; == SIG_ERR &#41; <BR> <BR> { <BR> fprintf&#40; stderr, &#34;Couldn&#39;t set SIGFPE
&#34; &#41;; <BR> abort&#40;&#41;; } <BR> <BR> /&#42; Save stack environment for return in case of error. First <BR> &#42; time through, jmpret is 0, so true conditional is executed. <BR> &#42; If an error occurs, jmpret will be set to -1 and false <BR> &#42; conditional will be executed. <BR> &#42;/ <BR> jmpret = setjmp&#40; mark &#41;; <BR> if&#40; jmpret == 0 &#41; <BR> { <BR> printf&#40; &#34;Test for invalid operation - &#34; &#41;; <BR> printf&#40; &#34;enter two numbers: &#34; &#41;; <BR> scanf&#40; &#34;%lf %lf&#34;, &amp;n1, &amp;n2 &#41;; <BR> r = n1 / n2; <BR> /&#42; This won&#39;t be reached if error occurs. &#42;/ <BR> printf&#40; &#34;

%4.3g / %4.3g = %4.3g
&#34;, n1, n2, r &#41;; <BR> <BR> r = n1 &#42; n2; <BR> /&#42; This won&#39;t be reached if error occurs. &#42;/ <BR> printf&#40; &#34;

%4.3g &#42; %4.3g = %4.3g
&#34;, n1, n2, r &#41;; <BR> } <BR> else <BR> fpcheck&#40;&#41;; <BR>} <BR>/&#42; fphandler handles SIGFPE &#40;floating-point error&#41; interrupt. Note <BR> &#42; that this prototype accepts two arguments and that the <BR> &#42; prototype for signal in the run-time library expects a signal <BR> &#42; handler to have only one argument. <BR> &#42; <BR> &#42; The second argument in this signal handler allows processing of <BR> &#42; _FPE_INVALID, _FPE_OVERFLOW, _FPE_UNDERFLOW, and <BR> &#42; _FPE_ZERODIVIDE, all of which are Microsoft-specific symbols <BR> &#42; that augment the information provided by SIGFPE. The compiler <BR> &#42; will generate a warning, which is harmless and expected. <BR> <BR> &#42;/ <BR>void fphandler&#40; int sig, int num &#41; <BR>{ <BR> /&#42; Set global for outside check since we don&#39;t want <BR> &#42; to do I/O in the handler. <BR> &#42;/ <BR> fperr = num; <BR> /&#42; Initialize floating-point package. &#42;/ <BR> _fpreset&#40;&#41;; <BR> /&#42; Restore calling environment and jump back to setjmp. Return <BR> &#42; -1 so that setjmp will return false for conditional test. <BR> &#42;/ <BR> longjmp&#40; mark, -1 &#41;; <BR>} <BR>void fpcheck&#40; void &#41; <BR>{ <BR> char fpstr[30]; <BR> switch&#40; fperr &#41; <BR> { <BR> case _FPE_INVALID: <BR> strcpy&#40; fpstr, &#34;Invalid number&#34; &#41;; <BR> break; <BR> case _FPE_OVERFLOW: <BR> strcpy&#40; fpstr, &#34;Overflow&#34; &#41;; <BR> <BR> break; <BR> case _FPE_UNDERFLOW: <BR> strcpy&#40; fpstr, &#34;Underflow&#34; &#41;; <BR> break; <BR> case _FPE_ZERODIVIDE: <BR> strcpy&#40; fpstr, &#34;Divide by zero&#34; &#41;; <BR> break; <BR> default: <BR> strcpy&#40; fpstr, &#34;Other floating point error&#34; &#41;; <BR> break; <BR> } <BR> printf&#40; &#34;Error %d: %s
&#34;, fperr, fpstr &#41;; <BR>} <BR> <BR>Output <BR> <BR> <BR> <BR> <BR>Test for invalid operation - enter two numbers: 5 0 <BR>Error 131: Divide by zero <BR> <BR>Process and Environment Control Routines <BR> <BR>See Also longjmp <BR> <BR> <BR> <BR>longjmp <BR>Restores stack environment and execution locale. <BR> <BR>void longjmp&#40; jmp_buf env, int value &#41;; <BR> <BR>Routine <BR> Required Header <BR> Compatibility <BR> <BR> <BR> <BR>longjmp <BR> &#60;setjmp.h&#62; <BR> ANSI, Win 95, Win NT <BR> <BR> <BR> <BR> <BR> <BR>For additional compatibility information, see Compatibility in the Introduction. <BR> <BR>Libraries <BR> <BR> <BR> <BR>LIBC.LIB <BR> Single thread static library, retail version <BR> <BR>LIBCMT.LIB <BR> Multithread static library, retail version <BR> <BR>MSVCRT.LIB <BR> Import library for MSVCRT.DLL, retail version <BR> <BR> <BR> <BR> <BR> <BR>Return Value <BR> <BR>None <BR> <BR>Parameters <BR> <BR>env Variable in which environment is stored <BR> <BR>value Value to be returned to setjmp call <BR> <BR>Remarks <BR> <BR>The longjmp function restores a stack environment and execution locale previously saved in env by setjmp. setjmp and longjmp provide a way to execute a nonlocal goto; they are typically used to pass execution control to error-handling or recovery code in a previously called routine without using the normal call and return conventions. <BR> <BR>A call to setjmp causes the current stack environment to be saved in env. A subsequent call to longjmp restores the saved environment and returns control to the point immediately following the corresponding setjmp call. Execution resumes as if value had just been returned by the setjmp call. The values of all variables &#40;except register variables&#41; that are accessible to the routine receiving control contain the values they had when longjmp was called. The values of register variables are unpredictable. The value returned by setjmp must be nonzero. If value is passed as 0, the value 1 is substituted in the actual return. <BR> <BR>Call longjmp before the function that called setjmp returns; otherwise the results are unpredictable. <BR> <BR>Observe the following restrictions when using longjmp: <BR> <BR>Do not assume that the values of the register variables will remain the same. The values of register variables in the routine calling setjmp may not be restored to the proper values after longjmp is executed. <BR>Do not use longjmp to transfer control out of an interrupt-handling routine unless the interrupt is caused by a floating-point exception. In this case, a program may return from an interrupt handler via longjmp if it first reinitializes the floating-point math package by calling _fpreset. <BR>Be careful when using setjmp and longjmp in C&#43;&#43; programs. Because these functions do not support C&#43;&#43; object semantics, it is safer to use the C&#43;&#43; exception-handling mechanism. <BR>Example <BR> <BR> <BR> <BR> <BR> <BR>/&#42; FPRESET.C: This program uses signal to set up a <BR> &#42; routine for handling floating-point errors. <BR> &#42;/ <BR> <BR>#include &#60;stdio.h&#62; <BR>#include &#60;signal.h&#62; <BR>#include &#60;setjmp.h&#62; <BR>#include &#60;stdlib.h&#62; <BR>#include &#60;float.h&#62; <BR>#include &#60;math.h&#62; <BR>#include &#60;string.h&#62; <BR> <BR>jmp_buf mark; /&#42; Address for long jump to jump to &#42;/ <BR>int fperr; /&#42; Global error number &#42;/ <BR> <BR>void __cdecl fphandler&#40; int sig, int num &#41;; /&#42; Prototypes &#42;/ <BR>void fpcheck&#40; void &#41;; <BR> <BR>void main&#40; void &#41; <BR>{ <BR> double n1, n2, r; <BR> int jmpret; <BR> /&#42; Unmask all floating-point exceptions. &#42;/ <BR> _control87&#40; 0, _MCW_EM &#41;; <BR> /&#42; Set up floating-point error handler. The compiler <BR> &#42; will generate a warning because it expects <BR> &#42; signal-handling functions to take only one argument. <BR> &#42;/ <BR> if&#40; signal&#40; SIGFPE, fphandler &#41; == SIG_ERR &#41; <BR> <BR> { <BR> fprintf&#40; stderr, &#34;Couldn&#39;t set SIGFPE
&#34; &#41;; <BR> abort&#40;&#41;; } <BR> <BR> /&#42; Save stack environment for return in case of error. First <BR> &#42; time through, jmpret is 0, so true conditional is executed. <BR> &#42; If an error occurs, jmpret will be set to -1 and false <BR> &#42; conditional will be executed. <BR> &#42;/ <BR> jmpret = setjmp&#40; mark &#41;; <BR> if&#40; jmpret == 0 &#41; <BR> { <BR> printf&#40; &#34;Test for invalid operation - &#34; &#41;; <BR> printf&#40; &#34;enter two numbers: &#34; &#41;; <BR> scanf&#40; &#34;%lf %lf&#34;, &amp;n1, &amp;n2 &#41;; <BR> r = n1 / n2; <BR> /&#42; This won&#39;t be reached if error occurs. &#42;/ <BR> printf&#40; &#34;

%4.3g / %4.3g = %4.3g
&#34;, n1, n2, r &#41;; <BR> <BR> r = n1 &#42; n2; <BR> /&#42; This won&#39;t be reached if error occurs. &#42;/ <BR> printf&#40; &#34;

%4.3g &#42; %4.3g = %4.3g
&#34;, n1, n2, r &#41;; <BR> } <BR> else <BR> fpcheck&#40;&#41;; <BR>} <BR>/&#42; fphandler handles SIGFPE &#40;floating-point error&#41; interrupt. Note <BR> &#42; that this prototype accepts two arguments and that the <BR> &#42; prototype for signal in the run-time library expects a signal <BR> &#42; handler to have only one argument. <BR> &#42; <BR> &#42; The second argument in this signal handler allows processing of <BR> &#42; _FPE_INVALID, _FPE_OVERFLOW, _FPE_UNDERFLOW, and <BR> &#42; _FPE_ZERODIVIDE, all of which are Microsoft-specific symbols <BR> &#42; that augment the information provided by SIGFPE. The compiler <BR> &#42; will generate a warning, which is harmless and expected. <BR> <BR> &#42;/ <BR>void fphandler&#40; int sig, int num &#41; <BR>{ <BR> /&#42; Set global for outside check since we don&#39;t want <BR> &#42; to do I/O in the handler. <BR> &#42;/ <BR> fperr = num; <BR> /&#42; Initialize floating-point package. &#42;/ <BR> _fpreset&#40;&#41;; <BR> /&#42; Restore calling environment and jump back to setjmp. Return <BR> &#42; -1 so that setjmp will return false for conditional test. <BR> &#42;/ <BR> longjmp&#40; mark, -1 &#41;; <BR>} <BR>void fpcheck&#40; void &#41; <BR>{ <BR> char fpstr[30]; <BR> switch&#40; fperr &#41; <BR> { <BR> case _FPE_INVALID: <BR> strcpy&#40; fpstr, &#34;Invalid number&#34; &#41;; <BR> break; <BR> case _FPE_OVERFLOW: <BR> strcpy&#40; fpstr, &#34;Overflow&#34; &#41;; <BR> <BR> break; <BR> case _FPE_UNDERFLOW: <BR> strcpy&#40; fpstr, &#34;Underflow&#34; &#41;; <BR> break; <BR> case _FPE_ZERODIVIDE: <BR> strcpy&#40; fpstr, &#34;Divide by zero&#34; &#41;; <BR> break; <BR> default: <BR> strcpy&#40; fpstr, &#34;Other floating point error&#34; &#41;; <BR> break; <BR> } <BR> printf&#40; &#34;Error %d: %s
&#34;, fperr, fpstr &#41;; <BR>} <BR> <BR>Output <BR> <BR> <BR> <BR> <BR>Test for invalid operation - enter two numbers: 5 0 <BR>Error 131: Divide by zero <BR> <BR>Process and Environment Control Routines <BR> <BR>See Also setjmp <BR>

Import-Script
17.09.2002, 10:37
HI Leech, <BR>das habe ich auch schon gefunden, aber ich wollte eigentlich wissen, ob die Benutzung von setjmp compiler/Prozessor/Betriebssystemabhängig ist und wie genau es implementiert ist. <BR>Code? <BR> <BR>Danke <BR> <BR>Thorsten <BR> <BR>

Import-Script
17.09.2002, 11:02
Hallo Thorsten <BR> <BR>setjmp und longjmp d&uuml;rften so ziemlich das Gefährlichste sein, was es gibt. <BR> <BR>Es werden quasi alle Funktionen mittendrin abgew&uuml;rgt, die zwischen setjmp und longjump aufgerufen und noch nicht beendet wurden. <BR>Und das mögen viele Funktionen garnicht. Z.B. wenn Speicher alloziiert wurde oder Files geöffnet oder Schnittstellen belegt wurden usw. Diese können die abgew&uuml;rgten Funktionen dann ja nicht mehr ordentlich schließen und freigeben. <BR>D.h. die Leichen bleiben liegen und weitere Zugriffe schlagen fehl. <BR> <BR>Deine Programme d&uuml;rften also wesentlich stabiler laufen, wenn Du diese Funktionen ganz schnell wieder vergißt. <BR> <BR>Peter <BR> <BR>

Import-Script
17.09.2002, 11:13
Zur Funktion: <BR> <BR>setjump merkt sich nur den augenblicklichen Zustand des Stacks. <BR> <BR>longjump springt dann an diese Stelle zur&uuml;ck. <BR>Da das aber aus anderen Funktionen heraus geschieht, ist der Stack inzwischen weiter gewachsen und wenn man das noch mehrere Male macht, läuft der Stack &uuml;ber. <BR> <BR>Deshalb wird der Stack noch mit dem bei setjmp gemerkten Wert &uuml;berschrieben. Der Stack ist damit wieder ausbalanciert, wie es bei einem regulären Return der Fall wäre. <BR> <BR> <BR>Peter <BR>