1*906afcb8SAndy Fiddaman /*********************************************************************** 2*906afcb8SAndy Fiddaman * * 3*906afcb8SAndy Fiddaman * This software is part of the ast package * 4*906afcb8SAndy Fiddaman * Copyright (c) 1986-2011 AT&T Intellectual Property * 5*906afcb8SAndy Fiddaman * and is licensed under the * 6*906afcb8SAndy Fiddaman * Eclipse Public License, Version 1.0 * 7*906afcb8SAndy Fiddaman * by AT&T Intellectual Property * 8*906afcb8SAndy Fiddaman * * 9*906afcb8SAndy Fiddaman * A copy of the License is available at * 10*906afcb8SAndy Fiddaman * http://www.eclipse.org/org/documents/epl-v10.html * 11*906afcb8SAndy Fiddaman * (with md5 checksum b35adb5213ca9657e911e9befb180842) * 12*906afcb8SAndy Fiddaman * * 13*906afcb8SAndy Fiddaman * Information and Software Systems Research * 14*906afcb8SAndy Fiddaman * AT&T Research * 15*906afcb8SAndy Fiddaman * Florham Park NJ * 16*906afcb8SAndy Fiddaman * * 17*906afcb8SAndy Fiddaman * Glenn Fowler <gsf@research.att.com> * 18*906afcb8SAndy Fiddaman * * 19*906afcb8SAndy Fiddaman ***********************************************************************/ 20*906afcb8SAndy Fiddaman #pragma prototyped 21*906afcb8SAndy Fiddaman /* 22*906afcb8SAndy Fiddaman * Glenn Fowler 23*906afcb8SAndy Fiddaman * AT&T Research 24*906afcb8SAndy Fiddaman * 25*906afcb8SAndy Fiddaman * preprocessor library trace and debug support 26*906afcb8SAndy Fiddaman */ 27*906afcb8SAndy Fiddaman 28*906afcb8SAndy Fiddaman #include "pplib.h" 29*906afcb8SAndy Fiddaman #include "ppfsm.h" 30*906afcb8SAndy Fiddaman 31*906afcb8SAndy Fiddaman #include <ctype.h> 32*906afcb8SAndy Fiddaman 33*906afcb8SAndy Fiddaman /* 34*906afcb8SAndy Fiddaman * convert token string to printable form 35*906afcb8SAndy Fiddaman */ 36*906afcb8SAndy Fiddaman 37*906afcb8SAndy Fiddaman char* 38*906afcb8SAndy Fiddaman pptokstr(register char* s, register int c) 39*906afcb8SAndy Fiddaman { 40*906afcb8SAndy Fiddaman register char* t; 41*906afcb8SAndy Fiddaman 42*906afcb8SAndy Fiddaman static char buf[8]; 43*906afcb8SAndy Fiddaman 44*906afcb8SAndy Fiddaman if (t = s) 45*906afcb8SAndy Fiddaman { 46*906afcb8SAndy Fiddaman while (*t == ' ' || *t == '\t') t++; 47*906afcb8SAndy Fiddaman c = *t ? *t : *s; 48*906afcb8SAndy Fiddaman } 49*906afcb8SAndy Fiddaman switch (c) 50*906afcb8SAndy Fiddaman { 51*906afcb8SAndy Fiddaman case 0: 52*906afcb8SAndy Fiddaman case 0400: 53*906afcb8SAndy Fiddaman return("`EOF'"); 54*906afcb8SAndy Fiddaman case ' ': 55*906afcb8SAndy Fiddaman return("`space'"); 56*906afcb8SAndy Fiddaman case '\f': 57*906afcb8SAndy Fiddaman return("`formfeed'"); 58*906afcb8SAndy Fiddaman case '\n': 59*906afcb8SAndy Fiddaman return("`newline'"); 60*906afcb8SAndy Fiddaman case '\t': 61*906afcb8SAndy Fiddaman return("`tab'"); 62*906afcb8SAndy Fiddaman case '\v': 63*906afcb8SAndy Fiddaman return("`vertical-tab'"); 64*906afcb8SAndy Fiddaman case T_TOKCAT: 65*906afcb8SAndy Fiddaman return("##"); 66*906afcb8SAndy Fiddaman default: 67*906afcb8SAndy Fiddaman if (iscntrl(c) || !isprint(c)) sfsprintf(buf, sizeof(buf), "`%03o'", c); 68*906afcb8SAndy Fiddaman else if (s) return(s); 69*906afcb8SAndy Fiddaman else sfsprintf(buf, sizeof(buf), "%c", c); 70*906afcb8SAndy Fiddaman return(buf); 71*906afcb8SAndy Fiddaman } 72*906afcb8SAndy Fiddaman } 73*906afcb8SAndy Fiddaman 74*906afcb8SAndy Fiddaman #if DEBUG & TRACE_debug 75*906afcb8SAndy Fiddaman 76*906afcb8SAndy Fiddaman #include "ppdebug.h" 77*906afcb8SAndy Fiddaman 78*906afcb8SAndy Fiddaman /* 79*906afcb8SAndy Fiddaman * return input stream name given index 80*906afcb8SAndy Fiddaman */ 81*906afcb8SAndy Fiddaman 82*906afcb8SAndy Fiddaman char* 83*906afcb8SAndy Fiddaman ppinstr(register struct ppinstk* p) 84*906afcb8SAndy Fiddaman { 85*906afcb8SAndy Fiddaman register int i; 86*906afcb8SAndy Fiddaman 87*906afcb8SAndy Fiddaman static char buf[128]; 88*906afcb8SAndy Fiddaman 89*906afcb8SAndy Fiddaman for (i = 0; i < elementsof(ppinmap); i++) 90*906afcb8SAndy Fiddaman if (p->type == ppinmap[i].val) 91*906afcb8SAndy Fiddaman { 92*906afcb8SAndy Fiddaman switch (p->type) 93*906afcb8SAndy Fiddaman { 94*906afcb8SAndy Fiddaman case IN_MACRO: 95*906afcb8SAndy Fiddaman #if MACDEF 96*906afcb8SAndy Fiddaman case IN_MULTILINE: 97*906afcb8SAndy Fiddaman #endif 98*906afcb8SAndy Fiddaman if (p->symbol) 99*906afcb8SAndy Fiddaman { 100*906afcb8SAndy Fiddaman sfsprintf(buf, sizeof(buf), "%s=%s", ppinmap[i].nam, p->symbol->name); 101*906afcb8SAndy Fiddaman return(buf); 102*906afcb8SAndy Fiddaman } 103*906afcb8SAndy Fiddaman break; 104*906afcb8SAndy Fiddaman } 105*906afcb8SAndy Fiddaman return(ppinmap[i].nam); 106*906afcb8SAndy Fiddaman } 107*906afcb8SAndy Fiddaman sfsprintf(buf, sizeof(buf), "UNKNOWN[%d]", p->type); 108*906afcb8SAndy Fiddaman return(buf); 109*906afcb8SAndy Fiddaman } 110*906afcb8SAndy Fiddaman 111*906afcb8SAndy Fiddaman /* 112*906afcb8SAndy Fiddaman * return string given fsm lex state 113*906afcb8SAndy Fiddaman */ 114*906afcb8SAndy Fiddaman 115*906afcb8SAndy Fiddaman char* 116*906afcb8SAndy Fiddaman pplexstr(register int lex) 117*906afcb8SAndy Fiddaman { 118*906afcb8SAndy Fiddaman register int i; 119*906afcb8SAndy Fiddaman int splice; 120*906afcb8SAndy Fiddaman static char buf[64]; 121*906afcb8SAndy Fiddaman 122*906afcb8SAndy Fiddaman if (lex < 0) lex &= ~lex; 123*906afcb8SAndy Fiddaman splice = (lex & SPLICE); 124*906afcb8SAndy Fiddaman lex &= 0x7f; 125*906afcb8SAndy Fiddaman for (i = 0; i < (elementsof(pplexmap) - 1) && (lex > pplexmap[i].val || lex == pplexmap[i+1].val); i++); 126*906afcb8SAndy Fiddaman if (lex != pplexmap[i].val) 127*906afcb8SAndy Fiddaman { 128*906afcb8SAndy Fiddaman if (pplexmap[i].val < 0) sfsprintf(buf, sizeof(buf), "%s|0x%04x%s", pplexmap[i].nam, lex, splice ? "|SPLICE" : ""); 129*906afcb8SAndy Fiddaman else sfsprintf(buf, sizeof(buf), "%s+%d", pplexmap[i-1].nam, lex - pplexmap[i-1].val, splice ? "|SPLICE" : ""); 130*906afcb8SAndy Fiddaman return(buf); 131*906afcb8SAndy Fiddaman } 132*906afcb8SAndy Fiddaman if (splice) 133*906afcb8SAndy Fiddaman { 134*906afcb8SAndy Fiddaman sfsprintf(buf, sizeof(buf), "%s|SPLICE", pplexmap[i].nam); 135*906afcb8SAndy Fiddaman return(buf); 136*906afcb8SAndy Fiddaman } 137*906afcb8SAndy Fiddaman return(pplexmap[i].nam); 138*906afcb8SAndy Fiddaman } 139*906afcb8SAndy Fiddaman 140*906afcb8SAndy Fiddaman /* 141*906afcb8SAndy Fiddaman * return string given map p of size n and flags 142*906afcb8SAndy Fiddaman */ 143*906afcb8SAndy Fiddaman 144*906afcb8SAndy Fiddaman static char* 145*906afcb8SAndy Fiddaman ppflagstr(register struct map* p, int n, register long flags) 146*906afcb8SAndy Fiddaman { 147*906afcb8SAndy Fiddaman register int i; 148*906afcb8SAndy Fiddaman register int k; 149*906afcb8SAndy Fiddaman register char* s; 150*906afcb8SAndy Fiddaman 151*906afcb8SAndy Fiddaman static char buf[128]; 152*906afcb8SAndy Fiddaman 153*906afcb8SAndy Fiddaman s = buf; 154*906afcb8SAndy Fiddaman for (i = 0; i < n; i++) 155*906afcb8SAndy Fiddaman if (flags & p[i].val) 156*906afcb8SAndy Fiddaman { 157*906afcb8SAndy Fiddaman k = strlen(p[i].nam); 158*906afcb8SAndy Fiddaman if ((elementsof(buf) - 2 - (s - buf)) > k) 159*906afcb8SAndy Fiddaman { 160*906afcb8SAndy Fiddaman if (s > buf) *s++ = '|'; 161*906afcb8SAndy Fiddaman strcpy(s, p[i].nam); 162*906afcb8SAndy Fiddaman s += k; 163*906afcb8SAndy Fiddaman } 164*906afcb8SAndy Fiddaman } 165*906afcb8SAndy Fiddaman *s = 0; 166*906afcb8SAndy Fiddaman return(buf); 167*906afcb8SAndy Fiddaman } 168*906afcb8SAndy Fiddaman 169*906afcb8SAndy Fiddaman /* 170*906afcb8SAndy Fiddaman * return string given pp.mode 171*906afcb8SAndy Fiddaman */ 172*906afcb8SAndy Fiddaman 173*906afcb8SAndy Fiddaman char* 174*906afcb8SAndy Fiddaman ppmodestr(register long mode) 175*906afcb8SAndy Fiddaman { 176*906afcb8SAndy Fiddaman return(ppflagstr(ppmodemap, elementsof(ppmodemap), mode)); 177*906afcb8SAndy Fiddaman } 178*906afcb8SAndy Fiddaman 179*906afcb8SAndy Fiddaman /* 180*906afcb8SAndy Fiddaman * return string given pp.option 181*906afcb8SAndy Fiddaman */ 182*906afcb8SAndy Fiddaman 183*906afcb8SAndy Fiddaman char* 184*906afcb8SAndy Fiddaman ppoptionstr(register long option) 185*906afcb8SAndy Fiddaman { 186*906afcb8SAndy Fiddaman return(ppflagstr(ppoptionmap, elementsof(ppoptionmap), option)); 187*906afcb8SAndy Fiddaman } 188*906afcb8SAndy Fiddaman 189*906afcb8SAndy Fiddaman /* 190*906afcb8SAndy Fiddaman * return string given pp.state 191*906afcb8SAndy Fiddaman */ 192*906afcb8SAndy Fiddaman 193*906afcb8SAndy Fiddaman char* 194*906afcb8SAndy Fiddaman ppstatestr(register long state) 195*906afcb8SAndy Fiddaman { 196*906afcb8SAndy Fiddaman return(ppflagstr(ppstatemap, elementsof(ppstatemap), state)); 197*906afcb8SAndy Fiddaman } 198*906afcb8SAndy Fiddaman 199*906afcb8SAndy Fiddaman #include <sig.h> 200*906afcb8SAndy Fiddaman 201*906afcb8SAndy Fiddaman /* 202*906afcb8SAndy Fiddaman * io stream stack trace 203*906afcb8SAndy Fiddaman * sig==0 registers the handler 204*906afcb8SAndy Fiddaman */ 205*906afcb8SAndy Fiddaman 206*906afcb8SAndy Fiddaman void 207*906afcb8SAndy Fiddaman pptrace(int sig) 208*906afcb8SAndy Fiddaman { 209*906afcb8SAndy Fiddaman register char* s; 210*906afcb8SAndy Fiddaman register char* x; 211*906afcb8SAndy Fiddaman register struct ppinstk* p; 212*906afcb8SAndy Fiddaman static int handling; 213*906afcb8SAndy Fiddaman 214*906afcb8SAndy Fiddaman if (!sig) 215*906afcb8SAndy Fiddaman { 216*906afcb8SAndy Fiddaman #ifdef SIGBUS 217*906afcb8SAndy Fiddaman signal(SIGBUS, pptrace); 218*906afcb8SAndy Fiddaman #endif 219*906afcb8SAndy Fiddaman #ifdef SIGSEGV 220*906afcb8SAndy Fiddaman signal(SIGSEGV, pptrace); 221*906afcb8SAndy Fiddaman #endif 222*906afcb8SAndy Fiddaman #ifdef SIGILL 223*906afcb8SAndy Fiddaman signal(SIGILL, pptrace); 224*906afcb8SAndy Fiddaman #endif 225*906afcb8SAndy Fiddaman signal(SIGQUIT, pptrace); 226*906afcb8SAndy Fiddaman return; 227*906afcb8SAndy Fiddaman } 228*906afcb8SAndy Fiddaman s = fmtsignal(sig); 229*906afcb8SAndy Fiddaman if (handling) 230*906afcb8SAndy Fiddaman { 231*906afcb8SAndy Fiddaman sfprintf(sfstderr, "\n%s during io stack trace\n", s); 232*906afcb8SAndy Fiddaman signal(handling, SIG_DFL); 233*906afcb8SAndy Fiddaman sigunblock(handling); 234*906afcb8SAndy Fiddaman kill(getpid(), handling); 235*906afcb8SAndy Fiddaman pause(); 236*906afcb8SAndy Fiddaman error(PANIC, "signal not redelivered"); 237*906afcb8SAndy Fiddaman } 238*906afcb8SAndy Fiddaman handling = sig; 239*906afcb8SAndy Fiddaman sfprintf(sfstderr, "\n%s - io stack trace\n", s); 240*906afcb8SAndy Fiddaman for (p = pp.in; p->prev; p = p->prev) 241*906afcb8SAndy Fiddaman { 242*906afcb8SAndy Fiddaman sfprintf(sfstderr, "\n[%s]\n", ppinstr(p)); 243*906afcb8SAndy Fiddaman if ((s = pp.in->nextchr) && *s) 244*906afcb8SAndy Fiddaman { 245*906afcb8SAndy Fiddaman if (*s != '\n') sfputc(sfstderr, '\t'); 246*906afcb8SAndy Fiddaman x = s + 256; 247*906afcb8SAndy Fiddaman while (*s && s < x) 248*906afcb8SAndy Fiddaman { 249*906afcb8SAndy Fiddaman sfputc(sfstderr, *s); 250*906afcb8SAndy Fiddaman if (*s++ == '\n' && *s && *s != '\n') sfputc(sfstderr, '\t'); 251*906afcb8SAndy Fiddaman } 252*906afcb8SAndy Fiddaman if (*s) sfprintf(sfstderr, " ..."); 253*906afcb8SAndy Fiddaman } 254*906afcb8SAndy Fiddaman } 255*906afcb8SAndy Fiddaman sfprintf(sfstderr, "\n"); 256*906afcb8SAndy Fiddaman handling = 0; 257*906afcb8SAndy Fiddaman signal(sig, SIG_DFL); 258*906afcb8SAndy Fiddaman sigunblock(sig); 259*906afcb8SAndy Fiddaman kill(getpid(), sig); 260*906afcb8SAndy Fiddaman pause(); 261*906afcb8SAndy Fiddaman error(PANIC, "signal not redelivered"); 262*906afcb8SAndy Fiddaman } 263*906afcb8SAndy Fiddaman 264*906afcb8SAndy Fiddaman #endif 265