Blob


1 /*
2 *
3 * debugger
4 *
5 */
7 #include "defs.h"
8 #include "fns.h"
10 void
11 scanform(long icount, int prt, char *ifp, Map *map, int literal)
12 {
13 char *fp;
14 char c;
15 int fcount;
16 ADDR savdot;
17 int firstpass;
19 firstpass = 1;
20 while (icount) {
21 fp=ifp;
22 savdot=dot;
23 /*now loop over format*/
24 while (*fp) {
25 if (!isdigit((uchar)*fp))
26 fcount = 1;
27 else {
28 fcount = 0;
29 while (isdigit((uchar)(c = *fp++))) {
30 fcount *= 10;
31 fcount += c-'0';
32 }
33 fp--;
34 }
35 if (*fp==0)
36 break;
37 fp=exform(fcount,prt,fp,map,literal,firstpass);
38 firstpass = 0;
39 }
40 dotinc=dot-savdot;
41 dot=savdot;
42 if (--icount)
43 dot=inkdot(dotinc);
44 }
45 }
47 char *
48 exform(int fcount, int prt, char *ifp, Map *map, int literal, int firstpass)
49 {
50 /* execute single format item `fcount' times
51 * sets `dotinc' and moves `dot'
52 * returns address of next format item
53 */
54 vlong v;
55 WORD w;
56 ulong savdot;
57 u16int u2;
58 u32int u4;
59 u64int u8;
60 char *fp;
61 char c, modifier;
62 int i;
63 ushort sh, *sp;
64 uchar ch, *cp;
65 Symbol s;
66 char buf[512];
67 extern int printcol;
69 fp = 0;
70 while (fcount > 0) {
71 fp = ifp;
72 c = *fp;
73 modifier = *fp++;
74 if (firstpass) {
75 firstpass = 0;
76 if (!literal && (c == 'i' || c == 'I' || c == 'M')
77 && (dot & (mach->pcquant-1))) {
78 dprint("warning: instruction not aligned");
79 printc('\n');
80 }
81 if (prt && modifier != 'a' && modifier != 'A') {
82 symoff(buf, 512, dot, CANY);
83 dprint("%s%c%16t", buf, map==symmap? '?':'/');
84 }
85 }
86 if (printcol==0 && modifier != 'a' && modifier != 'A')
87 dprint("\t\t");
88 switch(modifier) {
90 case SPC:
91 case TB:
92 dotinc = 0;
93 break;
95 case 't':
96 case 'T':
97 dprint("%*t", fcount);
98 dotinc = 0;
99 return(fp);
101 case 'a':
102 symoff(buf, sizeof(buf), dot, CANY);
103 dprint("%s%c%16t", buf, map==symmap? '?':'/');
104 dotinc = 0;
105 break;
107 case 'A':
108 dprint("%#lux%10t", dot);
109 dotinc = 0;
110 break;
112 case 'p':
113 if (get4(map, dot, &u4) < 0)
114 error("%r");
115 w = u4;
116 symoff(buf, sizeof(buf), w, CANY);
117 dprint("%s%16t", buf);
118 dotinc = mach->szaddr;
119 break;
121 case 'u':
122 case 'd':
123 case 'x':
124 case 'o':
125 case 'q':
126 if (literal)
127 u2 = (ushort) dot;
128 else if (get2(map, dot, &u2) < 0)
129 error("%r");
130 w = u2;
131 dotinc = 2;
132 if (c == 'u')
133 dprint("%-8lud", w);
134 else if (c == 'x')
135 dprint("%-8#lux", w);
136 else if (c == 'd')
137 dprint("%-8ld", w);
138 else if (c == 'o')
139 dprint("%-8#luo", w);
140 else if (c == 'q')
141 dprint("%-8#lo", w);
142 break;
144 case 'U':
145 case 'D':
146 case 'X':
147 case 'O':
148 case 'Q':
149 if (literal)
150 u4 = (long) dot;
151 else if (get4(map, dot, &u4) < 0)
152 error("%r");
153 dotinc = 4;
154 if (c == 'U')
155 dprint("%-16lud", u4);
156 else if (c == 'X')
157 dprint("%-16#lux", u4);
158 else if (c == 'D')
159 dprint("%-16ld", u4);
160 else if (c == 'O')
161 dprint("%-#16luo", u4);
162 else if (c == 'Q')
163 dprint("%-#16lo", u4);
164 break;
165 case 'Z':
166 case 'V':
167 case 'Y':
168 if (literal)
169 v = dot;
170 else if (get8(map, dot, &u8) < 0)
171 error("%r");
172 dotinc = 8;
173 if (c == 'Y')
174 dprint("%-20#llux", u8);
175 else if (c == 'V')
176 dprint("%-20lld", u8);
177 else if (c == 'Z')
178 dprint("%-20llud", u8);
179 break;
180 case 'B':
181 case 'b':
182 case 'c':
183 case 'C':
184 if (literal)
185 ch = (uchar) dot;
186 else if (get1(map, dot, &ch, 1) < 0)
187 error("%r");
188 if (modifier == 'C')
189 printesc(ch);
190 else if (modifier == 'B' || modifier == 'b')
191 dprint("%-8#lux", (long) ch);
192 else
193 printc(ch);
194 dotinc = 1;
195 break;
197 case 'r':
198 if (literal)
199 sh = (ushort) dot;
200 else if (get2(map, dot, &sh) < 0)
201 error("%r");
202 dprint("%C", sh);
203 dotinc = 2;
204 break;
206 case 'R':
207 if (literal) {
208 sp = (u16int*)(void*)&dot;
209 dprint("%C%C", sp[0], sp[1]);
210 endline();
211 dotinc = 4;
212 break;
214 savdot=dot;
215 while ((i = get2(map, dot, &u2) > 0) && u2) {
216 dot=inkdot(2);
217 dprint("%C", u2);
218 endline();
220 if (i < 0)
221 error("%r");
222 dotinc = dot-savdot+2;
223 dot=savdot;
224 break;
226 case 's':
227 if (literal) {
228 cp = (uchar*)(void*)&dot;
229 for (i = 0; i < 4; i++)
230 buf[i] = cp[i];
231 buf[i] = 0;
232 dprint("%s", buf);
233 endline();
234 dotinc = 4;
235 break;
237 savdot = dot;
238 for(;;){
239 i = 0;
240 do{
241 if (get1(map, dot, (uchar*)(void*)&buf[i], 1) < 0)
242 error("%r");
243 dot = inkdot(1);
244 i++;
245 }while(!fullrune(buf, i));
246 if(buf[0] == 0)
247 break;
248 buf[i] = 0;
249 dprint("%s", buf);
250 endline();
252 dotinc = dot-savdot+1;
253 dot = savdot;
254 break;
256 case 'S':
257 if (literal) {
258 cp = (uchar*) &dot;
259 for (i = 0; i < 4; i++)
260 printesc(cp[i]);
261 endline();
262 dotinc = 4;
263 break;
265 savdot=dot;
266 while ((i = get1(map, dot, &ch, 1) > 0) && ch) {
267 dot=inkdot(1);
268 printesc(ch);
269 endline();
271 if (i < 0)
272 error("%r");
273 dotinc = dot-savdot+1;
274 dot=savdot;
275 break;
278 case 'I':
279 case 'i':
280 dotinc = mach->das(map, dot, modifier, buf, sizeof(buf));
281 if (dotinc < 0)
282 error("%r");
283 dprint("%s\n", buf);
284 break;
286 case 'M':
287 dotinc = mach->hexinst(map, dot, buf, sizeof(buf));
288 if (dotinc < 0)
289 error("%r");
290 dprint("%s", buf);
291 if (*fp) {
292 dotinc = 0;
293 dprint("%48t");
294 } else
295 dprint("\n");
296 break;
298 case 'f':
299 /* BUG: 'f' and 'F' assume szdouble is sizeof(vlong) in the literal case */
300 if (literal) {
301 v = mach->swap8((ulong)dot);
302 memmove(buf, &v, mach->szfloat);
303 }else if (get1(map, dot, (uchar*)buf, mach->szfloat) < 0)
304 error("%r");
305 mach->ftoa32(buf, sizeof(buf), (void*) buf);
306 dprint("%s\n", buf);
307 dotinc = mach->szfloat;
308 break;
310 case 'F':
311 /* BUG: 'f' and 'F' assume szdouble is sizeof(vlong) in the literal case */
312 if (literal) {
313 v = mach->swap8(dot);
314 memmove(buf, &v, mach->szdouble);
315 }else if (get1(map, dot, (uchar*)buf, mach->szdouble) < 0)
316 error("%r");
317 mach->ftoa64(buf, sizeof(buf), (void*) buf);
318 dprint("%s\n", buf);
319 dotinc = mach->szdouble;
320 break;
322 case 'n':
323 case 'N':
324 printc('\n');
325 dotinc=0;
326 break;
328 case '"':
329 dotinc=0;
330 while (*fp != '"' && *fp)
331 printc(*fp++);
332 if (*fp)
333 fp++;
334 break;
336 case '^':
337 dot=inkdot(-dotinc*fcount);
338 return(fp);
340 case '+':
341 dot=inkdot((WORD)fcount);
342 return(fp);
344 case '-':
345 dot=inkdot(-(WORD)fcount);
346 return(fp);
348 case 'z':
349 if (findsym(locaddr(dot), CTEXT, &s) >= 0)
350 dprint("%s() ", s.name);
351 printsource(dot);
352 printc(EOR);
353 return fp;
355 default:
356 error("bad modifier");
358 if (map->seg[0].fd >= 0)
359 dot=inkdot(dotinc);
360 fcount--;
361 endline();
364 return(fp);
367 void
368 printesc(int c)
370 static char hex[] = "0123456789abcdef";
372 if (c < SPC || c >= 0177)
373 dprint("\\x%c%c", hex[(c&0xF0)>>4], hex[c&0xF]);
374 else
375 printc(c);
378 ADDR
379 inkdot(WORD incr)
381 ADDR newdot;
383 newdot=dot+incr;
384 if ((incr >= 0 && newdot < dot)
385 || (incr < 0 && newdot > dot))
386 error("address wraparound");
387 return(newdot);