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

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

Major updates; timers, LVM, miscellaneous.

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