File: break.c

package info (click to toggle)
sdcc 4.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 112,804 kB
  • sloc: ansic: 622,685; cpp: 259,454; makefile: 81,253; sh: 40,203; asm: 19,222; perl: 12,139; yacc: 7,761; awk: 3,378; lisp: 1,677; python: 1,097; lex: 1,028; sed: 76
file content (613 lines) | stat: -rw-r--r-- 18,965 bytes parent folder | download | duplicates (7)
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
/*-------------------------------------------------------------------------
  break.c - Source file for break point management
        Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!
-------------------------------------------------------------------------*/

#include "sdcdb.h"
#undef DATADIR
#include "symtab.h"
#include "break.h"
#include "cmd.h"
#include "newalloc.h"

hTab *bptable = NULL;
char doingSteps    = 0;
char userBpPresent = 0;

/* call stack can be 1024 deep */
STACK_DCL(callStack,function *,1024)

#ifdef SDCDB_DEBUG
char *debug_bp_type_strings[] =
    {"ERR-0",
     "DATA",
    "CODE"    ,
    "A_CODE"  ,
    "USER"    ,
    "STEP"    ,
    "NEXT"    ,
    "FENTRY"  ,
    "FEXIT", "TMPUSER", ""};
#endif

static long bpnum = 0;

long getLastBreakptNumber()
{
    return bpnum;
}

void resetHitCount()
{
    int k;
    breakp *bp;
    for ( bp = hTabFirstItem(bptable,&k); bp ;
          bp = hTabNextItem(bptable,&k))
    {
        bp->hitCnt    = 0;
        bp->ignoreCnt = 0;
    }
}

/*-----------------------------------------------------------------*/
/* setBreakPoint - creates an entry in the break point table       */
/*-----------------------------------------------------------------*/
int setBreakPoint (unsigned addr, char addrType, char bpType,
        int (*callBack)(unsigned,breakp *,context *) ,
        char *fileName, int lineno)
{
    breakp *bp, *bpl;

    Dprintf(D_break, ("setBreakPoint: addr:%x atype:%s bpType:%s [%s:%d]\n",
        addr,
        debug_bp_type_strings[(int)addrType],
        debug_bp_type_strings[(int)bpType],
        fileName, lineno+1));

    /* allocate & init a new bp */
    bp = Safe_calloc(1,sizeof(breakp));
    bp->addr = addr;
    bp->addrType = addrType;
    bp->bpType = bpType;
    bp->callBack = callBack;
    bp->bpnum = ((bpType == USER || bpType == TMPUSER)? ++bpnum : 0);
    bp->filename = fileName;
    bp->lineno = lineno;

    /* if this is an user break point then
       check if there already exists one for this address */
    if (bpType == USER || bpType == TMPUSER)
    {
        for ( bpl = hTabFirstItemWK(bptable,addr) ; bpl;
              bpl = hTabNextItemWK(bptable))
        {

            /* if also a user break point then issue Note : */
            if (bpl->bpType == USER || bpType == TMPUSER)
                fprintf(stderr,"Note: breakpoint %d also set at pc 0x%x\n",
                        bpl->bpnum,bpl->addr);
        }

        fprintf(stderr,"Breakpoint %d at 0x%x: file %s, line %d.\n",
                bp->bpnum, addr, fileName, lineno+1);

        userBpPresent++;
    }

    if (bpType != STEP && bpType != NEXT)
    {
        /* if a break point does not already exist then
           send command to simulator to add one */
        if (!hTabSearch(bptable,addr))
            /* send the break command to the simulator */
            simSetBP (addr);
    }

    /* now add the break point to list */
    hTabAddItem(&bptable,addr,bp);

    return bp->bpnum ;
}

/*-----------------------------------------------------------------*/
/* deleteSTEPbp - deletes all step break points                    */
/*-----------------------------------------------------------------*/
void deleteSTEPbp ()
{
    breakp *bp;
    int k;

    Dprintf(D_break, ("break: Deleting all STEP BPs\n"));
    /* for break points delete if they are STEP */
    for ( bp = hTabFirstItem(bptable,&k); bp ;
          bp = hTabNextItem(bptable,&k))
    {

        /* if this is a step then delete */
        if (bp->bpType == STEP)
        {
            hTabDeleteItem(&bptable,bp->addr,bp,DELETE_ITEM,NULL);
            Safe_free(bp);
        }
    }

}

/*-----------------------------------------------------------------*/
/* deleteNEXTbp - deletes all step break points                    */
/*-----------------------------------------------------------------*/
void deleteNEXTbp ()
{
    breakp *bp;
    int k;

    Dprintf(D_break, ("break: Deleting all NEXT BPs\n"));

    /* for break points delete if they are NEXT */
    for ( bp = hTabFirstItem(bptable,&k); bp ;
          bp = hTabNextItem(bptable,&k))
    {

        /* if this is a step then delete */
        if (bp->bpType == NEXT)
        {
            hTabDeleteItem(&bptable,bp->addr,bp,DELETE_ITEM,NULL);
            Safe_free(bp);
        }
    }

}

static void freeUSERbp(breakp *bp)
{
    if ( bp->commands )
        Safe_free(bp->commands);
    if ( bp->condition )
        Safe_free(bp->condition);
    Safe_free(bp);
}

/*-----------------------------------------------------------------*/
/* deleteUSERbp - deletes USER break point with number             */
/*-----------------------------------------------------------------*/
void deleteUSERbp (int bpnum)
{
    breakp *bp;
    int k;

    Dprintf(D_break, ("break: deleteUSERbp %d\n", bpnum));

    /* for break points delete if they are STEP */
    for ( bp = hTabFirstItem(bptable,&k); bp ;
          bp = hTabNextItem(bptable,&k)) {

        /* if this is a user then delete if break
           point matches or bpnumber == -1 (meaning delete
           all user break points */
        if ((bp->bpType == USER || bp->bpType == TMPUSER )
            && ( bp->bpnum == bpnum || bpnum == -1))
        {
            hTabDeleteItem(&bptable,bp->addr,bp,DELETE_ITEM,NULL);

            /* if this leaves no other break points then
               send command to simulator to delete bp from this addr */
            if (hTabSearch(bptable,bp->addr) == NULL) {
                simClearBP (bp->addr);
                Dprintf(D_break, ("break: deleteUSERbp:simClearBP 0x%x\n", bp->addr));

            }
            fprintf(stdout,"Deleted breakpoint %d\n",bp->bpnum);
            userBpPresent-- ;
            freeUSERbp(bp);
            if (bpnum == -1)
                continue ;
            else
                break;
        }
    }

    if (!bp && bpnum != -1)
        fprintf(stderr,"No breakpoint number %d.\n",bpnum);
}

/*-----------------------------------------------------------------*/
/* setUserbpCommand - set commandstring for breakpoint             */
/*-----------------------------------------------------------------*/
void setUserbpCommand (int bpnum, char *cmds)
{
    breakp *bp;
    int k;
    Dprintf(D_break, ("break: setUserbpCommand %d: commands:\n%send\n",
                      bpnum, cmds));

    for ( bp = hTabFirstItem(bptable,&k); bp ;
          bp = hTabNextItem(bptable,&k))
    {
        if ((bp->bpType == USER || bp->bpType == TMPUSER )
            && ( bp->bpnum == bpnum ))
        {
            if ( bp->commands )
                Safe_free(bp->commands);
            bp->commands = cmds;
            return;
        }
    }
    fprintf(stderr,"No breakpoint number %d.\n",bpnum);
}

/*-----------------------------------------------------------------*/
/* setUserbpCondition - set condition string for breakpoint        */
/*-----------------------------------------------------------------*/
void setUserbpCondition (int bpnum, char *cond)
{
    breakp *bp;
    int k;
    Dprintf(D_break, ("break: setUserbpCondition %d: condition:'%s'\n",
                      bpnum, cond?cond:""));

    for ( bp = hTabFirstItem(bptable,&k); bp ;
          bp = hTabNextItem(bptable,&k))
    {
        if ((bp->bpType == USER || bp->bpType == TMPUSER )
            && ( bp->bpnum == bpnum ))
        {
            if ( bp->condition )
                Safe_free(bp->condition);
            bp->condition = cond;
            return;
        }
    }
    fprintf(stderr,"No breakpoint number %d.\n",bpnum);
}

/*-----------------------------------------------------------------*/
/* setUserbpIgnCount - set ignorecount for breakpoint              */
/*-----------------------------------------------------------------*/
void setUserbpIgnCount (int bpnum, int ignorecnt )
{
    breakp *bp;
    int k;
    Dprintf(D_break, ("break: setUserbpIgnCount %d: ignorecnt=%d\n",
                      bpnum, ignorecnt));

    for ( bp = hTabFirstItem(bptable,&k); bp ;
          bp = hTabNextItem(bptable,&k))
    {
        if ((bp->bpType == USER || bp->bpType == TMPUSER )
            && ( bp->bpnum == bpnum ))
        {
            bp->ignoreCnt = bp->hitCnt + ignorecnt;
            return;
        }
    }
    fprintf(stderr,"No breakpoint number %d.\n",bpnum);
}

/*-----------------------------------------------------------------*/
/* listUSERbp - list all user break points                         */
/*-----------------------------------------------------------------*/
void listUSERbp ()
{
    breakp *bp;
    int k, isuser;

    /* if there are none then say so & return */
    if (!userBpPresent) {
        fprintf(stdout,"No breakpoints.\n");
        return ;
    }
    fprintf(stdout,"Num Type           Disp Enb Address    What\n");
    for ( bp = hTabFirstItem(bptable,&k) ; bp ;
          bp = hTabNextItem(bptable,&k)) {

        isuser = 0;
        if (bp->bpType == USER ) {
            fprintf(stdout,"%-3d breakpoint     keep y   0x%08x at %s:%d\n",
                    bp->bpnum,bp->addr,
                    bp->filename,bp->lineno+1);
            isuser = 1;
        }
        else if (bp->bpType == TMPUSER ) {
            fprintf(stdout,"%-3d breakpoint      del y   0x%08x at %s:%d\n",
                    bp->bpnum,bp->addr,
                    bp->filename,bp->lineno+1);
            isuser = 1;
        }
        if ( ! isuser )
            continue;
        if ( bp->ignoreCnt > bp->hitCnt )
            fprintf(stdout,"\tignore next %d hits\n",
                    bp->ignoreCnt - bp->hitCnt );
        if ( bp->condition )
            fprintf(stdout,"\tstop only if %s\n",bp->condition );
        if ( bp->hitCnt > 0 )
            fprintf(stdout,"\tbreakpoint already hit %d time%s\n",
                    bp->hitCnt,bp->hitCnt>1?"s":"" );


    }
}

/*-----------------------------------------------------------------*/
/* simStopBPCB - simulator stopped break point                     */
/*-----------------------------------------------------------------*/
static int simStopBPCB( unsigned int addr)
{
  fprintf(stdout,"Simulator stopped at Address 0x%04x\n",addr);
  fprintf(stdout,"%s",simResponse());
  return 1;
}

/*-----------------------------------------------------------------*/
/* clearUSERbp - deletes USER break point at address               */
/*-----------------------------------------------------------------*/
void clearUSERbp ( unsigned int addr )
{
    breakp *bp;

    /* for break points delete if they are STEP */
    for ( bp = hTabFirstItemWK(bptable,addr); bp ;
          bp = hTabNextItemWK(bptable)) {

        /* if this is a step then delete */
        if (bp->bpType == USER || bp->bpType == TMPUSER)
        {
            hTabDeleteItem(&bptable,bp->addr,bp,DELETE_ITEM,NULL);

            /* if this leaves no other break points then
               send command to simulator to delete bp from this addr */
            if (hTabSearch(bptable,bp->addr) == NULL)
            {
                simClearBP(bp->addr);
            }
            fprintf(stdout,"Deleted breakpoint %d\n",
                    bp->bpnum);
            userBpPresent-- ;
            freeUSERbp(bp);
            break;
        }
    }

    if (!bp)
        fprintf(stderr,"No breakpoint at address 0x%x.\n",addr);
}

/*-----------------------------------------------------------------*/
/* dispatchCB - will lookup the bp table and dispatch the cb funcs */
/*-----------------------------------------------------------------*/
int dispatchCB (unsigned addr, context *ctxt)
{
    breakp *bp;
    int rv =0;

    Dprintf(D_break, ("break: dispatchCB: addr:0x%x \n", addr));

    /* if no break points set for this address
       then use a simulator stop break point */
    if ((bp = hTabFirstItemWK(bptable,addr)) == NULL) {
      if ( doingSteps == 2 ) return 1;
      if ( doingSteps ) return 0;
      if ( contsim    ) return 0;
      return simStopBPCB(addr);
    }

    /* dispatch the call back functions */
    for (; bp; bp = hTabNextItemWK(bptable))
    {
        rv += (*bp->callBack)(addr,bp,ctxt);
    }

    if (rv == 0) {
      Dprintf(D_break, ("break: dispatchCB: WARNING rv==0\n"));
    }

    return rv;
}

/*-----------------------------------------------------------------*/
/* fentryCB - callback function for function entry                 */
/*-----------------------------------------------------------------*/
BP_CALLBACK(fentryCB)
{
    function *func;

    /* we save the value of SP
       which will be used to display the value of local variables
       and parameters on the stack */
    ctxt->func->stkaddr = simGetValue (0x81,'I',1);

    Dprintf(D_break, ("break: fentryCB: BP_CALLBACK entry %s sp=0x%02x %p\n",
                      ctxt->func->sym->name,
                      ctxt->func->stkaddr, p_callStack));

    /* and set laddr of calling function to return addr from stack */
    if ((func = STACK_PEEK(callStack)))
    {
        /* extern stack ?? 'A' */
        func->laddr = simGetValue (ctxt->func->stkaddr-1,'B',2);
    }
    /* add the current function into the call stack */
    STACK_PUSH(callStack,ctxt->func);

    return 0;
}

/*-----------------------------------------------------------------*/
/* fexitCB - call back for function exit                           */
/*-----------------------------------------------------------------*/
BP_CALLBACK(fexitCB)
{
    function *func;
    /* pop the top most from the call stack */
    func = STACK_POP(callStack);

    if (!func)
    {
        fprintf(stdout, "Stack underflow\n");
        return 1;
    }

    Dprintf(D_break, ("break: fexitCB: BP_CALLBACK entry %s %p\n", func->sym->name, p_callStack));

    /* check main function */
    if ( !strcmp(func->sym->name, "main"))
    {
        fprintf(stdout, "Program exited with code %lu.\n", simGetValue (0x82,'I',2));
        return 1;
    }
    return 0;
}
/*-----------------------------------------------------------------*/
/* userBpCB - call back function for user break points             */
/*-----------------------------------------------------------------*/
BP_CALLBACK(userBpCB)
{
    bp->hitCnt++ ;
    Dprintf(D_break, ("break: userBpCB: BP_CALLBACK entry hit=%d ignor=%d\n",
                      bp->hitCnt, bp->ignoreCnt));

    if ( bp->ignoreCnt > bp->hitCnt )
        return 0;

    if ( bp->condition )
    {
        if (! conditionIsTrue( bp->condition, ctxt ))
            return 0;
    }

    if ( bp->commands )
    {
        Dprintf(D_break, ("break: userBpCB: commands:%p\n", bp->commands));
        setCmdLine(bp->commands);
    }

    if (srcMode == SRC_CMODE) {
        fprintf(stdout,"Breakpoint %d, %s() at %s:%d\n",
                bp->bpnum,
                ctxt->func->sym->name,
                ctxt->func->mod->c_name,
                ctxt->cline+1);
        if (ctxt->func->mod && ctxt->cline > 0)
            fprintf(stdout,"%d\t%s",ctxt->cline+1,
                    ctxt->func->mod->cLines[ctxt->cline]->src);
    } else {
        fprintf(stdout,"Breakpoint %d, %s() at %s:%d\n",
                bp->bpnum,
                ctxt->func->sym->name,
                ctxt->func->mod->asm_name,
                ctxt->asmline+1);
        if (ctxt->func->mod && ctxt->asmline > 0)
            fprintf(stdout,"%d\t%s",ctxt->asmline+1,
                    ctxt->func->mod->asmLines[ctxt->asmline]->src);
    }

    if ( bp->bpType == TMPUSER && bp->bpnum > 0 )
    {
        hTabDeleteItem(&bptable,bp->addr,bp,DELETE_ITEM,NULL);

        /* if this leaves no other break points then
           send command to simulator to delete bp from this addr */
        if (hTabSearch(bptable,bp->addr) == NULL)
        {
            simClearBP (bp->addr);
            Dprintf(D_break, ("break: simClearBP 0x%x\n", bp->addr));

        }
        userBpPresent-- ;
        freeUSERbp(bp);
    }
    return 1;
}

/*-----------------------------------------------------------------*/
/* stepBpCB - call back function for step break points             */
/*-----------------------------------------------------------------*/
BP_CALLBACK(stepBpCB)
{
    static function *lfunc = NULL;

    Dprintf(D_break, ("break: stepBpCB: BP_CALLBACK entry\n"));

    if (srcMode == SRC_CMODE) {
  if ((lfunc && lfunc != ctxt->func) || !lfunc)
      fprintf(stdout,"%s () at %s:%d\n",
        ctxt->func->sym->name,
        ctxt->func->mod->c_name,
        ctxt->cline+1);

  if (ctxt->func->mod && ctxt->cline > 0) {
      fprintf(stdout,"%d\t%s",ctxt->cline+1 ,
        ctxt->func->mod->cLines[ctxt->cline]->src);
  }
    } else {
  if ((lfunc && lfunc != ctxt->func) || !lfunc)
      fprintf(stdout,"%s () at %s:%d\n",
        ctxt->func->sym->name,
        ctxt->func->mod->asm_name,
        ctxt->asmline);

  if (ctxt->func->mod && ctxt->cline > 0) {
      fprintf(stdout,"%d\t%s",ctxt->asmline ,
        ctxt->func->mod->asmLines[ctxt->asmline]->src);
  }
    }
    lfunc = ctxt->func;

    deleteSTEPbp();
    return 1;
}

/*-----------------------------------------------------------------*/
/* nextBpCB - call back function for next break points             */
/*-----------------------------------------------------------------*/
BP_CALLBACK(nextBpCB)
{
    static function *lfunc = NULL;

    Dprintf(D_break, ("break: nextBpCB: BP_CALLBACK entry\n"));

    if (srcMode == SRC_CMODE) {
  if ((lfunc && lfunc != ctxt->func) || !lfunc)
      fprintf(stdout,"%s () at %s:%d\n",
        ctxt->func->sym->name,
        ctxt->func->mod->c_name,
        ctxt->cline+1);

  if (ctxt->func->mod && ctxt->cline > 0)
      fprintf(stdout,"%d\t%s",ctxt->cline+1,
        ctxt->func->mod->cLines[ctxt->cline]->src);
    } else {
  if ((lfunc && lfunc != ctxt->func) || !lfunc)
      fprintf(stdout,"%s () at %s:%d\n",
        ctxt->func->sym->name,
        ctxt->func->mod->asm_name,
        ctxt->asmline);

  if (ctxt->func->mod && ctxt->asmline > 0)
      fprintf(stdout,"%d\t%s",ctxt->asmline,
        ctxt->func->mod->asmLines[ctxt->asmline]->src);

    }
    lfunc = ctxt->func;

    deleteNEXTbp();
    return 1;
}