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 #include "vm/debugger.h" | 5 #include "vm/debugger.h" |
6 | 6 |
7 #include "vm/code_index_table.h" | 7 #include "vm/code_index_table.h" |
8 #include "vm/code_patcher.h" | 8 #include "vm/code_patcher.h" |
9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/flags.h" | 10 #include "vm/flags.h" |
11 #include "vm/globals.h" | 11 #include "vm/globals.h" |
12 #include "vm/object.h" | 12 #include "vm/object.h" |
13 #include "vm/object_store.h" | 13 #include "vm/object_store.h" |
14 #include "vm/os.h" | 14 #include "vm/os.h" |
15 #include "vm/stack_frame.h" | 15 #include "vm/stack_frame.h" |
16 #include "vm/stub_code.h" | 16 #include "vm/stub_code.h" |
17 #include "vm/visitor.h" | 17 #include "vm/visitor.h" |
18 | 18 |
19 | 19 |
20 namespace dart { | 20 namespace dart { |
21 | 21 |
22 DEFINE_FLAG(bool, debugger, false, "Debug breakpoint at main"); | 22 |
23 DEFINE_FLAG(charp, bpt, NULL, "Debug breakpoint at <func>"); | 23 DEFINE_FLAG(charp, bpt, NULL, "Debug breakpoint at <func>"); |
24 | 24 |
25 | 25 |
26 | |
27 Breakpoint::Breakpoint(const Function& func, intptr_t pc_desc_index) | 26 Breakpoint::Breakpoint(const Function& func, intptr_t pc_desc_index) |
28 : function_(func.raw()), | 27 : function_(func.raw()), |
29 pc_desc_index_(pc_desc_index), | 28 pc_desc_index_(pc_desc_index), |
30 pc_(0), | 29 pc_(0), |
31 line_number_(-1), | 30 line_number_(-1), |
32 next_(NULL) { | 31 next_(NULL) { |
33 Code& code = Code::Handle(func.code()); | 32 Code& code = Code::Handle(func.code()); |
34 ASSERT(!code.IsNull()); // Function must be compiled. | 33 ASSERT(!code.IsNull()); // Function must be compiled. |
35 PcDescriptors& desc = PcDescriptors::Handle(code.pc_descriptors()); | 34 PcDescriptors& desc = PcDescriptors::Handle(code.pc_descriptors()); |
36 ASSERT(pc_desc_index < desc.Length()); | 35 ASSERT(pc_desc_index < desc.Length()); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 } | 177 } |
179 | 178 |
180 | 179 |
181 Debugger::Debugger() | 180 Debugger::Debugger() |
182 : initialized_(false), | 181 : initialized_(false), |
183 bp_handler_(NULL), | 182 bp_handler_(NULL), |
184 breakpoints_(NULL) { | 183 breakpoints_(NULL) { |
185 } | 184 } |
186 | 185 |
187 | 186 |
188 static RawFunction* ResolveLibraryFunction(const String& fname) { | 187 bool Debugger::IsActive() { |
189 Isolate* isolate = Isolate::Current(); | 188 return breakpoints_ != NULL; |
siva
2011/12/07 05:44:48
If you start deleting breakpoints then this condit
hausner
2011/12/07 22:14:26
Yes, it would be an issue. Another solution (which
| |
190 const Library& root_lib = | 189 } |
191 Library::Handle(isolate->object_store()->root_library()); | 190 |
192 ASSERT(!root_lib.IsNull()); | 191 |
192 static RawFunction* ResolveLibraryFunction( | |
193 const Library& library, | |
194 const String& fname) { | |
195 ASSERT(!library.IsNull()); | |
193 Function& function = Function::Handle(); | 196 Function& function = Function::Handle(); |
194 const Object& object = Object::Handle(root_lib.LookupObject(fname)); | 197 const Object& object = Object::Handle(library.LookupObject(fname)); |
195 if (!object.IsNull() && object.IsFunction()) { | 198 if (!object.IsNull() && object.IsFunction()) { |
196 function ^= object.raw(); | 199 function ^= object.raw(); |
197 } | 200 } |
198 return function.raw(); | 201 return function.raw(); |
199 } | 202 } |
200 | 203 |
201 | 204 |
202 static RawFunction* ResolveFunction(const String& class_name, | 205 RawFunction* Debugger::ResolveFunction(const Library& library, |
203 const String& function_name) { | 206 const String& class_name, |
204 Isolate* isolate = Isolate::Current(); | 207 const String& function_name) { |
205 const Library& root_lib = | 208 ASSERT(!library.IsNull()); |
206 Library::Handle(isolate->object_store()->root_library()); | 209 ASSERT(!class_name.IsNull()); |
207 const Class& cls = Class::Handle(root_lib.LookupClass(class_name)); | 210 ASSERT(!function_name.IsNull()); |
208 | 211 if (class_name.Length() == 0) { |
212 return ResolveLibraryFunction(library, function_name); | |
213 } | |
214 const Class& cls = Class::Handle(library.LookupClass(class_name)); | |
209 Function& function = Function::Handle(); | 215 Function& function = Function::Handle(); |
210 if (!cls.IsNull()) { | 216 if (!cls.IsNull()) { |
211 function = cls.LookupStaticFunction(function_name); | 217 function = cls.LookupStaticFunction(function_name); |
212 if (function.IsNull()) { | 218 if (function.IsNull()) { |
213 function = cls.LookupDynamicFunction(function_name); | 219 function = cls.LookupDynamicFunction(function_name); |
214 } | 220 } |
215 } | 221 } |
216 return function.raw(); | 222 return function.raw(); |
217 } | 223 } |
218 | 224 |
219 | 225 |
220 void Debugger::SetBreakpointAtEntry(const String& class_name, | 226 Breakpoint* Debugger::SetBreakpointAtEntry(const Function& target_function) { |
221 const String& function_name) { | 227 ASSERT(!target_function.IsNull()); |
222 Function& func = Function::Handle(); | 228 if (!target_function.HasCode()) { |
223 if (class_name.IsNull() || (class_name.Length() == 0)) { | 229 Compiler::CompileFunction(target_function); |
224 func = ResolveLibraryFunction(function_name); | |
225 } else { | |
226 func = ResolveFunction(class_name, function_name); | |
227 } | 230 } |
228 if (func.IsNull()) { | 231 Code& code = Code::Handle(target_function.code()); |
229 OS::Print("could not find function '%s'\n", function_name.ToCString()); | |
230 return; | |
231 } | |
232 if (!func.HasCode()) { | |
233 Compiler::CompileFunction(func); | |
234 } | |
235 Code& code = Code::Handle(func.code()); | |
236 ASSERT(!code.IsNull()); | 232 ASSERT(!code.IsNull()); |
237 PcDescriptors& desc = PcDescriptors::Handle(code.pc_descriptors()); | 233 PcDescriptors& desc = PcDescriptors::Handle(code.pc_descriptors()); |
238 for (int i = 0; i < desc.Length(); i++) { | 234 for (int i = 0; i < desc.Length(); i++) { |
239 PcDescriptors::Kind kind = desc.DescriptorKind(i); | 235 PcDescriptors::Kind kind = desc.DescriptorKind(i); |
240 Breakpoint* bpt = NULL; | 236 Breakpoint* bpt = NULL; |
241 if (kind == PcDescriptors::kIcCall) { | 237 if (kind == PcDescriptors::kIcCall) { |
242 CodePatcher::PatchInstanceCallAt( | 238 CodePatcher::PatchInstanceCallAt( |
243 desc.PC(i), StubCode::BreakpointDynamicEntryPoint()); | 239 desc.PC(i), StubCode::BreakpointDynamicEntryPoint()); |
244 bpt = new Breakpoint(func, i); | 240 bpt = new Breakpoint(target_function, i); |
245 } else if (kind == PcDescriptors::kOther) { | 241 } else if (kind == PcDescriptors::kOther) { |
246 if (CodePatcher::IsDartCall(desc.PC(i))) { | 242 if (CodePatcher::IsDartCall(desc.PC(i))) { |
247 CodePatcher::PatchStaticCallAt( | 243 CodePatcher::PatchStaticCallAt( |
248 desc.PC(i), StubCode::BreakpointStaticEntryPoint()); | 244 desc.PC(i), StubCode::BreakpointStaticEntryPoint()); |
249 bpt = new Breakpoint(func, i); | 245 bpt = new Breakpoint(target_function, i); |
250 } | 246 } |
251 } | 247 } |
252 if (bpt != NULL) { | 248 if (bpt != NULL) { |
253 OS::Print("Setting breakpoint at '%s' line %d (PC %p)\n", | 249 OS::Print("Setting breakpoint at '%s' line %d (PC %p)\n", |
254 String::Handle(bpt->SourceUrl()).ToCString(), | 250 String::Handle(bpt->SourceUrl()).ToCString(), |
255 bpt->LineNumber(), | 251 bpt->LineNumber(), |
256 bpt->pc()); | 252 bpt->pc()); |
257 AddBreakpoint(bpt); | 253 AddBreakpoint(bpt); |
258 return; | 254 return bpt; |
259 } | 255 } |
260 } | 256 } |
261 OS::Print("no breakpoint location found in function '%s'\n", | 257 return NULL; |
262 function_name.ToCString()); | |
263 } | 258 } |
264 | 259 |
265 | 260 |
266 void Debugger::VisitObjectPointers(ObjectPointerVisitor* visitor) { | 261 void Debugger::VisitObjectPointers(ObjectPointerVisitor* visitor) { |
267 ASSERT(visitor != NULL); | 262 ASSERT(visitor != NULL); |
268 Breakpoint* bpt = this->breakpoints_; | 263 Breakpoint* bpt = this->breakpoints_; |
269 while (bpt != NULL) { | 264 while (bpt != NULL) { |
270 bpt->VisitObjectPointers(visitor); | 265 bpt->VisitObjectPointers(visitor); |
271 bpt = bpt->next(); | 266 bpt = bpt->next(); |
272 } | 267 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 (*bp_handler_)(bpt, stack_trace); | 308 (*bp_handler_)(bpt, stack_trace); |
314 } | 309 } |
315 } | 310 } |
316 | 311 |
317 | 312 |
318 void Debugger::Initialize(Isolate* isolate) { | 313 void Debugger::Initialize(Isolate* isolate) { |
319 if (initialized_) { | 314 if (initialized_) { |
320 return; | 315 return; |
321 } | 316 } |
322 initialized_ = true; | 317 initialized_ = true; |
323 if (!FLAG_debugger) { | |
324 return; | |
325 } | |
326 if (FLAG_bpt == NULL) { | |
327 FLAG_bpt = "main"; | |
328 } | |
329 String& cname = String::Handle(); | |
330 String& fname = String::Handle(); | |
331 const char* dot = strchr(FLAG_bpt, '.'); | |
332 if (dot == NULL) { | |
333 fname = String::New(FLAG_bpt); | |
334 } else { | |
335 fname = String::New(dot + 1); | |
336 cname = String::New(reinterpret_cast<const uint8_t*>(FLAG_bpt), | |
337 dot - FLAG_bpt); | |
338 } | |
339 SetBreakpointHandler(DefaultBreakpointHandler); | 318 SetBreakpointHandler(DefaultBreakpointHandler); |
340 SetBreakpointAtEntry(cname, fname); | |
341 } | 319 } |
342 | 320 |
343 | 321 |
344 Breakpoint* Debugger::GetBreakpoint(uword breakpoint_address) { | 322 Breakpoint* Debugger::GetBreakpoint(uword breakpoint_address) { |
345 Breakpoint* bpt = this->breakpoints_; | 323 Breakpoint* bpt = this->breakpoints_; |
346 while (bpt != NULL) { | 324 while (bpt != NULL) { |
347 if (bpt->pc() == breakpoint_address) { | 325 if (bpt->pc() == breakpoint_address) { |
348 return bpt; | 326 return bpt; |
349 } | 327 } |
350 bpt = bpt->next(); | 328 bpt = bpt->next(); |
351 } | 329 } |
352 return NULL; | 330 return NULL; |
353 } | 331 } |
354 | 332 |
355 | 333 |
356 void Debugger::AddBreakpoint(Breakpoint* bpt) { | 334 void Debugger::AddBreakpoint(Breakpoint* bpt) { |
357 ASSERT(bpt->next() == NULL); | 335 ASSERT(bpt->next() == NULL); |
358 bpt->set_next(this->breakpoints_); | 336 bpt->set_next(this->breakpoints_); |
359 this->breakpoints_ = bpt; | 337 this->breakpoints_ = bpt; |
360 } | 338 } |
361 | 339 |
362 | 340 |
363 } // namespace dart | 341 } // namespace dart |
OLD | NEW |