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(Isolate* isolate, Dart_Handle receivePort) { |
| 336 const ReceivePort& rp = Api::UnwrapReceivePortHandle(isolate, receivePort); |
| 337 if (rp.IsNull()) { |
| 338 return ILLEGAL_PORT; |
| 339 } |
| 340 return rp.Id(); |
| 341 } |
| 342 |
| 343 |
| 344 static void OnStart(Dart_NativeArguments args) { |
| 345 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
| 346 Isolate* isolate = arguments->isolate(); |
| 347 StackZone zone(isolate); |
| 348 HANDLESCOPE(isolate); |
| 349 { |
| 350 // Boot the dart:vmservice library. |
| 351 Dart_EnterScope(); |
| 352 Dart_Handle url_str = |
| 353 Dart_NewStringFromCString(Symbols::Name(Symbols::kDartVMServiceId)); |
| 354 Dart_Handle library = Dart_LookupLibrary(url_str); |
| 355 ASSERT(Dart_IsLibrary(library)); |
| 356 Dart_Handle result = |
| 357 Dart_Invoke(library, Dart_NewStringFromCString("boot"), 0, NULL); |
| 358 ASSERT(!Dart_IsError(result)); |
| 359 Dart_Port port = ExtractPort(isolate, result); |
| 360 ASSERT(port != ILLEGAL_PORT); |
| 361 Service::set_port(port); |
| 362 Dart_ExitScope(); |
| 363 } |
| 364 { |
| 365 // Register running isolates with service. |
| 366 RegisterRunningIsolatesVisitor register_isolates(isolate); |
| 367 Isolate::VisitIsolates(®ister_isolates); |
| 368 } |
| 369 } |
| 370 |
| 371 |
| 372 struct VmServiceNativeEntry { |
| 373 const char* name; |
| 374 int num_arguments; |
| 375 Dart_NativeFunction function; |
| 376 }; |
| 377 |
| 378 |
| 379 static VmServiceNativeEntry _VmServiceNativeEntries[] = { |
| 380 {"VMService_SendIsolateServiceMessage", 2, SendIsolateServiceMessage}, |
| 381 {"VMService_SendRootServiceMessage", 1, SendRootServiceMessage}, |
| 382 {"VMService_SetEventMask", 1, SetEventMask}, |
| 383 {"VMService_OnStart", 0, OnStart }, |
| 384 }; |
| 385 |
| 386 |
| 387 static Dart_NativeFunction VmServiceNativeResolver(Dart_Handle name, |
| 388 int num_arguments, |
| 389 bool* auto_setup_scope) { |
| 390 const Object& obj = Object::Handle(Api::UnwrapHandle(name)); |
| 391 if (!obj.IsString()) { |
| 392 return NULL; |
| 393 } |
| 394 const char* function_name = obj.ToCString(); |
| 395 ASSERT(function_name != NULL); |
| 396 ASSERT(auto_setup_scope != NULL); |
| 397 *auto_setup_scope = true; |
| 398 intptr_t n = |
| 399 sizeof(_VmServiceNativeEntries) / sizeof(_VmServiceNativeEntries[0]); |
| 400 for (intptr_t i = 0; i < n; i++) { |
| 401 VmServiceNativeEntry entry = _VmServiceNativeEntries[i]; |
| 402 if ((strcmp(function_name, entry.name) == 0) && |
| 403 (num_arguments == entry.num_arguments)) { |
| 404 return entry.function; |
| 405 } |
| 406 } |
| 407 return NULL; |
| 408 } |
| 409 |
| 410 |
| 411 EmbedderServiceHandler* Service::isolate_service_handler_head_ = NULL; |
| 412 EmbedderServiceHandler* Service::root_service_handler_head_ = NULL; |
| 413 Isolate* Service::service_isolate_ = NULL; |
| 414 Dart_LibraryTagHandler Service::embedder_provided_handler_ = NULL; |
| 415 Dart_Port Service::port_ = ILLEGAL_PORT; |
| 416 uint32_t Service::event_mask_ = 0; |
| 417 |
394 Isolate* Service::GetServiceIsolate(void* callback_data) { | 418 Isolate* Service::GetServiceIsolate(void* callback_data) { |
395 if (service_isolate_ != NULL) { | 419 if (service_isolate_ != NULL) { |
396 // Already initialized, return service isolate. | 420 // Already initialized, return service isolate. |
397 return service_isolate_; | 421 return service_isolate_; |
398 } | 422 } |
399 Dart_ServiceIsolateCreateCalback create_callback = | 423 Dart_ServiceIsolateCreateCalback create_callback = |
400 Isolate::ServiceCreateCallback(); | 424 Isolate::ServiceCreateCallback(); |
401 if (create_callback == NULL) { | 425 if (create_callback == NULL) { |
402 return NULL; | 426 return NULL; |
403 } | 427 } |
404 Isolate::SetCurrent(NULL); | 428 Isolate::SetCurrent(NULL); |
405 char* error = NULL; | 429 char* error = NULL; |
406 Isolate* isolate = reinterpret_cast<Isolate*>( | 430 Isolate* isolate = |
407 create_callback(callback_data, &error)); | 431 reinterpret_cast<Isolate*>(create_callback(callback_data, &error)); |
408 if (isolate == NULL) { | 432 if (isolate == NULL) { |
409 return NULL; | 433 return NULL; |
410 } | 434 } |
411 StartIsolateScope isolate_scope(isolate); | 435 StartIsolateScope isolate_scope(isolate); |
412 { | 436 { |
413 // Install the dart:vmservice library. | 437 // Install the dart:vmservice library. |
414 StackZone zone(isolate); | 438 StackZone zone(isolate); |
415 HANDLESCOPE(isolate); | 439 HANDLESCOPE(isolate); |
416 Library& library = | 440 Library& library = |
417 Library::Handle(isolate, isolate->object_store()->root_library()); | 441 Library::Handle(isolate, isolate->object_store()->root_library()); |
(...skipping 27 matching lines...) Expand all Loading... |
445 ASSERT(error.IsNull()); | 469 ASSERT(error.IsNull()); |
446 Dart_Handle result = Dart_FinalizeLoading(false); | 470 Dart_Handle result = Dart_FinalizeLoading(false); |
447 ASSERT(!Dart_IsError(result)); | 471 ASSERT(!Dart_IsError(result)); |
448 Dart_ExitScope(); | 472 Dart_ExitScope(); |
449 | 473 |
450 // Install embedder default library tag handler again. | 474 // Install embedder default library tag handler again. |
451 isolate->set_library_tag_handler(embedder_provided_handler_); | 475 isolate->set_library_tag_handler(embedder_provided_handler_); |
452 embedder_provided_handler_ = NULL; | 476 embedder_provided_handler_ = NULL; |
453 library.set_native_entry_resolver(VmServiceNativeResolver); | 477 library.set_native_entry_resolver(VmServiceNativeResolver); |
454 } | 478 } |
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); | 479 service_isolate_ = reinterpret_cast<Isolate*>(isolate); |
477 return service_isolate_; | 480 return service_isolate_; |
478 } | 481 } |
479 | 482 |
480 | 483 |
481 bool Service::SendIsolateStartupMessage() { | 484 bool Service::SendIsolateStartupMessage() { |
482 if (!IsRunning()) { | 485 if (!IsRunning()) { |
483 return false; | 486 return false; |
484 } | 487 } |
485 Isolate* isolate = Isolate::Current(); | 488 Isolate* isolate = Isolate::Current(); |
(...skipping 2224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2710 while (current != NULL) { | 2713 while (current != NULL) { |
2711 if (strcmp(name, current->name()) == 0) { | 2714 if (strcmp(name, current->name()) == 0) { |
2712 return current; | 2715 return current; |
2713 } | 2716 } |
2714 current = current->next(); | 2717 current = current->next(); |
2715 } | 2718 } |
2716 return NULL; | 2719 return NULL; |
2717 } | 2720 } |
2718 | 2721 |
2719 } // namespace dart | 2722 } // namespace dart |
OLD | NEW |