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

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

Coupla bugfixes.

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