OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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/service.h" | 5 #include "vm/service.h" |
6 | 6 |
7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
9 | 9 |
10 #include "vm/compiler.h" | 10 #include "vm/compiler.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "vm/object_id_ring.h" | 23 #include "vm/object_id_ring.h" |
24 #include "vm/object_store.h" | 24 #include "vm/object_store.h" |
25 #include "vm/port.h" | 25 #include "vm/port.h" |
26 #include "vm/profiler.h" | 26 #include "vm/profiler.h" |
27 #include "vm/reusable_handles.h" | 27 #include "vm/reusable_handles.h" |
28 #include "vm/stack_frame.h" | 28 #include "vm/stack_frame.h" |
29 #include "vm/symbols.h" | 29 #include "vm/symbols.h" |
30 #include "vm/unicode.h" | 30 #include "vm/unicode.h" |
31 #include "vm/version.h" | 31 #include "vm/version.h" |
32 | 32 |
33 | |
34 namespace dart { | 33 namespace dart { |
35 | 34 |
36 DEFINE_FLAG(bool, trace_service, false, "Trace VM service requests."); | 35 DEFINE_FLAG(bool, trace_service, false, "Trace VM service requests."); |
37 DEFINE_FLAG(bool, trace_service_pause_events, false, | 36 DEFINE_FLAG(bool, trace_service_pause_events, false, |
38 "Trace VM service isolate pause events."); | 37 "Trace VM service isolate pause events."); |
39 DECLARE_FLAG(bool, enable_type_checks); | 38 DECLARE_FLAG(bool, enable_type_checks); |
40 DECLARE_FLAG(bool, enable_asserts); | 39 DECLARE_FLAG(bool, enable_asserts); |
41 | 40 |
42 struct ResourcesEntry { | 41 struct ResourcesEntry { |
43 const char* path_; | 42 const char* path_; |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 void SetEventMask(Dart_NativeArguments args) { | 254 void SetEventMask(Dart_NativeArguments args) { |
256 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 255 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
257 Isolate* isolate = arguments->isolate(); | 256 Isolate* isolate = arguments->isolate(); |
258 StackZone zone(isolate); | 257 StackZone zone(isolate); |
259 HANDLESCOPE(isolate); | 258 HANDLESCOPE(isolate); |
260 GET_NON_NULL_NATIVE_ARGUMENT(Integer, mask, arguments->NativeArgAt(0)); | 259 GET_NON_NULL_NATIVE_ARGUMENT(Integer, mask, arguments->NativeArgAt(0)); |
261 Service::SetEventMask(mask.AsTruncatedUint32Value()); | 260 Service::SetEventMask(mask.AsTruncatedUint32Value()); |
262 } | 261 } |
263 | 262 |
264 | 263 |
265 struct VmServiceNativeEntry { | |
266 const char* name; | |
267 int num_arguments; | |
268 Dart_NativeFunction function; | |
269 }; | |
270 | |
271 | |
272 static VmServiceNativeEntry _VmServiceNativeEntries[] = { | |
273 {"VMService_SendIsolateServiceMessage", 2, SendIsolateServiceMessage}, | |
274 {"VMService_SendRootServiceMessage", 1, SendRootServiceMessage}, | |
275 {"VMService_SetEventMask", 1, SetEventMask}, | |
276 }; | |
277 | |
278 | |
279 static Dart_NativeFunction VmServiceNativeResolver(Dart_Handle name, | |
280 int num_arguments, | |
281 bool* auto_setup_scope) { | |
282 const Object& obj = Object::Handle(Api::UnwrapHandle(name)); | |
283 if (!obj.IsString()) { | |
284 return NULL; | |
285 } | |
286 const char* function_name = obj.ToCString(); | |
287 ASSERT(function_name != NULL); | |
288 ASSERT(auto_setup_scope != NULL); | |
289 *auto_setup_scope = true; | |
290 intptr_t n = | |
291 sizeof(_VmServiceNativeEntries) / sizeof(_VmServiceNativeEntries[0]); | |
292 for (intptr_t i = 0; i < n; i++) { | |
293 VmServiceNativeEntry entry = _VmServiceNativeEntries[i]; | |
294 if ((strcmp(function_name, entry.name) == 0) && | |
295 (num_arguments == entry.num_arguments)) { | |
296 return entry.function; | |
297 } | |
298 } | |
299 return NULL; | |
300 } | |
301 | |
302 | |
303 EmbedderServiceHandler* Service::isolate_service_handler_head_ = NULL; | |
304 EmbedderServiceHandler* Service::root_service_handler_head_ = NULL; | |
305 Isolate* Service::service_isolate_ = NULL; | |
306 Dart_LibraryTagHandler Service::embedder_provided_handler_ = NULL; | |
307 Dart_Port Service::port_ = ILLEGAL_PORT; | |
308 uint32_t Service::event_mask_ = 0; | |
309 | |
310 | |
311 static Dart_Port ExtractPort(Dart_Handle receivePort) { | |
312 HANDLESCOPE(Isolate::Current()); | |
313 const Object& unwrapped_rp = Object::Handle(Api::UnwrapHandle(receivePort)); | |
314 const Instance& rp = Instance::Cast(unwrapped_rp); | |
315 // Extract RawReceivePort port id. | |
316 if (!rp.IsReceivePort()) { | |
317 return ILLEGAL_PORT; | |
318 } | |
319 return ReceivePort::Cast(rp).Id(); | |
320 } | |
321 | |
322 | |
323 // These must be kept in sync with service/constants.dart | 264 // These must be kept in sync with service/constants.dart |
324 #define VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID 1 | 265 #define VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID 1 |
325 #define VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID 2 | 266 #define VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID 2 |
326 | 267 |
327 | 268 |
328 static RawArray* MakeServiceControlMessage(Dart_Port port_id, intptr_t code, | 269 static RawArray* MakeServiceControlMessage(Dart_Port port_id, intptr_t code, |
329 const String& name) { | 270 const String& name) { |
330 const Array& list = Array::Handle(Array::New(4)); | 271 const Array& list = Array::Handle(Array::New(4)); |
331 ASSERT(!list.IsNull()); | 272 ASSERT(!list.IsNull()); |
332 const Integer& code_int = Integer::Handle(Integer::New(code)); | 273 const Integer& code_int = Integer::Handle(Integer::New(code)); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 r = DartEntry::InvokeFunction(register_function_, args); | 325 r = DartEntry::InvokeFunction(register_function_, args); |
385 ASSERT(!r.IsError()); | 326 ASSERT(!r.IsError()); |
386 } | 327 } |
387 | 328 |
388 private: | 329 private: |
389 Function& register_function_; | 330 Function& register_function_; |
390 Isolate* service_isolate_; | 331 Isolate* service_isolate_; |
391 }; | 332 }; |
392 | 333 |
393 | 334 |
335 static Dart_Port ExtractPort(Dart_Handle receivePort) { | |
336 HANDLESCOPE(Isolate::Current()); | |
337 const Object& unwrapped_rp = Object::Handle(Api::UnwrapHandle(receivePort)); | |
338 const Instance& rp = Instance::Cast(unwrapped_rp); | |
339 // Extract RawReceivePort port id. | |
340 if (!rp.IsReceivePort()) { | |
341 return ILLEGAL_PORT; | |
342 } | |
343 return ReceivePort::Cast(rp).Id(); | |
siva
2014/09/09 00:40:58
The more standard way to write this is:
static Da
Cutch
2014/09/10 15:11:23
Done.
| |
344 } | |
345 | |
346 | |
347 static void OnStart(Dart_NativeArguments args) { | |
348 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | |
349 Isolate* isolate = arguments->isolate(); | |
350 StackZone zone(isolate); | |
351 HANDLESCOPE(isolate); | |
352 { | |
353 // Boot the dart:vmservice library. | |
354 Dart_EnterScope(); | |
355 Dart_Handle result; | |
356 Dart_Handle url_str = | |
357 Dart_NewStringFromCString(Symbols::Name(Symbols::kDartVMServiceId)); | |
358 Dart_Handle library = Dart_LookupLibrary(url_str); | |
359 ASSERT(Dart_IsLibrary(library)); | |
360 result = Dart_Invoke(library, Dart_NewStringFromCString("boot"), 0, NULL); | |
siva
2014/09/09 00:40:58
Dart_Handle result = Dart_Invoke(...);
Cutch
2014/09/10 15:11:23
Done.
| |
361 ASSERT(!Dart_IsError(result)); | |
362 Dart_Port port = ExtractPort(result); | |
siva
2014/09/09 00:40:58
ExtractPort(isolate, result);
Cutch
2014/09/10 15:11:23
Done.
| |
363 ASSERT(port != ILLEGAL_PORT); | |
364 Service::set_port(port); | |
365 Dart_ExitScope(); | |
366 } | |
367 { | |
368 // Register running isolates with service. | |
369 RegisterRunningIsolatesVisitor register_isolates(isolate); | |
370 Isolate::VisitIsolates(®ister_isolates); | |
371 } | |
372 } | |
373 | |
374 | |
375 struct VmServiceNativeEntry { | |
376 const char* name; | |
377 int num_arguments; | |
378 Dart_NativeFunction function; | |
379 }; | |
380 | |
381 | |
382 static VmServiceNativeEntry _VmServiceNativeEntries[] = { | |
383 {"VMService_SendIsolateServiceMessage", 2, SendIsolateServiceMessage}, | |
384 {"VMService_SendRootServiceMessage", 1, SendRootServiceMessage}, | |
385 {"VMService_SetEventMask", 1, SetEventMask}, | |
386 {"VMService_OnStart", 0, OnStart }, | |
387 }; | |
388 | |
389 | |
390 static Dart_NativeFunction VmServiceNativeResolver(Dart_Handle name, | |
391 int num_arguments, | |
392 bool* auto_setup_scope) { | |
393 const Object& obj = Object::Handle(Api::UnwrapHandle(name)); | |
394 if (!obj.IsString()) { | |
395 return NULL; | |
396 } | |
397 const char* function_name = obj.ToCString(); | |
398 ASSERT(function_name != NULL); | |
399 ASSERT(auto_setup_scope != NULL); | |
400 *auto_setup_scope = true; | |
401 intptr_t n = | |
402 sizeof(_VmServiceNativeEntries) / sizeof(_VmServiceNativeEntries[0]); | |
403 for (intptr_t i = 0; i < n; i++) { | |
404 VmServiceNativeEntry entry = _VmServiceNativeEntries[i]; | |
405 if ((strcmp(function_name, entry.name) == 0) && | |
406 (num_arguments == entry.num_arguments)) { | |
407 return entry.function; | |
408 } | |
409 } | |
410 return NULL; | |
411 } | |
412 | |
413 | |
414 EmbedderServiceHandler* Service::isolate_service_handler_head_ = NULL; | |
415 EmbedderServiceHandler* Service::root_service_handler_head_ = NULL; | |
416 Isolate* Service::service_isolate_ = NULL; | |
417 Dart_LibraryTagHandler Service::embedder_provided_handler_ = NULL; | |
418 Dart_Port Service::port_ = ILLEGAL_PORT; | |
419 uint32_t Service::event_mask_ = 0; | |
420 | |
394 Isolate* Service::GetServiceIsolate(void* callback_data) { | 421 Isolate* Service::GetServiceIsolate(void* callback_data) { |
395 if (service_isolate_ != NULL) { | 422 if (service_isolate_ != NULL) { |
396 // Already initialized, return service isolate. | 423 // Already initialized, return service isolate. |
397 return service_isolate_; | 424 return service_isolate_; |
398 } | 425 } |
399 Dart_ServiceIsolateCreateCalback create_callback = | 426 Dart_ServiceIsolateCreateCalback create_callback = |
400 Isolate::ServiceCreateCallback(); | 427 Isolate::ServiceCreateCallback(); |
401 if (create_callback == NULL) { | 428 if (create_callback == NULL) { |
402 return NULL; | 429 return NULL; |
403 } | 430 } |
404 Isolate::SetCurrent(NULL); | 431 Isolate::SetCurrent(NULL); |
405 char* error = NULL; | 432 char* error = NULL; |
406 Isolate* isolate = reinterpret_cast<Isolate*>( | 433 Isolate* isolate = |
407 create_callback(callback_data, &error)); | 434 reinterpret_cast<Isolate*>(create_callback(callback_data, &error)); |
408 if (isolate == NULL) { | 435 if (isolate == NULL) { |
409 return NULL; | 436 return NULL; |
410 } | 437 } |
411 StartIsolateScope isolate_scope(isolate); | 438 StartIsolateScope isolate_scope(isolate); |
412 { | 439 { |
413 // Install the dart:vmservice library. | 440 // Install the dart:vmservice library. |
414 StackZone zone(isolate); | 441 StackZone zone(isolate); |
415 HANDLESCOPE(isolate); | 442 HANDLESCOPE(isolate); |
416 Library& library = | 443 Library& library = |
417 Library::Handle(isolate, isolate->object_store()->root_library()); | 444 Library::Handle(isolate, isolate->object_store()->root_library()); |
(...skipping 27 matching lines...) Expand all Loading... | |
445 ASSERT(error.IsNull()); | 472 ASSERT(error.IsNull()); |
446 Dart_Handle result = Dart_FinalizeLoading(false); | 473 Dart_Handle result = Dart_FinalizeLoading(false); |
447 ASSERT(!Dart_IsError(result)); | 474 ASSERT(!Dart_IsError(result)); |
448 Dart_ExitScope(); | 475 Dart_ExitScope(); |
449 | 476 |
450 // Install embedder default library tag handler again. | 477 // Install embedder default library tag handler again. |
451 isolate->set_library_tag_handler(embedder_provided_handler_); | 478 isolate->set_library_tag_handler(embedder_provided_handler_); |
452 embedder_provided_handler_ = NULL; | 479 embedder_provided_handler_ = NULL; |
453 library.set_native_entry_resolver(VmServiceNativeResolver); | 480 library.set_native_entry_resolver(VmServiceNativeResolver); |
454 } | 481 } |
455 { | |
456 // Boot the dart:vmservice library. | |
457 Dart_EnterScope(); | |
458 Dart_Handle result; | |
459 Dart_Handle url_str = | |
460 Dart_NewStringFromCString(Symbols::Name(Symbols::kDartVMServiceId)); | |
461 Dart_Handle library = Dart_LookupLibrary(url_str); | |
462 ASSERT(Dart_IsLibrary(library)); | |
463 result = Dart_Invoke(library, Dart_NewStringFromCString("boot"), 0, NULL); | |
464 ASSERT(!Dart_IsError(result)); | |
465 port_ = ExtractPort(result); | |
466 ASSERT(port_ != ILLEGAL_PORT); | |
467 Dart_ExitScope(); | |
468 } | |
469 { | |
470 // Register existing isolates. | |
471 StackZone zone(isolate); | |
472 HANDLESCOPE(isolate); | |
473 RegisterRunningIsolatesVisitor register_isolates(isolate); | |
474 Isolate::VisitIsolates(®ister_isolates); | |
475 } | |
476 service_isolate_ = reinterpret_cast<Isolate*>(isolate); | 482 service_isolate_ = reinterpret_cast<Isolate*>(isolate); |
477 return service_isolate_; | 483 return service_isolate_; |
478 } | 484 } |
479 | 485 |
480 | 486 |
481 bool Service::SendIsolateStartupMessage() { | 487 bool Service::SendIsolateStartupMessage() { |
482 if (!IsRunning()) { | 488 if (!IsRunning()) { |
483 return false; | 489 return false; |
484 } | 490 } |
485 Isolate* isolate = Isolate::Current(); | 491 Isolate* isolate = Isolate::Current(); |
(...skipping 2224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2710 while (current != NULL) { | 2716 while (current != NULL) { |
2711 if (strcmp(name, current->name()) == 0) { | 2717 if (strcmp(name, current->name()) == 0) { |
2712 return current; | 2718 return current; |
2713 } | 2719 } |
2714 current = current->next(); | 2720 current = current->next(); |
2715 } | 2721 } |
2716 return NULL; | 2722 return NULL; |
2717 } | 2723 } |
2718 | 2724 |
2719 } // namespace dart | 2725 } // namespace dart |
OLD | NEW |