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

Last change on this file since 69 was 68, checked in by umoeller, 24 years ago

Lotsa fixes from the last two weeks.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 38.6 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
262/*
263 *@@category: Helpers\Control program helpers\Exceptions/debugging
264 * See except.c.
265 */
266
267/* ******************************************************************
268 *
269 * Exception helper routines
270 *
271 ********************************************************************/
272
273/*
274 *@@ excDescribePage:
275 *
276 */
277
278VOID excDescribePage(FILE *file, ULONG ulCheck)
279{
280 APIRET arc;
281 ULONG ulCountPages = 1;
282 ULONG ulFlagsPage = 0;
283 arc = DosQueryMem((PVOID)ulCheck, &ulCountPages, &ulFlagsPage);
284
285 if (arc == NO_ERROR)
286 {
287 fprintf(file, "valid, flags: ");
288 if (ulFlagsPage & PAG_READ)
289 fprintf(file, "read ");
290 if (ulFlagsPage & PAG_WRITE)
291 fprintf(file, "write ");
292 if (ulFlagsPage & PAG_EXECUTE)
293 fprintf(file, "execute ");
294 if (ulFlagsPage & PAG_GUARD)
295 fprintf(file, "guard ");
296 if (ulFlagsPage & PAG_COMMIT)
297 fprintf(file, "committed ");
298 if (ulFlagsPage & PAG_SHARED)
299 fprintf(file, "shared ");
300 if (ulFlagsPage & PAG_FREE)
301 fprintf(file, "free ");
302 if (ulFlagsPage & PAG_BASE)
303 fprintf(file, "base ");
304 }
305 else if (arc == ERROR_INVALID_ADDRESS)
306 fprintf(file, "invalid");
307}
308
309/*
310 *@@ excPrintStackFrame:
311 * wrapper for dbgPrintStackFrame to format
312 * output stuff right.
313 *
314 *@@added V0.9.2 (2000-03-10) [umoeller]
315 *@@changed V0.9.12 (2001-05-12) [umoeller]: added seg:ofs to output always
316 */
317
318VOID excPrintStackFrame(FILE *file, // in: output log file
319 PSZ pszDescription, // in: description for stack frame (should be eight chars)
320 ULONG ulAddress) // in: address to debug
321{
322 APIRET arc = NO_ERROR;
323 HMODULE hmod1 = NULLHANDLE;
324 CHAR szMod1[2*CCHMAXPATH] = "unknown";
325 ULONG ulObject = 0,
326 ulOffset = 0;
327 fprintf(file,
328 " %-8s: %08lX ",
329 pszDescription,
330 ulAddress);
331 arc = DosQueryModFromEIP(&hmod1,
332 &ulObject,
333 sizeof(szMod1), szMod1,
334 &ulOffset,
335 ulAddress);
336
337 if (arc != NO_ERROR)
338 {
339 // error:
340 fprintf(file,
341 " %-8s Error: DosQueryModFromEIP returned %lu\n",
342 szMod1,
343 arc);
344 }
345 else
346 {
347 CHAR szFullName[2*CCHMAXPATH];
348
349 fprintf(file,
350 " %-8s %02lX:%08lX\n ",
351 szMod1,
352 ulObject + 1, // V0.9.12 (2001-05-12) [umoeller]
353 ulOffset); // V0.9.12 (2001-05-12) [umoeller]
354
355 DosQueryModuleName(hmod1, sizeof(szFullName), szFullName);
356 dbgPrintStackFrame(file,
357 szFullName,
358 ulObject,
359 ulOffset);
360
361 fprintf(file, "\n");
362
363 // make a 'tick' sound to let the user know we're still alive
364 DosBeep(2000, 10);
365 }
366}
367
368/*
369 *@@ excDumpStackFrames:
370 * called from excExplainException to dump the
371 * thread's stack frames. This calls excPrintStackFrame
372 * for each stack frame found.
373 *
374 *@@added V0.9.4 (2000-06-15) [umoeller]
375 */
376
377VOID excDumpStackFrames(FILE *file, // in: logfile from fopen()
378 PTIB ptib,
379 PCONTEXTRECORD pContextRec) // in: excpt info
380{
381 PULONG pulStackWord = 0;
382
383 fprintf(file, "\n\nStack frames:\n Address Module seg:ofs\n");
384
385 // first the trapping address itself
386 excPrintStackFrame(file,
387 "CS:EIP ",
388 pContextRec->ctx_RegEip);
389
390
391 pulStackWord = (PULONG)pContextRec->ctx_RegEbp;
392 /* if (pContextRec->ctx_RegEbp < pContextRec->ctx_RegEsp)
393 pulStackWord = (PULONG)(pContextRec->ctx_RegEbp & 0xFFFFFFF0);
394 else
395 pulStackWord = (PULONG)(pContextRec->ctx_RegEsp & 0xFFFFFFF0); */
396
397 while ( (pulStackWord != 0)
398 && (pulStackWord < (PULONG)ptib->tib_pstacklimit)
399 )
400 {
401 CHAR szAddress[20];
402
403 if (((ULONG)pulStackWord & 0x00000FFF) == 0x00000000)
404 {
405 // we're on a page boundary: check access
406 ULONG ulCountPages = 0x1000;
407 ULONG ulFlagsPage = 0;
408 APIRET arc = DosQueryMem((void *)pulStackWord,
409 &ulCountPages,
410 &ulFlagsPage);
411 if ( (arc != NO_ERROR)
412 || ( (arc == NO_ERROR)
413 && ( !( ((ulFlagsPage & (PAG_COMMIT|PAG_READ))
414 == (PAG_COMMIT|PAG_READ)
415 )
416 )
417 )
418 )
419 )
420 {
421 fprintf(file, "\n %08lX: ", (ULONG)pulStackWord);
422 fprintf(file, "Page inaccessible");
423 pulStackWord += 0x1000;
424 continue; // for
425 }
426 }
427
428 sprintf(szAddress, "%08lX",
429 (ULONG)pulStackWord);
430 excPrintStackFrame(file,
431 szAddress,
432 *(pulStackWord+1));
433 pulStackWord = (PULONG)*(pulStackWord);
434
435 if (pulStackWord == 0)
436 fprintf(file, "\n pulStackWord == 0");
437 else if (pulStackWord >= (PULONG)ptib->tib_pstacklimit)
438 fprintf(file, "\n pulStackWord >= (PULONG)ptib->tib_pstacklimit");
439 } // end while
440}
441
442/*
443 *@@ excExplainException:
444 * used by the exception handlers below to write
445 * LOTS of information about the exception into a logfile.
446 *
447 * This calls excPrintStackFrame for each stack frame.
448 *
449 *@@changed V0.9.0 [umoeller]: added support for application hook
450 *@@changed V0.9.0 (99-11-02) [umoeller]: added TID to dump
451 *@@changed V0.9.2 (2000-03-10) [umoeller]: now using excPrintStackFrame
452 *@@changed V0.9.3 (2000-05-03) [umoeller]: fixed crashes
453 *@@changed V0.9.6 (2000-11-06) [umoeller]: added more register dumps
454 */
455
456VOID excExplainException(FILE *file, // in: logfile from fopen()
457 PSZ pszHandlerName, // in: descriptive string
458 PEXCEPTIONREPORTRECORD pReportRec, // in: excpt info
459 PCONTEXTRECORD pContextRec) // in: excpt info
460{
461 ULONG aulBuf[3];
462 const char *pcszVersion = "unknown";
463
464 PTIB ptib = NULL;
465 PPIB ppib = NULL;
466 HMODULE hMod1, hMod2;
467 CHAR szMod1[CCHMAXPATH] = "unknown",
468 szMod2[CCHMAXPATH] = "unknown";
469 ULONG ulObjNum,
470 ulOffset;
471 ULONG ul;
472
473 ULONG ulOldPriority = 0x0100; // regular, delta 0
474
475 // raise this thread's priority, because this
476 // might take some time
477 if (DosGetInfoBlocks(&ptib, &ppib) == NO_ERROR)
478 if (ptib)
479 if (ptib->tib_ptib2)
480 {
481 ulOldPriority = ptib->tib_ptib2->tib2_ulpri;
482 DosSetPriority(PRTYS_THREAD,
483 PRTYC_REGULAR,
484 PRTYD_MAXIMUM,
485 0); // current thread
486 }
487
488 // make some noise
489 if (G_fBeepOnException)
490 {
491 DosBeep( 250, 30);
492 DosBeep( 500, 30);
493 DosBeep(1000, 30);
494 DosBeep(2000, 30);
495 DosBeep(4000, 30);
496 DosBeep(2000, 30);
497 DosBeep(1000, 30);
498 DosBeep( 500, 30);
499 DosBeep( 250, 30);
500 }
501
502 // generic exception info
503 DosQuerySysInfo(QSV_VERSION_MAJOR, // 11
504 QSV_VERSION_MINOR, // 12
505 &aulBuf, sizeof(aulBuf));
506 // Warp 3 is reported as 20.30
507 // Warp 4 is reported as 20.40
508 // Aurora is reported as 20.45
509
510 if (aulBuf[0] == 20)
511 {
512 switch (aulBuf[1])
513 {
514 case 30: pcszVersion = "Warp 3"; break;
515 case 40: pcszVersion = "Warp 4"; break;
516 case 45: pcszVersion = "WSeB kernel"; break;
517 }
518 }
519 fprintf(file,
520 "Running OS/2 version: %u.%u (%s)\n",
521 aulBuf[0], // major
522 aulBuf[1],
523 pcszVersion);
524
525
526 // generic exception info
527 fprintf(file,
528 "\n%s:\n Exception type: %08lX\n Address: %08lX\n Params: ",
529 pszHandlerName,
530 pReportRec->ExceptionNum,
531 (ULONG)pReportRec->ExceptionAddress);
532 for (ul = 0; ul < pReportRec->cParameters; ul++)
533 {
534 fprintf(file, "%08lX ",
535 pReportRec->ExceptionInfo[ul]);
536 }
537
538 // now explain the exception in a bit more detail;
539 // depending on the exception, pReportRec->ExceptionInfo
540 // contains some useful data
541 switch (pReportRec->ExceptionNum)
542 {
543 case XCPT_ACCESS_VIOLATION:
544 fprintf(file, "\nXCPT_ACCESS_VIOLATION: ");
545 if (pReportRec->ExceptionInfo[0] & XCPT_READ_ACCESS)
546 fprintf(file, "Invalid read access from 0x%04lX:%08lX.\n",
547 pContextRec->ctx_SegDs, pReportRec->ExceptionInfo[1]);
548 else if (pReportRec->ExceptionInfo[0] & XCPT_WRITE_ACCESS)
549 fprintf(file, "Invalid write access to 0x%04lX:%08lX.\n",
550 pContextRec->ctx_SegDs, pReportRec->ExceptionInfo[1]);
551 else if (pReportRec->ExceptionInfo[0] & XCPT_SPACE_ACCESS)
552 fprintf(file, "Invalid space access at 0x%04lX.\n",
553 pReportRec->ExceptionInfo[1]);
554 else if (pReportRec->ExceptionInfo[0] & XCPT_LIMIT_ACCESS)
555 fprintf(file, "Invalid limit access occurred.\n");
556 else if (pReportRec->ExceptionInfo[0] == XCPT_UNKNOWN_ACCESS)
557 fprintf(file, "unknown at 0x%04lX:%08lX\n",
558 pContextRec->ctx_SegDs, pReportRec->ExceptionInfo[1]);
559 fprintf(file,
560 "Explanation: An attempt was made to access a memory object which does\n"
561 " not belong to the current process. Most probable causes\n"
562 " for this are that an invalid pointer was used, there was\n"
563 " confusion with administering memory or error conditions \n"
564 " were not properly checked for.\n");
565 break;
566
567 case XCPT_INTEGER_DIVIDE_BY_ZERO:
568 fprintf(file, "\nXCPT_INTEGER_DIVIDE_BY_ZERO.\n");
569 fprintf(file,
570 "Explanation: An attempt was made to divide an integer value by zero,\n"
571 " which is not defined.\n");
572 break;
573
574 case XCPT_ILLEGAL_INSTRUCTION:
575 fprintf(file, "\nXCPT_ILLEGAL_INSTRUCTION.\n");
576 fprintf(file,
577 "Explanation: An attempt was made to execute an instruction that\n"
578 " is not defined on this machine's architecture.\n");
579 break;
580
581 case XCPT_PRIVILEGED_INSTRUCTION:
582 fprintf(file, "\nXCPT_PRIVILEGED_INSTRUCTION.\n");
583 fprintf(file,
584 "Explanation: An attempt was made to execute an instruction that\n"
585 " is not permitted in the current machine mode or that\n"
586 " the program had no permission to execute.\n");
587 break;
588
589 case XCPT_INTEGER_OVERFLOW:
590 fprintf(file, "\nXCPT_INTEGER_OVERFLOW.\n");
591 fprintf(file,
592 "Explanation: An integer operation generated a carry-out of the most\n"
593 " significant bit. This is a sign of an attempt to store\n"
594 " a value which does not fit into an integer variable.\n");
595 break;
596
597 default:
598 fprintf(file, "\nUnknown OS/2 exception number %d.\n", pReportRec->ExceptionNum);
599 fprintf(file, "Look this up in the OS/2 header files.\n");
600 break;
601 }
602
603 if (DosGetInfoBlocks(&ptib, &ppib) == NO_ERROR)
604 {
605 /*
606 * process info:
607 *
608 */
609
610 if ((ptib) && (ppib)) // (99-11-01) [umoeller]
611 {
612 if (pContextRec->ContextFlags & CONTEXT_CONTROL)
613 {
614 // get the main module
615 hMod1 = ppib->pib_hmte;
616 DosQueryModuleName(hMod1,
617 sizeof(szMod1),
618 szMod1);
619
620 // get the trapping module
621 DosQueryModFromEIP(&hMod2,
622 &ulObjNum,
623 sizeof(szMod2),
624 szMod2,
625 &ulOffset,
626 pContextRec->ctx_RegEip);
627 DosQueryModuleName(hMod2,
628 sizeof(szMod2),
629 szMod2);
630 }
631
632 fprintf(file,
633 "\nProcess information:"
634 "\n Process ID: 0x%lX"
635 "\n Process module: 0x%lX (%s)"
636 "\n Trapping module: 0x%lX (%s)"
637 "\n Object: %lu\n",
638 ppib->pib_ulpid,
639 hMod1, szMod1,
640 hMod2, szMod2,
641 ulObjNum);
642
643 fprintf(file,
644 "\nTrapping thread information:"
645 "\n Thread ID: 0x%lX (%lu)"
646 "\n Priority: 0x%lX\n",
647 ptib->tib_ptib2->tib2_ultid, ptib->tib_ptib2->tib2_ultid,
648 ulOldPriority);
649 }
650 else
651 fprintf(file, "\nProcess information was not available.");
652
653 /*
654 * now call the hook, if one has been defined,
655 * so that the application can write additional
656 * information to the traplog (V0.9.0)
657 */
658
659 if (G_pfnExcHook)
660 {
661 (*G_pfnExcHook)(file, ptib);
662 }
663
664 // *** registers
665
666 fprintf(file, "\nRegisters:");
667 if (pContextRec->ContextFlags & CONTEXT_INTEGER)
668 {
669 // DS the following 4 added V0.9.6 (2000-11-06) [umoeller]
670 fprintf(file, "\n DS = %08lX ", pContextRec->ctx_SegDs);
671 excDescribePage(file, pContextRec->ctx_SegDs);
672 // ES
673 fprintf(file, "\n ES = %08lX ", pContextRec->ctx_SegEs);
674 excDescribePage(file, pContextRec->ctx_SegEs);
675 // FS
676 fprintf(file, "\n FS = %08lX ", pContextRec->ctx_SegFs);
677 excDescribePage(file, pContextRec->ctx_SegFs);
678 // GS
679 fprintf(file, "\n GS = %08lX ", pContextRec->ctx_SegGs);
680 excDescribePage(file, pContextRec->ctx_SegGs);
681
682 // EAX
683 fprintf(file, "\n EAX = %08lX ", pContextRec->ctx_RegEax);
684 excDescribePage(file, pContextRec->ctx_RegEax);
685 // EBX
686 fprintf(file, "\n EBX = %08lX ", pContextRec->ctx_RegEbx);
687 excDescribePage(file, pContextRec->ctx_RegEbx);
688 // ECX
689 fprintf(file, "\n ECX = %08lX ", pContextRec->ctx_RegEcx);
690 excDescribePage(file, pContextRec->ctx_RegEcx);
691 // EDX
692 fprintf(file, "\n EDX = %08lX ", pContextRec->ctx_RegEdx);
693 excDescribePage(file, pContextRec->ctx_RegEdx);
694 // ESI
695 fprintf(file, "\n ESI = %08lX ", pContextRec->ctx_RegEsi);
696 excDescribePage(file, pContextRec->ctx_RegEsi);
697 // EDI
698 fprintf(file, "\n EDI = %08lX ", pContextRec->ctx_RegEdi);
699 excDescribePage(file, pContextRec->ctx_RegEdi);
700 fprintf(file, "\n");
701 }
702 else
703 fprintf(file, " not available\n");
704
705 if (pContextRec->ContextFlags & CONTEXT_CONTROL)
706 {
707
708 // *** instruction
709
710 fprintf(file, "Instruction pointer (where exception occured):\n CS:EIP = %04lX:%08lX ",
711 pContextRec->ctx_SegCs,
712 pContextRec->ctx_RegEip);
713 excDescribePage(file, pContextRec->ctx_RegEip);
714
715 // *** CPU flags
716
717 fprintf(file, "\n EFLAGS = %08lX", pContextRec->ctx_EFlags);
718
719 /*
720 * stack:
721 *
722 */
723
724 fprintf(file, "\nStack:\n Base: %08lX\n Limit: %08lX",
725 (ULONG)(ptib ? ptib->tib_pstack : 0),
726 (ULONG)(ptib ? ptib->tib_pstacklimit : 0));
727 fprintf(file, "\n SS:ESP = %04lX:%08lX ",
728 pContextRec->ctx_SegSs,
729 pContextRec->ctx_RegEsp);
730 excDescribePage(file, pContextRec->ctx_RegEsp);
731
732 fprintf(file, "\n EBP = %08lX ", pContextRec->ctx_RegEbp);
733 excDescribePage(file, pContextRec->ctx_RegEbp);
734
735 /*
736 * stack dump:
737 */
738
739 if (ptib != 0)
740 {
741 excDumpStackFrames(file, ptib, pContextRec);
742 }
743 }
744 }
745 fprintf(file, "\n");
746
747 // reset old priority
748 DosSetPriority(PRTYS_THREAD,
749 (ulOldPriority & 0x0F00) >> 8,
750 (UCHAR)ulOldPriority,
751 0); // current thread
752}
753
754/* ******************************************************************
755 *
756 * Exported routines
757 *
758 ********************************************************************/
759
760/*
761 *@@ excRegisterHooks:
762 * this registers hooks which get called for
763 * exception handlers. You can set any of the
764 * hooks to NULL for safe defaults (see top of
765 * except.c for details). You can set none,
766 * one, or both of the hooks, and you can call
767 * this function several times.
768 *
769 * Both hooks get called whenever an exception
770 * occurs, so there better be no bugs in these
771 * routines. ;-) They only get called from
772 * within excHandlerLoud (because excHandlerQuiet
773 * writes no trap logs).
774 *
775 * The hooks are as follows:
776 *
777 * -- pfnExcOpenFileNew gets called to open
778 * the trap log file. This must return a FILE*
779 * pointer from fopen(). If this is not defined,
780 * ?:\TRAP.LOG is used. Use this to specify a
781 * different file and have some notes written
782 * into it before the actual exception info.
783 *
784 * -- pfnExcHookNew gets called while the trap log
785 * is being written. At this point,
786 * the following info has been written into
787 * the trap log already:
788 * -- exception type/address block
789 * -- exception explanation
790 * -- process information
791 *
792 * _After_ the hook, the exception handler
793 * continues with the "Registers" information
794 * and stack dump/analysis.
795 *
796 * Use this hook to write additional application
797 * info into the trap log, such as the state
798 * of your own threads and mutexes.
799 *
800 * -- pfnExcHookError gets called when the TRY_* macros
801 * fail to install an exception handler (when
802 * DosSetExceptionHandler fails). I've never seen
803 * this happen.
804 *
805 *@@added V0.9.0 [umoeller]
806 *@@changed V0.9.2 (2000-03-10) [umoeller]: pfnExcHookError added
807 */
808
809VOID excRegisterHooks(PFNEXCOPENFILE pfnExcOpenFileNew,
810 PFNEXCHOOK pfnExcHookNew,
811 PFNEXCHOOKERROR pfnExcHookError,
812 BOOL fBeepOnExceptionNew)
813{
814 // adjust the global variables
815 G_pfnExcOpenFile = pfnExcOpenFileNew;
816 G_pfnExcHook = pfnExcHookNew;
817 G_pfnExcHookError = pfnExcHookError;
818 G_fBeepOnException = fBeepOnExceptionNew;
819}
820
821/*
822 *@@ excHandlerLoud:
823 * this is the "sophisticated" exception handler;
824 * which gives forth a loud sequence of beeps thru the
825 * speaker, writes a trap log and then returns back
826 * to the thread to continue execution, i.e. the
827 * default OS/2 exception handler will never get
828 * called.
829 *
830 * This requires a setjmp() call on
831 * EXCEPTIONREGISTRATIONRECORD2.jmpThread before
832 * being installed. The TRY_LOUD macro will take
833 * care of this for you (see except.c).
834 *
835 * This intercepts the following exceptions (see
836 * the OS/2 Control Program Reference for details):
837 *
838 * -- XCPT_ACCESS_VIOLATION (traps 0x0d, 0x0e)
839 * -- XCPT_INTEGER_DIVIDE_BY_ZERO (trap 0)
840 * -- XCPT_ILLEGAL_INSTRUCTION (trap 6)
841 * -- XCPT_PRIVILEGED_INSTRUCTION
842 * -- XCPT_INTEGER_OVERFLOW (trap 4)
843 *
844 * For these exceptions, we call the functions in debug.c
845 * to try to find debug code or SYM file information about
846 * what source code corresponds to the error.
847 *
848 * See excRegisterHooks for the default setup of this.
849 *
850 * Note that to get meaningful debugging information
851 * in this handler's traplog, you need the following:
852 *
853 * a) have a MAP file created at link time (/MAP)
854 *
855 * b) convert the MAP to a SYM file using MAPSYM
856 *
857 * c) put the SYM file in the same directory of
858 * the module (EXE or DLL). This must have the
859 * same filestem as the module.
860 *
861 * All other exceptions are passed to the next handler
862 * in the exception handler chain. This might be the
863 * C/C++ compiler handler or the default OS/2 handler,
864 * which will probably terminate the process.
865 *
866 *@@changed V0.9.0 [umoeller]: added support for thread termination
867 *@@changed V0.9.2 (2000-03-10) [umoeller]: switched date format to ISO
868 */
869
870ULONG _System excHandlerLoud(PEXCEPTIONREPORTRECORD pReportRec,
871 PEXCEPTIONREGISTRATIONRECORD2 pRegRec2,
872 PCONTEXTRECORD pContextRec,
873 PVOID pv)
874{
875 /* From the VAC++3 docs:
876 * "The first thing an exception handler should do is check the
877 * exception flags. If EH_EXIT_UNWIND is set, meaning
878 * the thread is ending, the handler tells the operating system
879 * to pass the exception to the next exception handler. It does the
880 * same if the EH_UNWINDING flag is set, the flag that indicates
881 * this exception handler is being removed.
882 * The EH_NESTED_CALL flag indicates whether the exception
883 * occurred within an exception handler. If the handler does
884 * not check this flag, recursive exceptions could occur until
885 * there is no stack remaining."
886 * So for all these conditions, we exit immediately.
887 */
888
889 if (pReportRec->fHandlerFlags & EH_EXIT_UNWIND)
890 return (XCPT_CONTINUE_SEARCH);
891 if (pReportRec->fHandlerFlags & EH_UNWINDING)
892 return (XCPT_CONTINUE_SEARCH);
893 if (pReportRec->fHandlerFlags & EH_NESTED_CALL)
894 return (XCPT_CONTINUE_SEARCH);
895
896 switch (pReportRec->ExceptionNum)
897 {
898 /* case XCPT_PROCESS_TERMINATE:
899 case XCPT_ASYNC_PROCESS_TERMINATE:
900 // thread terminated:
901 // if the handler has been registered to catch
902 // these exceptions, continue;
903 if (pRegRec2->pfnOnKill)
904 // call the "OnKill" function
905 pRegRec2->pfnOnKill(pRegRec2);
906 // get outta here, which will kill the thread
907 break; */
908
909 case XCPT_ACCESS_VIOLATION:
910 case XCPT_INTEGER_DIVIDE_BY_ZERO:
911 case XCPT_ILLEGAL_INSTRUCTION:
912 case XCPT_PRIVILEGED_INSTRUCTION:
913 case XCPT_INVALID_LOCK_SEQUENCE:
914 case XCPT_INTEGER_OVERFLOW:
915 {
916 // "real" exceptions:
917 FILE *file;
918
919 // open traplog file;
920 if (G_pfnExcOpenFile)
921 // hook defined for this: call it
922 file = (*G_pfnExcOpenFile)();
923 else
924 {
925 CHAR szFileName[100];
926 // no hook defined: open some
927 // default traplog file in root directory of
928 // boot drive
929 sprintf(szFileName, "%c:\\trap.log", doshQueryBootDrive());
930 file = fopen(szFileName, "a");
931
932 if (file)
933 {
934 DATETIME DT;
935 DosGetDateTime(&DT);
936 fprintf(file,
937 "\nTrap message -- Date: %04d-%02d-%02d, Time: %02d:%02d:%02d\n",
938 DT.year, DT.month, DT.day,
939 DT.hours, DT.minutes, DT.seconds);
940 fprintf(file, "------------------------------------------------\n");
941
942 }
943 }
944
945 // write error log
946 excExplainException(file,
947 "excHandlerLoud",
948 pReportRec,
949 pContextRec);
950 fclose(file);
951
952 // jump back to failing routine
953 /* DosSetPriority(PRTYS_THREAD,
954 PRTYC_REGULAR,
955 0, // delta
956 0); // current thread
957 */
958 longjmp(pRegRec2->jmpThread, pReportRec->ExceptionNum);
959 break; }
960 }
961
962 // not handled
963 return (XCPT_CONTINUE_SEARCH);
964}
965
966/*
967 *@@ excHandlerQuiet:
968 * "quiet" xcpt handler, which simply suppresses exceptions;
969 * this is useful for certain error-prone functions, where
970 * exceptions are likely to appear, for example used by
971 * wpshCheckObject to implement a fail-safe SOM object check.
972 *
973 * This does _not_ write an error log and makes _no_ sound.
974 * This simply jumps back to the trapping thread or
975 * calls EXCEPTIONREGISTRATIONRECORD2.pfnOnKill.
976 *
977 * Other than that, this behaves like excHandlerLoud.
978 *
979 * This is best registered thru the TRY_QUIET macro
980 * (new with V0.84, described in except.c), which
981 * does the necessary setup.
982 *
983 *@@changed V0.9.0 [umoeller]: added support for thread termination
984 */
985
986ULONG _System excHandlerQuiet(PEXCEPTIONREPORTRECORD pReportRec,
987 PEXCEPTIONREGISTRATIONRECORD2 pRegRec2,
988 PCONTEXTRECORD pContextRec,
989 PVOID pv)
990{
991 if (pReportRec->fHandlerFlags & EH_EXIT_UNWIND)
992 return (XCPT_CONTINUE_SEARCH);
993 if (pReportRec->fHandlerFlags & EH_UNWINDING)
994 return (XCPT_CONTINUE_SEARCH);
995 if (pReportRec->fHandlerFlags & EH_NESTED_CALL)
996 return (XCPT_CONTINUE_SEARCH);
997
998 switch (pReportRec->ExceptionNum)
999 {
1000 /* case XCPT_PROCESS_TERMINATE:
1001 case XCPT_ASYNC_PROCESS_TERMINATE:
1002 // thread terminated:
1003 // if the handler has been registered to catch
1004 // these exceptions, continue;
1005 if (pRegRec2->pfnOnKill)
1006 // call the "OnKill" function
1007 pRegRec2->pfnOnKill(pRegRec2);
1008 // get outta here, which will kill the thread
1009 break; */
1010
1011 case XCPT_ACCESS_VIOLATION:
1012 case XCPT_INTEGER_DIVIDE_BY_ZERO:
1013 case XCPT_ILLEGAL_INSTRUCTION:
1014 case XCPT_PRIVILEGED_INSTRUCTION:
1015 case XCPT_INVALID_LOCK_SEQUENCE:
1016 case XCPT_INTEGER_OVERFLOW:
1017 // write excpt explanation only if the
1018 // resp. debugging #define is set (setup.h)
1019 #ifdef DEBUG_WRITEQUIETEXCPT
1020 {
1021 FILE *file = excOpenTraplogFile();
1022 excExplainException(file,
1023 "excHandlerQuiet",
1024 pReportRec,
1025 pContextRec);
1026 fclose(file);
1027 }
1028 #endif
1029
1030 // jump back to failing routine
1031 longjmp(pRegRec2->jmpThread, pReportRec->ExceptionNum);
1032 break;
1033
1034 default:
1035 break;
1036 }
1037
1038 return (XCPT_CONTINUE_SEARCH);
1039}
1040
1041
Note: See TracBrowser for help on using the repository browser.