File: debugger.cpp

package info (click to toggle)
kdelibs 4:2.2.2-13.woody.14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 36,832 kB
  • ctags: 40,077
  • sloc: cpp: 313,284; ansic: 20,558; xml: 11,448; sh: 11,318; makefile: 2,426; perl: 2,084; yacc: 1,663; java: 1,538; lex: 629; python: 300
file content (229 lines) | stat: -rw-r--r-- 4,851 bytes parent folder | download
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
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef KJS_DEBUGGER

#include "debugger.h"
#include "kjs.h"
#include "internal.h"
#include "ustring.h"

using namespace KJS;

Debugger::Debugger(KJScript *engine)
  : eng(0L),
    sid(-1)
{
  attach(engine);
}

Debugger::~Debugger()
{
  detach();
}

void Debugger::attach(KJScript *e)
{
  dmode = Disabled;
  if (e) {
    if (!eng || e->rep != eng->rep) {
      eng = e;
      eng->rep->attachDebugger(this);
    }
  } else {
    eng = 0L;
  }
  reset();
}

KJScript *Debugger::engine() const
{
  return eng;
}

void Debugger::detach()
{
  reset();
  if (!eng)
    return;
  eng->rep->attachDebugger(0L);
  eng = 0L;
}

void Debugger::setMode(Mode m)
{
  dmode = m;
}

Debugger::Mode Debugger::mode() const
{
  return dmode;
}

// supposed to be overriden by the user
bool Debugger::stopEvent()
{
  return true;
}

void Debugger::callEvent(const UString &, const UString &)
{
}

void Debugger::returnEvent()
{
}

void Debugger::reset()
{
  l = -1;
}

int Debugger::freeSourceId() const
{
  return eng ? eng->rep->sourceId()+1 : -1;
}

bool Debugger::setBreakpoint(int id, int line)
{
  if (!eng)
    return false;
  return eng->rep->setBreakpoint(id, line, true);
}

bool Debugger::deleteBreakpoint(int id, int line)
{
  if (!eng)
    return false;
  return eng->rep->setBreakpoint(id, line, false);
}

void Debugger::clearAllBreakpoints(int id)
{
  if (!eng)
    return;
  eng->rep->setBreakpoint(id, -1, false);
}

UString Debugger::varInfo(const UString &ident)
{
  if (!eng)
    return UString();

  int dot = ident.find('.');
  if (dot < 0)
      dot = ident.size();
  UString sub = ident.substr(0, dot);
  KJSO obj;
  // resolve base
  if (sub == "this") {
      obj = Context::current()->thisValue();
  } else {
      const List *chain = Context::current()->pScopeChain();
      ListIterator scope = chain->begin();
      while (scope != chain->end()) {
	  if (scope->hasProperty(ident)) {
	      obj = scope->get(ident);
	      break;
	  }
	  scope++;
      }
      if (scope == chain->end())
	return UString();
  }
  // look up each part of a.b.c.
  while (dot < ident.size()) {
    int olddot = dot;
    dot = ident.find('.', olddot+1);
    if (dot < 0)
      dot = ident.size();
    sub = ident.substr(olddot+1, dot-olddot-1);
    obj = obj.get(sub);
    if (!obj.isDefined())
      break;
  }

  return sub + "=" + objInfo(obj) + ":" + UString(obj.imp()->typeInfo()->name);
}

// called by varInfo() and recursively by itself on each properties
UString Debugger::objInfo(const KJSO &obj) const
{
  const char *cnames[] = { "Undefined", "Array", "String", "Boolean",
			   "Number", "Object", "Date", "RegExp",
			   "Error", "Function" };
  PropList *plist = obj.imp()->propList(0, 0, false);
  if (!plist)
    return obj.toString().value();
  else {
    UString result = "{";
    while (1) {
      result += plist->name + "=";
      KJSO p = obj.get(plist->name);
      result += objInfo(p) + ":";
      Object obj = Object::dynamicCast(p);
      if (obj.isNull())
	result += p.imp()->typeInfo()->name;
      else
	result += cnames[int(obj.getClass())];
      plist = plist->next;
      if (!plist)
	break;
      result += ",";
    }
    result += "}";
    return result;
  }
}

bool Debugger::setVar(const UString &ident, const KJSO &value)
{
  if (!eng)
    return false;
  const List *chain = Context::current()->pScopeChain();
  ListIterator scope = chain->begin();
  while (scope != chain->end()) {
    if (scope->hasProperty(ident)) {
      if (!scope->canPut(ident))
	return false;
      scope->put(ident, value);
      return true;
    }
    scope++;
  }
  // didn't find variable
  return false;
}

// called from the scripting engine each time a statement node is hit.
bool Debugger::hit(int line, bool breakPoint)
{
  l = line;
  if (!eng)
    return true;

  if (!breakPoint && ( mode() == Continue || mode() == Disabled ) )
      return true;

  bool ret = stopEvent();
  eng->init();	// in case somebody used a different interpreter meanwhile
  return ret;
}

#endif