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

Last change on this file since 154 was 153, checked in by umoeller, 23 years ago

Lots of changes from the last three weeks.

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