-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbgrun.c
1762 lines (1509 loc) · 49.4 KB
/
dbgrun.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/TADS2/DBGRUN.C,v 1.3 1999/07/11 00:46:29 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1992, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
dbgrun.c - functions needed when actually debugging
Function
This module contains functions actually used while debugging, as
opposed to functions needed in the compiler and/or runtime to set
up debugging without activating an interactive debugger session.
These functions are in a separate file to reduce the size of the
runtime and compiler, which don't need to link these functions.
Notes
None
Modified
04/11/99 CNebel - Fix signing errors.
04/20/92 MJRoberts - creation
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "os.h"
#include "dbg.h"
#include "tio.h"
#include "obj.h"
#include "prp.h"
#include "tok.h"
#include "run.h"
#include "tok.h"
#include "prs.h"
#include "linf.h"
#include "bif.h"
#include "lst.h"
/* indicate that the debugger is present */
int dbgpresent()
{
return TRUE;
}
/* format a display of where execution is stopped */
void dbgwhere(dbgcxdef *ctx, char *p)
{
dbgfdef *f;
f = &ctx->dbgcxfrm[ctx->dbgcxfcn - 1];
if (f->dbgftarg == MCMONINV)
p += dbgnam(ctx, p, TOKSTBIFN, f->dbgfbif);
else
p += dbgnam(ctx, p,
(f->dbgfself == MCMONINV ? TOKSTFUNC : TOKSTOBJ),
(int)f->dbgftarg);
if (f->dbgfself != MCMONINV && f->dbgfself != f->dbgftarg)
{
memcpy(p, "<self=", (size_t)6);
p += 6;
p += dbgnam(ctx, p, TOKSTOBJ, (int)f->dbgfself);
*p++ = '>';
}
if (f->dbgfprop)
{
*p++ = '.';
p += dbgnam(ctx, p, TOKSTPROP, (int)f->dbgfprop);
}
if (f->dbgfself == MCMONINV || f->dbgfbif)
{
*p++ = '(';
*p++ = ')';
}
*p = '\0';
}
/*
* Evaluate breakpoint condition; returns TRUE if breakpoint condition
* was non-nil, FALSE if it was nil or had no value.
*/
static int dbgbpeval(dbgcxdef *ctx, dbgbpdef *bp, dbgfdef *fr)
{
uchar *objptr;
runsdef *oldsp;
runsdef val;
int err;
objptr = mcmlck(ctx->dbgcxmem, bp->dbgbpcond);
ERRBEGIN(ctx->dbgcxerr)
oldsp = ctx->dbgcxrun->runcxsp;
runexe(ctx->dbgcxrun, objptr, fr->dbgfself, bp->dbgbpcond,
(prpnum)0, 0);
ERRCATCH(ctx->dbgcxerr, err)
/* no error recover - just proceed as normal */
;
ERREND(ctx->dbgcxerr)
/* done with condition code; unlock it */
mcmunlck(ctx->dbgcxmem, bp->dbgbpcond);
/* if condition evaluated to non-nil, break now */
if (ctx->dbgcxrun->runcxsp != oldsp)
{
runpop(ctx->dbgcxrun, &val);
if (val.runstyp == DAT_NIL)
return FALSE; /* value was nil - condition failed */
else
return TRUE; /* value was non-nil - condition met */
}
else
return FALSE; /* expression returned no value - treat as nil */
}
/* single-step interrupt */
void dbgss(dbgcxdef *ctx, uint ofs, int instr, int err,
uchar *noreg *instrp)
{
dbgbpdef *bp;
int i;
dbgfdef *fr;
int bphit = 0;
voccxdef *vcx = ctx->dbgcxrun->runcxvoc;
unsigned int exec_ofs;
errdef *inner_frame;
int stepping;
/* don't re-enter debugger - return if running from tdb */
if (ctx->dbgcxflg & DBGCXFIND)
return;
/*
* If the current debugger stack context is in a built-in function,
* and we can resume from an error, pop a level of context -- we
* always show the context inside of the p-code, because we want to
* be able to move the execution point before resuming execution.
* If we can't resume from an error, leave the stack as it is so
* that the user can see exactly which built-in function was called
* to cause the error.
*/
if (dbgu_err_resume(ctx))
{
/* remove any built-in functions from the top of the stack */
while (ctx->dbgcxfcn > 1
&& ctx->dbgcxfrm[ctx->dbgcxfcn-1].dbgftarg == MCMONINV)
ctx->dbgcxfcn--;
}
/* get the frame pointer */
fr = (ctx->dbgcxfcn == 0 ? 0 : &ctx->dbgcxfrm[ctx->dbgcxfcn - 1]);
/* presume we're not single-stepping */
stepping = FALSE;
/*
* Check to see if we should stop due to single-stepping; if so,
* there's no need to consider breakpoints at all.
*/
if ((ctx->dbgcxflg & DBGCXFSS) != 0)
{
/* single-step mode - check for step-over mode */
if ((ctx->dbgcxflg & DBGCXFSO) != 0)
{
/*
* step-over mode - stop only if we're in the stack level
* where we started step-over mode (not in a routine called
* from that stack level, which is what we're stepping over)
*/
stepping = (ctx->dbgcxdep <= ctx->dbgcxsof);
}
else
{
/* normal step-in mode - always stop */
stepping = TRUE;
}
}
/*
* if we're not going to stop for single-stepping or because of a
* run-time error, look for breakpoints
*/
if (!stepping && err == 0)
{
/* check the instruction that cause debugger entry */
if (instr == OPCBP)
{
/* look up breakpoint, and make sure 'self' is correct */
for (i = 0, bp = ctx->dbgcxbp ; i < DBGBPMAX ; ++bp, ++i)
{
if (fr != 0
&& (bp->dbgbpflg & DBGBPFUSED)
&& !(bp->dbgbpflg & DBGBPFDISA)
&& (bp->dbgbpself == fr->dbgfself
|| bp->dbgbpself == MCMONINV
|| bifinh(vcx, vocinh(vcx, fr->dbgfself),
bp->dbgbpself))
&& bp->dbgbptarg == fr->dbgftarg
&& bp->dbgbpofs + 3 == ofs)
break;
}
/* if we didn't find the breakpoint, ignore the stop */
if (i == DBGBPMAX)
return;
/* if this is a conditional bp, make sure condition is true */
if (!(ctx->dbgcxflg & DBGCXFSS) && (bp->dbgbpflg & DBGBPFCOND)
&& !dbgbpeval(ctx, bp, fr))
return;
/* note that we hit a breakpoint */
bphit = i + 1;
}
else
{
int brk;
/* check for global breakpoints */
if (fr != 0 && (ctx->dbgcxflg & DBGCXFGBP) != 0)
{
/*
* Not single-stepping, but one or more global
* breakpoints are in effect. for each global
* breakpoint, eval condition; if any are true, stop
* execution, otherwise resume execution.
*/
for (brk = FALSE, i = 0, bp = ctx->dbgcxbp ;
i < DBGBPMAX ; ++bp, ++i)
{
if ((bp->dbgbpflg & DBGBPFUSED)
&& !(bp->dbgbpflg & DBGBPFDISA)
&& bp->dbgbptarg == MCMONINV
&& dbgbpeval(ctx, bp, fr))
{
brk = TRUE;
bp->dbgbpflg |= DBGBPFDISA; /* auto disable */
bphit = i + 1;
break;
}
}
}
else
{
/* we're not at a global breakpoint */
brk = FALSE;
}
/* if there's no breakpoint, don't stop */
if (!brk)
return;
}
}
/* note that we're in the debugger, to avoid re-entry */
ctx->dbgcxflg |= DBGCXFIND;
/*
* unlock the object, in case we mess with it in the debugger;
* before we do, though, remember the current execution offset
* within the object, so that we can restore the pointer before we
* return
*/
if (instrp != 0 && fr != 0 && fr->dbgftarg != MCMONINV)
{
exec_ofs = *instrp - mcmobjptr(ctx->dbgcxmem, (mcmon)fr->dbgftarg);
mcmunlck(ctx->dbgcxmem, (mcmon)fr->dbgftarg);
}
else
exec_ofs = 0;
/* remember the current error frame, so we can copy its arguments */
inner_frame = ctx->dbgcxerr->errcxptr;
ERRBEGIN(ctx->dbgcxerr)
/*
* if we're stopping on an error, copy the error parameters from
* the inner frame so they'll be available for messages within
* this new enclosing frame
*/
if (err != 0)
errcopyargs(ctx->dbgcxerr, inner_frame);
/* call user-interface main command processor routine */
dbgucmd(ctx, bphit, err, &exec_ofs);
ERRCLEAN(ctx->dbgcxerr)
/* make sure we clear the in-debugger flag if we exit via an error */
ctx->dbgcxflg &= ~DBGCXFIND;
ERRENDCLN(ctx->dbgcxerr);
/* relock the object */
if (instrp != 0 && fr != 0 && fr->dbgftarg != MCMONINV)
*instrp = mcmlck(ctx->dbgcxmem, (mcmon)fr->dbgftarg) + exec_ofs;
/* we're no longer in the debugger */
ctx->dbgcxflg &= ~DBGCXFIND;
}
/* activate debugger; returns TRUE if no debugger is present */
int dbgstart(dbgcxdef *ctx)
{
if (!ctx || !(ctx->dbgcxflg & DBGCXFOK)) return TRUE;
ctx->dbgcxflg |= DBGCXFSS; /* activate single-step mode */
return FALSE;
}
/* save a text string in the dbgcxname buffer; TRUE ==> can't store it */
static int dbgnamsav(dbgcxdef *ctx, char *nam, uint *ofsp)
{
size_t len = strlen(nam) + 1;
if (ctx->dbgcxnam
&& ctx->dbgcxnamf + len < ctx->dbgcxnams)
{
memcpy(ctx->dbgcxnam + ctx->dbgcxnamf, nam, len);
*ofsp = ctx->dbgcxnamf;
ctx->dbgcxnamf += len;
return FALSE;
}
else
return TRUE; /* uanble to store - return error */
}
/* delete a string from the dbgcxname area, adjusting bp/wx offsets */
static void dbgnamdel(dbgcxdef *ctx, uint ofs)
{
int i;
dbgbpdef *bp;
dbgwxdef *wx;
uint delsiz;
/* if no name text is being stored, we need do nothing */
if (!ctx->dbgcxnam) return;
/* compute size of area to be deleted */
delsiz = strlen(ctx->dbgcxnam + ofs) + 1;
/* go through breakpoints, moving text if necessary */
for (i = DBGBPMAX, bp = ctx->dbgcxbp ; i ; ++bp, --i)
{
if ((bp->dbgbpflg & DBGBPFUSED) && (bp->dbgbpflg & DBGBPFNAME)
&& bp->dbgbpnam > ofs)
bp->dbgbpnam -= delsiz;
if ((bp->dbgbpflg & DBGBPFUSED) && (bp->dbgbpflg & DBGBPFCONDNAME)
&& bp->dbgbpcondnam > ofs)
bp->dbgbpcondnam -= delsiz;
}
/* do the same for the watch expressions */
for (i = DBGWXMAX, wx = ctx->dbgcxwx ; i ; ++wx, --i)
{
if ((wx->dbgwxflg & DBGWXFUSED) && (wx->dbgwxflg & DBGWXFNAME)
&& wx->dbgwxnam > ofs)
wx->dbgwxnam -= delsiz;
}
/* now actually remove the string from the dbgcxname area */
if (ctx->dbgcxnamf - ofs - delsiz)
memmove(ctx->dbgcxnam + ofs, ctx->dbgcxnam + ofs + delsiz,
(size_t)(ctx->dbgcxnamf - ofs - delsiz));
ctx->dbgcxnamf -= delsiz;
}
/*
* Set a breakpoint at an object + offset location, optionally with a
* condition. We'll find a new slot for the breakpoint.
*/
int dbgbpat(dbgcxdef *ctx, objnum target, objnum self,
uint ofs, int *bpnum, char *bpname, int toggle, char *cond,
int *did_set)
{
int i;
dbgbpdef *bp;
/* find a slot for the new breakpoint */
for (i = 0, bp = ctx->dbgcxbp ; i < DBGBPMAX ; ++bp, ++i)
{
/* if this one's free, use it */
if (!(bp->dbgbpflg & DBGBPFUSED))
break;
}
/*
* Note that we don't check for a valid slot yet, because we may not
* end up setting a new breakpoint.
*/
/* tell the caller which breakpoint we set */
*bpnum = i + 1;
/* go set the breakpoint at the ID we allocated */
return dbgbpatid(ctx, *bpnum, target, self, ofs,
bpname, toggle, cond, did_set);
}
/*
* Set a breakpoint at an object + offset location, optionally with a
* condition, using an existing breakpoint slot. If the slot is already
* in use, we'll return an error.
*/
int dbgbpatid(dbgcxdef *ctx, int bpnum, objnum target, objnum self,
uint ofs, char *bpname, int toggle, char *cond,
int *did_set)
{
uchar *objp;
int done;
dbgbpdef *bp;
int err;
objnum condobj = MCMONINV;
/* get the slot */
--bpnum;
bp = &ctx->dbgcxbp[bpnum];
/* if this is a global breakpoint, no OPCBP is needed */
if (target == MCMONINV)
{
/* compile the condition, with no local frame */
dbgfdef fr;
/* make sure the slot is valid and not in use */
if (bpnum == DBGBPMAX)
return ERR_MANYBP;
else if ((bp->dbgbpflg & DBGBPFUSED) != 0)
return ERR_BPINUSE;
fr.dbgffr = 0;
fr.dbgftarg = MCMONINV;
if ((err = dbgcompile(ctx, cond, &fr, &condobj, FALSE)) != 0)
return err;
/* set up the breakpoint record */
bp->dbgbpflg = DBGBPFUSED | DBGBPFCOND;
bp->dbgbpself =
bp->dbgbptarg = MCMONINV;
bp->dbgbpofs = 0;
bp->dbgbpcond = condobj;
/* save the name */
if (!dbgnamsav(ctx, bpname, &bp->dbgbpnam))
bp->dbgbpflg |= DBGBPFNAME;
/* save the condition */
if (!dbgnamsav(ctx, cond, &bp->dbgbpcondnam))
bp->dbgbpflg |= DBGBPFCONDNAME;
/* it's a global breakpoint */
ctx->dbgcxflg |= DBGCXFGBP;
/* success */
return 0;
}
/* lock the object */
objp = mcmlck(ctx->dbgcxmem, (mcmon)target);
/* skip any ENTER, CHKARGC, or FRAME instructions */
for (done = FALSE ; !done ; )
{
switch(*(objp + ofs))
{
case OPCENTER:
ofs += 3;
break;
case OPCCHKARGC:
ofs += 2;
break;
case OPCFRAME:
ofs += 1 + osrp2(objp + ofs + 1);
break;
default:
done = TRUE;
break;
}
}
/* presume we're going to set a new breakpoint */
if (did_set != 0)
*did_set = TRUE;
/* see what kind of instruction we've found */
if (*(objp + ofs) == OPCBP)
{
/* if we're toggling and not adding a condition, remove the bp */
if (toggle && (cond == 0 || *cond == '\0'))
{
int i;
/* tell the caller we're removing a breakpoint */
if (did_set != 0)
*did_set = FALSE;
/* remove all breakpoints matching this description */
for (i = 0, bp = ctx->dbgcxbp ; i < DBGBPMAX ; ++bp, ++i)
{
if ((bp->dbgbpflg & DBGBPFUSED) != 0
&& bp->dbgbptarg == target
&& bp->dbgbpofs == ofs
&& bp->dbgbpself == self)
{
err = dbgbpdel(ctx, i+1);
goto return_error;
}
}
}
/* no error */
err = 0;
}
else if (*(objp + ofs) != OPCLINE)
{
/* can't set a breakpoint here - it's not a line */
err = ERR_BPNOTLN;
}
else if (bpnum == DBGBPMAX)
{
/* no slots left for a new breakpoint */
err = ERR_MANYBP;
}
else if ((bp->dbgbpflg & DBGBPFUSED) != 0)
{
/* slot is already in use */
return ERR_BPINUSE;
}
else
{
/* presume we won't have an error */
err = 0;
}
/* check for a condition attached to the breakpoint */
if (cond && *cond)
{
dbgfdef fr;
fr.dbgffr = osrp2(objp + ofs + 2);
fr.dbgftarg = target;
err = dbgcompile(ctx, cond, &fr, &condobj, FALSE);
}
if (err == 0)
{
/* set up the breakpoint structure */
bp->dbgbpflg = DBGBPFUSED;
bp->dbgbpself = self;
bp->dbgbptarg = target;
bp->dbgbpofs = ofs;
if (cond && *cond)
{
/* remember the condition */
bp->dbgbpcond = condobj;
bp->dbgbpflg |= DBGBPFCOND;
if (!dbgnamsav(ctx, cond, &bp->dbgbpcondnam))
bp->dbgbpflg |= DBGBPFCONDNAME;
}
/*
* replace the OPCLINE in the p-code with a breakpoint
* instruction, so that the interpreter actually stops when it
* reaches this point
*/
*(objp + ofs) = OPCBP;
mcmtch(ctx->dbgcxmem, (mcmon)target);
}
return_error:
mcmunlck(ctx->dbgcxmem, (mcmon)target);
/* free condition object if we have one */
if (err && condobj != MCMONINV)
mcmfre(ctx->dbgcxmem, condobj);
/* store breakpoint name for displaying to user */
if (!err && !dbgnamsav(ctx, bpname, &bp->dbgbpnam))
bp->dbgbpflg |= DBGBPFNAME;
return err;
}
/*
* Set a new condition for an existing breakpoint
*/
int dbgbpsetcond(dbgcxdef *ctx, int bpnum, char *cond)
{
dbgbpdef *bp;
int err;
objnum condobj;
dbgfdef fr;
/* make sure we have a valid breakpoint */
--bpnum;
bp = &ctx->dbgcxbp[bpnum];
if (bpnum < 0 || bpnum >= DBGBPMAX || !(bp->dbgbpflg & DBGBPFUSED))
return ERR_BPNSET;
/* check to see if it's a global breakpoint */
if (bp->dbgbptarg == MCMONINV)
{
/* it's global - compile the condition with no local frame */
fr.dbgffr = 0;
fr.dbgftarg = MCMONINV;
err = dbgcompile(ctx, cond, &fr, &condobj, FALSE);
}
else
{
uchar *objp;
/* lock the object containing the code containing the breakpoint */
objp = mcmlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
/* compile the condition */
fr.dbgffr = osrp2(objp + bp->dbgbpofs + 2);
fr.dbgftarg = bp->dbgbptarg;
err = dbgcompile(ctx, cond, &fr, &condobj, FALSE);
/* done with the object */
mcmunlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
}
/* don't proceed if we couldn't compile the expression */
if (err != 0)
return err;
/* delete the old condition object */
if (bp->dbgbpflg & DBGBPFCOND)
mcmfre(ctx->dbgcxmem, bp->dbgbpcond);
/* delete the old condition name */
if (bp->dbgbpflg & DBGBPFCONDNAME)
{
dbgnamdel(ctx, bp->dbgbpcondnam);
bp->dbgbpflg &= ~DBGBPFCONDNAME;
}
/* remember the new condition object */
bp->dbgbpcond = condobj;
/* remember the new condition name */
if (!dbgnamsav(ctx, cond, &bp->dbgbpcondnam))
bp->dbgbpflg |= DBGBPFCONDNAME;
/* success */
return 0;
}
/*
* Determine if there's a breakpoint at a given code location. If there
* is, we fill in *bpnum with the identifier for the breakpoint and
* return true, otherwise we return false.
*/
int dbgisbp(dbgcxdef *ctx, objnum target, objnum self, uint ofs, int *bpnum)
{
dbgbpdef *bp;
int i;
/* scan the breakpoints for one matching the given description */
for (i = 0, bp = ctx->dbgcxbp ; i < DBGBPMAX ; ++bp, ++i)
{
/* check to see if this one matches the one we're looking for */
if ((bp->dbgbpflg & DBGBPFUSED) != 0
&& bp->dbgbptarg == target
&& bp->dbgbpself == self
&& bp->dbgbpofs == ofs)
{
/*
* this is the one - set its ID (which is 1-based) and
* return success
*/
if (bpnum != 0)
*bpnum = i+1;
return TRUE;
}
}
/* didn't find any breakpoints matching this description */
return FALSE;
}
/*
* Determine if the given breakpoint is enabled
*/
int dbgisbpena(dbgcxdef *ctx, int bpnum)
{
dbgbpdef *bp;
/* make sure the identifier is in range */
--bpnum;
if (bpnum < 0 || bpnum >= DBGBPMAX)
return FALSE;
/* get the breakpoint */
bp = &ctx->dbgcxbp[bpnum];
/* return true if it exists and isn't disabled, false otherwise */
return ((bp->dbgbpflg & DBGBPFUSED) != 0
&& (bp->dbgbpflg & DBGBPFDISA) == 0);
}
/* set a breakpoint at a symbolic address: function or object.property */
int dbgbpset(dbgcxdef *ctx, char *addr, int *bpnum)
{
char *p;
char psav;
char *tok1;
char tok1sav;
char *tok1end;
char *tok2;
toksdef sym1;
toksdef sym2;
toktdef *tab;
uint hsh1;
uint hsh2;
objnum objn;
uint ofs;
uchar *objp;
prpdef *prp;
dattyp typ;
objnum target;
char *end;
char buf1[40];
char buf2[40];
/* determine what kind of address we have */
for (p = addr ; *p && t_isspace(*p) ; ++p) ;
for (tok1 = addr = p ; *p && TOKISSYM(*p) ; ++p) ;
/* see if the very first thing is "when" */
if (!strnicmp(tok1, "when ", (size_t)5))
{
for (end = tok1 + 5 ; t_isspace(*end) ; ++end) ;
return(dbgbpat(ctx, MCMONINV, MCMONINV, 0, bpnum,
addr, FALSE, end, 0));
}
/* see if we have a second token */
for (tok1end = p ; t_isspace(*p) ; ++p) ;
if (*p == '.')
{
for (++p ; t_isspace(*p) ; ++p) ;
for (tok2 = p ; TOKISSYM(*p) ; ++p) ;
psav = *p;
*p = '\0';
end = (psav ? p + 1 : p);
}
else
{
tok2 = (char *)0;
end = tok1end;
if (*end) ++end;
p = (char *)0;
}
tok1sav = *tok1end;
*tok1end = '\0';
/* look up the symbols */
tab = (toktdef *)ctx->dbgcxtab;
if (ctx->dbgcxprs->prscxtok->tokcxflg & TOKCXCASEFOLD)
{
strcpy(buf1, tok1);
os_strlwr(buf1);
tok1 = buf1;
}
hsh1 = tokhsh(tok1);
if (!(*tab->toktfsea)(tab, tok1, (int)strlen(tok1), hsh1, &sym1))
return ERR_BPSYM;
objn = sym1.toksval;
if (tok2)
{
/* we have "object.property" */
if (ctx->dbgcxprs->prscxtok->tokcxflg & TOKCXCASEFOLD)
{
strcpy(buf2, tok2);
os_strlwr(buf2);
tok2 = buf2;
}
hsh2 = tokhsh(tok2);
if (!(*tab->toktfsea)(tab, tok2, (int)strlen(tok2), hsh2, &sym2))
return ERR_BPSYM;
if (sym1.tokstyp != TOKSTOBJ) return ERR_BPOBJ;
/* we need to look up the property */
ofs = objgetap(ctx->dbgcxmem, objn, (prpnum)sym2.toksval,
&target, FALSE);
if (!ofs) return ERR_BPNOPRP;
/* make sure the property is code */
objp = mcmlck(ctx->dbgcxmem, (mcmon)target);
prp = objofsp((objdef *)objp, ofs);
typ = prptype(prp);
ofs = ((uchar *)prpvalp(prp)) - objp;
mcmunlck(ctx->dbgcxmem, (mcmon)target);
if (typ != DAT_CODE) return ERR_BPPRPNC;
}
else
{
/* we have just "function" */
if (sym1.tokstyp != TOKSTFUNC) return ERR_BPFUNC;
ofs = 0; /* functions always start at offset zero */
target = objn; /* for function, target is always same as object */
objn = MCMONINV; /* there is no "self" for a function */
}
/* undo our changes to the string text */
if (p) *p = psav;
*tok1end = tok1sav;
/* check for a "when" expression */
if (*end)
{
while (t_isspace(*end)) ++end;
if (*end)
{
if (strnicmp(end, "when ", (size_t)5))
return ERR_EXTRTXT;
for (end += 5 ; t_isspace(*end) ; ++end) ;
}
}
/* set a breakpoint at this object + offset */
return(dbgbpat(ctx, target, objn, ofs, bpnum, addr, FALSE, end, 0));
}
/* delete a breakpoint */
int dbgbpdel(dbgcxdef *ctx, int bpnum)
{
dbgbpdef *bp = &ctx->dbgcxbp[--bpnum];
dbgbpdef *bp2;
uchar *objp;
int i;
int bpset;
/* make sure it's a valid breakpoint */
if (bpnum < 0 || bpnum >= DBGBPMAX || !(bp->dbgbpflg & DBGBPFUSED))
return ERR_BPNSET;
/* see if we now have NO breakpoints set on this location */
for (bpset = FALSE, i = DBGBPMAX, bp2 = ctx->dbgcxbp ; i ; ++bp2, --i)
{
if (bp != bp2 && (bp2->dbgbpflg & DBGBPFUSED)
&& bp2->dbgbptarg == bp->dbgbptarg
&& bp2->dbgbpofs == bp->dbgbpofs)
{
bpset = TRUE;
break;
}
}
/* if no other bp's here, convert the OPCBP back into an OPCLINE */
if (!bpset)
{
if (bp->dbgbptarg == MCMONINV)
{
/* no more global breakpoints - delete global BP flag */
ctx->dbgcxflg &= ~DBGCXFGBP;
}
else
{
/* convert the OPCBP back into OPCLINE */
objp = mcmlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
*(objp + bp->dbgbpofs) = OPCLINE;
mcmtch(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
mcmunlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
}
}
/* delete the names from the buffer */
if (bp->dbgbpflg & DBGBPFNAME)
dbgnamdel(ctx, bp->dbgbpnam); /* delete name from text buffer */
if (bp->dbgbpflg & DBGBPFCONDNAME)
dbgnamdel(ctx, bp->dbgbpcondnam);
/* the slot is no longer in use */
bp->dbgbpflg &= ~DBGBPFUSED;
/* free condition object, if one has been allocated */
if (bp->dbgbpflg & DBGBPFCOND)
{
mcmfre(ctx->dbgcxmem, bp->dbgbpcond);
bp->dbgbpflg &= ~DBGBPFCOND;
}
return 0;
}
/* list breakpoints using user callback */
void dbgbplist(dbgcxdef *ctx,
void (*dispfn)(void *ctx, const char *str, int len),
void *dispctx)
{
dbgbpdef *bp = ctx->dbgcxbp;
int i;
char *p;
char buf[10];
/* if we're not recording names, there's nothing we can do */
if (!ctx->dbgcxnam) return;
for (i = 0 ; i < DBGBPMAX ; ++bp, ++i)
{
if ((bp->dbgbpflg & DBGBPFUSED) && (bp->dbgbpflg & DBGBPFNAME))
{
p = ctx->dbgcxnam + bp->dbgbpnam;
sprintf(buf, "%d: ", i + 1);
(*dispfn)(dispctx, buf, (int)strlen(buf));
(*dispfn)(dispctx, p, (int)strlen(p));
if (bp->dbgbpflg & DBGBPFDISA)
(*dispfn)(dispctx, " [disabled]", 11);
(*dispfn)(dispctx, "\n", 1);
}
}
}
/* enumerate breakpoints */
void dbgbpenum(dbgcxdef *ctx,
void (*cbfunc)(void *cbctx, int bpnum, const char *desc,
const char *cond, int disabled), void *cbctx)
{
dbgbpdef *bp = ctx->dbgcxbp;
int i;
char *bpname;
char *cond;
/* if we're not recording names, there's nothing we can do */
if (ctx->dbgcxnam == 0)
return;
/* run through our list of breakpoints */
for (i = 0 ; i < DBGBPMAX ; ++bp, ++i)
{
/*
* if this slot is in use, and has a name, include it in the
* enumeration
*/
if ((bp->dbgbpflg & DBGBPFUSED) && (bp->dbgbpflg & DBGBPFNAME))
{
/* get the name */
bpname = ctx->dbgcxnam + bp->dbgbpnam;
/* if there's a condition, get the condition */
cond = ((bp->dbgbpflg & DBGBPFCONDNAME) != 0
? ctx->dbgcxnam + bp->dbgbpcondnam : 0);
/* invoke the callback */
(*cbfunc)(cbctx, i + 1, bpname, cond,
(bp->dbgbpflg & DBGBPFDISA) != 0);
}
}
}
/* enable/disable a breakpoint */
int dbgbpdis(dbgcxdef *ctx, int bpnum, int disable)
{
dbgbpdef *bp = &ctx->dbgcxbp[--bpnum];
/* make sure it's a valid breakpoint */
if (bpnum < 0 || bpnum >= DBGBPMAX || !(bp->dbgbpflg & DBGBPFUSED))
return ERR_BPNSET;
if (disable) bp->dbgbpflg |= DBGBPFDISA;
else bp->dbgbpflg &= ~DBGBPFDISA;
return 0;
}
/*
* Call user callback with lindef data for each breakpoint; global
* breakpoints are NOT passed to user callback, since they don't
* correspond to source locations.
*/
void dbgbpeach(dbgcxdef *ctx, void (*fn)(void *, int, uchar *, uint),
void *fnctx)
{
int i;
dbgbpdef *bp = ctx->dbgcxbp;
uchar *p;
uint len;
for (i = 0 ; i < DBGBPMAX ; ++bp, ++i)
{
if ((bp->dbgbpflg & DBGBPFUSED)
&& bp->dbgbptarg != MCMONINV)
{
p = mcmlck(ctx->dbgcxmem, (mcmon)bp->dbgbptarg);
p += bp->dbgbpofs + 1;
len = *p - 4;
p += 3;
(*fn)(fnctx, (int)(*p), p + 1, len);