dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libpp / common / pptrace.c
blobd46e50e23d6ad4092171b02eb7a8939bc462117c
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1986-2009 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * *
19 ***********************************************************************/
20 #pragma prototyped
22 * Glenn Fowler
23 * AT&T Research
25 * preprocessor library trace and debug support
28 #include "pplib.h"
29 #include "ppfsm.h"
31 #include <ctype.h>
34 * convert token string to printable form
37 char*
38 pptokstr(register char* s, register int c)
40 register char* t;
42 static char buf[8];
44 if (t = s)
46 while (*t == ' ' || *t == '\t') t++;
47 c = *t ? *t : *s;
49 switch (c)
51 case 0:
52 case 0400:
53 return("`EOF'");
54 case ' ':
55 return("`space'");
56 case '\f':
57 return("`formfeed'");
58 case '\n':
59 return("`newline'");
60 case '\t':
61 return("`tab'");
62 case '\v':
63 return("`vertical-tab'");
64 case T_TOKCAT:
65 return("##");
66 default:
67 if (iscntrl(c) || !isprint(c)) sfsprintf(buf, sizeof(buf), "`%03o'", c);
68 else if (s) return(s);
69 else sfsprintf(buf, sizeof(buf), "%c", c);
70 return(buf);
74 #if DEBUG & TRACE_debug
76 #include "ppdebug.h"
79 * return input stream name given index
82 char*
83 ppinstr(register struct ppinstk* p)
85 register int i;
87 static char buf[128];
89 for (i = 0; i < elementsof(ppinmap); i++)
90 if (p->type == ppinmap[i].val)
92 switch (p->type)
94 case IN_MACRO:
95 #if MACDEF
96 case IN_MULTILINE:
97 #endif
98 if (p->symbol)
100 sfsprintf(buf, sizeof(buf), "%s=%s", ppinmap[i].nam, p->symbol->name);
101 return(buf);
103 break;
105 return(ppinmap[i].nam);
107 sfsprintf(buf, sizeof(buf), "UNKNOWN[%d]", p->type);
108 return(buf);
112 * return string given fsm lex state
115 char*
116 pplexstr(register int lex)
118 register int i;
119 int splice;
120 static char buf[64];
122 if (lex < 0) lex &= ~lex;
123 splice = (lex & SPLICE);
124 lex &= 0x7f;
125 for (i = 0; i < (elementsof(pplexmap) - 1) && (lex > pplexmap[i].val || lex == pplexmap[i+1].val); i++);
126 if (lex != pplexmap[i].val)
128 if (pplexmap[i].val < 0) sfsprintf(buf, sizeof(buf), "%s|0x%04x%s", pplexmap[i].nam, lex, splice ? "|SPLICE" : "");
129 else sfsprintf(buf, sizeof(buf), "%s+%d", pplexmap[i-1].nam, lex - pplexmap[i-1].val, splice ? "|SPLICE" : "");
130 return(buf);
132 if (splice)
134 sfsprintf(buf, sizeof(buf), "%s|SPLICE", pplexmap[i].nam);
135 return(buf);
137 return(pplexmap[i].nam);
141 * return string given map p of size n and flags
144 static char*
145 ppflagstr(register struct map* p, int n, register long flags)
147 register int i;
148 register int k;
149 register char* s;
151 static char buf[128];
153 s = buf;
154 for (i = 0; i < n; i++)
155 if (flags & p[i].val)
157 k = strlen(p[i].nam);
158 if ((elementsof(buf) - 2 - (s - buf)) > k)
160 if (s > buf) *s++ = '|';
161 strcpy(s, p[i].nam);
162 s += k;
165 *s = 0;
166 return(buf);
170 * return string given pp.mode
173 char*
174 ppmodestr(register long mode)
176 return(ppflagstr(ppmodemap, elementsof(ppmodemap), mode));
180 * return string given pp.option
183 char*
184 ppoptionstr(register long option)
186 return(ppflagstr(ppoptionmap, elementsof(ppoptionmap), option));
190 * return string given pp.state
193 char*
194 ppstatestr(register long state)
196 return(ppflagstr(ppstatemap, elementsof(ppstatemap), state));
199 #include <sig.h>
202 * io stream stack trace
203 * sig==0 registers the handler
206 void
207 pptrace(int sig)
209 register char* s;
210 register char* x;
211 register struct ppinstk* p;
212 static int handling;
214 if (!sig)
216 #ifdef SIGBUS
217 signal(SIGBUS, pptrace);
218 #endif
219 #ifdef SIGSEGV
220 signal(SIGSEGV, pptrace);
221 #endif
222 #ifdef SIGILL
223 signal(SIGILL, pptrace);
224 #endif
225 signal(SIGQUIT, pptrace);
226 return;
228 s = fmtsignal(sig);
229 if (handling)
231 sfprintf(sfstderr, "\n%s during io stack trace\n", s);
232 signal(handling, SIG_DFL);
233 sigunblock(handling);
234 kill(getpid(), handling);
235 pause();
236 error(PANIC, "signal not redelivered");
238 handling = sig;
239 sfprintf(sfstderr, "\n%s - io stack trace\n", s);
240 for (p = pp.in; p->prev; p = p->prev)
242 sfprintf(sfstderr, "\n[%s]\n", ppinstr(p));
243 if ((s = pp.in->nextchr) && *s)
245 if (*s != '\n') sfputc(sfstderr, '\t');
246 x = s + 256;
247 while (*s && s < x)
249 sfputc(sfstderr, *s);
250 if (*s++ == '\n' && *s && *s != '\n') sfputc(sfstderr, '\t');
252 if (*s) sfprintf(sfstderr, " ...");
255 sfprintf(sfstderr, "\n");
256 handling = 0;
257 signal(sig, SIG_DFL);
258 sigunblock(sig);
259 kill(getpid(), sig);
260 pause();
261 error(PANIC, "signal not redelivered");
264 #endif