1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
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. |
4 | |
5 | #ifndef RUNTIME_VM_SERVICE_H_ |
6 | #define RUNTIME_VM_SERVICE_H_ |
7 | |
8 | #include "include/dart_tools_api.h" |
9 | |
10 | #include "vm/allocation.h" |
11 | #include "vm/object_id_ring.h" |
12 | #include "vm/os_thread.h" |
13 | #include "vm/tagged_pointer.h" |
14 | |
15 | namespace dart { |
16 | |
17 | #define SERVICE_PROTOCOL_MAJOR_VERSION 3 |
18 | #define SERVICE_PROTOCOL_MINOR_VERSION 37 |
19 | |
20 | class Array; |
21 | class EmbedderServiceHandler; |
22 | class Error; |
23 | class GCEvent; |
24 | class GrowableObjectArray; |
25 | class Instance; |
26 | class Isolate; |
27 | class IsolateGroup; |
28 | class JSONStream; |
29 | class JSONObject; |
30 | class Object; |
31 | class ServiceEvent; |
32 | class String; |
33 | |
34 | class ServiceIdZone { |
35 | public: |
36 | ServiceIdZone(); |
37 | virtual ~ServiceIdZone(); |
38 | |
39 | // Returned string will be zone allocated. |
40 | virtual char* GetServiceId(const Object& obj) = 0; |
41 | |
42 | private: |
43 | }; |
44 | |
45 | #define ISOLATE_SERVICE_ID_FORMAT_STRING "isolates/%" Pd64 "" |
46 | #define ISOLATE_GROUP_SERVICE_ID_PREFIX "isolateGroups/" |
47 | #define ISOLATE_GROUP_SERVICE_ID_FORMAT_STRING \ |
48 | ISOLATE_GROUP_SERVICE_ID_PREFIX "%" Pu64 "" |
49 | |
50 | class RingServiceIdZone : public ServiceIdZone { |
51 | public: |
52 | RingServiceIdZone(); |
53 | virtual ~RingServiceIdZone(); |
54 | |
55 | void Init(ObjectIdRing* ring, ObjectIdRing::IdPolicy policy); |
56 | |
57 | // Returned string will be zone allocated. |
58 | virtual char* GetServiceId(const Object& obj); |
59 | |
60 | void set_policy(ObjectIdRing::IdPolicy policy) { policy_ = policy; } |
61 | |
62 | ObjectIdRing::IdPolicy policy() const { return policy_; } |
63 | |
64 | private: |
65 | ObjectIdRing* ring_; |
66 | ObjectIdRing::IdPolicy policy_; |
67 | }; |
68 | |
69 | class StreamInfo { |
70 | public: |
71 | explicit StreamInfo(const char* id) : id_(id), enabled_(false) {} |
72 | |
73 | const char* id() const { return id_; } |
74 | |
75 | void set_enabled(bool value) { enabled_ = value; } |
76 | bool enabled() const { return enabled_; } |
77 | |
78 | void set_consumer(Dart_NativeStreamConsumer consumer) { |
79 | callback_ = consumer; |
80 | } |
81 | Dart_NativeStreamConsumer consumer() const { return callback_; } |
82 | |
83 | private: |
84 | const char* id_; |
85 | bool enabled_; |
86 | Dart_NativeStreamConsumer callback_; |
87 | }; |
88 | |
89 | class Service : public AllStatic { |
90 | public: |
91 | // Handles a message which is not directed to an isolate. |
92 | static ErrorPtr HandleRootMessage(const Array& message); |
93 | |
94 | // Handles a message which is not directed to an isolate and also |
95 | // expects the parameter keys and values to be actual dart objects. |
96 | static ErrorPtr HandleObjectRootMessage(const Array& message); |
97 | |
98 | // Handles a message which is directed to a particular isolate. |
99 | static ErrorPtr HandleIsolateMessage(Isolate* isolate, const Array& message); |
100 | |
101 | static void HandleEvent(ServiceEvent* event); |
102 | |
103 | static void RegisterIsolateEmbedderCallback( |
104 | const char* name, |
105 | Dart_ServiceRequestCallback callback, |
106 | void* user_data); |
107 | |
108 | static void RegisterRootEmbedderCallback(const char* name, |
109 | Dart_ServiceRequestCallback callback, |
110 | void* user_data); |
111 | |
112 | static void SetEmbedderInformationCallback( |
113 | Dart_EmbedderInformationCallback callback); |
114 | |
115 | static void SetEmbedderStreamCallbacks( |
116 | Dart_ServiceStreamListenCallback listen_callback, |
117 | Dart_ServiceStreamCancelCallback cancel_callback); |
118 | |
119 | static void SetNativeServiceStreamCallback(Dart_NativeStreamConsumer consumer, |
120 | const char* stream_id); |
121 | |
122 | static void SetGetServiceAssetsCallback( |
123 | Dart_GetVMServiceAssetsArchive get_service_assets); |
124 | |
125 | static void SendEchoEvent(Isolate* isolate, const char* text); |
126 | static void SendInspectEvent(Isolate* isolate, const Object& inspectee); |
127 | |
128 | static void SendEmbedderEvent(Isolate* isolate, |
129 | const char* stream_id, |
130 | const char* event_kind, |
131 | const uint8_t* bytes, |
132 | intptr_t bytes_len); |
133 | |
134 | static void SendLogEvent(Isolate* isolate, |
135 | int64_t sequence_number, |
136 | int64_t timestamp, |
137 | intptr_t level, |
138 | const String& name, |
139 | const String& message, |
140 | const Instance& zone, |
141 | const Object& error, |
142 | const Instance& stack_trace); |
143 | |
144 | static void SendExtensionEvent(Isolate* isolate, |
145 | const String& event_kind, |
146 | const String& event_data); |
147 | |
148 | // Takes ownership of 'data'. |
149 | static void SendEventWithData(const char* stream_id, |
150 | const char* event_type, |
151 | intptr_t reservation, |
152 | const char* metadata, |
153 | intptr_t metadata_size, |
154 | uint8_t* data, |
155 | intptr_t data_size); |
156 | |
157 | static void PostError(const String& method_name, |
158 | const Array& parameter_keys, |
159 | const Array& parameter_values, |
160 | const Instance& reply_port, |
161 | const Instance& id, |
162 | const Error& error); |
163 | |
164 | // Well-known streams. |
165 | static StreamInfo vm_stream; |
166 | static StreamInfo isolate_stream; |
167 | static StreamInfo debug_stream; |
168 | static StreamInfo gc_stream; |
169 | static StreamInfo echo_stream; |
170 | static StreamInfo heapsnapshot_stream; |
171 | static StreamInfo logging_stream; |
172 | static StreamInfo extension_stream; |
173 | static StreamInfo timeline_stream; |
174 | |
175 | static bool ListenStream(const char* stream_id); |
176 | static void CancelStream(const char* stream_id); |
177 | |
178 | static ObjectPtr RequestAssets(); |
179 | |
180 | static Dart_ServiceStreamListenCallback stream_listen_callback() { |
181 | return stream_listen_callback_; |
182 | } |
183 | static Dart_ServiceStreamCancelCallback stream_cancel_callback() { |
184 | return stream_cancel_callback_; |
185 | } |
186 | |
187 | static void PrintJSONForEmbedderInformation(JSONObject *jsobj); |
188 | static void PrintJSONForVM(JSONStream* js, bool ref); |
189 | |
190 | static void CheckForPause(Isolate* isolate, JSONStream* stream); |
191 | |
192 | static int64_t (); |
193 | static int64_t (); |
194 | |
195 | static void SetDartLibraryKernelForSources(const uint8_t* kernel_bytes, |
196 | intptr_t kernel_length); |
197 | static bool HasDartLibraryKernelForSources() { |
198 | return (dart_library_kernel_ != NULL); |
199 | } |
200 | |
201 | static const uint8_t* dart_library_kernel() { return dart_library_kernel_; } |
202 | |
203 | static intptr_t dart_library_kernel_length() { |
204 | return dart_library_kernel_len_; |
205 | } |
206 | |
207 | private: |
208 | static ErrorPtr InvokeMethod(Isolate* isolate, |
209 | const Array& message, |
210 | bool parameters_are_dart_objects = false); |
211 | |
212 | static void EmbedderHandleMessage(EmbedderServiceHandler* handler, |
213 | JSONStream* js); |
214 | |
215 | static EmbedderServiceHandler* FindIsolateEmbedderHandler(const char* name); |
216 | static EmbedderServiceHandler* FindRootEmbedderHandler(const char* name); |
217 | static void ScheduleExtensionHandler(const Instance& handler, |
218 | const String& method_name, |
219 | const Array& parameter_keys, |
220 | const Array& parameter_values, |
221 | const Instance& reply_port, |
222 | const Instance& id); |
223 | |
224 | // Takes ownership of 'bytes'. |
225 | static void SendEvent(const char* stream_id, |
226 | const char* event_type, |
227 | uint8_t* bytes, |
228 | intptr_t bytes_length); |
229 | |
230 | static void PostEvent(Isolate* isolate, |
231 | const char* stream_id, |
232 | const char* kind, |
233 | JSONStream* event); |
234 | |
235 | static ErrorPtr MaybePause(Isolate* isolate, const Error& error); |
236 | |
237 | static EmbedderServiceHandler* isolate_service_handler_head_; |
238 | static EmbedderServiceHandler* root_service_handler_head_; |
239 | static Dart_ServiceStreamListenCallback stream_listen_callback_; |
240 | static Dart_ServiceStreamCancelCallback stream_cancel_callback_; |
241 | static Dart_GetVMServiceAssetsArchive get_service_assets_callback_; |
242 | static Dart_EmbedderInformationCallback embedder_information_callback_; |
243 | |
244 | static const uint8_t* dart_library_kernel_; |
245 | static intptr_t dart_library_kernel_len_; |
246 | }; |
247 | |
248 | } // namespace dart |
249 | |
250 | #endif // RUNTIME_VM_SERVICE_H_ |
251 | |