File: cmd_break.cc

package info (click to toggle)
gpsim 0.26.1-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,200 kB
  • sloc: cpp: 86,839; sh: 18,875; asm: 16,815; ansic: 4,468; makefile: 1,308; lex: 1,129; yacc: 823; pascal: 177; perl: 93; awk: 44
file content (362 lines) | stat: -rw-r--r-- 10,656 bytes parent folder | download | duplicates (2)
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
/*
   Copyright (C) 1999 T. Scott Dattalo

This file is part of gpsim.

gpsim 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.

gpsim 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 gpsim; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */


#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <assert.h>

#include "command.h"
#include "cmd_break.h"

#include "../src/cmd_gpsim.h"
#include "../src/pic-processor.h"
#include "../src/operator.h"

cmd_break c_break;

#define CYCLE       1
#define EXECUTION   2
#define WRITE       3
#define READ        4
#define REGCHANGE   5
#define STK_OVERFLOW  7
#define STK_UNDERFLOW 8
#define WDT           9

static cmd_options cmd_break_options[] =
{
  {"c",   CYCLE,        OPT_TT_BITFLAG},
  {"e",   EXECUTION,    OPT_TT_BITFLAG},
  {"w",   WRITE,        OPT_TT_BITFLAG},
  {"r",   READ,         OPT_TT_BITFLAG},
  {"ch",  REGCHANGE,    OPT_TT_BITFLAG},
  {"so",  STK_OVERFLOW, OPT_TT_BITFLAG},
  {"su",  STK_UNDERFLOW,OPT_TT_BITFLAG},
  {"wdt", WDT,          OPT_TT_BITFLAG},
  {0,0,0}
};


cmd_break::cmd_break()
  : command("break", "br")
{ 
  brief_doc = string("Set a break point");

  long_doc = string ("The 'break' command can be used to examine or set breakpoints.\n"
                     "gpsim supports execution style breaks, register access breaks,\n"
                     "complex expression breaks, attribute breaks, and other special breaks.\n"
                     "Program Memory breaks:\n"
                     "  break e|r|w ADDRESS [,expr] [,\"message\"]\n"
                     "    Halts when the address is executed, read, or written. The ADDRESS can be \n"
                     "    a symbol or a number. If the optional expr is specified, then it must\n"
                     "    evaluate to true before the simulation will halt. The optional message\n"
		     "    allows a description to be associated with the break.\n"
                     "Register Memory breaks:\n"
                     "  break r|w|ch REGISTER [,expr] [,\"message\"]\n"
                     "    Halts when 'REGISTER' is read, written, or changed\n"
                     "    and the optional expression evaluates to true\n"
                     "  break r|w|ch boolean_expression\n"
                     "    The boolean expression can only be of the form:\n"
		     "       a) reg & mask == value\n"
		     "       b) reg == value\n"
                     "  - Note the 'ch' option is similar to the write option.\n"
                     "    The change option evaluates the expression before and after\n"
                     "    a register write and halts if the evaluation differs.\n"
                     "Cycle counter breaks:\n"
                     "  break c VALUE  [,\"message\"]\n"
                     "    Halts when the cycle counter reaches 'VALUE'.\n"
                     "Attribute breaks:\n"
                     "  break attribute\n"
                     "    Arms the breakpoint condition for those attributes that support breaks.\n"
                     "    For example, the stopwatch (help stopwatch) attribute can cause a break.\n"
                     "Miscellaneous breaks:\n"
                     "  break so   # halts on stack overflow.\n"
                     "  break su   # halts on stack underflow.\n"
                     "  break wdt  # halts on Watch Dog Timer timeout.\n"
                     "Expressions:\n"
                     "  The conditional expressions mentioned above are syntactically similar to C's\n"
                     "  expressions.\n"
                     "Examples:\n"
                     "\tbreak              # display all of the break points\n"
                     "\tbreak e 0x20       # set an execution break point at address 0x20\n"
                     "\tbreak w reg1 == 0  # break if a zero is written to register reg1\n"
                     "\tbreak w reg2 & 0x30 == 0xf0 # break if '3' is written to the\n"
                     "\t                            # upper nibble or reg2\n"
                     "\tbreak w reg3, (reg4 > 45)   # break if reg4>45 while writing to reg3\n"
                     "\tbreak c 1000000    # break on the one million'th cycle\n"
                     );

  op = cmd_break_options; 
}


void cmd_break::list(guint64 value)
{
  if(value == CMDBREAK_BAD_BREAK_NUMBER)
    get_bp().dump();
  else
    get_bp().dump1((unsigned int)value);
}

const char *TOO_FEW_ARGS="missing register or location\n";
const char *TOO_MANY_ARGS="too many arguments\n";


//------------------------------------------------------------------------
static gpsimObject::ObjectBreakTypes MapBreakActions(int co_value)
{
  switch(co_value) {
  case READ:
    return gpsimObject::eBreakRead;
  case WRITE:
    return gpsimObject::eBreakWrite;
  case REGCHANGE:
    return gpsimObject::eBreakChange;
  case EXECUTION:
    return gpsimObject::eBreakExecute;
  }
  return gpsimObject::eBreakAny;
}

//------------------------------------------------------------------------
unsigned int cmd_break::set_break(cmd_options *co, ExprList_t *pEL, bool bLog)
{
  if (!co) {
    list();
    return MAX_BREAKPOINTS;
  }

  if (!pEL || pEL->size()>3) {
    // FIXME - fix this error message
    cout << "ERROR: Bad expression for break command\n";
    return MAX_BREAKPOINTS;
  }


  ExprList_itor ei = pEL->begin();
  Expression *pFirst = *ei;
  ++ei;
  Expression *pSecond  = (ei != pEL->end()) ? *ei++ : 0;

  Expression *pThird = (ei != pEL->end()) ? *ei : 0;

  if (verbose) {
    cout << "setting breakpoint:\n";
    if (pFirst)
      cout << " first expression" << pFirst->toString() << endl;
    if (pSecond)
      cout << " second expression" << pSecond->toString() << endl;
    if (pThird)
      cout << " third expression" << pThird->toString() << endl;
  }

  LiteralString *pString=0;
  string m;
  if (pSecond) {
    pString = dynamic_cast<LiteralString*>(pSecond);
    if (pString) {
      String *pS = (String *)pString->evaluate();
      m = string(pS->getVal());
      delete pSecond;
      delete pS;
      pSecond =0;
    }
  }

  // If there is a third expression and the second expression is not
  // a string, then try to cast the third expression into a string.
  if (pThird && !pString) {
    pString = dynamic_cast<LiteralString*>(pThird);
    if (pString) {
      String *pS = (String *)pString->evaluate();
      m = string(pS->getVal());
      delete pThird;
      delete pS;
      pThird =0;
    }
  }

  if (!pFirst)
    return set_break(co->value,bLog);

  // See if the expression supports break points. If it does, the break points
  // will get set and the expressions deleted.
  int bpn = pFirst ? pFirst->set_break(MapBreakActions(co->value), 
				       (bLog ? gpsimObject::eActionLog : gpsimObject::eActionHalt),
				       pSecond) : -1;
  if (bpn == -1 && co->value!=CYCLE)
    GetUserInterface().DisplayMessage("break cannot be set on '%s'\n",
      pFirst->toString().c_str());

  if (bpn<0) {

    // We failed to set a break point from the first expression. 
    // It may be that we have a type of break point that is not supported
    // by the expression code.

    if (co->value==CYCLE) {
      LiteralInteger *pLitInt = dynamic_cast<LiteralInteger*>(pFirst);
      Integer *pInt = pLitInt ? dynamic_cast<Integer*>(pLitInt->evaluate()) : 0;
      guint64 ui64Val =  pInt ? (guint64)pInt->getVal() : 0;
      if (pInt)
        bpn = get_bp().set_cycle_break(GetActiveCPU(),ui64Val);
      delete pInt;
    }
  }

  if (bpn>=0) {
    if (pString)
      get_bp().set_message(bpn, m);
    if(verbose)get_bp().dump1(bpn); //RRR
    if (dynamic_cast<LiteralInteger*>(pFirst))
      delete pFirst;

  } else {

    delete pFirst;
    if(pSecond != 0)
      delete pSecond;
  }

  delete pEL;
  return bpn;

}

//------------------------------------------------------------------------
// set_break(cmd_options *co, 
//           Expression *pExpr1,
//           Expression *pExpr2)
//
// Given two expressions, this function will call the set

unsigned int cmd_break::set_break(cmd_options *co, 
				  Expression *pExpr1,
				  Expression *pExpr2,
				  bool bLog)

{

  if (!co) {
    list();
    return MAX_BREAKPOINTS;
  }

  unsigned int bit_flag = co->value;
  if (!pExpr1)
    return set_break(bit_flag,bLog);

  // See if the expression supports break points. If it does, the break points
  // will get set and the expressions deleted.
  int i = pExpr1 ? pExpr1->set_break(MapBreakActions(co->value), 
				     (bLog ? gpsimObject::eActionLog : gpsimObject::eActionHalt), pExpr2) : -1;
  if (i>=0) {
    get_bp().dump1(i);
    return i;
  }

  unsigned int b = MAX_BREAKPOINTS;
  delete pExpr1;
  delete pExpr2;

  return b;

}


unsigned int cmd_break::set_break(cmd_options *co, bool bLog)
{

  if (!co) {
    list();
    return MAX_BREAKPOINTS;
  }

  int bit_flag = co->value;
  return set_break(bit_flag, bLog);
}

unsigned int cmd_break::set_break(int bit_flag, bool bLog)
{
  unsigned int b = MAX_BREAKPOINTS;

  if(!GetActiveCPU())
    return b;

  switch(bit_flag) {

  case STK_OVERFLOW:
    b = get_bp().set_stk_overflow_break(GetActiveCPU());

    if(b < MAX_BREAKPOINTS)
      cout << "break when stack over flows.  " <<
        "bp#: " << b << '\n';

    break;

  case STK_UNDERFLOW:
    b = get_bp().set_stk_underflow_break(GetActiveCPU());

    if(b < MAX_BREAKPOINTS)
      cout << "break when stack under flows.  " <<
        "bp#: " << b << '\n';

    break;


  case WDT:
    b = get_bp().set_wdt_break(GetActiveCPU());

    if(b < MAX_BREAKPOINTS)
      cout << "break when wdt times out.  " <<
        "bp#: " << b << '\n';

    break;

  case CYCLE:
    get_bp().dump(Breakpoints::BREAK_ON_CYCLE);
    break;
  case EXECUTION:
    get_bp().dump(Breakpoints::BREAK_ON_EXECUTION);
    break;
  case WRITE:
    get_bp().dump(Breakpoints::BREAK_ON_REG_WRITE);
    break;
  case READ:
    get_bp().dump(Breakpoints::BREAK_ON_REG_READ);
    break;
  default:
    cout << TOO_FEW_ARGS;
    break;
  }

  return b;
}
 // attribute breakpoints 	 
unsigned int cmd_break::set_break(gpsimObject *v)
{
  if (v)
    v->set_break();

  return MAX_BREAKPOINTS;
}