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

Last change on this file since 384 was 384, checked in by pr, 15 years ago

Fix variable init. bugs.

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