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

Last change on this file since 120 was 119, checked in by umoeller, 24 years ago

Minor fixes.

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