Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / shell / DebugCLI.h
blob2e15cdf7de9ec94a26a185f250dd63c517df742c
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef __avmshell_AvmDebugCLI__
41 #define __avmshell_AvmDebugCLI__
43 #ifdef DEBUGGER
44 namespace avmshell
46 /**
47 * Represents a single breakpoint in the Debugger.
49 class BreakAction : public MMgc::GCObject
51 public:
52 GCMember<BreakAction> prev;
53 DWB(BreakAction*) next;
54 DWB(SourceFile*) sourceFile;
55 int id;
56 DRCWB(Stringp) filename;
57 int linenum;
59 BreakAction(SourceFile *sourceFile,
60 int id,
61 Stringp filename,
62 int linenum)
64 this->sourceFile = sourceFile;
65 this->id = id;
66 this->filename = filename;
67 this->linenum = linenum;
70 void print(PrintWriter& out);
73 /**
74 * This can be either an l-value or an r-value.
76 class IValue : public MMgc::GCFinalizedObject
78 public:
79 virtual ~IValue() {}
81 virtual bool isLValue() = 0;
82 virtual Atom get() = 0;
83 virtual void set(Atom newValue) = 0;
86 class ConstantValue: public IValue
88 public:
89 ConstantValue(Atom value) : value(value) { }
91 virtual bool isLValue() { return false; }
92 virtual Atom get() { return value; }
93 virtual void set(Atom /*newValue*/) { AvmAssert(false); }
95 private:
96 ATOM_WB value;
99 /**
100 * An IValue representing a local variable.
102 class LocalValue: public IValue
104 public:
105 LocalValue(DebugFrame* frame, int index) : frame(frame), index(index) { }
107 virtual bool isLValue() { return true; }
108 virtual Atom get()
110 Atom* locals;
111 int countLocals;
112 frame->locals(locals, countLocals);
113 return locals[index];
115 virtual void set(Atom newValue)
117 frame->setLocal(index, newValue);
120 private:
121 DWB(DebugFrame*) frame;
122 int index;
126 * An IValue representing an argument to a function.
128 class ArgumentValue: public IValue
130 public:
131 ArgumentValue(DebugFrame* frame, int index) : frame(frame), index(index) { }
133 virtual bool isLValue() { return true; }
134 virtual Atom get()
136 Atom* arguments;
137 int countArguments;
138 frame->arguments(arguments, countArguments);
139 return arguments[index];
141 virtual void set(Atom newValue)
143 frame->setArgument(index, newValue);
146 private:
147 DWB(DebugFrame*) frame;
148 int index;
152 * An IValue representing a property of an object.
154 class PropertyValue: public IValue
156 public:
157 PropertyValue(ScriptObject* parent, Multiname& propertyname)
158 : parent(parent), propertyname(propertyname) { }
160 virtual bool isLValue() { return true; }
161 virtual Atom get()
163 return parent->toplevel()->getproperty(parent->atom(), propertyname, parent->vtable);
165 virtual void set(Atom newValue)
167 parent->toplevel()->setproperty(parent->atom(), propertyname, newValue, parent->vtable);
170 private:
171 DRCWB(ScriptObject*) parent;
172 HeapMultiname propertyname;
176 * A simple command line interface for the Debugger.
177 * Supports a gdb-like command line.
179 class GC_CPP_EXACT_IFDEF(DebugCLI, avmplus::Debugger, DEBUGGER)
181 private:
182 DebugCLI(AvmCore *core, Debugger::TraceLevel tracelevel);
183 public:
184 /** @name command codes */
185 /*@{*/
186 enum {
187 CMD_UNKNOWN = 0,
188 CMD_QUIT,
189 CMD_CONTINUE,
190 CMD_STEP,
191 CMD_NEXT,
192 CMD_FINISH,
193 CMD_BREAK,
194 CMD_SET,
195 CMD_LIST,
196 CMD_PRINT,
197 CMD_INFO,
198 CMD_DELETE,
199 CMD_COMMENT,
200 CMD_HELP,
202 /*@}*/
204 /** @name info sub commands */
205 /*@{*/
206 enum {
207 INFO_UNKNOWN_CMD = 100,
208 INFO_ARGS_CMD,
209 INFO_BREAK_CMD,
210 INFO_FILES_CMD,
211 INFO_FUNCTIONS_CMD,
212 INFO_LOCALS_CMD,
213 INFO_STACK_CMD,
215 /*@}*/
218 * StringInt is used to store the command arrays
219 * used to translate command strings into command codes
221 struct StringInt
223 const char *text;
224 int id;
227 REALLY_INLINE static DebugCLI* create(MMgc::GC* gc, AvmCore *core, Debugger::TraceLevel tracelevel)
229 return new (gc, MMgc::kExact) DebugCLI(core, tracelevel);
232 ~DebugCLI();
234 void enterDebugger();
235 void setCurrentSource(Stringp file);
236 bool filterException(Exception *exception, bool willBeCaught);
237 bool hitWatchpoint() { return false; }
240 * @name command implementations
242 /*@{*/
243 void breakpoint(char *location);
244 void deleteBreakpoint(char *idstr);
245 void showBreakpoints();
246 void help();
247 void locals(int frameNumber);
248 void arguments(int frameNumber);
250 static void bt();
251 static bool printFrame(int frameNumber);
252 static bool debuggerInterruptOnEnter;
255 void info();
256 void set();
257 void list(const char* line);
258 void print(char *name);
259 void quit();
260 /*@}*/
262 void activate() { activeFlag = true; }
264 static MethodInfo* functionFor(SourceInfo* src, int line);
267 * Formats a value for display to the user. Very similar to
268 * AvmCore::format(), but does a few extra things.
270 Stringp formatValue(Atom value);
272 GC_DATA_BEGIN(DebugCLI)
274 private:
275 bool activeFlag;
276 char *currentSource;
277 uint32_t currentSourceLen;
278 DRCWB(Stringp) GC_POINTER(currentFile);
279 int breakpointCount;
280 bool warnMissingSource;
282 DWB(BreakAction*) GC_POINTER(firstBreakAction);
283 DWB(BreakAction*) GC_POINTER(lastBreakAction);
285 enum { kMaxCommandLine = 1024 };
286 char commandLine[kMaxCommandLine];
287 char lastCommand[kMaxCommandLine];
288 char *currentToken;
290 GC_DATA_END(DebugCLI)
292 char *nextToken();
294 void printIP();
295 void displayLines(int linenum, int count);
297 char* lineStart(int linenum);
298 Atom ease2Atom(const char* to, Atom baseline);
300 void throwUndefinedVarError(const char* name);
303 * Gets a value. "name" can be either a constant (string,
304 * number, boolean, undefined, null, xml, xmllist), or the
305 * name of a local, argument, or member of a variable on
306 * the scope chain.
308 IValue* getValue(const char *name);
311 * Evaluates an expression, and returns its value.
312 * *outSawTrailingDot is set to true if the expression has a trailing
313 * dot, e.g. "foo." or "foo.bar."
315 IValue* evaluate(char *expr, bool* outSawTrailingDot);
318 * @name command name arrays and support code
320 /*@{*/
321 int determineCommand(StringInt cmdList[],
322 const char *input,
323 int defCmd);
324 const char* commandNumberToCommandName(StringInt cmdList[],
325 int cmdNumber);
326 int commandFor(const char *input) {
327 return determineCommand(commandArray, input, CMD_UNKNOWN);
329 int infoCommandFor(const char *input) {
330 return determineCommand(infoCommandArray, input, INFO_UNKNOWN_CMD);
333 static StringInt commandArray[];
334 static StringInt infoCommandArray[];
335 /*@}*/
339 * Helper class to iterate over all properties of an object (both slots
340 * and dynamic properties).
342 class PropertyIterator
344 public:
346 * atom: the object whose properties we will iterate over.
348 PropertyIterator(AvmCore* core, Atom atom);
349 virtual ~PropertyIterator() {}
352 * Iterates over all the object's properties, and calls process()
353 * for each one. If process() ever returns false, iteration halts.
355 void iterate();
357 protected:
359 * Called once for each property that has been found. If process()
360 * ever returns false, iteration halts.
362 virtual bool process(Multiname* key, BindingKind bk) = 0;
364 AvmCore* core;
365 ScriptObject* object;
369 * Helper to find a property with the given name. Since this is for the
370 * debugger, it ignores visibility issues -- e.g. it will match any property
371 * with the given name, even if that property is private and therefore not
372 * visible from the caller's context.
374 * After you call iterate(), 'value' will be the property's value if it was
375 * found, or NULL if it was not found.
377 * This class can only be used on the stack. ('value' does not have a write
378 * barrier around it.)
380 class StPropertyFinder: public PropertyIterator
382 public:
384 * atom: The object in which we want to look for a property.
385 * propertyname: The property name to look for.
387 StPropertyFinder(AvmCore* core, Atom atom, const char* propertyname);
389 IValue* value;
391 protected:
393 * Returns true if the key matches the originally passed-in property name.
395 bool matches(const Multiname* key) const;
397 virtual bool process(Multiname* key, BindingKind bk);
399 const char* propertyname;
403 * Prints all of an object's properties, and their values, to core->console.
405 class PropertyPrinter: public PropertyIterator
407 public:
408 PropertyPrinter(AvmCore* core, DebugCLI* debugger, Atom atom);
410 protected:
411 virtual bool process(Multiname* key, BindingKind bk);
413 DebugCLI* debugger;
416 #endif
418 #endif /* __avmshell_AvmDebugCLI__ */