/*----------------------------------------------------------------*/ /* trapcode.c 02/20/2011 Keven Miller kevenm@3kranger.com */ /*----------------------------------------------------------------*/ #ifdef __mpexl #pragma list off #endif /*------------------------------------------------------------------ Trapcode shows how to trap Invalid memory access, alignment, and invalid instructions. The trap displays the trap error type, a dump of the surrounding PArisc code, and the machine registers. Note - the +XG switch inserts line-number statements into the PArisc code. setvar ccopts "-Aa -C +e +w1 +XG" ccxllk trapcdec, trapcde ------------------------------------------------------------------*/ #include /* printf,fprintf */ #include /* strcpy */ #pragma intrinsic HPDEBUG #pragma intrinsic SETJCW #pragma intrinsic TERMINATE #pragma intrinsic XCODETRAP #define proc #define VERSION "E.00.00" #define JCW_FATAL 32768u #ifndef S16 #define S16 typedef short s16; typedef unsigned short u16; typedef int s32; typedef unsigned int u32; #endif /* mpe debug: * put level back to error code * display full procedure trace * display code back 12 and forward 2 instructions * display registers * display loadinfo */ #define DEBUG_CMD \ "|ignore;{wl \"ABORT: \"+nmfile(nmpw);" \ "lev 0 2;tr,f,i;dc pc-2c,#14;dr;loadinfo};c|" typedef struct { u32 inst; u32 offset; u32 spaceid; s32 err; } codetrap_t; /*----------------------------------------------------------------*/ proc void trapcode ( void *codetrap_data ) { int fn; s32 err; char str [40], debugcmd [] = DEBUG_CMD; codetrap_t *trap; /* disable codetraps */ XCODETRAP ( 0, &fn ); trap = (codetrap_t*) codetrap_data; /* clear out any remaining output; display codetrap header */ fflush ( stdout ); /* set trap message */ switch ( trap->err ) { /* 1 High priority machine check */ /* 2 Power failure */ /* 3 Recovery counter trap */ /* 4 External interrupt */ /* 5 Low priority machine check */ /* 6 Instruction TLB/page miss fault */ case 7: strcpy ( str, "Instruction memory protection trap" ); break; case 8: strcpy ( str, "Illegal Instruction" ); break; case 9: strcpy ( str, "BREAK Instruction" ); break; case 10: strcpy ( str, "Privileged instruction" ); break; /* 11 Privileged register */ /* 12 Overflow */ /* 13 Trap on condition */ /* 14 Assist exception */ case 15: strcpy ( str, "Illegal data address" ); break; /* 16 Non-access instruction TLB miss */ /* 17 Non-access data TLB miss/page fault */ case 18: strcpy ( str, "Data memory protection/Unaligned" ); break; /* 19 Data memory break */ /* 20 TLB dirty */ /* 21 Page reference trap */ /* 22 Assist emulation */ /* 23 Higher privilage transfer */ /* 24 Lower privilege transfer */ /* 25 Taken branch trace trap */ /* 26 Data memory access rights trap */ /* 27 Data memory protection ID trap */ case 28: strcpy ( str, "Unaligned data reference" ); break; default: sprintf( str, "Unknown trap type: %d", trap->err ); } fprintf ( stderr, "\n!! TrapCode Begin ----------------------------------------------\n" "!! %x.%8.8x: %8.8x\n" "!! %s (%d)\n\n", trap->spaceid, (trap->offset & ~3), trap->inst, str, trap->err ); fflush ( stderr ); /* setup and dump MPE-PA-Risc trap display */ fn = _mpe_fileno ( fileno ( stderr )); HPDEBUG ( &err, debugcmd, 1, fn, 2, 0 ); if ( err ) fprintf ( stderr, "!! TrapCode HPDebug err %d %s\n", err, str ); /* display codetrap trailer; clear buffers */ fprintf ( stderr, "!! TrapCode End ------------------------------------------------\n\n" ); fflush ( stderr ); /* set environment variable JCW to FATAL */ SETJCW ( JCW_FATAL + trap->err ); TERMINATE (); } /*----------------------------------------------------------------*/ proc int main () { int old; XCODETRAP ((int) &trapcode, &old); printf ( "attempt to read DB+0" ); *(int*)0; } /*----------------------------------------------------------------*/