OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include "sky/engine/core/script/dart_debugger.h" |
6 | 6 |
7 #include "dart/runtime/include/dart_api.h" | 7 #include "dart/runtime/include/dart_api.h" |
8 #include "dart/runtime/include/dart_debugger_api.h" | 8 #include "dart/runtime/include/dart_debugger_api.h" |
9 #include "dart/runtime/include/dart_native_api.h" | 9 #include "dart/runtime/include/dart_native_api.h" |
10 #include "mojo/dart/embedder/dart_debugger.h" | |
11 | 10 |
12 namespace mojo { | 11 |
13 namespace dart { | 12 namespace blink { |
14 | 13 |
15 void DartDebuggerIsolate::MessageLoop() { | 14 void DartDebuggerIsolate::MessageLoop() { |
16 MonitorLocker ml(&monitor_); | 15 MonitorLocker ml(&monitor_); |
17 // Request notification on isolate messages. This allows us to | 16 // Request notification on isolate messages. This allows us to |
18 // respond to vm service messages while at breakpoint. | 17 // respond to vm service messages while at breakpoint. |
19 Dart_SetMessageNotifyCallback(DartDebugger::NotifyIsolate); | 18 Dart_SetMessageNotifyCallback(DartDebugger::NotifyIsolate); |
20 while (true) { | 19 while (true) { |
21 // Handle all available vm service messages, up to a resume | 20 // Handle all available vm service messages, up to a resume |
22 // request. | 21 // request. |
23 bool resume = false; | 22 bool resume = false; |
(...skipping 14 matching lines...) Expand all Loading... |
38 intptr_t bp_id, | 37 intptr_t bp_id, |
39 const Dart_CodeLocation& location) { | 38 const Dart_CodeLocation& location) { |
40 // Nothing to do here. Service event is dispatched to let Observatory know | 39 // Nothing to do here. Service event is dispatched to let Observatory know |
41 // that a breakpoint was resolved. | 40 // that a breakpoint was resolved. |
42 } | 41 } |
43 | 42 |
44 void DartDebugger::PausedEventHandler(Dart_IsolateId isolate_id, | 43 void DartDebugger::PausedEventHandler(Dart_IsolateId isolate_id, |
45 intptr_t bp_id, | 44 intptr_t bp_id, |
46 const Dart_CodeLocation& loc) { | 45 const Dart_CodeLocation& loc) { |
47 Dart_EnterScope(); | 46 Dart_EnterScope(); |
48 DartDebuggerIsolate* debugger_isolate = | 47 intptr_t isolate_index = FindIsolateIndexById(isolate_id); |
49 FindIsolateById(isolate_id); | 48 CHECK(isolate_index != -1); |
50 CHECK(debugger_isolate != nullptr); | 49 (*isolates_)[isolate_index]->MessageLoop(); |
51 debugger_isolate->MessageLoop(); | |
52 Dart_ExitScope(); | 50 Dart_ExitScope(); |
53 } | 51 } |
54 | 52 |
55 void DartDebugger::ExceptionThrownHandler(Dart_IsolateId isolate_id, | 53 void DartDebugger::ExceptionThrownHandler(Dart_IsolateId isolate_id, |
56 Dart_Handle exception, | 54 Dart_Handle exception, |
57 Dart_StackTrace stack_trace) { | 55 Dart_StackTrace stack_trace) { |
58 Dart_EnterScope(); | 56 Dart_EnterScope(); |
59 DartDebuggerIsolate* debugger_isolate = | 57 intptr_t isolate_index = FindIsolateIndexById(isolate_id); |
60 FindIsolateById(isolate_id); | 58 CHECK(isolate_index != -1); |
61 CHECK(debugger_isolate != nullptr); | 59 (*isolates_)[isolate_index]->MessageLoop(); |
62 debugger_isolate->MessageLoop(); | |
63 Dart_ExitScope(); | 60 Dart_ExitScope(); |
64 } | 61 } |
65 | 62 |
66 void DartDebugger::IsolateEventHandler(Dart_IsolateId isolate_id, | 63 void DartDebugger::IsolateEventHandler(Dart_IsolateId isolate_id, |
67 Dart_IsolateEvent kind) { | 64 Dart_IsolateEvent kind) { |
68 Dart_EnterScope(); | 65 Dart_EnterScope(); |
69 if (kind == Dart_IsolateEvent::kCreated) { | 66 if (kind == Dart_IsolateEvent::kCreated) { |
70 AddIsolate(isolate_id); | 67 AddIsolate(isolate_id); |
71 } else { | 68 } else { |
72 DartDebuggerIsolate* debugger_isolate = | 69 intptr_t isolate_index = FindIsolateIndexById(isolate_id); |
73 FindIsolateById(isolate_id); | 70 CHECK(isolate_index != -1); |
74 CHECK(debugger_isolate != nullptr); | |
75 if (kind == Dart_IsolateEvent::kInterrupted) { | 71 if (kind == Dart_IsolateEvent::kInterrupted) { |
76 debugger_isolate->MessageLoop(); | 72 (*isolates_)[isolate_index]->MessageLoop(); |
77 } else { | 73 } else { |
78 CHECK(kind == Dart_IsolateEvent::kShutdown); | 74 CHECK(kind == Dart_IsolateEvent::kShutdown); |
79 RemoveIsolate(isolate_id); | 75 RemoveIsolate(isolate_id); |
80 } | 76 } |
81 } | 77 } |
82 Dart_ExitScope(); | 78 Dart_ExitScope(); |
83 } | 79 } |
84 | 80 |
85 void DartDebugger::NotifyIsolate(Dart_Isolate isolate) { | 81 void DartDebugger::NotifyIsolate(Dart_Isolate isolate) { |
86 base::AutoLock al(*lock_); | 82 base::AutoLock al(*lock_); |
87 Dart_IsolateId isolate_id = Dart_GetIsolateId(isolate); | 83 Dart_IsolateId isolate_id = Dart_GetIsolateId(isolate); |
88 DartDebuggerIsolate* debugger_isolate = | 84 intptr_t isolate_index = FindIsolateIndexByIdLocked(isolate_id); |
89 FindIsolateByIdLocked(isolate_id); | 85 if (isolate_index >= 0) { |
90 if (debugger_isolate != nullptr) { | 86 (*isolates_)[isolate_index]->Notify(); |
91 debugger_isolate->Notify(); | |
92 } | 87 } |
93 } | 88 } |
94 | 89 |
95 void DartDebugger::InitDebugger() { | 90 void DartDebugger::InitDebugger() { |
96 Dart_SetIsolateEventHandler(IsolateEventHandler); | 91 Dart_SetIsolateEventHandler(IsolateEventHandler); |
97 Dart_SetPausedEventHandler(PausedEventHandler); | 92 Dart_SetPausedEventHandler(PausedEventHandler); |
98 Dart_SetBreakpointResolvedHandler(BptResolvedHandler); | 93 Dart_SetBreakpointResolvedHandler(BptResolvedHandler); |
99 Dart_SetExceptionThrownHandler(ExceptionThrownHandler); | 94 Dart_SetExceptionThrownHandler(ExceptionThrownHandler); |
100 lock_ = new base::Lock(); | 95 lock_ = new base::Lock(); |
| 96 isolates_ = new std::vector<std::unique_ptr<DartDebuggerIsolate>>(); |
101 } | 97 } |
102 | 98 |
103 DartDebuggerIsolate* DartDebugger::FindIsolateById(Dart_IsolateId id) { | 99 intptr_t DartDebugger::FindIsolateIndexById(Dart_IsolateId id) { |
104 base::AutoLock al(*lock_); | 100 base::AutoLock al(*lock_); |
105 return FindIsolateByIdLocked(id); | 101 return FindIsolateIndexByIdLocked(id); |
106 } | 102 } |
107 | 103 |
108 DartDebuggerIsolate* DartDebugger::FindIsolateByIdLocked( | 104 intptr_t DartDebugger::FindIsolateIndexByIdLocked( |
109 Dart_IsolateId id) { | 105 Dart_IsolateId id) { |
110 lock_->AssertAcquired(); | 106 lock_->AssertAcquired(); |
111 for (size_t i = 0; i < isolates_.size(); i++) { | 107 for (size_t i = 0; i < isolates_->size(); i++) { |
112 DartDebuggerIsolate* isolate = isolates_[i]; | 108 if ((*isolates_)[i]->id() == id) { |
113 if (id == isolate->id()) { | 109 return i; |
114 return isolate; | |
115 } | 110 } |
116 } | 111 } |
117 return nullptr; | 112 return -1; |
118 } | 113 } |
119 | 114 |
120 DartDebuggerIsolate* DartDebugger::AddIsolate(Dart_IsolateId id) { | 115 void DartDebugger::AddIsolate(Dart_IsolateId id) { |
121 base::AutoLock al(*lock_); | 116 base::AutoLock al(*lock_); |
122 CHECK(FindIsolateByIdLocked(id) == nullptr); | 117 CHECK(FindIsolateIndexByIdLocked(id) == -1); |
123 DartDebuggerIsolate* debugger_isolate = | 118 std::unique_ptr<DartDebuggerIsolate> debugger_isolate = |
124 new DartDebuggerIsolate(id); | 119 std::unique_ptr<DartDebuggerIsolate>(new DartDebuggerIsolate(id)); |
125 isolates_.push_back(debugger_isolate); | 120 isolates_->push_back(std::move(debugger_isolate)); |
126 return debugger_isolate; | |
127 } | 121 } |
128 | 122 |
129 void DartDebugger::RemoveIsolate(Dart_IsolateId id) { | 123 void DartDebugger::RemoveIsolate(Dart_IsolateId id) { |
130 base::AutoLock al(*lock_); | 124 base::AutoLock al(*lock_); |
131 for (size_t i = 0; i < isolates_.size(); i++) { | 125 for (size_t i = 0; i < isolates_->size(); i++) { |
132 DartDebuggerIsolate* isolate = isolates_[i]; | 126 if (id == (*isolates_)[i]->id()) { |
133 if (id == isolate->id()) { | 127 isolates_->erase(isolates_->begin() + i); |
134 isolates_.erase(isolates_.begin() + i); | |
135 return; | 128 return; |
136 } | 129 } |
137 } | 130 } |
138 NOTREACHED(); | 131 NOTREACHED(); |
139 } | 132 } |
140 | 133 |
141 base::Lock* DartDebugger::lock_ = nullptr; | 134 base::Lock* DartDebugger::lock_ = nullptr; |
142 std::vector<DartDebuggerIsolate*> DartDebugger::isolates_; | 135 std::vector<std::unique_ptr<DartDebuggerIsolate>>* DartDebugger::isolates_ = |
| 136 nullptr; |
143 | 137 |
144 } // namespace apps | 138 } // namespace blink |
145 } // namespace mojo | |
OLD | NEW |