source: branches/branch-1-0/src/helpers/except.c@ 472

Last change on this file since 472 was 453, checked in by rlwalsh, 2 years ago

except.c: make trap logs less verbose/pedantic; minor formatting changes

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