Debian Codegraph @

terps/tads/tads2/dbgrun.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/DBGRUN.C,v 1.3 1999/07/11 00:46:29 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 dbgrun.c - functions needed when actually debugging
15 Function
16 This module contains functions actually used while debugging, as
17 opposed to functions needed in the compiler and/or runtime to set
18 up debugging without activating an interactive debugger session.
19 These functions are in a separate file to reduce the size of the
20 runtime and compiler, which don't need to link these functions.
21 Notes
22 None
23 Modified
24 04/11/99 CNebel - Fix signing errors.
25 04/20/92 MJRoberts - creation
26 */
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include "os.h"
33 #include "dbg.h"
34 #include "tio.h"
35 #include "obj.h"
36 #include "prp.h"
37 #include "tok.h"
38 #include "run.h"
39 #include "tok.h"
40 #include "prs.h"
41 #include "linf.h"
42 #include "bif.h"
43 #include "lst.h"
44
45 /* indicate that the debugger is present */
46 int dbgpresent()
47 {
48 return TRUE;
49 }
50
51 /* format a display of where execution is stopped */
52 void dbgwhere(dbgcxdef *ctx, char *p)
53 {
54 dbgfdef *f;
55
56 f = &ctx->dbgcxfrm[ctx->dbgcxfcn - 1];
57 if (f->dbgftarg == MCMONINV)
58 p += dbgnam(ctx, p, TOKSTBIFN, f->dbgfbif);
59 else
60 p += dbgnam(ctx, p,
61 (f->dbgfself == MCMONINV ? TOKSTFUNC : TOKSTOBJ),
62 (int)f->dbgftarg);
63
64 if (f->dbgfself != MCMONINV && f->dbgfself != f->dbgftarg)
65 {
66 memcpy(p, "<self=", (size_t)6);
67 p += 6;
68 p += dbgnam(ctx, p, TOKSTOBJ, (int)f->dbgfself);
69 *p++ = '>';
70 }
71 if (f->dbgfprop)
72 {
73 *p++ = '.';
74 p += dbgnam(ctx, p, TOKSTPROP, (int)f->dbgfprop);
75 }
76 if (f->dbgfself == MCMONINV || f->dbgfbif)
77 {
78 *p++ = '(';
79 *p++ = ')';
80 }
81 *p = '\0';
82 }
83
84 /*
85 * Evaluate breakpoint condition; returns TRUE if breakpoint condition
86 * was non-nil, FALSE if it was nil or had no value.
87 */
88 static int dbgbpeval(dbgcxdef *ctx, dbgbpdef *bp, dbgfdef *fr)
89 {
90 uchar *objptr;
91 runsdef *oldsp;
92 runsdef val;
93 int err;
94
95 objptr = mcmlck(ctx->dbgcxmem, bp->dbgbpcond);
96
97 ERRBEGIN(ctx->dbgcxerr)
98
99 oldsp = ctx->dbgcxrun->runcxsp;
100 runexe(ctx->dbgcxrun, objptr, fr->dbgfself, bp->dbgbpcond,
101 (prpnum)0, 0);
102
103 ERRCATCH(ctx->dbgcxerr, err)
104 /* no error recover - just proceed as normal */
105 ;
106 ERREND(ctx->dbgcxerr)
107
108 /* done with condition code; unlock it */
109 mcmunlck(ctx->dbgcxmem, bp->dbgbpcond);
110
111 /* if condition evaluated to non-nil, break now */
112 if (ctx->dbgcxrun->runcxsp != oldsp)
113 {
114 runpop(ctx->dbgcxrun, &val);
115 if (val.runstyp == DAT_NIL)
116 return FALSE; /* value was nil - condition failed */
117 else
118 return TRUE; /* value was non-nil - condition met */
119 }
120 else
121 return FALSE; /* expression returned no value - treat as nil */
122 }
123
124 /* single-step interrupt */
125 void dbgss(dbgcxdef *ctx, uint ofs, int instr, int err,
126 uchar *noreg *instrp)
127 {
128 dbgbpdef *bp;
129 int i;
130 dbgfdef *fr;
131 int bphit = 0;
132 voccxdef *vcx = ctx->dbgcxrun->runcxvoc;
133 unsigned int exec_ofs;
134 errdef *inner_frame;
135 int stepping;
136
137 /* don't re-enter debugger - return if running from tdb */
138 if (ctx->dbgcxflg & DBGCXFIND)
139 return;
140
141 /*
142 * If the current debugger stack context is in a built-in function,
143 * and we can resume from an error, pop a level of context -- we
144 * always show the context inside of the p-code, because we want to
145 * be able to move the execution point before resuming execution.
146 * If we can't resume from an error, leave the stack as it is so
147 * that the user can see exactly which built-in function was called
148 * to cause the error.
149 */
150 if (dbgu_err_resume(ctx))
151 {
152 /* remove any built-in functions from the top of the stack */
153 while (ctx->dbgcxfcn > 1
154 && ctx->dbgcxfrm[ctx->dbgcxfcn-1].dbgftarg == MCMONINV)
155 ctx->dbgcxfcn--;
156 }
157
158 /* get the frame pointer */
159 fr = (ctx->dbgcxfcn == 0 ? 0 : &ctx->dbgcxfrm[ctx->dbgcxfcn - 1]);
160
161 /* presume we're not single-stepping */
162 stepping = FALSE;
163
164 /*
165 * Check to see if we should stop due to single-stepping; if so,
166 * there's no need to consider breakpoints at all.
167 */
168 if ((ctx->dbgcxflg & DBGCXFSS) != 0)
169 {
170 /* single-step mode - check for step-over mode */
171 if ((ctx->dbgcxflg & DBGCXFSO) != 0)
172 {
173 /*
174 * step-over mode - stop only if we're in the stack level
175 * where we started step-over mode (not in a routine called
176 * from that stack level, which is what we're stepping over)
177 */
178 stepping = (ctx->dbgcxdep <= ctx->dbgcxsof);
179 }
180 else
181 {
182 /* normal step-in mode - always stop */
183 stepping = TRUE;
184 }
185 }
186
187 /*
188 * if we're not going to stop for single-stepping or because of a
189 * run-time error, look for breakpoints
190 */
191 if (!stepping && err == 0)
192 {
193 /* check the instruction that cause debugger entry */
194 if (instr == OPCBP)
195 {
196 /* look up breakpoint, and make sure 'self' is correct */
197 for (i = 0, bp = ctx->dbgcxbp ; i < DBGBPMAX ; ++bp, ++i)
198 {
199 if (fr != 0
200 && (bp->dbgbpflg & DBGBPFUSED)
201 && !(bp->dbgbpflg & DBGBPFDISA)
202 && (bp->dbgbpself == fr->dbgfself
203 || bp->dbgbpself == MCMONINV
204 || bifinh(vcx, vocinh(vcx, fr->dbgfself),
205 bp->dbgbpself))
206 && bp->dbgbptarg == fr->dbgftarg
207 && bp->dbgbpofs + 3 == ofs)
208 break;
209 }
210
211 /* if we didn't find the breakpoint, ignore the stop */
212 if (i == DBGBPMAX)
213 return;
214
215 /* if this is a conditional bp, make sure condition is true */
216 if (!(ctx->dbgcxflg & DBGCXFSS) && (bp->dbgbpflg & DBGBPFCOND)
217 && !dbgbpeval(ctx, bp, fr))
218 return;
219
220 /* note that we hit a breakpoint */
221 bphit = i + 1;
222 }
223 else
224 {
225 int brk;
226
227 /* check for global breakpoints */
228 if (fr != 0 && (ctx->dbgcxflg & DBGCXFGBP) != 0)
229 {
230 /*
231 * Not single-stepping, but one or more global
232 * breakpoints are in effect. for each global
233 * breakpoint, eval condition; if any are true, stop
234 * execution, otherwise resume execution.
235 */
236 for (brk = FALSE, i = 0, bp = ctx->dbgcxbp ;
237 i < DBGBPMAX ; ++bp, ++i)
238 {
239 if ((bp->dbgbpflg & DBGBPFUSED)
240 && !(bp->dbgbpflg & DBGBPFDISA)
241 && bp->dbgbptarg == MCMONINV
242 && dbgbpeval(ctx, bp, fr))
243 {
244 brk = TRUE;
245 bp->dbgbpflg |= DBGBPFDISA; /* auto disable */
246 bphit = i + 1;
247 break;
248 }
249 }
250 }
251 else
252 {
253 /* we're not at a global breakpoint */
254 brk = FALSE;
255 }
256
257 /* if there's no breakpoint, don't stop */
258 if (!brk)
259 return;
260 }
261 }
262
263 /* note that we're in the debugger, to avoid re-entry */
264 ctx->dbgcxflg |= DBGCXFIND;
265
266 /*
267 * unlock the object, in case we mess with it in the debugger;
268 * before we do, though, remember the current execution offset
269 * within the object, so that we can restore the pointer before we
270 * return
271 */
272 if (instrp != 0 && fr != 0 && fr->dbgftarg != MCMONINV)
273 {
274 exec_ofs = *instrp - mcmobjptr(ctx->dbgcxmem, (mcmon)fr->dbgftarg);
275 mcmunlck(ctx->dbgcxmem, (mcmon)fr->dbgftarg);
276 }
277 else
278 exec_ofs = 0;
279
280 /* remember the current error frame, so we can copy its arguments */
281 inner_frame = ctx->dbgcxerr->errcxptr;
282
283 ERRBEGIN(ctx->dbgcxerr)
284 /*
285 * if we're stopping on an error, copy the error parameters from
286 * the inner frame so they'll be available for messages within
287 * this new enclosing frame
288 */
289 if (err != 0)
290 errcopyargs(ctx->dbgcxerr, inner_frame);
291
292 /* call user-interface main command processor routine */
293 dbgucmd(ctx, bphit, err, &exec_ofs);
294 ERRCLEAN(ctx->dbgcxerr)
295 /* make sure we clear the in-debugger flag if we exit via an error */
296 ctx->dbgcxflg &= ~DBGCXFIND;
297 ERRENDCLN(ctx->dbgcxerr);
298
299 /* relock the object */
300 if (instrp != 0 && fr != 0 && fr->dbgftarg != MCMONINV)
301 *instrp = mcmlck(ctx->dbgcxmem, (mcmon)fr->dbgftarg) + exec_ofs;
302
303 /* we're no longer in the debugger */
304 ctx->dbgcxflg &= ~DBGCXFIND;
305 }
306
307 /* activate debugger; returns TRUE if no debugger is present */
308 int dbgstart(dbgcxdef *ctx)
309 {
310 if (!ctx || !(ctx->dbgcxflg & DBGCXFOK)) return TRUE;
311 ctx->dbgcxflg |= DBGCXFSS; /* activate single-step mode */
312 return FALSE;
313 }
314
315 /* save a text string in the dbgcxname buffer; TRUE ==> can't store it */
316 static int dbgnamsav(dbgcxdef *ctx, char *nam, uint *ofsp)
317 {
318 size_t len = strlen(nam) + 1;
319
320 if (ctx->dbgcxnam
321 && ctx->dbgcxnamf + len < ctx->dbgcxnams)
322 {
323 memcpy(ctx->dbgcxnam + ctx->dbgcxnamf, nam, len);
324 *ofsp = ctx->dbgcxnamf;
325 ctx->dbgcxnamf += len;
326 return FALSE;
327 }
328 else
329 return TRUE; /* uanble to store - return error */
330 }
331
332 /* delete a string from the dbgcxname area, adjusting bp/wx offsets */
333 static void dbgnamdel(dbgcxdef *ctx, uint ofs)
334 {
335 int i;
336 dbgbpdef *bp;
337 dbgwxdef *wx;
338 uint delsiz;
339
340 /* if no name text is being stored, we need do nothing */
341 if (!ctx->dbgcxnam) return;
342
343 /* compute size of area to be deleted */
344 delsiz = strlen(ctx->dbgcxnam + ofs) + 1;
345
346 /* go through breakpoints, moving text if necessary */
347 for (i = DBGBPMAX, bp = ctx->dbgcxbp ; i ; ++bp, --i)
348 {
349 if ((bp->dbgbpflg & DBGBPFUSED) && (bp->dbgbpflg & DBGBPFNAME)
350 && bp->dbgbpnam > ofs)
351 bp->dbgbpnam -= delsiz;
352 if ((bp->dbgbpflg & DBGBPFUSED) && (bp->dbgbpflg & DBGBPFCONDNAME)
353 && bp->dbgbpcondnam > ofs)
354 bp->dbgbpcondnam -= delsiz;
355 }
356
357 /* do the same for the watch expressions */
358 for (i = DBGWXMAX, wx = ctx->dbgcxwx ; i ; ++wx, --i)
359 {
360 if ((wx->dbgwxflg & DBGWXFUSED) && (wx->dbgwxflg & DBGWXFNAME)
361 && wx->dbgwxnam > ofs)
362 wx->dbgwxnam -= delsiz;
363 }
364
365 /* now actually remove the string from the dbgcxname area */
366 if (ctx->dbgcxnamf - ofs - delsiz)
367 memmove(ctx->dbgcxnam + ofs, ctx->dbgcxnam + ofs + delsiz,
368 (size_t)(ctx->dbgcxnamf - ofs - delsiz));
369 ctx->dbgcxnamf -= delsiz;
370 }
371
372 /*
373 * Set a breakpoint at an object + offset location, optionally with a
374 * condition. We'll find a new slot for the breakpoint.
375 */
376 int dbgbpat(dbgcxdef *ctx, objnum target, objnum self,
377 uint ofs, int *bpnum, char *bpname, int toggle, char *cond,
378 int *did_set)
379 {
380 int i;
381 dbgbpdef *bp;
382
383 /* find a slot for the new breakpoint */
384 for (i = 0, bp = ctx->dbgcxbp ; i < DBGBPMAX ; ++bp, ++i)
385 {
386 /* if this one's free, use it */
387 if (!(bp->dbgbpflg & DBGBPFUSED))
388 break;
389 }
390
391 /*
392 * Note that we don't check for a valid slot yet, because we may not
393 * end up setting a new breakpoint.
394 */
395
396 /* tell the caller which breakpoint we set */
397 *bpnum = i + 1;
398
399 /* go set the breakpoint at the ID we allocated */
400 return dbgbpatid(ctx, *bpnum, target, self, ofs,
401 bpname, toggle, cond, did_set);
402 }
403
404 /*
405 * Set a breakpoint at an object + offset location, optionally with a
406 * condition, using an existing breakpoint slot. If the slot is already
407 * in use, we'll return an error.
408 */
409 int dbgbpatid(dbgcxdef *ctx, int bpnum, objnum target, objnum self,
410 uint ofs, char *bpname, int toggle, char *cond,
411 int *did_set)
412 {
413 uchar *objp;
414 int done;
415 dbgbpdef *bp;
416 int err;
417 objnum condobj = MCMONINV;
418
419 /* get the slot */
420 --bpnum;
421 bp = &ctx->dbgcxbp[bpnum];
422
423 /* if this is a global breakpoint, no OPCBP is needed */
424 if (target == MCMONINV)
425 {
426 /* compile the condition, with no local frame */
427 dbgfdef fr;
428
429 /* make sure the slot is valid and not in use */
430 if (bpnum == DBGBPMAX)
431 return ERR_MANYBP;
432 else if ((bp->dbgbpflg & DBGBPFUSED) != 0)
433 return ERR_BPINUSE;
434
435 fr.dbgffr = 0;
436 fr.dbgftarg = MCMONINV;
437 if ((err = dbgcompile(ctx, cond, &fr, &condobj, FALSE)) != 0)
438 return err;
439
440 /* set up the breakpoint record */
441 bp->dbgbpflg = DBGBPFUSED | DBGBPFCOND;
442 bp->dbgbpself =
443 bp->dbgbptarg = MCMONINV;
444 bp->dbgbpofs = 0;
445 bp->dbgbpcond = condobj;
446
447 /* save the name */
448 if (!dbgnamsav(ctx, bpname, &bp->dbgbpnam))
449 bp->dbgbpflg |= DBGBPFNAME;
450
451 /* save the condition */
452 if (!dbgnamsav(ctx, cond, &bp->dbgbpcondnam))
453 bp->dbgbpflg |= DBGBPFCONDNAME;
454
455 /* it's a global breakpoint */
456 ctx->dbgcxflg |= DBGCXFGBP;
457
458 /* success */
459 return 0;
460 }
461
462 /* lock the object */
463 objp = mcmlck(ctx->dbgcxmem, (mcmon)target);
464
465 /* skip any ENTER, CHKARGC, or FRAME instructions */
466 for (done = FALSE ; !done ; )
467 {
468 switch(*(objp + ofs))
469 {
470 case OPCENTER:
471 ofs += 3;
472 break;
473 case OPCCHKARGC:
474 ofs += 2;
475 break;
476 case OPCFRAME:
477 ofs += 1 + osrp2(objp + ofs + 1);
478 break;
479 default:
480 done = TRUE;
481 break;
482 }
483 }
484
485 /* presume we're going to set a new breakpoint */
486 if (did_set != 0)
487 *did_set = TRUE;
488
489 /* see what kind of instruction we've found */
490 if (*(objp + ofs) == OPCBP)
491 {
492 /* if we're toggling and not adding a condition, remove the bp */
493 if (toggle && (cond == 0 || *cond == '\0'))
494 {
495 int i;
496
497 /* tell the caller we're removing a breakpoint */
498 if (did_set != 0)
499 *did_set = FALSE;
500
501 /* remove all breakpoints matching this description */
502 for (i = 0, bp = ctx->dbgcxbp ; i < DBGBPMAX ; ++bp, ++i)
503 {
504 if ((bp->dbgbpflg & DBGBPFUSED) != 0
505 && bp->dbgbptarg == target
506 && bp->dbgbpofs == ofs
507 && bp->dbgbpself == self)
508 {
509 err = dbgbpdel(ctx, i+1);
510 goto return_error;
511 }
512 }
513 }
514
515 /* no error */
516 err = 0;
517 }
518 else if (*(objp + ofs) != OPCLINE)
519 {
520 /* can't set a breakpoint here - it's not a line */
521 err = ERR_BPNOTLN;
522 }
523 else if (bpnum == DBGBPMAX)
524 {
525 /* no slots left for a new breakpoint */
526 err = ERR_MANYBP;
527 }
528 else if ((bp->dbgbpflg & DBGBPFUSED) != 0)
529 {
530 /* slot is already in use */
531 return ERR_BPINUSE;
532 }
533 else
534 {
535 /* presume we won't have an error */
536 err = 0;
537 }
538
539 /* check for a condition attached to the breakpoint */
540 if (cond && *cond)
541 {
542 dbgfdef fr;
543
544 fr.dbgffr = osrp2(objp + ofs + 2);
545 fr.dbgftarg = target;
546 err = dbgcompile(ctx, cond, &fr, &condobj, FALSE);
547 }
548
549 if (err == 0)
550 {
551 /* set up the breakpoint structure */
552 bp->dbgbpflg = DBGBPFUSED;
553 bp->dbgbpself = self;
554 bp->dbgbptarg = target;
555 bp->dbgbpofs = ofs;
556
557 if (cond && *cond)
558 {
559 /* remember the condition */
560 bp->dbgbpcond = condobj;
561 bp->dbgbpflg |= DBGBPFCOND;
562 if (!dbgnamsav(ctx, cond, &bp->dbgbpcondnam))
563 bp->dbgbpflg |= DBGBPFCONDNAME;
564 }
565
566 /*
567 * replace the OPCLINE in the p-code with a breakpoint
568 * instruction, so that the interpreter actually stops when it
569 * reaches this point
570 */
571 *(objp + ofs) = OPCBP;
572 mcmtch(ctx->dbgcxmem, (mcmon)target);
573 }
574
575 return_error:
576 mcmunlck(ctx->dbgcxmem, (mcmon)target);
577
578 /* free condition object if we have one */
579 if (err && condobj != MCMONINV)
580 mcmfre(ctx->dbgcxmem, condobj);
581
582 /* store breakpoint name for displaying to user */
583 if (!err && !dbgnamsav(ctx, bpname, &bp->dbgbpnam))
584 bp->dbgbpflg |= DBGBPFNAME;
585
586 return err;
587 }
588
589 /*
590 * Set a new condition for an existing breakpoint
591 */
592 int dbgbpsetcond(dbgcxdef *ctx, int bpnum, char *cond)
593 {
594 dbgbpdef *bp;
595 int err;
596 objnum condobj;
597 dbgfdef fr;
598
599 /* make sure we have a valid breakpoint */
600 --bpnum;
601 bp = &ctx->dbgcxbp[bpnum];
602 if (bpnum < 0 || bpnum >= DBGBPMAX || !(bp->dbgbpflg & DBGBPFUSED))
603 return ERR_BPNSET;
604
605 /* check to see if it's a global breakpoint */
606 if (bp->dbgbptarg == MCMONINV)
607 {
608 /* it's global - compile the condition with no local frame */
609 fr.dbgffr = 0;
610 fr.dbgftarg = MCMONINV;
611 err = dbgcompile(ctx, cond, &fr, &condobj, FALSE);
612 }
613 else
614 {
615 uchar *objp;
616
617 /* lock the object containing the code containing the breakpoint */
618 objp = mcmlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
619
620 /* compile the condition */
621 fr.dbgffr = osrp2(objp + bp->dbgbpofs + 2);
622 fr.dbgftarg = bp->dbgbptarg;
623 err = dbgcompile(ctx, cond, &fr, &condobj, FALSE);
624
625 /* done with the object */
626 mcmunlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
627 }
628
629 /* don't proceed if we couldn't compile the expression */
630 if (err != 0)
631 return err;
632
633 /* delete the old condition object */
634 if (bp->dbgbpflg & DBGBPFCOND)
635 mcmfre(ctx->dbgcxmem, bp->dbgbpcond);
636
637 /* delete the old condition name */
638 if (bp->dbgbpflg & DBGBPFCONDNAME)
639 {
640 dbgnamdel(ctx, bp->dbgbpcondnam);
641 bp->dbgbpflg &= ~DBGBPFCONDNAME;
642 }
643
644 /* remember the new condition object */
645 bp->dbgbpcond = condobj;
646
647 /* remember the new condition name */
648 if (!dbgnamsav(ctx, cond, &bp->dbgbpcondnam))
649 bp->dbgbpflg |= DBGBPFCONDNAME;
650
651 /* success */
652 return 0;
653 }
654
655 /*
656 * Determine if there's a breakpoint at a given code location. If there
657 * is, we fill in *bpnum with the identifier for the breakpoint and
658 * return true, otherwise we return false.
659 */
660 int dbgisbp(dbgcxdef *ctx, objnum target, objnum self, uint ofs, int *bpnum)
661 {
662 dbgbpdef *bp;
663 int i;
664
665 /* scan the breakpoints for one matching the given description */
666 for (i = 0, bp = ctx->dbgcxbp ; i < DBGBPMAX ; ++bp, ++i)
667 {
668 /* check to see if this one matches the one we're looking for */
669 if ((bp->dbgbpflg & DBGBPFUSED) != 0
670 && bp->dbgbptarg == target
671 && bp->dbgbpself == self
672 && bp->dbgbpofs == ofs)
673 {
674 /*
675 * this is the one - set its ID (which is 1-based) and
676 * return success
677 */
678 if (bpnum != 0)
679 *bpnum = i+1;
680 return TRUE;
681 }
682 }
683
684 /* didn't find any breakpoints matching this description */
685 return FALSE;
686 }
687
688 /*
689 * Determine if the given breakpoint is enabled
690 */
691 int dbgisbpena(dbgcxdef *ctx, int bpnum)
692 {
693 dbgbpdef *bp;
694
695 /* make sure the identifier is in range */
696 --bpnum;
697 if (bpnum < 0 || bpnum >= DBGBPMAX)
698 return FALSE;
699
700 /* get the breakpoint */
701 bp = &ctx->dbgcxbp[bpnum];
702
703 /* return true if it exists and isn't disabled, false otherwise */
704 return ((bp->dbgbpflg & DBGBPFUSED) != 0
705 && (bp->dbgbpflg & DBGBPFDISA) == 0);
706 }
707
708 /* set a breakpoint at a symbolic address: function or object.property */
709 int dbgbpset(dbgcxdef *ctx, char *addr, int *bpnum)
710 {
711 char *p;
712 char psav;
713 char *tok1;
714 char tok1sav;
715 char *tok1end;
716 char *tok2;
717 toksdef sym1;
718 toksdef sym2;
719 toktdef *tab;
720 uint hsh1;
721 uint hsh2;
722 objnum objn;
723 uint ofs;
724 uchar *objp;
725 prpdef *prp;
726 dattyp typ;
727 objnum target;
728 char *end;
729 char buf1[40];
730 char buf2[40];
731
732 /* determine what kind of address we have */
733 for (p = addr ; *p && t_isspace(*p) ; ++p) ;
734 for (tok1 = addr = p ; *p && TOKISSYM(*p) ; ++p) ;
735
736 /* see if the very first thing is "when" */
737 if (!strnicmp(tok1, "when ", (size_t)5))
738 {
739 for (end = tok1 + 5 ; t_isspace(*end) ; ++end) ;
740 return(dbgbpat(ctx, MCMONINV, MCMONINV, 0, bpnum,
741 addr, FALSE, end, 0));
742 }
743
744 /* see if we have a second token */
745 for (tok1end = p ; t_isspace(*p) ; ++p) ;
746 if (*p == '.')
747 {
748 for (++p ; t_isspace(*p) ; ++p) ;
749 for (tok2 = p ; TOKISSYM(*p) ; ++p) ;
750 psav = *p;
751 *p = '\0';
752 end = (psav ? p + 1 : p);
753 }
754 else
755 {
756 tok2 = (char *)0;
757 end = tok1end;
758 if (*end) ++end;
759 p = (char *)0;
760 }
761
762 tok1sav = *tok1end;
763 *tok1end = '\0';
764
765 /* look up the symbols */
766 tab = (toktdef *)ctx->dbgcxtab;
767 if (ctx->dbgcxprs->prscxtok->tokcxflg & TOKCXCASEFOLD)
768 {
769 strcpy(buf1, tok1);
770 os_strlwr(buf1);
771 tok1 = buf1;
772 }
773 hsh1 = tokhsh(tok1);
774 if (!(*tab->toktfsea)(tab, tok1, (int)strlen(tok1), hsh1, &sym1))
775 return ERR_BPSYM;
776 objn = sym1.toksval;
777
778 if (tok2)
779 {
780 /* we have "object.property" */
781 if (ctx->dbgcxprs->prscxtok->tokcxflg & TOKCXCASEFOLD)
782 {
783 strcpy(buf2, tok2);
784 os_strlwr(buf2);
785 tok2 = buf2;
786 }
787 hsh2 = tokhsh(tok2);
788 if (!(*tab->toktfsea)(tab, tok2, (int)strlen(tok2), hsh2, &sym2))
789 return ERR_BPSYM;
790 if (sym1.tokstyp != TOKSTOBJ) return ERR_BPOBJ;
791
792 /* we need to look up the property */
793 ofs = objgetap(ctx->dbgcxmem, objn, (prpnum)sym2.toksval,
794 &target, FALSE);
795 if (!ofs) return ERR_BPNOPRP;
796
797 /* make sure the property is code */
798 objp = mcmlck(ctx->dbgcxmem, (mcmon)target);
799 prp = objofsp((objdef *)objp, ofs);
800 typ = prptype(prp);
801 ofs = ((uchar *)prpvalp(prp)) - objp;
802 mcmunlck(ctx->dbgcxmem, (mcmon)target);
803
804 if (typ != DAT_CODE) return ERR_BPPRPNC;
805 }
806 else
807 {
808 /* we have just "function" */
809 if (sym1.tokstyp != TOKSTFUNC) return ERR_BPFUNC;
810 ofs = 0; /* functions always start at offset zero */
811 target = objn; /* for function, target is always same as object */
812 objn = MCMONINV; /* there is no "self" for a function */
813 }
814
815 /* undo our changes to the string text */
816 if (p) *p = psav;
817 *tok1end = tok1sav;
818
819 /* check for a "when" expression */
820 if (*end)
821 {
822 while (t_isspace(*end)) ++end;
823 if (*end)
824 {
825 if (strnicmp(end, "when ", (size_t)5))
826 return ERR_EXTRTXT;
827 for (end += 5 ; t_isspace(*end) ; ++end) ;
828 }
829 }
830
831 /* set a breakpoint at this object + offset */
832 return(dbgbpat(ctx, target, objn, ofs, bpnum, addr, FALSE, end, 0));
833 }
834
835 /* delete a breakpoint */
836 int dbgbpdel(dbgcxdef *ctx, int bpnum)
837 {
838 dbgbpdef *bp = &ctx->dbgcxbp[--bpnum];
839 dbgbpdef *bp2;
840 uchar *objp;
841 int i;
842 int bpset;
843
844 /* make sure it's a valid breakpoint */
845 if (bpnum < 0 || bpnum >= DBGBPMAX || !(bp->dbgbpflg & DBGBPFUSED))
846 return ERR_BPNSET;
847
848 /* see if we now have NO breakpoints set on this location */
849 for (bpset = FALSE, i = DBGBPMAX, bp2 = ctx->dbgcxbp ; i ; ++bp2, --i)
850 {
851 if (bp != bp2 && (bp2->dbgbpflg & DBGBPFUSED)
852 && bp2->dbgbptarg == bp->dbgbptarg
853 && bp2->dbgbpofs == bp->dbgbpofs)
854 {
855 bpset = TRUE;
856 break;
857 }
858 }
859
860 /* if no other bp's here, convert the OPCBP back into an OPCLINE */
861 if (!bpset)
862 {
863 if (bp->dbgbptarg == MCMONINV)
864 {
865 /* no more global breakpoints - delete global BP flag */
866 ctx->dbgcxflg &= ~DBGCXFGBP;
867 }
868 else
869 {
870 /* convert the OPCBP back into OPCLINE */
871 objp = mcmlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
872 *(objp + bp->dbgbpofs) = OPCLINE;
873 mcmtch(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
874 mcmunlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
875 }
876 }
877
878 /* delete the names from the buffer */
879 if (bp->dbgbpflg & DBGBPFNAME)
880 dbgnamdel(ctx, bp->dbgbpnam); /* delete name from text buffer */
881 if (bp->dbgbpflg & DBGBPFCONDNAME)
882 dbgnamdel(ctx, bp->dbgbpcondnam);
883
884 /* the slot is no longer in use */
885 bp->dbgbpflg &= ~DBGBPFUSED;
886
887 /* free condition object, if one has been allocated */
888 if (bp->dbgbpflg & DBGBPFCOND)
889 {
890 mcmfre(ctx->dbgcxmem, bp->dbgbpcond);
891 bp->dbgbpflg &= ~DBGBPFCOND;
892 }
893
894 return 0;
895 }
896
897 /* list breakpoints using user callback */
898 void dbgbplist(dbgcxdef *ctx,
899 void (*dispfn)(void *ctx, const char *str, int len),
900 void *dispctx)
901 {
902 dbgbpdef *bp = ctx->dbgcxbp;
903 int i;
904 char *p;
905 char buf[10];
906
907 /* if we're not recording names, there's nothing we can do */
908 if (!ctx->dbgcxnam) return;
909
910 for (i = 0 ; i < DBGBPMAX ; ++bp, ++i)
911 {
912 if ((bp->dbgbpflg & DBGBPFUSED) && (bp->dbgbpflg & DBGBPFNAME))
913 {
914 p = ctx->dbgcxnam + bp->dbgbpnam;
915 sprintf(buf, "%d: ", i + 1);
916 (*dispfn)(dispctx, buf, (int)strlen(buf));
917 (*dispfn)(dispctx, p, (int)strlen(p));
918
919 if (bp->dbgbpflg & DBGBPFDISA)
920 (*dispfn)(dispctx, " [disabled]", 11);
921
922 (*dispfn)(dispctx, "\n", 1);
923 }
924 }
925 }
926
927 /* enumerate breakpoints */
928 void dbgbpenum(dbgcxdef *ctx,
929 void (*cbfunc)(void *cbctx, int bpnum, const char *desc,
930 const char *cond, int disabled), void *cbctx)
931 {
932 dbgbpdef *bp = ctx->dbgcxbp;
933 int i;
934 char *bpname;
935 char *cond;
936
937 /* if we're not recording names, there's nothing we can do */
938 if (ctx->dbgcxnam == 0)
939 return;
940
941 /* run through our list of breakpoints */
942 for (i = 0 ; i < DBGBPMAX ; ++bp, ++i)
943 {
944 /*
945 * if this slot is in use, and has a name, include it in the
946 * enumeration
947 */
948 if ((bp->dbgbpflg & DBGBPFUSED) && (bp->dbgbpflg & DBGBPFNAME))
949 {
950 /* get the name */
951 bpname = ctx->dbgcxnam + bp->dbgbpnam;
952
953 /* if there's a condition, get the condition */
954 cond = ((bp->dbgbpflg & DBGBPFCONDNAME) != 0
955 ? ctx->dbgcxnam + bp->dbgbpcondnam : 0);
956
957 /* invoke the callback */
958 (*cbfunc)(cbctx, i + 1, bpname, cond,
959 (bp->dbgbpflg & DBGBPFDISA) != 0);
960 }
961 }
962 }
963
964 /* enable/disable a breakpoint */
965 int dbgbpdis(dbgcxdef *ctx, int bpnum, int disable)
966 {
967 dbgbpdef *bp = &ctx->dbgcxbp[--bpnum];
968
969 /* make sure it's a valid breakpoint */
970 if (bpnum < 0 || bpnum >= DBGBPMAX || !(bp->dbgbpflg & DBGBPFUSED))
971 return ERR_BPNSET;
972
973 if (disable) bp->dbgbpflg |= DBGBPFDISA;
974 else bp->dbgbpflg &= ~DBGBPFDISA;
975 return 0;
976 }
977
978 /*
979 * Call user callback with lindef data for each breakpoint; global
980 * breakpoints are NOT passed to user callback, since they don't
981 * correspond to source locations.
982 */
983 void dbgbpeach(dbgcxdef *ctx, void (*fn)(void *, int, uchar *, uint),
984 void *fnctx)
985 {
986 int i;
987 dbgbpdef *bp = ctx->dbgcxbp;
988 uchar *p;
989 uint len;
990
991 for (i = 0 ; i < DBGBPMAX ; ++bp, ++i)
992 {
993 if ((bp->dbgbpflg & DBGBPFUSED)
994 && bp->dbgbptarg != MCMONINV)
995 {
996 p = mcmlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
997 p += bp->dbgbpofs + 1;
998 len = *p - 4;
999 p += 3;
1000 (*fn)(fnctx, (int)(*p), p + 1, len);
1001 mcmunlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
1002 }
1003 }
1004 }
1005
1006 /*
1007 * Get information on a specific breakpoint. Returns zero on success,
1008 * non-zero on failure.
1009 */
1010 int dbgbpgetinfo(dbgcxdef *ctx, int bpnum, char *descbuf, size_t descbuflen,
1011 char *condbuf, size_t condbuflen)
1012 {
1013 dbgbpdef *bp = &ctx->dbgcxbp[--bpnum];
1014
1015 /* make sure it's a valid breakpoint */
1016 if (bpnum < 0 || bpnum >= DBGBPMAX || !(bp->dbgbpflg & DBGBPFUSED))
1017 return ERR_BPNSET;
1018
1019 /* if the caller wants the name, copy the name */
1020 if (descbuf != 0 && descbuflen != 0)
1021 {
1022 /* check to make sure we have a name */
1023 if ((bp->dbgbpflg & DBGBPFNAME) != 0)
1024 {
1025 size_t outlen;
1026 char *bpname;
1027
1028 /* get the name */
1029 bpname = ctx->dbgcxnam + bp->dbgbpnam;
1030
1031 /* limit the copy to the buffer size */
1032 outlen = strlen(bpname);
1033 if (outlen > descbuflen - 1)
1034 outlen = descbuflen - 1;
1035
1036 /* copy it and null-terminate the result */
1037 memcpy(descbuf, bpname, outlen);
1038 descbuf[outlen] = '\0';
1039 }
1040 else
1041 {
1042 /* there's no name; clear out the caller's buffer */
1043 descbuf[0] = '\0';
1044 }
1045 }
1046
1047 /* if the caller wants the condition, copy it */
1048 if (condbuf != 0 && condbuflen != 0)
1049 {
1050 /* check to make sure we have a name */
1051 if ((bp->dbgbpflg & DBGBPFCONDNAME) != 0)
1052 {
1053 size_t outlen;
1054 char *condname;
1055
1056 /* get the name */
1057 condname = ctx->dbgcxnam + bp->dbgbpcondnam;
1058
1059 /* limit the copy to the buffer size */
1060 outlen = strlen(condname);
1061 if (outlen > condbuflen - 1)
1062 outlen = condbuflen - 1;
1063
1064 /* copy it and null-terminate the result */
1065 memcpy(condbuf, condname, outlen);
1066 condbuf[outlen] = '\0';
1067 }
1068 else
1069 {
1070 /* there's no name; clear out the caller's buffer */
1071 condbuf[0] = '\0';
1072 }
1073 }
1074
1075 /* success */
1076 return 0;
1077 }
1078
1079
1080 /* ====================== debug line source routines ====================== */
1081 static int dbglinget(lindef *lin)
1082 {
1083 if (lin->linflg & LINFEOF)
1084 return TRUE; /* expression has only one line - now at EOF */
1085 else
1086 {
1087 lin->linflg |= LINFEOF;
1088 return FALSE;
1089 }
1090 }
1091
1092 static void dbglincls(lindef *lin)
1093 {
1094 VARUSED(lin);
1095 }
1096
1097 /* never called */
1098 static void dbglinpos(lindef *lin, char *buf, uint bufl)
1099 {
1100 VARUSED(lin);
1101 VARUSED(buf);
1102 VARUSED(bufl);
1103 }
1104
1105 /* never called */
1106 static void dbglinglo(lindef *lin, uchar *buf)
1107 {
1108 VARUSED(lin);
1109 VARUSED(buf);
1110 }
1111
1112 /* never called */
1113 static int dbglinwrt(lindef *lin, osfildef *fp)
1114 {
1115 VARUSED(lin);
1116 VARUSED(fp);
1117 return TRUE;
1118 }
1119
1120 /* never called */
1121 static void dbglincmp(lindef *lin, uchar *buf)
1122 {
1123 VARUSED(lin);
1124 VARUSED(buf);
1125 }
1126
1127 /* maximum number of symbols that can be mentioned in an eval expression */
1128 #define DBGMAXSYM 30
1129
1130 struct toktfdef
1131 {
1132 toktdef toktfsc;
1133 mcmcxdef *toktfmem;
1134 dbgcxdef *toktfdbg;
1135 dbgfdef *toktffr; /* frame to be used for symbol */
1136 };
1137 typedef struct toktfdef toktfdef;
1138
1139 /* search debug frame tables for a symbol */
1140 int dbgtabsea(toktdef *tab, char *name, int namel, int hash, toksdef *ret)
1141 {
1142 dbgfdef *fr;
1143 uchar *objp;
1144 toktfdef *ftab = (toktfdef *)tab;
1145 mcmcxdef *mctx = ftab->toktfmem;
1146 uint len;
1147 uint symlen;
1148 uint symval;
1149 uint ofs;
1150 uint encofs;
1151 uchar *framep;
1152
1153 fr = ftab->toktffr;
1154
1155 /* if there is no local context, we obviously can't find a symbol */
1156 if (fr->dbgftarg == MCMONINV)
1157 return FALSE;
1158
1159 /* lock object and get its frame pointer */
1160 objp = mcmlck(mctx, fr->dbgftarg);
1161
1162 for (ofs = fr->dbgffr ; ofs ; ofs = encofs)
1163 {
1164 /* look through this frame table */
1165 framep = objp + ofs;
1166 len = osrp2(framep) - 4; /* get length out of table */
1167 encofs = osrp2(framep + 2); /* get enclosing frame's offset */
1168 framep += 4; /* skip the header */
1169
1170 while (len)
1171 {
1172 symval = osrp2(framep);
1173 framep += 2;
1174 symlen = *framep++;
1175 len -= 3 + symlen;
1176
1177 if (symlen == (uint)namel && !memcmp(name, framep, (size_t)namel))
1178 {
1179 /* unlock the object and set up the toksdef for return */
1180 mcmunlck(mctx, fr->dbgftarg);
1181 ret->toksval = symval;
1182 ret->tokstyp = TOKSTLOCAL;
1183 ret->tokslen = namel;
1184 ret->toksfr = ofs;
1185 return TRUE;
1186 }
1187
1188 /* advance past this symbol */
1189 framep += symlen;
1190 }
1191 }
1192
1193 /* nothing found - unlock object and return */
1194 mcmunlck(mctx, fr->dbgftarg);
1195 return FALSE;
1196 }
1197
1198 /* compile an expression; returns error if one occurs */
1199 int dbgcompile(dbgcxdef *ctx, char *expr, dbgfdef *fr, objnum *objnp,
1200 int speculative)
1201 {
1202 lindef lin;
1203 ushort oldflg = 0;
1204 int err;
1205 prscxdef *prs = ctx->dbgcxprs;
1206 toktfdef tab;
1207 mcmon objn;
1208
1209 /* set up a lindef to provide the source line to the compiler */
1210 prs->prscxtok->tokcxlin = &lin;
1211 lin.lingetp = dbglinget;
1212 lin.linclsp = dbglincls;
1213 lin.linppos = dbglinpos;
1214 lin.linglop = dbglinglo;
1215 lin.linwrtp = dbglinwrt;
1216 lin.lincmpp = dbglincmp;
1217 lin.linpar = (lindef *)0;
1218 lin.linnxt = (lindef *)0;
1219 lin.linid = 0;
1220 lin.linbuf = expr;
1221 lin.linflg = LINFNOINC;
1222 lin.linlen = strlen(expr);
1223 lin.linlln = 0;
1224
1225 /* set emit frame object */
1226 prs->prscxemt->emtcxfrob = fr->dbgftarg;
1227
1228 /* set up special frame-searching symbol table */
1229 tab.toktfsc.toktfadd = 0; /* we'll never add symbols to this table */
1230 tab.toktfsc.toktfsea = dbgtabsea; /* we will, however, search it */
1231 tab.toktfsc.toktnxt = prs->prscxtok->tokcxstab;
1232 tab.toktfsc.tokterr = prs->prscxerr;
1233 tab.toktfmem = ctx->dbgcxrun->runcxmem;
1234 tab.toktfdbg = ctx;
1235 tab.toktffr = fr;
1236
1237 /* presume no error and no object allocated */
1238 err = 0;
1239
1240 /* allocate an object for emitting the code */
1241 prs->prscxemt->emtcxptr = mcmalo(prs->prscxmem, (ushort)512, &objn);
1242 prs->prscxemt->emtcxobj = objn;
1243 prs->prscxemt->emtcxofs = 0;
1244
1245 ERRBEGIN(ctx->dbgcxerr)
1246
1247 /* set the parsing context to generate no debug records */
1248 oldflg = prs->prscxflg;
1249 prs->prscxflg &= ~(PRSCXFLIN + PRSCXFLCL);
1250
1251 /* set topmost symbol table to the debug frame table */
1252 prs->prscxtok->tokcxstab = &tab.toktfsc;
1253
1254 /* parse the expression */
1255 prsrstn(prs);
1256 prs->prscxtok->tokcxlen = 0; /* purge anything left in buffer */
1257 toknext(prs->prscxtok);
1258
1259 /* generate code for the expression */
1260 if (speculative)
1261 prsxgen_spec(prs);
1262 else
1263 prsxgen(prs);
1264
1265 /* make certain it returns */
1266 emtop(prs->prscxemt, OPCDBGRET);
1267
1268 ERRCATCH(ctx->dbgcxerr, err)
1269 /* unlock and free the object used for holding the pcode */
1270 mcmunlck(prs->prscxmem, objn);
1271 mcmfre(prs->prscxmem, objn);
1272
1273 /* restore original parsing flags and symbol table */
1274 prs->prscxflg = oldflg;
1275 prs->prscxtok->tokcxstab = tab.toktfsc.toktnxt;
1276
1277 return err;
1278 ERREND(ctx->dbgcxerr)
1279
1280 /* restore original parsing flags and symbol table */
1281 prs->prscxflg = oldflg;
1282 prs->prscxtok->tokcxstab = tab.toktfsc.toktnxt;
1283
1284 mcmunlck(prs->prscxmem, objn);
1285 *objnp = objn;
1286 return 0;
1287 }
1288
1289 /* evaluate an expression */
1290 int dbgeval(dbgcxdef *ctx, char *expr,
1291 void (*dispfn)(void *, const char *, int),
1292 void *dispctx, int level,
1293 int showtype)
1294 {
1295 /* invoke extended version, using a null callback */
1296 return dbgevalext(ctx, expr, dispfn, dispctx, level, showtype, 0, 0, 0,
1297 FALSE);
1298 }
1299
1300 /*
1301 * Enumerate the properties of an object
1302 */
1303 static void dbgenumprop(dbgcxdef *ctx, objnum objn, objnum self,
1304 void (*func)(void *ctx, const char *propname,
1305 int propnamelen,
1306 const char *relationship),
1307 void *cbctx)
1308 {
1309 objdef *objptr;
1310 int cnt;
1311 prpdef *p;
1312 int sccnt;
1313 uchar *sc;
1314
1315 /* if it's not a valid object, there's nothing to do */
1316 if (objn == MCMONINV)
1317 return;
1318
1319 /*
1320 * make sure the object hasn't been deleted - check the vocab
1321 * inheritance records for confirmation
1322 */
1323 if (vocinh(ctx->dbgcxrun->runcxvoc, objn) == 0)
1324 return;
1325
1326 /* get the object's memory */
1327 objptr = (objdef *)mcmlck(ctx->dbgcxmem, objn);
1328
1329 /* get the number of properties */
1330 cnt = objnprop(objptr);
1331
1332 /* loop through its properties */
1333 for (p = objprp(objptr) ; cnt != 0 ; p = objpnxt(p), --cnt)
1334 {
1335 prpnum prpn;
1336 char prpname[TOKNAMMAX + 1];
1337 int len;
1338 objnum srcobj;
1339
1340 /* get this property's ID */
1341 prpn = prpprop(p);
1342
1343 /*
1344 * check the type; if it's code or another non-evaluable type,
1345 * skip it
1346 */
1347 switch(prptype(p))
1348 {
1349 case DAT_BASEPTR:
1350 case DAT_CODE:
1351 case DAT_DSTRING:
1352 case DAT_TPL:
1353 case DAT_DEMAND:
1354 case DAT_SYN:
1355 case DAT_REDIR:
1356 case DAT_TPL2:
1357 /*
1358 * these types do not represent values that can be evaluated
1359 * in the debugger, so skip properties of these types
1360 */
1361 continue;
1362 }
1363
1364 /*
1365 * If we've recursed into a superclass, make sure that this
1366 * property actually comes from this superclass in the object.
1367 * If it doesn't, skip it, because we'll pick it up on the
1368 * overriding object.
1369 */
1370 if (objn != self
1371 && (objgetap(ctx->dbgcxmem, self, prpn, &srcobj, FALSE) == 0
1372 || srcobj != objn))
1373 continue;
1374
1375 /* get the name of this property */
1376 len = dbgnam(ctx, prpname, TOKSTPROP, prpn);
1377
1378 /* tell the callback about it */
1379 (*func)(cbctx, prpname, len, ".");
1380 }
1381
1382 /*
1383 * Go through the object's superclasses, and enumerate the
1384 * properties for each of them.
1385 */
1386 sccnt = objnsc(objptr);
1387 for (sc = objsc(objptr) ; sccnt != 0 ; sc += 2, --sccnt)
1388 {
1389 /* recurse into this superclass object */
1390 dbgenumprop(ctx, (objnum)osrp2(sc), self, func, cbctx);
1391 }
1392
1393 /* done with the object - unlock it */
1394 mcmunlck(ctx->dbgcxmem, objn);
1395 }
1396
1397 /*
1398 * Evaluate an expression, extended version. For aggregate values
1399 * (objects, lists), we'll invoke a callback function for each value
1400 * contained by the aggregate value.
1401 */
1402 int dbgevalext(dbgcxdef *ctx, char *expr,
1403 void (*dispfn)(void *, const char *, int), void *dispctx,
1404 int level, int showtype, dattyp *dat,
1405 void (*aggcb)(void *, const char *, int, const char *),
1406 void *aggctx, int speculative)
1407 {
1408 runsdef *oldsp;
1409 runsdef val;
1410 objnum objn;
1411 uchar *objptr;
1412 int err;
1413 int oldfcn;
1414 int olddep;
1415
1416 /* compile the expression */
1417 if ((err = dbgcompile(ctx, expr, &ctx->dbgcxfrm[level], &objn,
1418 speculative)) != 0)
1419 return err;
1420
1421 /*
1422 * remember the current stack frame position -- running code will
1423 * mess with it, and if we get an error it won't be set back
1424 * properly without us doing so explicitly
1425 */
1426 oldfcn = ctx->dbgcxfcn;
1427 olddep = ctx->dbgcxdep;
1428
1429 ERRBEGIN(ctx->dbgcxerr)
1430
1431 objptr = mcmlck(ctx->dbgcxmem, objn);
1432
1433 /* execute the generated code */
1434 oldsp = ctx->dbgcxrun->runcxsp; /* remember sp so we can detect change */
1435 runexe(ctx->dbgcxrun, objptr, ctx->dbgcxfrm[level].dbgfself,
1436 objn, (prpnum)0, 0);
1437
1438 /* if the expression left a value on the stack, display it */
1439 if (ctx->dbgcxrun->runcxsp != oldsp)
1440 {
1441 /* get the value */
1442 runpop(ctx->dbgcxrun, &val);
1443
1444 /* display it via the callback, then display a newline */
1445 dbgpval(ctx, &val, dispfn, dispctx, showtype);
1446 (*dispfn)(dispctx, "\n", 1);
1447
1448 /* if the caller wants to know the type, inform the caller */
1449 if (dat != 0)
1450 *dat = val.runstyp;
1451 }
1452
1453 /*
1454 * If the value is an aggregate, and an aggregate callback was
1455 * provided, invoke the aggregate callback for each item contained
1456 * by the value.
1457 */
1458 if (aggcb != 0)
1459 {
1460 switch(val.runstyp)
1461 {
1462 case DAT_OBJECT:
1463 /*
1464 * go through the object's properties, and those of its
1465 * superclasses
1466 */
1467 dbgenumprop(ctx, val.runsv.runsvobj, val.runsv.runsvobj,
1468 aggcb, aggctx);
1469 break;
1470
1471 case DAT_LIST:
1472 {
1473 uchar *p;
1474 uint rem;
1475 uint idxcnt;
1476 uint idx;
1477
1478 /* count the number of elements */
1479 p = val.runsv.runsvstr;
1480 rem = osrp2(p) - 2;
1481 p += 2;
1482 for (idxcnt = 0 ; rem != 0 ; ++idxcnt, lstadv(&p, &rem)) ;
1483
1484 /* invoke the callback once per element */
1485 for (idx = 1 ; idx <= idxcnt ; ++idx)
1486 {
1487 char idxbuf[30];
1488
1489 /* build the name of this subitem */
1490 sprintf(idxbuf, "[%u]", idx);
1491
1492 /* invoke the callback with this subitem */
1493 (*aggcb)(aggctx, idxbuf, strlen(idxbuf), "");
1494 }
1495 }
1496 break;
1497 }
1498 }
1499
1500 ERRCATCH(ctx->dbgcxerr, err)
1501
1502 /*
1503 * an error occurred evaluating the expression - in case we
1504 * entered a stack frame and threw an error without resetting
1505 * it, go back to the original frame position now
1506 */
1507 ctx->dbgcxfcn = oldfcn;
1508 ctx->dbgcxdep = olddep;
1509
1510 ERREND(ctx->dbgcxerr)
1511
1512 /* free the object that we used to hold the compiled code */
1513 mcmunlck(ctx->dbgcxmem, objn);
1514 mcmfre(ctx->dbgcxmem, objn);
1515
1516 /* return the error status, if any */
1517 return err;
1518 }
1519
1520 /*
1521 * enumerate local variables at a given stack context level by calling
1522 * the given function once for each local variable
1523 */
1524 void dbgenumlcl(dbgcxdef *ctx, int level,
1525 void (*func)(void *ctx, const char *lclnam, size_t lclnamlen),
1526 void *cbctx)
1527 {
1528 dbgfdef *fr;
1529 mcmcxdef *mctx = ctx->dbgcxmem;
1530 uchar *objp;
1531 uint ofs;
1532 uint encofs;
1533 int err;
1534
1535 /* get the frame at this level */
1536 fr = &ctx->dbgcxfrm[level];
1537
1538 /* if there's no local context, there are no symbols */
1539 if (fr->dbgftarg == MCMONINV)
1540 return;
1541
1542 ERRBEGIN(ctx->dbgcxerr)
1543 /* lock the object and get its frame pointer */
1544 objp = mcmlck(mctx, fr->dbgftarg);
1545 ERRCATCH(ctx->dbgcxerr, err)
1546 /* can't enumerate locals */
1547 return;
1548 ERREND(ctx->dbgcxerr)
1549
1550 ERRBEGIN(ctx->dbgcxerr)
1551
1552 for (ofs = fr->dbgffr ; ofs != 0 ; ofs = encofs)
1553 {
1554 uchar *framep;
1555 uint len;
1556
1557 /* look through this frame table */
1558 framep = objp + ofs;
1559 len = osrp2(framep) - 4; /* get length out of table */
1560 encofs = osrp2(framep + 2); /* get enclosing frame's offset */
1561 framep += 4; /* skip the header */
1562
1563 /* scan the frame table */
1564 while (len != 0)
1565 {
1566 uint symlen;
1567
1568 /* read this symbol's information */
1569 framep += 2;
1570 symlen = *framep++;
1571 len -= 3 + symlen;
1572
1573 /* invoke the callback with this symbol */
1574 (*func)(cbctx, (char *)framep, (size_t)symlen);
1575
1576 /* move on to the next symbol */
1577 framep += symlen;
1578 }
1579 }
1580
1581 ERRCATCH(ctx->dbgcxerr, err)
1582 /* ignore the error */
1583 ERREND(ctx->dbgcxerr)
1584
1585 /* unlock the target, and we're done */
1586 mcmunlck(mctx, fr->dbgftarg);
1587 }
1588
1589 /* Set a watchpoint; returns error if any */
1590 int dbgwxset(dbgcxdef *ctx, char *expr, int *wxnum, int level)
1591 {
1592 dbgwxdef *wx;
1593 int i;
1594 objnum objn;
1595 int err;
1596 uint oldflg;
1597
1598 /* scan off leading spaces in expression text */
1599 while (t_isspace(*expr)) ++expr;
1600
1601 /* get a free watch slot */
1602 for (i = 0, wx = ctx->dbgcxwx ; i < DBGWXMAX ; ++wx, ++i)
1603 if (!(wx->dbgwxflg & DBGWXFUSED)) break;
1604
1605 if (i == DBGWXMAX) return ERR_MANYWX;
1606 *wxnum = i + 1;
1607
1608 /* compile the expression */
1609 oldflg = ctx->dbgcxprs->prscxflg; /* save parse flags */
1610 ctx->dbgcxprs->prscxflg |= PRSCXFWTCH; /* note this is a watchpoint */
1611 err = dbgcompile(ctx, expr, &ctx->dbgcxfrm[level], &objn, FALSE);
1612 ctx->dbgcxprs->prscxflg = oldflg; /* restore old flags */
1613 if (err)
1614 return err;
1615
1616 /* record the watch data */
1617 wx->dbgwxobj = objn;
1618 wx->dbgwxflg = DBGWXFUSED;
1619 wx->dbgwxself = ctx->dbgcxfrm[level].dbgfself;
1620 if (!dbgnamsav(ctx, expr, &wx->dbgwxnam)) wx->dbgwxflg |= DBGWXFNAME;
1621
1622 return 0;
1623 }
1624
1625 /* delete a watch expression */
1626 int dbgwxdel(dbgcxdef *ctx, int wxnum)
1627 {
1628 dbgwxdef *wx = &ctx->dbgcxwx[--wxnum];
1629
1630 /* make sure it's valid */
1631 if (wxnum < 0 || wxnum > DBGWXMAX || !(wx->dbgwxflg & DBGWXFUSED))
1632 return ERR_WXNSET;
1633
1634 mcmfre(ctx->dbgcxmem, wx->dbgwxobj); /* delete pcode for object */
1635 wx->dbgwxflg &= ~DBGWXFUSED; /* watch expression is no longer in use */
1636 if (wx->dbgwxflg & DBGWXFNAME)
1637 dbgnamdel(ctx, wx->dbgwxnam); /* delete expr text from name buffer */
1638
1639 return 0;
1640 }
1641
1642 /* update all watch expressions */
1643 void dbgwxupd(dbgcxdef *ctx, void (*dispfn)(void *, const char *, int),
1644 void *dispctx)
1645 {
1646 int i;
1647 dbgwxdef *wx;
1648 int err;
1649 uchar *objptr;
1650 runsdef *oldsp;
1651 runsdef val;
1652 char buf[50];
1653 int first = TRUE;
1654
1655 /* suppress all output while processing watchpoints */
1656 outwx(1);
1657
1658 for (i = 0, wx = ctx->dbgcxwx ; i < DBGWXMAX ; ++wx, ++i)
1659 {
1660 if (!(wx->dbgwxflg & DBGWXFUSED)) continue;
1661
1662 /* display watch number and expression text */
1663 if (!first) (*dispfn)(dispctx, "\n", 1);
1664 else first = FALSE;
1665
1666 sprintf(buf, "[%d] %s = ", i + 1, ctx->dbgcxnam + wx->dbgwxnam);
1667 (*dispfn)(dispctx, buf, (int)strlen(buf));
1668
1669 /* lock the object containing the compiled expression */
1670 objptr = mcmlck(ctx->dbgcxmem, wx->dbgwxobj);
1671
1672 ERRBEGIN(ctx->dbgcxerr)
1673
1674 oldsp = ctx->dbgcxrun->runcxsp;
1675 runexe(ctx->dbgcxrun, objptr, wx->dbgwxself, wx->dbgwxobj,
1676 (prpnum)0, 0);
1677
1678 if (ctx->dbgcxrun->runcxsp != oldsp)
1679 {
1680 runpop(ctx->dbgcxrun, &val);
1681 dbgpval(ctx, &val, dispfn, dispctx, FALSE);
1682 }
1683 else
1684 (*dispfn)(dispctx, "<no value>", 10);
1685
1686 ERRCATCH(ctx->dbgcxerr, err)
1687 if (err == ERR_INACTFR)
1688 (*dispfn)(dispctx, "<not available>", 15);
1689 else
1690 {
1691 sprintf(buf, "<error %d>", err);
1692 (*dispfn)(dispctx, buf, (int)strlen(buf));
1693 }
1694 ERREND(ctx->dbgcxerr)
1695
1696 mcmunlck(ctx->dbgcxmem, wx->dbgwxobj);
1697 }
1698
1699 /* stop suppressing output */
1700 outwx(0);
1701 }
1702
1703
1704 /*
1705 * Find a base pointer, given the object+offset of the frame. If the
1706 * frame is not active, this routine signals ERR_INACTFR; otherwise, the
1707 * bp value for the frame is returned.
1708 */
1709 runsdef *dbgfrfind(dbgcxdef *ctx, objnum frobj, uint frofs)
1710 {
1711 int i;
1712 dbgfdef *f;
1713 mcmon objn;
1714 uchar *objptr;
1715 uint ofs;
1716
1717 /* search stack */
1718 for (i = ctx->dbgcxfcn, f = &ctx->dbgcxfrm[i-1] ; i ; --f, --i)
1719 {
1720 /* ignore this stack level if we have the wrong object */
1721 objn = f->dbgftarg;
1722 if (frobj != objn) continue;
1723
1724 /* search nested frames within this stack level */
1725 objptr = mcmlck(ctx->dbgcxmem, objn);
1726
1727 for (ofs = f->dbgffr ; ofs ; )
1728 {
1729 if (ofs == frofs)
1730 {
1731 /* object and offset match - this is the correct frame */
1732 mcmunlck(ctx->dbgcxmem, objn);
1733 return f->dbgfbp;
1734 }
1735
1736 /* this isn't it; move on to the enclosing frame */
1737 ofs = osrp2(objptr + ofs + 2);
1738 }
1739
1740 /* done with this object - unlock it */
1741 mcmunlck(ctx->dbgcxmem, objn);
1742 }
1743
1744 /* couldn't find the frame at all - signal INACTIVE error */
1745 errsig(ctx->dbgcxerr, ERR_INACTFR);
1746 NOTREACHEDV(runsdef *);
1747 return 0;
1748 }
1749
1750 /*
1751 * Switch to a new current lindef, closing the file referenced by the
1752 * current lindef. This lets us keep only one file open at a time when
1753 * running under the stand-alone source debugger, which reads the source
1754 * from disk files.
1755 */
1756 void dbgswitch(lindef **linp, lindef *newlin)
1757 {
1758 if (*linp) lindisact(*linp);
1759 if (newlin) linactiv(newlin);
1760 *linp = newlin;
1761 }
1762