Debian Codegraph @

terps/tads/tads2/dbg.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/DBG.C,v 1.3 1999/05/29 15:51:02 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1992, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 dbg.c - debugger functions
15 Function
16 Implements those debugger functions independent of user interface.
17 A separate module handles the user interface, so that the debugger's
18 interface can be replaced easily.
19 Notes
20 none
21 Modified
22 03/28/92 MJRoberts - creation
23 */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include "os.h"
30 #include "dbg.h"
31 #include "tio.h"
32 #include "obj.h"
33 #include "prp.h"
34 #include "tok.h"
35 #include "run.h"
36 #include "tok.h"
37 #include "prs.h"
38 #include "lst.h"
39
40
41 /* add a string to the history buffer */
42 void dbgaddhist(dbgcxdef *ctx, char *buf, int l)
43 {
44 char *p;
45 int dell;
46
47 if (ctx->dbgcxhstf + l + 1 >= ctx->dbgcxhstl)
48 {
49 /* delete first lines until we have enough space */
50 for (dell = 0, p = ctx->dbgcxhstp ; *p || dell < l ; ++p, ++dell) ;
51 if (*p) ++p;
52 memmove(ctx->dbgcxhstp, ctx->dbgcxhstp + dell,
53 (size_t)(ctx->dbgcxhstf - dell));
54 ctx->dbgcxhstf -= dell;
55 }
56 memcpy(ctx->dbgcxhstp + ctx->dbgcxhstf, buf, (size_t)l);
57 ctx->dbgcxhstf += l;
58 }
59
60 /* callback for dbgent - saves history line to a char buffer */
61 static void dbgent1(void *ctx0, const char *str, int strl)
62 {
63 char **ctx = (char **)ctx0;
64 memcpy(*ctx, str, (size_t)strl);
65 *ctx += strl;
66 }
67
68 void dbgent(dbgcxdef *ctx, runsdef *bp, objnum self, objnum target,
69 prpnum prop, int binum, int argc)
70 {
71 dbgfdef *p;
72
73 ++(ctx->dbgcxdep); /* increment actual depth */
74 if (ctx->dbgcxfcn == DBGMAXFRAME)
75 {
76 --(ctx->dbgcxfcn); /* back to top frame */
77 memmove(ctx->dbgcxfrm, ctx->dbgcxfrm + 1,
78 (size_t)((DBGMAXFRAME - 1) * sizeof(dbgfdef)));
79 }
80 p = &ctx->dbgcxfrm[ctx->dbgcxfcn];
81 ++(ctx->dbgcxfcn); /* increment frame pointer */
82
83 p->dbgfbp = bp;
84 p->dbgfself = self;
85 p->dbgftarg = target;
86 p->dbgfprop = prop;
87 p->dbgfbif = binum;
88 p->dbgfargc = argc;
89 p->dbgffr = 0; /* no frame has yet been recorded */
90 p->dbgflin = 0;
91
92 /* save call history */
93 if (ctx->dbgcxflg & DBGCXFTRC)
94 {
95 char buf[128];
96 char *p;
97 int l;
98
99 p = buf;
100 dbgstktr(ctx, dbgent1, &p, -1, TRUE, FALSE);
101 if ((l = (p - buf)) > 0 && buf[l-1] == '\n') --l;
102 buf[l++] = '\0';
103 dbgaddhist(ctx, buf, l);
104 }
105 }
106
107 void dbglv(dbgcxdef *ctx, int exittype)
108 {
109 --(ctx->dbgcxdep); /* decrement actual depth */
110 if (ctx->dbgcxfcn) --(ctx->dbgcxfcn); /* decrement frame pointer */
111
112 /*
113 * if we're in STEP OUT/OVER mode, and the target context is level
114 * 0, and we're now at level 0, it means that we are stepping out of
115 * a routine called directly by the system and the debugger is
116 * supposed to break when that happens -- return to single-stepping
117 * mode so that we break into the debugger the next time the system
118 * calls a method
119 */
120 if ((ctx->dbgcxflg & DBGCXFSS) != 0
121 && (ctx->dbgcxflg & DBGCXFSO) != 0
122 && ctx->dbgcxsof == 0 && ctx->dbgcxdep == 0)
123 {
124 /*
125 * stepping out/over at level 0 - go to normal single-step mode
126 * (clear the out/over flag)
127 */
128 ctx->dbgcxflg &= ~DBGCXFSO;
129 }
130
131 /* record exit in call history if appropriate */
132 if (ctx->dbgcxflg & DBGCXFTRC)
133 {
134 char buf[128];
135 char *p;
136
137 switch(exittype)
138 {
139 case DBGEXVAL:
140 if (ctx->dbgcxfcn > 1)
141 {
142 memset(buf, ' ', (size_t)(ctx->dbgcxfcn - 1));
143 dbgaddhist(ctx, buf, (int)ctx->dbgcxfcn - 1);
144 }
145 memcpy(buf, " => ", (size_t)4);
146 p = buf + 4;
147 dbgpval(ctx, ctx->dbgcxrun->runcxsp - 1,
148 dbgent1, &p, TRUE);
149 *p++ = '\0';
150 dbgaddhist(ctx, buf, (int)(p - buf));
151 break;
152
153 case DBGEXPASS:
154 memcpy(buf, " [pass]", (size_t)8);
155 dbgaddhist(ctx, buf, 8);
156 break;
157 }
158 }
159 }
160
161 /* get a symbol name; returns length of name */
162 int dbgnam(dbgcxdef *ctx, char *outbuf, int typ, int val)
163 {
164 toksdef sym;
165
166 if (!ctx->dbgcxtab || !ctx->dbgcxtab->tokthhsh)
167 {
168 memcpy(outbuf, "<NO SYMBOL TABLE>", (size_t)17);
169 return(17);
170 }
171
172 if (tokthfind((toktdef *)ctx->dbgcxtab, typ, val, &sym))
173 {
174 memcpy(outbuf, sym.toksnam, (size_t)sym.tokslen);
175 return(sym.tokslen);
176 }
177 else if (typ == TOKSTOBJ)
178 {
179 if ((mcmon)val == MCMONINV)
180 {
181 memcpy(outbuf, "<invalid object>", 16);
182 return 16;
183 }
184 else
185 {
186 sprintf(outbuf, "<object#%u>", val);
187 return strlen(outbuf);
188 }
189 }
190 else
191 {
192 memcpy(outbuf, "<UNKNOWN>", (size_t)9);
193 return(9);
194 }
195 }
196
197 /* send a buffer value (as from a list) to ui callback for display */
198 static void dbgpbval(dbgcxdef *ctx, dattyp typ, uchar *val,
199 void (*dispfn)(void *, const char *, int),
200 void *dispctx)
201 {
202 char buf[TOKNAMMAX + 1];
203 char *p = buf;
204 uint len;
205
206 switch(typ)
207 {
208 case DAT_NUMBER:
209 sprintf(buf, "%ld", (long)osrp4s(val));
210 len = strlen(buf);
211 break;
212
213 case DAT_OBJECT:
214 len = dbgnam(ctx, buf, TOKSTOBJ, osrp2(val));
215 break;
216
217 case DAT_SSTRING:
218 len = osrp2(val) - 2;
219 p = (char *)val + 2;
220 break;
221
222 case DAT_NIL:
223 p = "nil";
224 len = 3;
225 break;
226
227 case DAT_LIST:
228 (*dispfn)(dispctx, "[", 1);
229 len = osrp2(val) - 2;
230 p = (char *)val + 2;
231 while (len)
232 {
233 dbgpbval(ctx, (dattyp)*p, (uchar *)(p + 1), dispfn, dispctx);
234 lstadv((uchar **)&p, &len);
235 if (len) (*dispfn)(dispctx, " ", 1);
236 }
237 (*dispfn)(dispctx, "]", 1);
238 len = 0;
239 break;
240
241 case DAT_TRUE:
242 p = "true";
243 len = 4;
244 break;
245
246 case DAT_FNADDR:
247 (*dispfn)(dispctx, "&", 1);
248 len = dbgnam(ctx, buf, TOKSTFUNC, osrp2(val));
249 break;
250
251 case DAT_PROPNUM:
252 (*dispfn)(dispctx, "&", 1);
253 len = dbgnam(ctx, buf, TOKSTPROP, osrp2(val));
254 break;
255
256 default:
257 p = "[unknown type]";
258 len = 14;
259 break;
260 }
261
262 if (typ == DAT_SSTRING) (*dispfn)(dispctx, "'", 1);
263 if (len) (*dispfn)(dispctx, p, len);
264 if (typ == DAT_SSTRING) (*dispfn)(dispctx, "'", 1);
265 }
266
267 /* send a value to ui callback for display */
268 void dbgpval(dbgcxdef *ctx, runsdef *val,
269 void (*dispfn)(void *ctx, const char *str, int strl),
270 void *dispctx,
271 int showtype)
272 {
273 uchar buf[TOKNAMMAX + 1];
274 uint len;
275 uchar *p = buf;
276 char *typ = 0;
277
278 switch(val->runstyp)
279 {
280 case DAT_NUMBER:
281 sprintf((char *)buf, "%ld", val->runsv.runsvnum);
282 len = strlen((char *)buf);
283 typ = "number";
284 break;
285
286 case DAT_OBJECT:
287 len = dbgnam(ctx, (char *)buf, TOKSTOBJ, val->runsv.runsvobj);
288 typ = "object";
289 break;
290
291 case DAT_SSTRING:
292 len = osrp2(val->runsv.runsvstr) - 2;
293 p = val->runsv.runsvstr + 2;
294 typ = "string";
295 break;
296
297 case DAT_NIL:
298 p = (uchar *)"nil";
299 len = 3;
300 break;
301
302 case DAT_LIST:
303 if (showtype) (*dispfn)(dispctx, "list: ", 6);
304 (*dispfn)(dispctx, "[", 1);
305 len = osrp2(val->runsv.runsvstr) - 2;
306 p = val->runsv.runsvstr + 2;
307 while (len)
308 {
309 dbgpbval(ctx, (dattyp)*p, (uchar *)(p + 1), dispfn, dispctx);
310 lstadv(&p, &len);
311 if (len) (*dispfn)(dispctx, " ", 1);
312 }
313 (*dispfn)(dispctx, "]", 1);
314 len = 0;
315 break;
316
317 case DAT_TRUE:
318 p = (uchar *)"true";
319 len = 4;
320 break;
321
322 case DAT_FNADDR:
323 len = dbgnam(ctx, (char *)buf, TOKSTFUNC, val->runsv.runsvobj);
324 typ = "function pointer";
325 break;
326
327 case DAT_PROPNUM:
328 len = dbgnam(ctx, (char *)buf, TOKSTPROP, val->runsv.runsvprp);
329 typ = "property pointer";
330 break;
331
332 default:
333 p = (uchar *)"[unknown type]";
334 len = 14;
335 break;
336 }
337
338 /* show the type prefix if desired, or add a quote if it's a string */
339 if (typ && showtype)
340 {
341 /* show the type prefix */
342 (*dispfn)(dispctx, typ, (int)strlen(typ));
343 (*dispfn)(dispctx, ": ", 2);
344 }
345 else if (val->runstyp == DAT_SSTRING)
346 {
347 /* it's a string, and we're not showing a type - add a quote */
348 (*dispfn)(dispctx, "'", 1);
349 }
350
351 /*
352 * if possible, null-terminate the buffer - do this only if the
353 * length is actually within the buffer, which won't be the case if
354 * the text comes from someplace outside the buffer (which is the
355 * case if it's a string, for example)
356 */
357 if (len < sizeof(buf))
358 buf[len] = '\0';
359
360 /* display a "&" prefix if it's an address of some kind */
361 if (val->runstyp == DAT_PROPNUM || val->runstyp == DAT_FNADDR)
362 (*dispfn)(dispctx, "&", 1);
363
364 /* display the text */
365 if (len != 0)
366 (*dispfn)(dispctx, (char *)p, len);
367
368 /* add a closing quote if it's a string and we showed an open quote */
369 if (val->runstyp == DAT_SSTRING && !(typ && showtype))
370 (*dispfn)(dispctx, "'", 1);
371 }
372
373 void dbgstktr(dbgcxdef *ctx,
374 void (*dispfn)(void *ctx, const char *str, int strl),
375 void *dispctx,
376 int level, int toponly, int include_markers)
377 {
378 dbgfdef *f;
379 int i;
380 int j;
381 int k;
382 char buf[128];
383 char *p;
384 char c;
385
386 for (i = ctx->dbgcxfcn, j = ctx->dbgcxdep, f = &ctx->dbgcxfrm[i-1]
387 ; i ; --f, --j, --i)
388 {
389 p = buf;
390 if (toponly)
391 {
392 int k = (i < 50 ? i : 50);
393
394 if (k > 1)
395 {
396 memset(buf, ' ', (size_t)(k - 1));
397 dbgaddhist(ctx, buf, k-1);
398 }
399 }
400 else if (include_markers)
401 {
402 c = (i == level + 1 ? '*' : ' ');
403 sprintf(buf, "%3d%c ", j, c);
404 p += 4;
405 }
406
407 if (f->dbgftarg == MCMONINV)
408 p += dbgnam(ctx, p, TOKSTBIFN, f->dbgfbif);
409 else
410 p += dbgnam(ctx, p,
411 (f->dbgfself == MCMONINV ? TOKSTFUNC : TOKSTOBJ),
412 (int)f->dbgftarg);
413
414 if (f->dbgfself != MCMONINV && f->dbgfself != f->dbgftarg)
415 {
416 memcpy(p, "<self=", (size_t)6);
417 p += 6;
418 p += dbgnam(ctx, p, TOKSTOBJ, (int)f->dbgfself);
419 *p++ = '>';
420 }
421 if (f->dbgfprop)
422 {
423 *p++ = '.';
424 p += dbgnam(ctx, p, TOKSTPROP, (int)f->dbgfprop);
425 }
426
427 /* display what we have so far */
428 *p++ = '\0';
429 (*dispfn)(dispctx, buf, (int)strlen(buf));
430
431 /* display arguments if there are any */
432 if (f->dbgfself == MCMONINV || f->dbgfargc != 0)
433 {
434 (*dispfn)(dispctx, "(", 1);
435 for (k = 0 ; k < f->dbgfargc ; ++k)
436 {
437 dbgpval(ctx, f->dbgfbp - k - 2, dispfn, dispctx, FALSE);
438 if (k + 1 < f->dbgfargc) (*dispfn)(dispctx, ", ", 2);
439 }
440 (*dispfn)(dispctx, ")", 1);
441 }
442
443 /* send out a newline, then move on to next frame */
444 (*dispfn)(dispctx, "\n", 1);
445
446 /* we're done if doing one function only */
447 if (toponly) break;
448 }
449 }
450
451 static void dbgdsdisp(void *ctx, const char *buf, int bufl)
452 {
453 if (buf[0] == '\n')
454 tioflush((tiocxdef *)ctx);
455 else
456 tioputslen((tiocxdef *)ctx, (char *)buf, bufl);
457 }
458
459 /* dump the stack */
460 void dbgds(dbgcxdef *ctx)
461 {
462 /* don't do stack dumps if we're running from the debugger command line */
463 if (ctx->dbgcxflg & DBGCXFIND) return;
464
465 tioflush(ctx->dbgcxtio);
466 tioshow(ctx->dbgcxtio);
467 dbgstktr(ctx, dbgdsdisp, ctx->dbgcxtio, -1, FALSE, TRUE);
468 tioflush(ctx->dbgcxtio);
469 ctx->dbgcxfcn = ctx->dbgcxdep = 0;
470 }
471
472 /* get information about the currently executing source line */
473 void dbglget(dbgcxdef *ctx, uchar *buf)
474 {
475 dbglgetlvl(ctx, buf, 0);
476 }
477
478 /*
479 * Get information about a source line at a particular stack level into
480 * the buffer; leaves out frame info. level 0 is the currently
481 * executing line, 1 is the first enclosing level, and so on.
482 */
483 int dbglgetlvl(dbgcxdef *ctx, uchar *buf, int level)
484 {
485 uchar *linrec;
486 uchar *obj;
487 dbgfdef *fr;
488
489 /* make sure the level is valid */
490 if (level > ctx->dbgcxfcn - 1)
491 return 1;
492
493 /* get the frame at the given level */
494 fr = &ctx->dbgcxfrm[ctx->dbgcxfcn - level - 1];
495
496 /* if we're in an intrinsic, go to enclosing frame */
497 if (fr->dbgftarg == MCMONINV) --fr;
498
499 /* make sure we've encountered an OPCLINE in this frame */
500 if (fr->dbgflin == 0)
501 return 1;
502
503 /* we need to read from the target object - lock it */
504 obj = mcmlck(ctx->dbgcxmem, (mcmon)fr->dbgftarg);
505
506 linrec = obj + fr->dbgflin;
507 memcpy(buf, linrec + 3, (size_t)(*linrec - 3));
508
509 /* no longer need the target object locked */
510 mcmunlck(ctx->dbgcxmem, (mcmon)fr->dbgftarg);
511
512 /* success */
513 return 0;
514 }
515
516 /* tell the line source the location of the current line being compiled */
517 void dbgclin(tokcxdef *tokctx, objnum objn, uint ofs)
518 {
519 uchar buf[4];
520
521 /* package the information and send it to the line source */
522 oswp2(buf, objn);
523 oswp2(buf + 2, ofs);
524 lincmpinf(tokctx->tokcxlin, buf);
525 }
526