OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #include "vm/ast_printer.h" | 6 #include "vm/ast_printer.h" |
7 #include "vm/class_finalizer.h" | 7 #include "vm/class_finalizer.h" |
8 #include "vm/debugger.h" | 8 #include "vm/debugger.h" |
9 #include "vm/longjump.h" | 9 #include "vm/longjump.h" |
10 #include "vm/object.h" | 10 #include "vm/object.h" |
11 #include "vm/parser.h" | 11 #include "vm/parser.h" |
12 #include "vm/symbols.h" | 12 #include "vm/symbols.h" |
13 #include "vm/thread.h" | 13 #include "vm/thread.h" |
14 #include "vm/unit_test.h" | 14 #include "vm/unit_test.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 | 17 |
18 DECLARE_FLAG(bool, show_invisible_frames); | 18 DECLARE_FLAG(bool, show_invisible_frames); |
19 | 19 |
20 | 20 |
21 static void DumpFunction(const Library& lib, | 21 static void DumpFunction(const Library& lib, |
22 const char* cname, | 22 const char* cname, |
23 const char* fname) { | 23 const char* fname) { |
24 const String& classname = String::Handle(Symbols::New(Thread::Current(), | 24 const String& classname = |
25 cname)); | 25 String::Handle(Symbols::New(Thread::Current(), cname)); |
26 String& funcname = String::Handle(String::New(fname)); | 26 String& funcname = String::Handle(String::New(fname)); |
27 | 27 |
28 bool retval; | 28 bool retval; |
29 EXPECT(Isolate::Current() != NULL); | 29 EXPECT(Isolate::Current() != NULL); |
30 LongJumpScope jump; | 30 LongJumpScope jump; |
31 if (setjmp(*jump.Set()) == 0) { | 31 if (setjmp(*jump.Set()) == 0) { |
32 Class& cls = Class::Handle(lib.LookupClass(classname)); | 32 Class& cls = Class::Handle(lib.LookupClass(classname)); |
33 EXPECT(!cls.IsNull()); | 33 EXPECT(!cls.IsNull()); |
34 Function& function = | 34 Function& function = |
35 Function::ZoneHandle(cls.LookupStaticFunction(funcname)); | 35 Function::ZoneHandle(cls.LookupStaticFunction(funcname)); |
36 EXPECT(!function.IsNull()); | 36 EXPECT(!function.IsNull()); |
37 ParsedFunction* parsed_function = | 37 ParsedFunction* parsed_function = |
38 new ParsedFunction(Thread::Current(), function); | 38 new ParsedFunction(Thread::Current(), function); |
39 Parser::ParseFunction(parsed_function); | 39 Parser::ParseFunction(parsed_function); |
40 EXPECT(parsed_function->node_sequence() != NULL); | 40 EXPECT(parsed_function->node_sequence() != NULL); |
41 printf("Class %s function %s:\n", cname, fname); | 41 printf("Class %s function %s:\n", cname, fname); |
42 if (FLAG_support_ast_printer) { | 42 if (FLAG_support_ast_printer) { |
43 AstPrinter ast_printer; | 43 AstPrinter ast_printer; |
44 ast_printer.PrintFunctionNodes(*parsed_function); | 44 ast_printer.PrintFunctionNodes(*parsed_function); |
45 } else { | 45 } else { |
46 OS::Print("AST printer not supported."); | 46 OS::Print("AST printer not supported."); |
47 } | 47 } |
48 retval = true; | 48 retval = true; |
49 } else { | 49 } else { |
50 retval = false; | 50 retval = false; |
51 } | 51 } |
52 EXPECT(retval); | 52 EXPECT(retval); |
53 } | 53 } |
54 | 54 |
55 | 55 |
56 void CheckField(const Library& lib, | 56 void CheckField(const Library& lib, |
57 const char* class_name, | 57 const char* class_name, |
58 const char* field_name, | 58 const char* field_name, |
59 bool expect_static, | 59 bool expect_static, |
60 bool is_final) { | 60 bool is_final) { |
61 const String& classname = String::Handle(Symbols::New(Thread::Current(), | 61 const String& classname = |
62 class_name)); | 62 String::Handle(Symbols::New(Thread::Current(), class_name)); |
63 Class& cls = Class::Handle(lib.LookupClass(classname)); | 63 Class& cls = Class::Handle(lib.LookupClass(classname)); |
64 EXPECT(!cls.IsNull()); | 64 EXPECT(!cls.IsNull()); |
65 | 65 |
66 String& fieldname = String::Handle(String::New(field_name)); | 66 String& fieldname = String::Handle(String::New(field_name)); |
67 String& functionname = String::Handle(); | 67 String& functionname = String::Handle(); |
68 Function& function = Function::Handle(); | 68 Function& function = Function::Handle(); |
69 Field& field = Field::Handle(); | 69 Field& field = Field::Handle(); |
70 if (expect_static) { | 70 if (expect_static) { |
71 field ^= cls.LookupStaticFieldAllowPrivate(fieldname); | 71 field ^= cls.LookupStaticFieldAllowPrivate(fieldname); |
72 functionname ^= Field::GetterName(fieldname); | 72 functionname ^= Field::GetterName(fieldname); |
(...skipping 14 matching lines...) Expand all Loading... |
87 EXPECT(!field.IsNull()); | 87 EXPECT(!field.IsNull()); |
88 | 88 |
89 EXPECT_EQ(field.is_static(), expect_static); | 89 EXPECT_EQ(field.is_static(), expect_static); |
90 } | 90 } |
91 | 91 |
92 | 92 |
93 void CheckFunction(const Library& lib, | 93 void CheckFunction(const Library& lib, |
94 const char* class_name, | 94 const char* class_name, |
95 const char* function_name, | 95 const char* function_name, |
96 bool expect_static) { | 96 bool expect_static) { |
97 const String& classname = String::Handle(Symbols::New(Thread::Current(), | 97 const String& classname = |
98 class_name)); | 98 String::Handle(Symbols::New(Thread::Current(), class_name)); |
99 Class& cls = Class::Handle(lib.LookupClass(classname)); | 99 Class& cls = Class::Handle(lib.LookupClass(classname)); |
100 EXPECT(!cls.IsNull()); | 100 EXPECT(!cls.IsNull()); |
101 | 101 |
102 String& functionname = String::Handle(String::New(function_name)); | 102 String& functionname = String::Handle(String::New(function_name)); |
103 Function& function = Function::Handle(); | 103 Function& function = Function::Handle(); |
104 if (expect_static) { | 104 if (expect_static) { |
105 function ^= cls.LookupStaticFunction(functionname); | 105 function ^= cls.LookupStaticFunction(functionname); |
106 } else { | 106 } else { |
107 function ^= cls.LookupDynamicFunction(functionname); | 107 function ^= cls.LookupDynamicFunction(functionname); |
108 } | 108 } |
(...skipping 10 matching lines...) Expand all Loading... |
119 " final f2; \n" | 119 " final f2; \n" |
120 " final int f3, f4; \n" | 120 " final int f3, f4; \n" |
121 " static String s1, s2; \n" | 121 " static String s1, s2; \n" |
122 " static const int s3 = 8675309; \n" | 122 " static const int s3 = 8675309; \n" |
123 " static bar(i, [var d = 5]) { return 77; } \n" | 123 " static bar(i, [var d = 5]) { return 77; } \n" |
124 " static foo() native \"native_function_name\"; \n" | 124 " static foo() native \"native_function_name\"; \n" |
125 "} \n"; | 125 "} \n"; |
126 | 126 |
127 String& url = String::Handle(String::New("dart-test:Parser_TopLevel")); | 127 String& url = String::Handle(String::New("dart-test:Parser_TopLevel")); |
128 String& source = String::Handle(String::New(script_chars)); | 128 String& source = String::Handle(String::New(script_chars)); |
129 Script& script = Script::Handle(Script::New(url, | 129 Script& script = |
130 source, | 130 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); |
131 RawScript::kScriptTag)); | |
132 Library& lib = Library::ZoneHandle(Library::CoreLibrary()); | 131 Library& lib = Library::ZoneHandle(Library::CoreLibrary()); |
133 | 132 |
134 script.Tokenize(String::Handle(String::New(""))); | 133 script.Tokenize(String::Handle(String::New(""))); |
135 | 134 |
136 Parser::ParseCompilationUnit(lib, script); | 135 Parser::ParseCompilationUnit(lib, script); |
137 EXPECT(ClassFinalizer::ProcessPendingClasses()); | 136 EXPECT(ClassFinalizer::ProcessPendingClasses()); |
138 CheckField(lib, "A", "f1", false, false); | 137 CheckField(lib, "A", "f1", false, false); |
139 CheckField(lib, "A", "f2", false, true); | 138 CheckField(lib, "A", "f2", false, true); |
140 CheckField(lib, "A", "f3", false, true); | 139 CheckField(lib, "A", "f3", false, true); |
141 CheckField(lib, "A", "f4", false, true); | 140 CheckField(lib, "A", "f4", false, true); |
(...skipping 12 matching lines...) Expand all Loading... |
154 " static foo() { return 42; } \n" | 153 " static foo() { return 42; } \n" |
155 " static baz(var i) { var q = 5; return i + q; } \n" | 154 " static baz(var i) { var q = 5; return i + q; } \n" |
156 "} \n" | 155 "} \n" |
157 " \n" | 156 " \n" |
158 "class B { \n" | 157 "class B { \n" |
159 " static bam(k) { return A.foo(); } \n" | 158 " static bam(k) { return A.foo(); } \n" |
160 "} \n"; | 159 "} \n"; |
161 | 160 |
162 String& url = String::Handle(String::New("dart-test:Parser_TopLevel")); | 161 String& url = String::Handle(String::New("dart-test:Parser_TopLevel")); |
163 String& source = String::Handle(String::New(script_chars)); | 162 String& source = String::Handle(String::New(script_chars)); |
164 Script& script = Script::Handle(Script::New(url, | 163 Script& script = |
165 source, | 164 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); |
166 RawScript::kScriptTag)); | |
167 Library& lib = Library::ZoneHandle(Library::CoreLibrary()); | 165 Library& lib = Library::ZoneHandle(Library::CoreLibrary()); |
168 | 166 |
169 script.Tokenize(String::Handle(String::New(""))); | 167 script.Tokenize(String::Handle(String::New(""))); |
170 | 168 |
171 Parser::ParseCompilationUnit(lib, script); | 169 Parser::ParseCompilationUnit(lib, script); |
172 EXPECT(ClassFinalizer::ProcessPendingClasses()); | 170 EXPECT(ClassFinalizer::ProcessPendingClasses()); |
173 | 171 |
174 DumpFunction(lib, "A", "foo"); | 172 DumpFunction(lib, "A", "foo"); |
175 DumpFunction(lib, "A", "bar"); | 173 DumpFunction(lib, "A", "bar"); |
176 DumpFunction(lib, "A", "baz"); | 174 DumpFunction(lib, "A", "baz"); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 | 208 |
211 output[0] = '\0'; | 209 output[0] = '\0'; |
212 return output_buffer; | 210 return output_buffer; |
213 } | 211 } |
214 | 212 |
215 | 213 |
216 // Saves the var descriptors for all frames on the stack as a string. | 214 // Saves the var descriptors for all frames on the stack as a string. |
217 static void SaveVars(Dart_IsolateId isolate_id, | 215 static void SaveVars(Dart_IsolateId isolate_id, |
218 intptr_t bp_id, | 216 intptr_t bp_id, |
219 const Dart_CodeLocation& loc) { | 217 const Dart_CodeLocation& loc) { |
220 DebuggerStackTrace* stack = | 218 DebuggerStackTrace* stack = Isolate::Current()->debugger()->StackTrace(); |
221 Isolate::Current()->debugger()->StackTrace(); | |
222 intptr_t num_frames = stack->Length(); | 219 intptr_t num_frames = stack->Length(); |
223 const int kBufferLen = 2048; | 220 const int kBufferLen = 2048; |
224 char* buffer = new char[kBufferLen]; | 221 char* buffer = new char[kBufferLen]; |
225 char* pos = buffer; | 222 char* pos = buffer; |
226 LocalVarDescriptors& var_desc = LocalVarDescriptors::Handle(); | 223 LocalVarDescriptors& var_desc = LocalVarDescriptors::Handle(); |
227 for (intptr_t i = 0; i < num_frames; i++) { | 224 for (intptr_t i = 0; i < num_frames; i++) { |
228 ActivationFrame* frame = stack->FrameAt(i); | 225 ActivationFrame* frame = stack->FrameAt(i); |
229 var_desc = frame->code().GetLocalVarDescriptors(); | 226 var_desc = frame->code().GetLocalVarDescriptors(); |
230 const char* var_str = SkipIndex(var_desc.ToCString()); | 227 const char* var_str = SkipIndex(var_desc.ToCString()); |
231 const char* function_str = String::Handle( | 228 const char* function_str = |
232 frame->function().QualifiedUserVisibleName()).ToCString(); | 229 String::Handle(frame->function().QualifiedUserVisibleName()) |
233 pos += OS::SNPrint(pos, (kBufferLen - (pos - buffer)), | 230 .ToCString(); |
234 "%s\n%s", | 231 pos += OS::SNPrint(pos, (kBufferLen - (pos - buffer)), "%s\n%s", |
235 function_str, | 232 function_str, var_str); |
236 var_str); | 233 delete[] var_str; |
237 delete [] var_str; | |
238 } | 234 } |
239 pos[0] = '\0'; | 235 pos[0] = '\0'; |
240 saved_vars = buffer; | 236 saved_vars = buffer; |
241 } | 237 } |
242 | 238 |
243 | 239 |
244 // Uses the debugger to pause the program and capture the variable | 240 // Uses the debugger to pause the program and capture the variable |
245 // descriptors for all frames on the stack. | 241 // descriptors for all frames on the stack. |
246 static const char* CaptureVarsAtLine(Dart_Handle lib, | 242 static const char* CaptureVarsAtLine(Dart_Handle lib, |
247 const char* entry, | 243 const char* entry, |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 " name=:current_context_var\n" | 567 " name=:current_context_var\n" |
572 " 1 ContextLevel level=1 scope=2 begin=3 end=79\n" | 568 " 1 ContextLevel level=1 scope=2 begin=3 end=79\n" |
573 " 2 ContextVar level=1 begin=9 end=79 name=x\n" | 569 " 2 ContextVar level=1 begin=9 end=79 name=x\n" |
574 " 3 StackVar scope=2 begin=11 end=79 name=b\n", | 570 " 3 StackVar scope=2 begin=11 end=79 name=b\n", |
575 CaptureVarsAtLine(lib, "a", 10)); | 571 CaptureVarsAtLine(lib, "a", 10)); |
576 } | 572 } |
577 | 573 |
578 #endif // !PRODUCT | 574 #endif // !PRODUCT |
579 | 575 |
580 } // namespace dart | 576 } // namespace dart |
OLD | NEW |