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

Last change on this file since 118 was 117, checked in by umoeller, 24 years ago

Tons of changes.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 39.4 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 // V0.9.16 (2001-11-02) [pr]: We already got this info. above - this overwrites the
616 // original values before the priority change, which is rather confusing.
617 // if (DosGetInfoBlocks(&ptib, &ppib) == NO_ERROR)
618 {
619 /*
620 * process info:
621 *
622 */
623
624 if ((ptib) && (ppib)) // (99-11-01) [umoeller]
625 {
626 if (pContextRec->ContextFlags & CONTEXT_CONTROL)
627 {
628 // get the main module
629 hMod1 = ppib->pib_hmte;
630 DosQueryModuleName(hMod1,
631 sizeof(szMod1),
632 szMod1);
633
634 // get the trapping module
635 DosQueryModFromEIP(&hMod2,
636 &ulObjNum,
637 sizeof(szMod2),
638 szMod2,
639 &ulOffset,
640 pContextRec->ctx_RegEip);
641 DosQueryModuleName(hMod2,
642 sizeof(szMod2),
643 szMod2);
644 }
645
646 fprintf(file,
647 "\nProcess information:"
648 "\n Process ID: 0x%lX"
649 "\n Process module: 0x%lX (%s)"
650 "\n Trapping module: 0x%lX (%s)"
651 "\n Object: %ld\n", // V0.9.16 (2001-11-02) [pr]: make this display signed
652 ppib->pib_ulpid,
653 hMod1, szMod1,
654 hMod2, szMod2,
655 ulObjNum);
656
657 fprintf(file,
658 "\nTrapping thread information:"
659 "\n Thread ID: 0x%lX (%lu)"
660 "\n Priority: 0x%lX\n",
661 ptib->tib_ptib2->tib2_ultid, ptib->tib_ptib2->tib2_ultid,
662 ulOldPriority);
663 }
664 else
665 fprintf(file, "\nProcess information was not available.");
666
667 /*
668 * now call the hook, if one has been defined,
669 * so that the application can write additional
670 * information to the traplog (V0.9.0)
671 */
672
673 if (G_pfnExcHook)
674 {
675 (*G_pfnExcHook)(file, ptib);
676 }
677
678 // *** registers
679
680 fprintf(file, "\nRegisters:");
681 if (pContextRec->ContextFlags & CONTEXT_INTEGER)
682 {
683 // DS the following 4 added V0.9.6 (2000-11-06) [umoeller]
684 fprintf(file, "\n DS = %08lX ", pContextRec->ctx_SegDs);
685 excDescribePage(file, pContextRec->ctx_SegDs);
686 // ES
687 fprintf(file, "\n ES = %08lX ", pContextRec->ctx_SegEs);
688 excDescribePage(file, pContextRec->ctx_SegEs);
689 // FS
690 fprintf(file, "\n FS = %08lX ", pContextRec->ctx_SegFs);
691 excDescribePage(file, pContextRec->ctx_SegFs);
692 // GS
693 fprintf(file, "\n GS = %08lX ", pContextRec->ctx_SegGs);
694 excDescribePage(file, pContextRec->ctx_SegGs);
695
696 // EAX
697 fprintf(file, "\n EAX = %08lX ", pContextRec->ctx_RegEax);
698 excDescribePage(file, pContextRec->ctx_RegEax);
699 // EBX
700 fprintf(file, "\n EBX = %08lX ", pContextRec->ctx_RegEbx);
701 excDescribePage(file, pContextRec->ctx_RegEbx);
702 // ECX
703 fprintf(file, "\n ECX = %08lX ", pContextRec->ctx_RegEcx);
704 excDescribePage(file, pContextRec->ctx_RegEcx);
705 // EDX
706 fprintf(file, "\n EDX = %08lX ", pContextRec->ctx_RegEdx);
707 excDescribePage(file, pContextRec->ctx_RegEdx);
708 // ESI
709 fprintf(file, "\n ESI = %08lX ", pContextRec->ctx_RegEsi);
710 excDescribePage(file, pContextRec->ctx_RegEsi);
711 // EDI
712 fprintf(file, "\n EDI = %08lX ", pContextRec->ctx_RegEdi);
713 excDescribePage(file, pContextRec->ctx_RegEdi);
714 fprintf(file, "\n");
715 }
716 else
717 fprintf(file, " not available\n");
718
719 if (pContextRec->ContextFlags & CONTEXT_CONTROL)
720 {
721
722 // *** instruction
723
724 fprintf(file, "Instruction pointer (where exception occured):\n CS:EIP = %04lX:%08lX ",
725 pContextRec->ctx_SegCs,
726 pContextRec->ctx_RegEip);
727 excDescribePage(file, pContextRec->ctx_RegEip);
728
729 // *** CPU flags
730
731 fprintf(file, "\n EFLAGS = %08lX", pContextRec->ctx_EFlags);
732
733 /*
734 * stack:
735 *
736 */
737
738 fprintf(file, "\nStack:\n Base: %08lX\n Limit: %08lX",
739 (ULONG)(ptib ? ptib->tib_pstack : 0),
740 (ULONG)(ptib ? ptib->tib_pstacklimit : 0));
741 fprintf(file, "\n SS:ESP = %04lX:%08lX ",
742 pContextRec->ctx_SegSs,
743 pContextRec->ctx_RegEsp);
744 excDescribePage(file, pContextRec->ctx_RegEsp);
745
746 fprintf(file, "\n EBP = %08lX ", pContextRec->ctx_RegEbp);
747 excDescribePage(file, pContextRec->ctx_RegEbp);
748
749 /*
750 * stack dump:
751 */
752
753 if (ptib != 0)
754 {
755 excDumpStackFrames(file, ptib, pContextRec);
756 }
757 }
758 }
759 fprintf(file, "\n");
760
761 // reset old priority
762 DosSetPriority(PRTYS_THREAD,
763 (ulOldPriority & 0x0F00) >> 8,
764 (UCHAR)ulOldPriority,
765 0); // current thread
766
767 // lower global flag again V0.9.13 (2001-06-19) [umoeller]
768 G_ulExplainExceptionRunning--;
769}
770
771/* ******************************************************************
772 *
773 * Exported routines
774 *
775 ********************************************************************/
776
777/*
778 *@@ excRegisterHooks:
779 * this registers hooks which get called for
780 * exception handlers. You can set any of the
781 * hooks to NULL for safe defaults (see top of
782 * except.c for details). You can set none,
783 * one, or both of the hooks, and you can call
784 * this function several times.
785 *
786 * Both hooks get called whenever an exception
787 * occurs, so there better be no bugs in these
788 * routines. ;-) They only get called from
789 * within excHandlerLoud (because excHandlerQuiet
790 * writes no trap logs).
791 *
792 * The hooks are as follows:
793 *
794 * -- pfnExcOpenFileNew gets called to open
795 * the trap log file. This must return a FILE*
796 * pointer from fopen(). If this is not defined,
797 * ?:\TRAP.LOG is used. Use this to specify a
798 * different file and have some notes written
799 * into it before the actual exception info.
800 *
801 * -- pfnExcHookNew gets called while the trap log
802 * is being written. At this point,
803 * the following info has been written into
804 * the trap log already:
805 * -- exception type/address block
806 * -- exception explanation
807 * -- process information
808 *
809 * _After_ the hook, the exception handler
810 * continues with the "Registers" information
811 * and stack dump/analysis.
812 *
813 * Use this hook to write additional application
814 * info into the trap log, such as the state
815 * of your own threads and mutexes.
816 *
817 * -- pfnExcHookError gets called when the TRY_* macros
818 * fail to install an exception handler (when
819 * DosSetExceptionHandler fails). I've never seen
820 * this happen.
821 *
822 *@@added V0.9.0 [umoeller]
823 *@@changed V0.9.2 (2000-03-10) [umoeller]: pfnExcHookError added
824 */
825
826VOID excRegisterHooks(PFNEXCOPENFILE pfnExcOpenFileNew,
827 PFNEXCHOOK pfnExcHookNew,
828 PFNEXCHOOKERROR pfnExcHookError,
829 BOOL fBeepOnExceptionNew)
830{
831 // adjust the global variables
832 G_pfnExcOpenFile = pfnExcOpenFileNew;
833 G_pfnExcHook = pfnExcHookNew;
834 G_pfnExcHookError = pfnExcHookError;
835 G_fBeepOnException = fBeepOnExceptionNew;
836}
837
838/*
839 *@@ excHandlerLoud:
840 * this is the "sophisticated" exception handler;
841 * which gives forth a loud sequence of beeps thru the
842 * speaker, writes a trap log and then returns back
843 * to the thread to continue execution, i.e. the
844 * default OS/2 exception handler will never get
845 * called.
846 *
847 * This requires a setjmp() call on
848 * EXCEPTIONREGISTRATIONRECORD2.jmpThread before
849 * being installed. The TRY_LOUD macro will take
850 * care of this for you (see except.c).
851 *
852 * This intercepts the following exceptions (see
853 * the OS/2 Control Program Reference for details):
854 *
855 * -- XCPT_ACCESS_VIOLATION (traps 0x0d, 0x0e)
856 * -- XCPT_INTEGER_DIVIDE_BY_ZERO (trap 0)
857 * -- XCPT_ILLEGAL_INSTRUCTION (trap 6)
858 * -- XCPT_PRIVILEGED_INSTRUCTION
859 * -- XCPT_INTEGER_OVERFLOW (trap 4)
860 *
861 * For these exceptions, we call the functions in debug.c
862 * to try to find debug code or SYM file information about
863 * what source code corresponds to the error.
864 *
865 * See excRegisterHooks for the default setup of this.
866 *
867 * Note that to get meaningful debugging information
868 * in this handler's traplog, you need the following:
869 *
870 * a) have a MAP file created at link time (/MAP)
871 *
872 * b) convert the MAP to a SYM file using MAPSYM
873 *
874 * c) put the SYM file in the same directory of
875 * the module (EXE or DLL). This must have the
876 * same filestem as the module.
877 *
878 * All other exceptions are passed to the next handler
879 * in the exception handler chain. This might be the
880 * C/C++ compiler handler or the default OS/2 handler,
881 * which will probably terminate the process.
882 *
883 *@@changed V0.9.0 [umoeller]: added support for thread termination
884 *@@changed V0.9.2 (2000-03-10) [umoeller]: switched date format to ISO
885 */
886
887ULONG _System excHandlerLoud(PEXCEPTIONREPORTRECORD pReportRec,
888 PEXCEPTIONREGISTRATIONRECORD2 pRegRec2,
889 PCONTEXTRECORD pContextRec,
890 PVOID pv)
891{
892 /* From the VAC++3 docs:
893 * "The first thing an exception handler should do is check the
894 * exception flags. If EH_EXIT_UNWIND is set, meaning
895 * the thread is ending, the handler tells the operating system
896 * to pass the exception to the next exception handler. It does the
897 * same if the EH_UNWINDING flag is set, the flag that indicates
898 * this exception handler is being removed.
899 * The EH_NESTED_CALL flag indicates whether the exception
900 * occurred within an exception handler. If the handler does
901 * not check this flag, recursive exceptions could occur until
902 * there is no stack remaining."
903 * So for all these conditions, we exit immediately.
904 */
905
906 if (pReportRec->fHandlerFlags & EH_EXIT_UNWIND)
907 return (XCPT_CONTINUE_SEARCH);
908 if (pReportRec->fHandlerFlags & EH_UNWINDING)
909 return (XCPT_CONTINUE_SEARCH);
910 if (pReportRec->fHandlerFlags & EH_NESTED_CALL)
911 return (XCPT_CONTINUE_SEARCH);
912
913 switch (pReportRec->ExceptionNum)
914 {
915 /* case XCPT_PROCESS_TERMINATE:
916 case XCPT_ASYNC_PROCESS_TERMINATE:
917 // thread terminated:
918 // if the handler has been registered to catch
919 // these exceptions, continue;
920 if (pRegRec2->pfnOnKill)
921 // call the "OnKill" function
922 pRegRec2->pfnOnKill(pRegRec2);
923 // get outta here, which will kill the thread
924 break; */
925
926 case XCPT_ACCESS_VIOLATION:
927 case XCPT_INTEGER_DIVIDE_BY_ZERO:
928 case XCPT_ILLEGAL_INSTRUCTION:
929 case XCPT_PRIVILEGED_INSTRUCTION:
930 case XCPT_INVALID_LOCK_SEQUENCE:
931 case XCPT_INTEGER_OVERFLOW:
932 {
933 // "real" exceptions:
934 FILE *file;
935
936 // open traplog file;
937 if (G_pfnExcOpenFile)
938 // hook defined for this: call it
939 file = (*G_pfnExcOpenFile)();
940 else
941 {
942 CHAR szFileName[100];
943 // no hook defined: open some
944 // default traplog file in root directory of
945 // boot drive
946 sprintf(szFileName, "%c:\\trap.log", doshQueryBootDrive());
947 file = fopen(szFileName, "a");
948
949 if (file)
950 {
951 DATETIME DT;
952 DosGetDateTime(&DT);
953 fprintf(file,
954 "\nTrap message -- Date: %04d-%02d-%02d, Time: %02d:%02d:%02d\n",
955 DT.year, DT.month, DT.day,
956 DT.hours, DT.minutes, DT.seconds);
957 fprintf(file, "------------------------------------------------\n");
958
959 }
960 }
961
962 // write error log
963 excExplainException(file,
964 "excHandlerLoud",
965 pReportRec,
966 pContextRec);
967 fclose(file);
968
969 // jump back to failing routine
970 /* DosSetPriority(PRTYS_THREAD,
971 PRTYC_REGULAR,
972 0, // delta
973 0); // current thread
974 */
975 longjmp(pRegRec2->jmpThread, pReportRec->ExceptionNum);
976 break; }
977 }
978
979 // not handled
980 return (XCPT_CONTINUE_SEARCH);
981}
982
983/*
984 *@@ excHandlerQuiet:
985 * "quiet" xcpt handler, which simply suppresses exceptions;
986 * this is useful for certain error-prone functions, where
987 * exceptions are likely to appear, for example used by
988 * wpshCheckObject to implement a fail-safe SOM object check.
989 *
990 * This does _not_ write an error log and makes _no_ sound.
991 * This simply jumps back to the trapping thread or
992 * calls EXCEPTIONREGISTRATIONRECORD2.pfnOnKill.
993 *
994 * Other than that, this behaves like excHandlerLoud.
995 *
996 * This is best registered thru the TRY_QUIET macro
997 * (new with V0.84, described in except.c), which
998 * does the necessary setup.
999 *
1000 *@@changed V0.9.0 [umoeller]: added support for thread termination
1001 */
1002
1003ULONG _System excHandlerQuiet(PEXCEPTIONREPORTRECORD pReportRec,
1004 PEXCEPTIONREGISTRATIONRECORD2 pRegRec2,
1005 PCONTEXTRECORD pContextRec,
1006 PVOID pv)
1007{
1008 if (pReportRec->fHandlerFlags & EH_EXIT_UNWIND)
1009 return (XCPT_CONTINUE_SEARCH);
1010 if (pReportRec->fHandlerFlags & EH_UNWINDING)
1011 return (XCPT_CONTINUE_SEARCH);
1012 if (pReportRec->fHandlerFlags & EH_NESTED_CALL)
1013 return (XCPT_CONTINUE_SEARCH);
1014
1015 switch (pReportRec->ExceptionNum)
1016 {
1017 /* case XCPT_PROCESS_TERMINATE:
1018 case XCPT_ASYNC_PROCESS_TERMINATE:
1019 // thread terminated:
1020 // if the handler has been registered to catch
1021 // these exceptions, continue;
1022 if (pRegRec2->pfnOnKill)
1023 // call the "OnKill" function
1024 pRegRec2->pfnOnKill(pRegRec2);
1025 // get outta here, which will kill the thread
1026 break; */
1027
1028 case XCPT_ACCESS_VIOLATION:
1029 case XCPT_INTEGER_DIVIDE_BY_ZERO:
1030 case XCPT_ILLEGAL_INSTRUCTION:
1031 case XCPT_PRIVILEGED_INSTRUCTION:
1032 case XCPT_INVALID_LOCK_SEQUENCE:
1033 case XCPT_INTEGER_OVERFLOW:
1034 // write excpt explanation only if the
1035 // resp. debugging #define is set (setup.h)
1036 #ifdef DEBUG_WRITEQUIETEXCPT
1037 {
1038 FILE *file = excOpenTraplogFile();
1039 excExplainException(file,
1040 "excHandlerQuiet",
1041 pReportRec,
1042 pContextRec);
1043 fclose(file);
1044 }
1045 #endif
1046
1047 // jump back to failing routine
1048 longjmp(pRegRec2->jmpThread, pReportRec->ExceptionNum);
1049 break;
1050
1051 default:
1052 break;
1053 }
1054
1055 return (XCPT_CONTINUE_SEARCH);
1056}
1057
1058
Note: See TracBrowser for help on using the repository browser.