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

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

Misc. updates.

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