John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
John McCutchan | ffdf1e7 | 2015-07-21 11:09:20 -0700 | [diff] [blame] | 5 | #ifndef TONIC_DART_DEBUGGER_H_ |
| 6 | #define TONIC_DART_DEBUGGER_H_ |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 7 | |
John McCutchan | f84766d | 2015-05-06 15:36:12 -0700 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
John McCutchan | ffdf1e7 | 2015-07-21 11:09:20 -0700 | [diff] [blame] | 11 | #include "base/synchronization/condition_variable.h" |
| 12 | #include "base/synchronization/lock.h" |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 13 | #include "dart/runtime/include/dart_api.h" |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 14 | #include "dart/runtime/include/dart_native_api.h" |
John McCutchan | 9b04b9b | 2015-06-19 13:23:29 -0700 | [diff] [blame] | 15 | #include "dart/runtime/include/dart_tools_api.h" |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | class Lock; |
| 19 | } |
| 20 | |
John McCutchan | ffdf1e7 | 2015-07-21 11:09:20 -0700 | [diff] [blame] | 21 | namespace tonic { |
| 22 | |
| 23 | class Monitor { |
| 24 | public: |
| 25 | Monitor() { |
| 26 | lock_ = new base::Lock(); |
| 27 | condition_variable_ = new base::ConditionVariable(lock_); |
| 28 | } |
| 29 | |
| 30 | ~Monitor() { |
| 31 | delete condition_variable_; |
| 32 | delete lock_; |
| 33 | } |
| 34 | |
| 35 | void Enter() { |
| 36 | lock_->Acquire(); |
| 37 | } |
| 38 | |
| 39 | void Exit() { |
| 40 | lock_->Release(); |
| 41 | } |
| 42 | |
| 43 | void Notify() { |
| 44 | condition_variable_->Signal(); |
| 45 | } |
| 46 | |
| 47 | void Wait() { |
| 48 | condition_variable_->Wait(); |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | base::Lock* lock_; |
| 53 | base::ConditionVariable* condition_variable_; |
| 54 | DISALLOW_COPY_AND_ASSIGN(Monitor); |
| 55 | }; |
| 56 | |
| 57 | class MonitorLocker { |
| 58 | public: |
| 59 | explicit MonitorLocker(Monitor* monitor) : monitor_(monitor) { |
| 60 | CHECK(monitor_); |
| 61 | monitor_->Enter(); |
| 62 | } |
| 63 | |
| 64 | virtual ~MonitorLocker(); |
| 65 | |
| 66 | void Wait() { |
| 67 | return monitor_->Wait(); |
| 68 | } |
| 69 | |
| 70 | void Notify() { |
| 71 | monitor_->Notify(); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | Monitor* const monitor_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(MonitorLocker); |
| 78 | }; |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 79 | |
| 80 | class DartDebuggerIsolate { |
| 81 | public: |
| 82 | DartDebuggerIsolate(Dart_IsolateId id) |
| 83 | : id_(id) { |
| 84 | } |
| 85 | |
| 86 | Dart_IsolateId id() const { |
| 87 | return id_; |
| 88 | } |
| 89 | |
| 90 | void Notify() { |
| 91 | monitor_.Notify(); |
| 92 | } |
| 93 | |
| 94 | void MessageLoop(); |
| 95 | |
| 96 | private: |
| 97 | const Dart_IsolateId id_; |
| 98 | Monitor monitor_; |
| 99 | }; |
| 100 | |
| 101 | class DartDebugger { |
| 102 | public: |
| 103 | static void InitDebugger(); |
| 104 | |
| 105 | private: |
| 106 | static void BptResolvedHandler(Dart_IsolateId isolate_id, |
| 107 | intptr_t bp_id, |
| 108 | const Dart_CodeLocation& location); |
| 109 | |
| 110 | static void PausedEventHandler(Dart_IsolateId isolate_id, |
| 111 | intptr_t bp_id, |
| 112 | const Dart_CodeLocation& loc); |
| 113 | |
| 114 | static void ExceptionThrownHandler(Dart_IsolateId isolate_id, |
| 115 | Dart_Handle exception, |
| 116 | Dart_StackTrace stack_trace); |
| 117 | |
| 118 | static void IsolateEventHandler(Dart_IsolateId isolate_id, |
| 119 | Dart_IsolateEvent kind); |
| 120 | |
| 121 | static void NotifyIsolate(Dart_Isolate isolate); |
| 122 | |
John McCutchan | f84766d | 2015-05-06 15:36:12 -0700 | [diff] [blame] | 123 | static intptr_t FindIsolateIndexById(Dart_IsolateId id); |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 124 | |
John McCutchan | f84766d | 2015-05-06 15:36:12 -0700 | [diff] [blame] | 125 | static intptr_t FindIsolateIndexByIdLocked(Dart_IsolateId id); |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 126 | |
John McCutchan | f84766d | 2015-05-06 15:36:12 -0700 | [diff] [blame] | 127 | static void AddIsolate(Dart_IsolateId id); |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 128 | |
| 129 | static void RemoveIsolate(Dart_IsolateId id); |
| 130 | |
| 131 | static base::Lock* lock_; |
John McCutchan | f84766d | 2015-05-06 15:36:12 -0700 | [diff] [blame] | 132 | static std::vector<std::unique_ptr<DartDebuggerIsolate>>* isolates_; |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 133 | |
| 134 | friend class DartDebuggerIsolate; |
| 135 | }; |
| 136 | |
John McCutchan | ffdf1e7 | 2015-07-21 11:09:20 -0700 | [diff] [blame] | 137 | } // namespace tonic |
John McCutchan | e55b616 | 2015-04-22 13:32:05 -0700 | [diff] [blame] | 138 | |
John McCutchan | ffdf1e7 | 2015-07-21 11:09:20 -0700 | [diff] [blame] | 139 | #endif // TONIC_DART_DEBUGGER_H_ |