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

Last change on this file since 108 was 81, checked in by umoeller, 24 years ago

Tons of changes from the last weeks.

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