blob: 779b35d0f68658fd40a67c46f3cbce4b0a7c3485 [file] [log] [blame]
John McCutchane55b6162015-04-22 13:32:05 -07001// 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 McCutchanffdf1e72015-07-21 11:09:20 -07005#ifndef TONIC_DART_DEBUGGER_H_
6#define TONIC_DART_DEBUGGER_H_
John McCutchane55b6162015-04-22 13:32:05 -07007
John McCutchanf84766d2015-05-06 15:36:12 -07008#include <memory>
9#include <vector>
10
John McCutchanffdf1e72015-07-21 11:09:20 -070011#include "base/synchronization/condition_variable.h"
12#include "base/synchronization/lock.h"
John McCutchane55b6162015-04-22 13:32:05 -070013#include "dart/runtime/include/dart_api.h"
John McCutchane55b6162015-04-22 13:32:05 -070014#include "dart/runtime/include/dart_native_api.h"
John McCutchan9b04b9b2015-06-19 13:23:29 -070015#include "dart/runtime/include/dart_tools_api.h"
John McCutchane55b6162015-04-22 13:32:05 -070016
17namespace base {
18 class Lock;
19}
20
John McCutchanffdf1e72015-07-21 11:09:20 -070021namespace tonic {
22
23class 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
57class 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 McCutchane55b6162015-04-22 13:32:05 -070079
80class 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
101class 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 McCutchanf84766d2015-05-06 15:36:12 -0700123 static intptr_t FindIsolateIndexById(Dart_IsolateId id);
John McCutchane55b6162015-04-22 13:32:05 -0700124
John McCutchanf84766d2015-05-06 15:36:12 -0700125 static intptr_t FindIsolateIndexByIdLocked(Dart_IsolateId id);
John McCutchane55b6162015-04-22 13:32:05 -0700126
John McCutchanf84766d2015-05-06 15:36:12 -0700127 static void AddIsolate(Dart_IsolateId id);
John McCutchane55b6162015-04-22 13:32:05 -0700128
129 static void RemoveIsolate(Dart_IsolateId id);
130
131 static base::Lock* lock_;
John McCutchanf84766d2015-05-06 15:36:12 -0700132 static std::vector<std::unique_ptr<DartDebuggerIsolate>>* isolates_;
John McCutchane55b6162015-04-22 13:32:05 -0700133
134 friend class DartDebuggerIsolate;
135};
136
John McCutchanffdf1e72015-07-21 11:09:20 -0700137} // namespace tonic
John McCutchane55b6162015-04-22 13:32:05 -0700138
John McCutchanffdf1e72015-07-21 11:09:20 -0700139#endif // TONIC_DART_DEBUGGER_H_