source: trunk/src/helpers/except.c@ 21

Last change on this file since 21 was 21, checked in by umoeller, 25 years ago

Final changes for 0.9.7, i hope...

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 37.7 KB
Line 
1
2/*
3 *@@sourcefile except.c:
4 * this file contains powerful exception handlers.
5 * except.h also defines easy-to-use macros for them.
6 *
7 * Usage: All OS/2 programs, PM or text mode.
8 *
9 * <B>Introduction</B>
10 *
11 * OS/2 exception handlers are a mess to program and,
12 * if installed wrongly, almost impossible to debug.
13 * The problem is that for any program that does a bit
14 * more than showing a message box, using exception
15 * handlers is a must to avoid system hangs. This
16 * especially applies to multi-thread programs using
17 * mutex semaphores (more on that below). The functions
18 * and macros in here are designed to make that more simple.
19 *
20 * The macros in except.h automatically insert code for properly
21 * registering and deregistering the handlers in except.c. You
22 * should ALWAYS use these macros instead of directly registering
23 * the handlers to avoid accidentally forgetting to deregister
24 * them. If you forget to deregister an exception handler, this
25 * can lead to really strange errors (crashes, hangs) which are
26 * nearly impossible to debug because the thread's stack space
27 * might get completely messed up.
28 *
29 * The general idea of these macros is to define TRY / CATCH
30 * blocks similar to C++. If an exception occurs in the TRY block,
31 * execution is transferred to the CATCH block. (This works in both
32 * C and C++, by the way.)
33 *
34 * The "OnKill" function that was added with V0.9.0 has been
35 * removed again with V0.9.7. Use DosEnterMustComplete instead.
36 * Details follow.
37 *
38 * The general usage is like this:
39 *
40 + int your_protected_func(int ...)
41 + {
42 + TRY_LOUD(excptid) // or: TRY_QUIET(excptid)
43 + {
44 + .... // the stuff in here is protected by
45 + // the excHandlerLoud or excHandlerQuiet
46 + // exception handler
47 + }
48 + CATCH(excptid)
49 + {
50 + .... // exception occured: react here
51 + } END_CATCH(); // always needed!
52 + } // end of your_func
53 *
54 * TRY_LOUD is for installing excHandlerLoud.
55 * TRY_QUIET is for installing excHandlerQuiet.
56 * CATCH / END_CATCH are the same for the two. This
57 * is where the exception handler jumps to if an
58 * exception occurs.
59 * The CATCH block is _required_ even if you do nothing
60 * in there.
61 *
62 * "excptid" can be any C identifier which is not used in
63 * your current variable scope, e.g. "excpt1". This
64 * is used for creating an EXCEPTSTRUCT variable of
65 * that name on the stack. The "excptid"'s in TRY_* and
66 * CATCH must match, since this is where the macros
67 * store the exception handler data.
68 *
69 * These macros may be nested if you use different
70 * "excptid"'s for sub-macros.
71 *
72 * Inside the TRY and CATCH blocks, you must not use
73 * "goto" (to a location outside the block) or "return",
74 * because this will not deregister the handler.
75 *
76 * Keep in mind that all the code in the TRY_* block is
77 * protected by the handler, including all functions that
78 * get called. So if you enclose your main() code in a
79 * TRY_* block, your entire application is protected.
80 *
81 * <B>Asynchronous exceptions</B>
82 *
83 * The exception handlers in this file (which are installed
84 * with the TRY/CATCH mechanism) only intercept synchronous
85 * exceptions (see excHandlerLoud for a list). They do not
86 * protect your code against asynchronous exceptions.
87 *
88 * OS/2 defines asynchronous exceptions to be those that
89 * can be delayed. With OS/2, there are only three of these:
90 *
91 * -- XCPT_PROCESS_TERMINATE
92 * -- XCPT_ASYNC_PROCESS_TERMINATE
93 * -- XCPT_SIGNAL (thread 1 only)
94 *
95 * To protect yourself against these also, put the section
96 * in question in a DosEnterMustComplete/DosExitMustComplete
97 * block as well.
98 *
99 * <B>Mutex semaphores</B>
100 *
101 * The problem with OS/2 mutex semaphores is that they are
102 * not automatically released when a thread terminates.
103 * If the thread owning the mutex died without releasing
104 * the mutex, other threads which are blocked on that mutex
105 * will wait forever and become zombie threads. Even worse,
106 * if this happens to a PM thread, this will hang the system.
107 *
108 * Here's the typical scenario with two threads:
109 *
110 * 1) Thread 2 requests a mutex and does lots of processing.
111 *
112 * 2) Thread 1 requests the mutex. Since it's still owned
113 * by thread 2, thread 1 blocks.
114 *
115 * 3) Thread 2 crashes in its processing. Without an
116 * exception handler, OS/2 will terminate the process.
117 * It will first kill thread 2 and then attempt to
118 * kill thread 1. This fails because it is still
119 * blocking on the semaphore that thread 2 never
120 * released. Boom.
121 *
122 * The same scenario happens when a process gets killed.
123 * Since OS/2 will kill secondary threads before thread 1,
124 * the same situation can arise.
125 *
126 * As a result, you must protect any section of code which
127 * requests a semaphore _both_ against crashes _and_
128 * termination.
129 *
130 * So _whenever_ you request a mutex semaphore, enclose
131 * the block with TRY/CATCH in case the code crashes.
132 * Besides, enclose the TRY/CATCH block in a must-complete
133 * section, like this:
134 *
135 + HMTX hmtx = ...
136 +
137 + int your_func(int)
138 + {
139 + BOOL fSemOwned = FALSE;
140 + ULONG ulNesting = 0;
141 +
142 + DosEnterMustComplete(&ulNesting);
143 + TRY_QUIET(excpt1, OnKillYourFunc) // or TRY_LOUD
144 + {
145 + fSemOwned = (WinRequestMutexSem(hmtx, ...) == NO_ERROR);
146 + if (fSemOwned)
147 + { ... // work on your protected data
148 + }
149 + }
150 + CATCH(excpt1) { } END_CATCH(); // always needed!
151 +
152 + if (fSemOwned) {
153 + // this gets executed always, even if an exception occured
154 + DosReleaseMutexSem(hmtx);
155 + fSemOwned = FALSE;
156 + }
157 + DosExitMustComplete(&ulNesting);
158 + } // end of your_func
159 *
160 * This way your mutex semaphore gets released in every
161 * possible condition.
162 *
163 * <B>Customizing</B>
164 *
165 * As opposed to versions before 0.9.0, this code is now
166 * completely independent of XWorkplace. This file now
167 * contains "pure" exception handlers only.
168 *
169 * However, you can customize these exception handlers by
170 * calling excRegisterHooks. This is what XWorkplace does now.
171 * This should be done upon initialization of your application.
172 * If excRegisterHooks is not called, the following safe
173 * defaults are used:
174 *
175 * -- the trap log file is TRAP.LOG in the root
176 * directory of your boot drive.
177 *
178 * For details on the provided exception handlers, refer
179 * to excHandlerLoud and excHandlerQuiet.
180 *
181 * More useful debug information can be found in the "OS/2 Debugging
182 * Handbook", which is now available in INF format on the IBM
183 * DevCon site ("http://service2.boulder.ibm.com/devcon/").
184 * This book shows worked examples of how to unwind a stack dump.
185 *
186 * This file incorporates code from the following:
187 * -- Monte Copeland, IBM Boca Ration, Florida, USA (1993)
188 * -- Roman Stangl, from the Program Commander/2 sources
189 * (1997-98)
190 * -- Marc Fiammante, John Currier, Kim Rasmussen,
191 * Anthony Cruise (EXCEPT3.ZIP package for a generic
192 * exception handling DLL, available at Hobbes).
193 *
194 * If not explicitly stated otherwise, the code has been written
195 * by me, Ulrich M”ller.
196 *
197 * Note: Version numbering in this file relates to XWorkplace version
198 * numbering.
199 *
200 *@@header "helpers\except.h"
201 */
202
203/*
204 * This file Copyright (C) 1992-99 Ulrich M”ller,
205 * Monte Copeland,
206 * Roman Stangl,
207 * Kim Rasmussen,
208 * Marc Fiammante,
209 * John Currier,
210 * Anthony Cruise.
211 * This file is part of the "XWorkplace helpers" source package.
212 * This is free software; you can redistribute it and/or modify
213 * it under the terms of the GNU General Public License as published
214 * by the Free Software Foundation, in version 2 as it comes in the
215 * "COPYING" file of the XWorkplace main distribution.
216 * This program is distributed in the hope that it will be useful,
217 * but WITHOUT ANY WARRANTY; without even the implied warranty of
218 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
219 * GNU General Public License for more details.
220 */
221
222#define OS2EMX_PLAIN_CHAR
223 // this is needed for "os2emx.h"; if this is defined,
224 // emx will define PSZ as _signed_ char, otherwise
225 // as unsigned char
226
227#define INCL_DOSMODULEMGR
228#define INCL_DOSEXCEPTIONS
229#define INCL_DOSPROCESS
230#define INCL_DOSERRORS
231#include <os2.h>
232
233// C library headers
234#include <stdio.h> // needed for except.h
235#include <stdlib.h>
236#include <time.h>
237#include <string.h>
238#include <setjmp.h> // needed for except.h
239#include <assert.h> // needed for except.h
240
241#define DONT_REPLACE_MALLOC
242#include "setup.h" // code generation and debugging options
243
244// headers in /helpers
245#include "helpers\dosh.h" // Control Program helper routines
246#include "helpers\except.h" // exception handling
247#include "helpers\debug.h" // symbol/debug code analysis
248
249#pragma hdrstop
250
251/* ******************************************************************
252 *
253 * Global variables
254 *
255 ********************************************************************/
256
257// hooks to be registered using excRegisterHooks
258PFNEXCOPENFILE G_pfnExcOpenFile = 0;
259PFNEXCHOOK G_pfnExcHook = 0;
260PFNEXCHOOKERROR G_pfnExcHookError = 0;
261// beep flag for excHandlerLoud
262BOOL G_fBeepOnException = TRUE;
263
264/*
265 *@@category: Helpers\Control program helpers\Exceptions/debugging
266 * See except.c.
267 */
268
269/* ******************************************************************
270 *
271 * Exception helper routines
272 *
273 ********************************************************************/
274
275/*
276 *@@ excDescribePage:
277 *
278 */
279
280VOID excDescribePage(FILE *file, ULONG ulCheck)
281{
282 APIRET arc;
283 ULONG ulCountPages = 1;
284 ULONG ulFlagsPage = 0;
285 arc = DosQueryMem((PVOID)ulCheck, &ulCountPages, &ulFlagsPage);
286
287 if (arc == NO_ERROR)
288 {
289 fprintf(file, "valid, flags: ");
290 if (ulFlagsPage & PAG_READ)
291 fprintf(file, "read ");
292 if (ulFlagsPage & PAG_WRITE)
293 fprintf(file, "write ");
294 if (ulFlagsPage & PAG_EXECUTE)
295 fprintf(file, "execute ");
296 if (ulFlagsPage & PAG_GUARD)
297 fprintf(file, "guard ");
298 if (ulFlagsPage & PAG_COMMIT)
299 fprintf(file, "committed ");
300 if (ulFlagsPage & PAG_SHARED)
301 fprintf(file, "shared ");
302 if (ulFlagsPage & PAG_FREE)
303 fprintf(file, "free ");
304 if (ulFlagsPage & PAG_BASE)
305 fprintf(file, "base ");
306 }
307 else if (arc == ERROR_INVALID_ADDRESS)
308 fprintf(file, "invalid");
309}
310
311/*
312 *@@ excPrintStackFrame:
313 * wrapper for dbgPrintStackFrame to format
314 * output stuff right.
315 *
316 *@@added V0.9.2 (2000-03-10) [umoeller]
317 */
318
319VOID excPrintStackFrame(FILE *file, // in: output log file
320 PSZ pszDescription, // in: description for stack frame (should be eight chars)
321 ULONG ulAddress) // in: address to debug
322{
323 APIRET arc = NO_ERROR;
324 HMODULE hmod1 = NULLHANDLE;
325 CHAR szMod1[2*CCHMAXPATH] = "unknown";
326 ULONG ulObject = 0,
327 ulOffset = 0;
328 fprintf(file,
329 " %-8s: %08lX ",
330 pszDescription,
331 ulAddress);
332 arc = DosQueryModFromEIP(&hmod1,
333 &ulObject,
334 sizeof(szMod1), szMod1,
335 &ulOffset,
336 ulAddress);
337
338 if (arc != NO_ERROR)
339 {
340 // error:
341 fprintf(file,
342 " %-8s:%lu Error: DosQueryModFromEIP returned %lu\n",
343 szMod1,
344 ulObject,
345 arc);
346 }
347 else
348 {
349 CHAR szFullName[2*CCHMAXPATH];
350
351 fprintf(file,
352 " %-8s:%lu ",
353 szMod1,
354 ulObject);
355
356 DosQueryModuleName(hmod1, sizeof(szFullName), szFullName);
357
358 dbgPrintStackFrame(file,
359 szFullName,
360 ulObject,
361 ulOffset);
362
363 fprintf(file, "\n");
364
365 // make a 'tick' sound to let the user know we're still alive
366 DosBeep(2000, 10);
367 }
368}
369
370/*
371 *@@ excDumpStackFrames:
372 * called from excExplainException to dump the
373 * thread's stack frames. This calls excPrintStackFrame
374 * for each stack frame found.
375 *
376 *@@added V0.9.4 (2000-06-15) [umoeller]
377 */
378
379VOID excDumpStackFrames(FILE *file, // in: logfile from fopen()
380 PTIB ptib,
381 PCONTEXTRECORD pContextRec) // in: excpt info
382{
383 PULONG pulStackWord = 0;
384
385 fprintf(file, "\n\nStack frames:\n Address Module:Object\n");
386
387 // first the trapping address itself
388 excPrintStackFrame(file,
389 "CS:EIP ",
390 pContextRec->ctx_RegEip);
391
392
393 pulStackWord = (PULONG)pContextRec->ctx_RegEbp;
394 /* if (pContextRec->ctx_RegEbp < pContextRec->ctx_RegEsp)
395 pulStackWord = (PULONG)(pContextRec->ctx_RegEbp & 0xFFFFFFF0);
396 else
397 pulStackWord = (PULONG)(pContextRec->ctx_RegEsp & 0xFFFFFFF0); */
398
399 while ( (pulStackWord != 0)
400 && (pulStackWord < (PULONG)ptib->tib_pstacklimit)
401 )
402 {
403 CHAR szAddress[20];
404
405 if (((ULONG)pulStackWord & 0x00000FFF) == 0x00000000)
406 {
407 // we're on a page boundary: check access
408 ULONG ulCountPages = 0x1000;
409 ULONG ulFlagsPage = 0;
410 APIRET arc = DosQueryMem((void *)pulStackWord,
411 &ulCountPages,
412 &ulFlagsPage);
413 if ( (arc != NO_ERROR)
414 || ( (arc == NO_ERROR)
415 && ( !( ((ulFlagsPage & (PAG_COMMIT|PAG_READ))
416 == (PAG_COMMIT|PAG_READ)
417 )
418 )
419 )
420 )
421 )
422 {
423 fprintf(file, "\n %08lX: ", (ULONG)pulStackWord);
424 fprintf(file, "Page inaccessible");
425 pulStackWord += 0x1000;
426 continue; // for
427 }
428 }
429
430 sprintf(szAddress, "%08lX",
431 (ULONG)pulStackWord);
432 excPrintStackFrame(file,
433 szAddress,
434 *(pulStackWord+1));
435 pulStackWord = (PULONG)*(pulStackWord);
436 } // end while
437}
438
439/*
440 *@@ excExplainException:
441 * used by the exception handlers below to write
442 * LOTS of information about the exception into a logfile.
443 *
444 * This calls excPrintStackFrame for each stack frame.
445 *
446 *@@changed V0.9.0 [umoeller]: added support for application hook
447 *@@changed V0.9.0 (99-11-02) [umoeller]: added TID to dump
448 *@@changed V0.9.2 (2000-03-10) [umoeller]: now using excPrintStackFrame
449 *@@changed V0.9.3 (2000-05-03) [umoeller]: fixed crashes
450 *@@changed V0.9.6 (2000-11-06) [umoeller]: added more register dumps
451 */
452
453VOID excExplainException(FILE *file, // in: logfile from fopen()
454 PSZ pszHandlerName, // in: descriptive string
455 PEXCEPTIONREPORTRECORD pReportRec, // in: excpt info
456 PCONTEXTRECORD pContextRec) // in: excpt info
457{
458 PTIB ptib = NULL;
459 PPIB ppib = NULL;
460 HMODULE hMod1, hMod2;
461 CHAR szMod1[CCHMAXPATH] = "unknown",
462 szMod2[CCHMAXPATH] = "unknown";
463 ULONG ulObjNum,
464 ulOffset;
465 /* ulCountPages,
466 ulFlagsPage; */
467 // APIRET arc;
468 // PULONG pulStackWord;
469 // pulStackBegin;
470 ULONG ul;
471
472 ULONG ulOldPriority = 0x0100; // regular, delta 0
473
474 // raise this thread's priority, because this
475 // might take some time
476 if (DosGetInfoBlocks(&ptib, &ppib) == NO_ERROR)
477 if (ptib)
478 if (ptib->tib_ptib2)
479 {
480 ulOldPriority = ptib->tib_ptib2->tib2_ulpri;
481 DosSetPriority(PRTYS_THREAD,
482 PRTYC_REGULAR,
483 PRTYD_MAXIMUM,
484 0); // current thread
485 }
486
487 // make some noise
488 if (G_fBeepOnException)
489 {
490 DosBeep( 250, 30);
491 DosBeep( 500, 30);
492 DosBeep(1000, 30);
493 DosBeep(2000, 30);
494 DosBeep(4000, 30);
495 DosBeep(2000, 30);
496 DosBeep(1000, 30);
497 DosBeep( 500, 30);
498 DosBeep( 250, 30);
499 }
500
501 // generic exception info
502 fprintf(file,
503 "\n%s:\n Exception type: %08lX\n Address: %08lX\n Params: ",
504 pszHandlerName,
505 pReportRec->ExceptionNum,
506 (ULONG)pReportRec->ExceptionAddress);
507 for (ul = 0; ul < pReportRec->cParameters; ul++)
508 {
509 fprintf(file, "%08lX ",
510 pReportRec->ExceptionInfo[ul]);
511 }
512
513 // now explain the exception in a bit more detail;
514 // depending on the exception, pReportRec->ExceptionInfo
515 // contains some useful data
516 switch (pReportRec->ExceptionNum)
517 {
518 case XCPT_ACCESS_VIOLATION:
519 fprintf(file, "\nXCPT_ACCESS_VIOLATION: ");
520 if (pReportRec->ExceptionInfo[0] & XCPT_READ_ACCESS)
521 fprintf(file, "Invalid read access from 0x%04lX:%08lX.\n",
522 pContextRec->ctx_SegDs, pReportRec->ExceptionInfo[1]);
523 else if (pReportRec->ExceptionInfo[0] & XCPT_WRITE_ACCESS)
524 fprintf(file, "Invalid write access to 0x%04lX:%08lX.\n",
525 pContextRec->ctx_SegDs, pReportRec->ExceptionInfo[1]);
526 else if (pReportRec->ExceptionInfo[0] & XCPT_SPACE_ACCESS)
527 fprintf(file, "Invalid space access at 0x%04lX.\n",
528 pReportRec->ExceptionInfo[1]);
529 else if (pReportRec->ExceptionInfo[0] & XCPT_LIMIT_ACCESS)
530 fprintf(file, "Invalid limit access occurred.\n");
531 else if (pReportRec->ExceptionInfo[0] == XCPT_UNKNOWN_ACCESS)
532 fprintf(file, "unknown at 0x%04lX:%08lX\n",
533 pContextRec->ctx_SegDs, pReportRec->ExceptionInfo[1]);
534 fprintf(file,
535 "Explanation: An attempt was made to access a memory object which does\n"
536 " not belong to the current process. Most probable causes\n"
537 " for this are that an invalid pointer was used, there was\n"
538 " confusion with administering memory or error conditions \n"
539 " were not properly checked for.\n");
540 break;
541
542 case XCPT_INTEGER_DIVIDE_BY_ZERO:
543 fprintf(file, "\nXCPT_INTEGER_DIVIDE_BY_ZERO.\n");
544 fprintf(file,
545 "Explanation: An attempt was made to divide an integer value by zero,\n"
546 " which is not defined.\n");
547 break;
548
549 case XCPT_ILLEGAL_INSTRUCTION:
550 fprintf(file, "\nXCPT_ILLEGAL_INSTRUCTION.\n");
551 fprintf(file,
552 "Explanation: An attempt was made to execute an instruction that\n"
553 " is not defined on this machine's architecture.\n");
554 break;
555
556 case XCPT_PRIVILEGED_INSTRUCTION:
557 fprintf(file, "\nXCPT_PRIVILEGED_INSTRUCTION.\n");
558 fprintf(file,
559 "Explanation: An attempt was made to execute an instruction that\n"
560 " is not permitted in the current machine mode or that\n"
561 " XFolder had no permission to execute.\n");
562 break;
563
564 case XCPT_INTEGER_OVERFLOW:
565 fprintf(file, "\nXCPT_INTEGER_OVERFLOW.\n");
566 fprintf(file,
567 "Explanation: An integer operation generated a carry-out of the most\n"
568 " significant bit. This is a sign of an attempt to store\n"
569 " a value which does not fit into an integer variable.\n");
570 break;
571
572 default:
573 fprintf(file, "\nUnknown OS/2 exception number %d.\n", pReportRec->ExceptionNum);
574 fprintf(file, "Look this up in the OS/2 header files.\n");
575 break;
576 }
577
578 if (DosGetInfoBlocks(&ptib, &ppib) == NO_ERROR)
579 {
580 /*
581 * process info:
582 *
583 */
584
585 if ((ptib) && (ppib)) // (99-11-01) [umoeller]
586 {
587 if (pContextRec->ContextFlags & CONTEXT_CONTROL)
588 {
589 // get the main module
590 hMod1 = ppib->pib_hmte;
591 DosQueryModuleName(hMod1,
592 sizeof(szMod1),
593 szMod1);
594
595 // get the trapping module
596 DosQueryModFromEIP(&hMod2,
597 &ulObjNum,
598 sizeof(szMod2),
599 szMod2,
600 &ulOffset,
601 pContextRec->ctx_RegEip);
602 DosQueryModuleName(hMod2,
603 sizeof(szMod2),
604 szMod2);
605 }
606
607 fprintf(file,
608 "\nProcess information:"
609 "\n Process ID: 0x%lX"
610 "\n Process module: 0x%lX (%s)"
611 "\n Trapping module: 0x%lX (%s)"
612 "\n Object: %lu\n",
613 ppib->pib_ulpid,
614 hMod1, szMod1,
615 hMod2, szMod2,
616 ulObjNum);
617
618 fprintf(file,
619 "\nTrapping thread information:"
620 "\n Thread ID: 0x%lX (%lu)"
621 "\n Priority: 0x%lX\n",
622 ptib->tib_ptib2->tib2_ultid, ptib->tib_ptib2->tib2_ultid,
623 ulOldPriority);
624 }
625 else
626 fprintf(file, "\nProcess information was not available.");
627
628 /*
629 * now call the hook, if one has been defined,
630 * so that the application can write additional
631 * information to the traplog (V0.9.0)
632 */
633
634 if (G_pfnExcHook)
635 {
636 (*G_pfnExcHook)(file, ptib);
637 }
638
639 // *** registers
640
641 fprintf(file, "\nRegisters:");
642 if (pContextRec->ContextFlags & CONTEXT_INTEGER)
643 {
644 // DS the following 4 added V0.9.6 (2000-11-06) [umoeller]
645 fprintf(file, "\n DS = %08lX ", pContextRec->ctx_SegDs);
646 excDescribePage(file, pContextRec->ctx_SegDs);
647 // ES
648 fprintf(file, "\n ES = %08lX ", pContextRec->ctx_SegEs);
649 excDescribePage(file, pContextRec->ctx_SegEs);
650 // FS
651 fprintf(file, "\n FS = %08lX ", pContextRec->ctx_SegFs);
652 excDescribePage(file, pContextRec->ctx_SegFs);
653 // GS
654 fprintf(file, "\n GS = %08lX ", pContextRec->ctx_SegGs);
655 excDescribePage(file, pContextRec->ctx_SegGs);
656
657 // EAX
658 fprintf(file, "\n EAX = %08lX ", pContextRec->ctx_RegEax);
659 excDescribePage(file, pContextRec->ctx_RegEax);
660 // EBX
661 fprintf(file, "\n EBX = %08lX ", pContextRec->ctx_RegEbx);
662 excDescribePage(file, pContextRec->ctx_RegEbx);
663 // ECX
664 fprintf(file, "\n ECX = %08lX ", pContextRec->ctx_RegEcx);
665 excDescribePage(file, pContextRec->ctx_RegEcx);
666 // EDX
667 fprintf(file, "\n EDX = %08lX ", pContextRec->ctx_RegEdx);
668 excDescribePage(file, pContextRec->ctx_RegEdx);
669 // ESI
670 fprintf(file, "\n ESI = %08lX ", pContextRec->ctx_RegEsi);
671 excDescribePage(file, pContextRec->ctx_RegEsi);
672 // EDI
673 fprintf(file, "\n EDI = %08lX ", pContextRec->ctx_RegEdi);
674 excDescribePage(file, pContextRec->ctx_RegEdi);
675 fprintf(file, "\n");
676 }
677 else
678 fprintf(file, " not available\n");
679
680 if (pContextRec->ContextFlags & CONTEXT_CONTROL)
681 {
682
683 // *** instruction
684
685 fprintf(file, "Instruction pointer (where exception occured):\n CS:EIP = %04lX:%08lX ",
686 pContextRec->ctx_SegCs,
687 pContextRec->ctx_RegEip);
688 excDescribePage(file, pContextRec->ctx_RegEip);
689
690 // *** CPU flags
691
692 fprintf(file, "\n EFLAGS = %08lX", pContextRec->ctx_EFlags);
693
694 /*
695 * stack:
696 *
697 */
698
699 fprintf(file, "\nStack:\n Base: %08lX\n Limit: %08lX",
700 (ULONG)(ptib ? ptib->tib_pstack : 0),
701 (ULONG)(ptib ? ptib->tib_pstacklimit : 0));
702 fprintf(file, "\n SS:ESP = %04lX:%08lX ",
703 pContextRec->ctx_SegSs,
704 pContextRec->ctx_RegEsp);
705 excDescribePage(file, pContextRec->ctx_RegEsp);
706
707 fprintf(file, "\n EBP = %08lX ", pContextRec->ctx_RegEbp);
708 excDescribePage(file, pContextRec->ctx_RegEbp);
709
710 /*
711 * stack dump:
712 */
713
714 if (ptib != 0)
715 excDumpStackFrames(file, ptib, pContextRec);
716 }
717 }
718 fprintf(file, "\n");
719
720 // reset old priority
721 DosSetPriority(PRTYS_THREAD,
722 (ulOldPriority & 0x0F00) >> 8,
723 (UCHAR)ulOldPriority,
724 0); // current thread
725}
726
727/* ******************************************************************
728 *
729 * Exported routines
730 *
731 ********************************************************************/
732
733/*
734 *@@ excRegisterHooks:
735 * this registers hooks which get called for
736 * exception handlers. You can set any of the
737 * hooks to NULL for safe defaults (see top of
738 * except.c for details). You can set none,
739 * one, or both of the hooks, and you can call
740 * this function several times.
741 *
742 * Both hooks get called whenever an exception
743 * occurs, so there better be no bugs in these
744 * routines. ;-) They only get called from
745 * within excHandlerLoud (because excHandlerQuiet
746 * writes no trap logs).
747 *
748 * The hooks are as follows:
749 *
750 * -- pfnExcOpenFileNew gets called to open
751 * the trap log file. This must return a FILE*
752 * pointer from fopen(). If this is not defined,
753 * ?:\TRAP.LOG is used. Use this to specify a
754 * different file and have some notes written
755 * into it before the actual exception info.
756 *
757 * -- pfnExcHookNew gets called while the trap log
758 * is being written. At this point,
759 * the following info has been written into
760 * the trap log already:
761 * -- exception type/address block
762 * -- exception explanation
763 * -- process information
764 *
765 * _After_ the hook, the exception handler
766 * continues with the "Registers" information
767 * and stack dump/analysis.
768 *
769 * Use this hook to write additional application
770 * info into the trap log, such as the state
771 * of your own threads and mutexes.
772 *
773 * -- pfnExcHookError gets called when the TRY_* macros
774 * fail to install an exception handler (when
775 * DosSetExceptionHandler fails). I've never seen
776 * this happen.
777 *
778 *@@added V0.9.0 [umoeller]
779 *@@changed V0.9.2 (2000-03-10) [umoeller]: pfnExcHookError added
780 */
781
782VOID excRegisterHooks(PFNEXCOPENFILE pfnExcOpenFileNew,
783 PFNEXCHOOK pfnExcHookNew,
784 PFNEXCHOOKERROR pfnExcHookError,
785 BOOL fBeepOnExceptionNew)
786{
787 // adjust the global variables
788 G_pfnExcOpenFile = pfnExcOpenFileNew;
789 G_pfnExcHook = pfnExcHookNew;
790 G_pfnExcHookError = pfnExcHookError;
791 G_fBeepOnException = fBeepOnExceptionNew;
792}
793
794/*
795 *@@ excHandlerLoud:
796 * this is the "sophisticated" exception handler;
797 * which gives forth a loud sequence of beeps thru the
798 * speaker, writes a trap log and then returns back
799 * to the thread to continue execution, i.e. the
800 * default OS/2 exception handler will never get
801 * called.
802 *
803 * This requires a setjmp() call on
804 * EXCEPTIONREGISTRATIONRECORD2.jmpThread before
805 * being installed. The TRY_LOUD macro will take
806 * care of this for you (see except.c).
807 *
808 * This intercepts the following exceptions (see
809 * the OS/2 Control Program Reference for details):
810 *
811 * -- XCPT_ACCESS_VIOLATION (traps 0x0d, 0x0e)
812 * -- XCPT_INTEGER_DIVIDE_BY_ZERO (trap 0)
813 * -- XCPT_ILLEGAL_INSTRUCTION (trap 6)
814 * -- XCPT_PRIVILEGED_INSTRUCTION
815 * -- XCPT_INTEGER_OVERFLOW (trap 4)
816 *
817 * For these exceptions, we call the functions in debug.c
818 * to try to find debug code or SYM file information about
819 * what source code corresponds to the error.
820 *
821 * See excRegisterHooks for the default setup of this.
822 *
823 * Note that to get meaningful debugging information
824 * in this handler's traplog, you need the following:
825 *
826 * a) have a MAP file created at link time (/MAP)
827 *
828 * b) convert the MAP to a SYM file using MAPSYM
829 *
830 * c) put the SYM file in the same directory of
831 * the module (EXE or DLL). This must have the
832 * same filestem as the module.
833 *
834 * All other exceptions are passed to the next handler
835 * in the exception handler chain. This might be the
836 * C/C++ compiler handler or the default OS/2 handler,
837 * which will probably terminate the process.
838 *
839 *@@changed V0.9.0 [umoeller]: added support for thread termination
840 *@@changed V0.9.2 (2000-03-10) [umoeller]: switched date format to ISO
841 */
842
843ULONG _System excHandlerLoud(PEXCEPTIONREPORTRECORD pReportRec,
844 PEXCEPTIONREGISTRATIONRECORD2 pRegRec2,
845 PCONTEXTRECORD pContextRec,
846 PVOID pv)
847{
848 /* From the VAC++3 docs:
849 * "The first thing an exception handler should do is check the
850 * exception flags. If EH_EXIT_UNWIND is set, meaning
851 * the thread is ending, the handler tells the operating system
852 * to pass the exception to the next exception handler. It does the
853 * same if the EH_UNWINDING flag is set, the flag that indicates
854 * this exception handler is being removed.
855 * The EH_NESTED_CALL flag indicates whether the exception
856 * occurred within an exception handler. If the handler does
857 * not check this flag, recursive exceptions could occur until
858 * there is no stack remaining."
859 * So for all these conditions, we exit immediately.
860 */
861
862 if (pReportRec->fHandlerFlags & EH_EXIT_UNWIND)
863 return (XCPT_CONTINUE_SEARCH);
864 if (pReportRec->fHandlerFlags & EH_UNWINDING)
865 return (XCPT_CONTINUE_SEARCH);
866 if (pReportRec->fHandlerFlags & EH_NESTED_CALL)
867 return (XCPT_CONTINUE_SEARCH);
868
869 switch (pReportRec->ExceptionNum)
870 {
871 /* case XCPT_PROCESS_TERMINATE:
872 case XCPT_ASYNC_PROCESS_TERMINATE:
873 // thread terminated:
874 // if the handler has been registered to catch
875 // these exceptions, continue;
876 if (pRegRec2->pfnOnKill)
877 // call the "OnKill" function
878 pRegRec2->pfnOnKill(pRegRec2);
879 // get outta here, which will kill the thread
880 break; */
881
882 case XCPT_ACCESS_VIOLATION:
883 case XCPT_INTEGER_DIVIDE_BY_ZERO:
884 case XCPT_ILLEGAL_INSTRUCTION:
885 case XCPT_PRIVILEGED_INSTRUCTION:
886 case XCPT_INVALID_LOCK_SEQUENCE:
887 case XCPT_INTEGER_OVERFLOW:
888 {
889 // "real" exceptions:
890 FILE *file;
891
892 // open traplog file;
893 if (G_pfnExcOpenFile)
894 // hook defined for this: call it
895 file = (*G_pfnExcOpenFile)();
896 else
897 {
898 CHAR szFileName[100];
899 // no hook defined: open some
900 // default traplog file in root directory of
901 // boot drive
902 sprintf(szFileName, "%c:\\trap.log", doshQueryBootDrive());
903 file = fopen(szFileName, "a");
904
905 if (file)
906 {
907 DATETIME DT;
908 DosGetDateTime(&DT);
909 fprintf(file,
910 "\nTrap message -- Date: %04d-%02d-%02d, Time: %02d:%02d:%02d\n",
911 DT.year, DT.month, DT.day,
912 DT.hours, DT.minutes, DT.seconds);
913 fprintf(file, "------------------------------------------------\n");
914
915 }
916 }
917
918 // write error log
919 excExplainException(file,
920 "excHandlerLoud",
921 pReportRec,
922 pContextRec);
923 fclose(file);
924
925 // jump back to failing routine
926 /* DosSetPriority(PRTYS_THREAD,
927 PRTYC_REGULAR,
928 0, // delta
929 0); // current thread
930 */
931 longjmp(pRegRec2->jmpThread, pReportRec->ExceptionNum);
932 break; }
933 }
934
935 // not handled
936 return (XCPT_CONTINUE_SEARCH);
937}
938
939/*
940 *@@ excHandlerQuiet:
941 * "quiet" xcpt handler, which simply suppresses exceptions;
942 * this is useful for certain error-prone functions, where
943 * exceptions are likely to appear, for example used by
944 * wpshCheckObject to implement a fail-safe SOM object check.
945 *
946 * This does _not_ write an error log and makes _no_ sound.
947 * This simply jumps back to the trapping thread or
948 * calls EXCEPTIONREGISTRATIONRECORD2.pfnOnKill.
949 *
950 * Other than that, this behaves like excHandlerLoud.
951 *
952 * This is best registered thru the TRY_QUIET macro
953 * (new with V0.84, described in except.c), which
954 * does the necessary setup.
955 *
956 *@@changed V0.9.0 [umoeller]: added support for thread termination
957 */
958
959ULONG _System excHandlerQuiet(PEXCEPTIONREPORTRECORD pReportRec,
960 PEXCEPTIONREGISTRATIONRECORD2 pRegRec2,
961 PCONTEXTRECORD pContextRec,
962 PVOID pv)
963{
964 if (pReportRec->fHandlerFlags & EH_EXIT_UNWIND)
965 return (XCPT_CONTINUE_SEARCH);
966 if (pReportRec->fHandlerFlags & EH_UNWINDING)
967 return (XCPT_CONTINUE_SEARCH);
968 if (pReportRec->fHandlerFlags & EH_NESTED_CALL)
969 return (XCPT_CONTINUE_SEARCH);
970
971 switch (pReportRec->ExceptionNum)
972 {
973 /* case XCPT_PROCESS_TERMINATE:
974 case XCPT_ASYNC_PROCESS_TERMINATE:
975 // thread terminated:
976 // if the handler has been registered to catch
977 // these exceptions, continue;
978 if (pRegRec2->pfnOnKill)
979 // call the "OnKill" function
980 pRegRec2->pfnOnKill(pRegRec2);
981 // get outta here, which will kill the thread
982 break; */
983
984 case XCPT_ACCESS_VIOLATION:
985 case XCPT_INTEGER_DIVIDE_BY_ZERO:
986 case XCPT_ILLEGAL_INSTRUCTION:
987 case XCPT_PRIVILEGED_INSTRUCTION:
988 case XCPT_INVALID_LOCK_SEQUENCE:
989 case XCPT_INTEGER_OVERFLOW:
990 // write excpt explanation only if the
991 // resp. debugging #define is set (setup.h)
992 #ifdef DEBUG_WRITEQUIETEXCPT
993 {
994 FILE *file = excOpenTraplogFile();
995 excExplainException(file,
996 "excHandlerQuiet",
997 pReportRec,
998 pContextRec);
999 fclose(file);
1000 }
1001 #endif
1002
1003 // jump back to failing routine
1004 longjmp(pRegRec2->jmpThread, pReportRec->ExceptionNum);
1005 break;
1006
1007 default:
1008 break;
1009 }
1010
1011 return (XCPT_CONTINUE_SEARCH);
1012}
1013
1014
Note: See TracBrowser for help on using the repository browser.