Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Side by Side Diff: runtime/vm/service.cc

Issue 871373012: Make service lifecycle checks thread safe (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 OS::Print("[%" Pd "] %s took %" Pd64 " micros.\n", 265 OS::Print("[%" Pd "] %s took %" Pd64 " micros.\n",
266 OS::ProcessId(), name_, elapsed); 266 OS::ProcessId(), name_, elapsed);
267 } 267 }
268 268
269 private: 269 private:
270 const char* name_; 270 const char* name_;
271 int64_t start_; 271 int64_t start_;
272 }; 272 };
273 273
274 274
275 bool Service::IsRunning() {
276 MonitorLocker ml(monitor_);
277 return (service_port_ != ILLEGAL_PORT) && (service_isolate_ != NULL);
278 }
279
280
281 void Service::SetServicePort(Dart_Port port) {
282 MonitorLocker ml(monitor_);
283 service_port_ = port;
284 }
285
286
287 void Service::SetServiceIsolate(Isolate* isolate) {
288 MonitorLocker ml(monitor_);
289 service_isolate_ = isolate;
290 }
291
292
293 bool Service::HasServiceIsolate() {
294 MonitorLocker ml(monitor_);
295 return service_isolate_ != NULL;
296 }
297
298
299 bool Service::IsServiceIsolate(Isolate* isolate) {
300 MonitorLocker ml(monitor_);
301 return isolate == service_isolate_;
302 }
303
275 Dart_Port Service::WaitForLoadPort() { 304 Dart_Port Service::WaitForLoadPort() {
276 MonitorLocker ml(monitor_); 305 MonitorLocker ml(monitor_);
277 306
278 while (initializing_ && (load_port_ == ILLEGAL_PORT)) { 307 while (initializing_ && (load_port_ == ILLEGAL_PORT)) {
279 ml.Wait(); 308 ml.Wait();
280 } 309 }
281 310
282 return load_port_; 311 return load_port_;
283 } 312 }
284 313
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 return list.raw(); 358 return list.raw();
330 } 359 }
331 360
332 361
333 class RegisterRunningIsolatesVisitor : public IsolateVisitor { 362 class RegisterRunningIsolatesVisitor : public IsolateVisitor {
334 public: 363 public:
335 explicit RegisterRunningIsolatesVisitor(Isolate* service_isolate) 364 explicit RegisterRunningIsolatesVisitor(Isolate* service_isolate)
336 : IsolateVisitor(), 365 : IsolateVisitor(),
337 register_function_(Function::Handle(service_isolate)), 366 register_function_(Function::Handle(service_isolate)),
338 service_isolate_(service_isolate) { 367 service_isolate_(service_isolate) {
339 ASSERT(Isolate::Current() == service_isolate_); 368 ASSERT(Service::IsServiceIsolate(Isolate::Current()));
340 // Get library. 369 // Get library.
341 const String& library_url = Symbols::DartVMService(); 370 const String& library_url = Symbols::DartVMService();
342 ASSERT(!library_url.IsNull()); 371 ASSERT(!library_url.IsNull());
343 const Library& library = 372 const Library& library =
344 Library::Handle(Library::LookupLibrary(library_url)); 373 Library::Handle(Library::LookupLibrary(library_url));
345 ASSERT(!library.IsNull()); 374 ASSERT(!library.IsNull());
346 // Get function. 375 // Get function.
347 const String& function_name = 376 const String& function_name =
348 String::Handle(String::New("_registerIsolate")); 377 String::Handle(String::New("_registerIsolate"));
349 ASSERT(!function_name.IsNull()); 378 ASSERT(!function_name.IsNull());
350 register_function_ = library.LookupFunctionAllowPrivate(function_name); 379 register_function_ = library.LookupFunctionAllowPrivate(function_name);
351 ASSERT(!register_function_.IsNull()); 380 ASSERT(!register_function_.IsNull());
352 } 381 }
353 382
354 virtual void VisitIsolate(Isolate* isolate) { 383 virtual void VisitIsolate(Isolate* isolate) {
355 ASSERT(Isolate::Current() == service_isolate_); 384 ASSERT(Service::IsServiceIsolate(Isolate::Current()));
356 if ((isolate == service_isolate_) || 385 if (Service::IsServiceIsolate(isolate) ||
357 (isolate == Dart::vm_isolate())) { 386 (isolate == Dart::vm_isolate())) {
358 // We do not register the service or vm isolate. 387 // We do not register the service or vm isolate.
359 return; 388 return;
360 } 389 }
361 // Setup arguments for call. 390 // Setup arguments for call.
362 Dart_Port port_id = isolate->main_port(); 391 Dart_Port port_id = isolate->main_port();
363 const Integer& port_int = Integer::Handle(Integer::New(port_id)); 392 const Integer& port_int = Integer::Handle(Integer::New(port_id));
364 ASSERT(!port_int.IsNull()); 393 ASSERT(!port_int.IsNull());
365 const SendPort& send_port = SendPort::Handle(SendPort::New(port_id)); 394 const SendPort& send_port = SendPort::Handle(SendPort::New(port_id));
366 const String& name = String::Handle(String::New(isolate->name())); 395 const String& name = String::Handle(String::New(isolate->name()));
367 ASSERT(!name.IsNull()); 396 ASSERT(!name.IsNull());
368 const Array& args = Array::Handle(Array::New(3)); 397 const Array& args = Array::Handle(Array::New(3));
369 ASSERT(!args.IsNull()); 398 ASSERT(!args.IsNull());
370 args.SetAt(0, port_int); 399 args.SetAt(0, port_int);
371 args.SetAt(1, send_port); 400 args.SetAt(1, send_port);
372 args.SetAt(2, name); 401 args.SetAt(2, name);
373 Object& r = Object::Handle(service_isolate_); 402 Object& r = Object::Handle(service_isolate_);
374 r = DartEntry::InvokeFunction(register_function_, args); 403 r = DartEntry::InvokeFunction(register_function_, args);
375 if (FLAG_trace_service) { 404 if (FLAG_trace_service) {
376 OS::Print("Isolate %s %" Pd64 " registered with service \n", 405 OS::Print("vm-service: Isolate %s %" Pd64 " registered.\n",
377 name.ToCString(), 406 name.ToCString(),
378 port_id); 407 port_id);
379 } 408 }
380 ASSERT(!r.IsError()); 409 ASSERT(!r.IsError());
381 } 410 }
382 411
383 private: 412 private:
384 Function& register_function_; 413 Function& register_function_;
385 Isolate* service_isolate_; 414 Isolate* service_isolate_;
386 }; 415 };
387 416
388 417
389 static Dart_Port ExtractPort(Isolate* isolate, Dart_Handle receivePort) { 418 static Dart_Port ExtractPort(Isolate* isolate, Dart_Handle receivePort) {
390 const ReceivePort& rp = Api::UnwrapReceivePortHandle(isolate, receivePort); 419 const ReceivePort& rp = Api::UnwrapReceivePortHandle(isolate, receivePort);
391 if (rp.IsNull()) { 420 if (rp.IsNull()) {
392 return ILLEGAL_PORT; 421 return ILLEGAL_PORT;
393 } 422 }
394 return rp.Id(); 423 return rp.Id();
395 } 424 }
396 425
397 426
398 static void OnStart(Dart_NativeArguments args) { 427 static void OnStart(Dart_NativeArguments args) {
399 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); 428 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
400 Isolate* isolate = arguments->isolate(); 429 Isolate* isolate = arguments->isolate();
401 StackZone zone(isolate); 430 StackZone zone(isolate);
402 HANDLESCOPE(isolate); 431 HANDLESCOPE(isolate);
403 { 432 {
404 if (FLAG_trace_service) { 433 if (FLAG_trace_service) {
405 OS::Print("Booting dart:vmservice library\n"); 434 OS::Print("vm-service: Booting dart:vmservice library.\n");
406 } 435 }
407 // Boot the dart:vmservice library. 436 // Boot the dart:vmservice library.
408 Dart_EnterScope(); 437 Dart_EnterScope();
409 Dart_Handle url_str = 438 Dart_Handle url_str =
410 Dart_NewStringFromCString(Symbols::Name(Symbols::kDartVMServiceId)); 439 Dart_NewStringFromCString(Symbols::Name(Symbols::kDartVMServiceId));
411 Dart_Handle library = Dart_LookupLibrary(url_str); 440 Dart_Handle library = Dart_LookupLibrary(url_str);
412 ASSERT(Dart_IsLibrary(library)); 441 ASSERT(Dart_IsLibrary(library));
413 Dart_Handle result = 442 Dart_Handle result =
414 Dart_Invoke(library, Dart_NewStringFromCString("boot"), 0, NULL); 443 Dart_Invoke(library, Dart_NewStringFromCString("boot"), 0, NULL);
415 ASSERT(!Dart_IsError(result)); 444 ASSERT(!Dart_IsError(result));
416 Dart_Port port = ExtractPort(isolate, result); 445 Dart_Port port = ExtractPort(isolate, result);
417 ASSERT(port != ILLEGAL_PORT); 446 ASSERT(port != ILLEGAL_PORT);
418 Service::set_port(port); 447 Service::SetServicePort(port);
419 Dart_ExitScope(); 448 Dart_ExitScope();
420 } 449 }
421 450
422 { 451 {
423 if (FLAG_trace_service) { 452 if (FLAG_trace_service) {
424 OS::Print("Registering running isolates\n"); 453 OS::Print("vm-service: Registering running isolates.\n");
425 } 454 }
426 // Register running isolates with service. 455 // Register running isolates with service.
427 RegisterRunningIsolatesVisitor register_isolates(isolate); 456 RegisterRunningIsolatesVisitor register_isolates(isolate);
428 Isolate::VisitIsolates(&register_isolates); 457 Isolate::VisitIsolates(&register_isolates);
429 } 458 }
430 } 459 }
431 460
432 461
433 struct VmServiceNativeEntry { 462 struct VmServiceNativeEntry {
434 const char* name; 463 const char* name;
(...skipping 30 matching lines...) Expand all
465 return entry.function; 494 return entry.function;
466 } 495 }
467 } 496 }
468 return NULL; 497 return NULL;
469 } 498 }
470 499
471 const char* Service::kServiceIsolateName = "vm-service"; 500 const char* Service::kServiceIsolateName = "vm-service";
472 EmbedderServiceHandler* Service::isolate_service_handler_head_ = NULL; 501 EmbedderServiceHandler* Service::isolate_service_handler_head_ = NULL;
473 EmbedderServiceHandler* Service::root_service_handler_head_ = NULL; 502 EmbedderServiceHandler* Service::root_service_handler_head_ = NULL;
474 Isolate* Service::service_isolate_ = NULL; 503 Isolate* Service::service_isolate_ = NULL;
475 Dart_Port Service::port_ = ILLEGAL_PORT; 504 Dart_Port Service::service_port_ = ILLEGAL_PORT;
476 Dart_Port Service::load_port_ = ILLEGAL_PORT; 505 Dart_Port Service::load_port_ = ILLEGAL_PORT;
477 Monitor* Service::monitor_ = NULL; 506 Monitor* Service::monitor_ = NULL;
478 bool Service::initializing_ = true; 507 bool Service::initializing_ = true;
479 uint32_t Service::event_mask_ = 0; 508 uint32_t Service::event_mask_ = 0;
480 509
481 510
482 bool Service::IsServiceIsolateName(const char* name) { 511 bool Service::IsServiceIsolateName(const char* name) {
483 ASSERT(name != NULL); 512 ASSERT(name != NULL);
484 return strcmp(name, kServiceIsolateName) == 0; 513 return strcmp(name, kServiceIsolateName) == 0;
485 } 514 }
(...skipping 14 matching lines...) Expand all
500 const Array& list = Array::Handle( 529 const Array& list = Array::Handle(
501 MakeServiceControlMessage(Dart_GetMainPortId(), 530 MakeServiceControlMessage(Dart_GetMainPortId(),
502 VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID, 531 VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID,
503 name)); 532 name));
504 ASSERT(!list.IsNull()); 533 ASSERT(!list.IsNull());
505 uint8_t* data = NULL; 534 uint8_t* data = NULL;
506 MessageWriter writer(&data, &allocator, false); 535 MessageWriter writer(&data, &allocator, false);
507 writer.WriteMessage(list); 536 writer.WriteMessage(list);
508 intptr_t len = writer.BytesWritten(); 537 intptr_t len = writer.BytesWritten();
509 if (FLAG_trace_service) { 538 if (FLAG_trace_service) {
510 OS::Print("Isolate %s %" Pd64 " registered with service \n", 539 OS::Print("vm-service: Isolate %s %" Pd64 " registered.\n",
511 name.ToCString(), 540 name.ToCString(),
512 Dart_GetMainPortId()); 541 Dart_GetMainPortId());
513 } 542 }
514 return PortMap::PostMessage( 543 return PortMap::PostMessage(
515 new Message(port_, data, len, Message::kNormalPriority)); 544 new Message(service_port_, data, len, Message::kNormalPriority));
516 } 545 }
517 546
518 547
519 bool Service::SendIsolateShutdownMessage() { 548 bool Service::SendIsolateShutdownMessage() {
520 if (!IsRunning()) { 549 if (!IsRunning()) {
521 return false; 550 return false;
522 } 551 }
523 Isolate* isolate = Isolate::Current(); 552 Isolate* isolate = Isolate::Current();
524 if (IsServiceIsolate(isolate)) { 553 if (IsServiceIsolate(isolate)) {
525 return false; 554 return false;
526 } 555 }
527 ASSERT(isolate != NULL); 556 ASSERT(isolate != NULL);
528 HANDLESCOPE(isolate); 557 HANDLESCOPE(isolate);
529 const String& name = String::Handle(String::New(isolate->name())); 558 const String& name = String::Handle(String::New(isolate->name()));
530 ASSERT(!name.IsNull()); 559 ASSERT(!name.IsNull());
531 const Array& list = Array::Handle( 560 const Array& list = Array::Handle(
532 MakeServiceControlMessage(Dart_GetMainPortId(), 561 MakeServiceControlMessage(Dart_GetMainPortId(),
533 VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID, 562 VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID,
534 name)); 563 name));
535 ASSERT(!list.IsNull()); 564 ASSERT(!list.IsNull());
536 uint8_t* data = NULL; 565 uint8_t* data = NULL;
537 MessageWriter writer(&data, &allocator, false); 566 MessageWriter writer(&data, &allocator, false);
538 writer.WriteMessage(list); 567 writer.WriteMessage(list);
539 intptr_t len = writer.BytesWritten(); 568 intptr_t len = writer.BytesWritten();
540 if (FLAG_trace_service) { 569 if (FLAG_trace_service) {
541 OS::Print("Isolate %s %" Pd64 " deregistered with service \n", 570 OS::Print("vm-service: Isolate %s %" Pd64 " deregistered.\n",
542 name.ToCString(), 571 name.ToCString(),
543 Dart_GetMainPortId()); 572 Dart_GetMainPortId());
544 } 573 }
545 return PortMap::PostMessage( 574 return PortMap::PostMessage(
546 new Message(port_, data, len, Message::kNormalPriority)); 575 new Message(service_port_, data, len, Message::kNormalPriority));
547 } 576 }
548 577
549 578
550 Dart_Handle Service::GetSource(const char* name) { 579 Dart_Handle Service::GetSource(const char* name) {
551 ASSERT(name != NULL); 580 ASSERT(name != NULL);
552 int i = 0; 581 int i = 0;
553 while (true) { 582 while (true) {
554 const char* path = Resources::Path(i); 583 const char* path = Resources::Path(i);
555 if (path == NULL) { 584 if (path == NULL) {
556 break; 585 break;
(...skipping 30 matching lines...) Expand all
587 } 616 }
588 Dart_Handle source = GetSource(url_string); 617 Dart_Handle source = GetSource(url_string);
589 if (Dart_IsError(source)) { 618 if (Dart_IsError(source)) {
590 return source; 619 return source;
591 } 620 }
592 return Dart_LoadSource(library, url, source, 0, 0); 621 return Dart_LoadSource(library, url, source, 0, 0);
593 } 622 }
594 623
595 624
596 void Service::MaybeInjectVMServiceLibrary(Isolate* isolate) { 625 void Service::MaybeInjectVMServiceLibrary(Isolate* isolate) {
597 if (service_isolate_ != NULL) { 626 ASSERT(isolate != NULL);
598 // Service isolate already exists. 627 ASSERT(isolate->name() != NULL);
599 return;
600 }
601 if (!Service::IsServiceIsolateName(isolate->name())) { 628 if (!Service::IsServiceIsolateName(isolate->name())) {
602 // Not service isolate. 629 // Not service isolate.
603 return; 630 return;
604 } 631 }
605 service_isolate_ = isolate; 632 if (HasServiceIsolate()) {
606 ASSERT(isolate != NULL); 633 // Service isolate already exists.
634 return;
635 }
636 SetServiceIsolate(isolate);
607 637
608 StackZone zone(isolate); 638 StackZone zone(isolate);
609 HANDLESCOPE(isolate); 639 HANDLESCOPE(isolate);
610 640
611 // Register dart:vmservice library. 641 // Register dart:vmservice library.
612 const String& url_str = String::Handle(Symbols::DartVMService().raw()); 642 const String& url_str = String::Handle(Symbols::DartVMService().raw());
613 const Library& library = Library::Handle(Library::New(url_str)); 643 const Library& library = Library::Handle(Library::New(url_str));
614 library.Register(); 644 library.Register();
615 library.set_native_entry_resolver(VmServiceNativeResolver); 645 library.set_native_entry_resolver(VmServiceNativeResolver);
616 646
(...skipping 29 matching lines...) Expand all
646 676
647 void Service::FinishedInitializing() { 677 void Service::FinishedInitializing() {
648 MonitorLocker ml(monitor_); 678 MonitorLocker ml(monitor_);
649 initializing_ = false; 679 initializing_ = false;
650 ml.NotifyAll(); 680 ml.NotifyAll();
651 } 681 }
652 682
653 683
654 static void ShutdownIsolate(uword parameter) { 684 static void ShutdownIsolate(uword parameter) {
655 Isolate* isolate = reinterpret_cast<Isolate*>(parameter); 685 Isolate* isolate = reinterpret_cast<Isolate*>(parameter);
686 ASSERT(Service::IsServiceIsolate(isolate));
656 { 687 {
657 // Print the error if there is one. This may execute dart code to 688 // Print the error if there is one. This may execute dart code to
658 // print the exception object, so we need to use a StartIsolateScope. 689 // print the exception object, so we need to use a StartIsolateScope.
659 StartIsolateScope start_scope(isolate); 690 StartIsolateScope start_scope(isolate);
660 StackZone zone(isolate); 691 StackZone zone(isolate);
661 HandleScope handle_scope(isolate); 692 HandleScope handle_scope(isolate);
662 Error& error = Error::Handle(); 693 Error& error = Error::Handle();
663 error = isolate->object_store()->sticky_error(); 694 error = isolate->object_store()->sticky_error();
664 if (!error.IsNull()) { 695 if (!error.IsNull()) {
665 OS::PrintErr("Service shutting down: %s\n", error.ToErrorCString()); 696 OS::PrintErr("vm-service: Error: %s\n", error.ToErrorCString());
666 } 697 }
667 Dart::RunShutdownCallback(); 698 Dart::RunShutdownCallback();
668 } 699 }
669 { 700 {
670 // Shut the isolate down. 701 // Shut the isolate down.
671 SwitchIsolateScope switch_scope(isolate); 702 SwitchIsolateScope switch_scope(isolate);
672 Dart::ShutdownIsolate(); 703 Dart::ShutdownIsolate();
673 } 704 }
705 Service::SetServiceIsolate(NULL);
706 Service::SetServicePort(ILLEGAL_PORT);
707 if (FLAG_trace_service) {
708 OS::Print("vm-service: Shutdown.\n");
709 }
674 } 710 }
675 711
676 712
677 class RunServiceTask : public ThreadPool::Task { 713 class RunServiceTask : public ThreadPool::Task {
678 public: 714 public:
679 virtual void Run() { 715 virtual void Run() {
680 ASSERT(Isolate::Current() == NULL); 716 ASSERT(Isolate::Current() == NULL);
681 char* error = NULL; 717 char* error = NULL;
682 Isolate* isolate = NULL; 718 Isolate* isolate = NULL;
683 719
684 Dart_IsolateCreateCallback create_callback = Isolate::CreateCallback(); 720 Dart_IsolateCreateCallback create_callback = Isolate::CreateCallback();
685 if (create_callback == NULL) { 721 if (create_callback == NULL) {
686 Service::FinishedInitializing(); 722 Service::FinishedInitializing();
687 return; 723 return;
688 } 724 }
689 725
690 isolate = 726 isolate =
691 reinterpret_cast<Isolate*>(create_callback(Service::kServiceIsolateName, 727 reinterpret_cast<Isolate*>(create_callback(Service::kServiceIsolateName,
692 NULL, 728 NULL,
693 NULL, 729 NULL,
694 NULL, 730 NULL,
695 &error)); 731 &error));
696 Isolate::SetCurrent(NULL); 732 Isolate::SetCurrent(NULL);
697 733
698 if (isolate == NULL) { 734 if (isolate == NULL) {
699 OS::PrintErr("Service startup error: %s\n", error); 735 OS::PrintErr("vm-service: Isolate creation error: %s\n", error);
700 Service::FinishedInitializing(); 736 Service::FinishedInitializing();
701 return; 737 return;
702 } 738 }
703 739
704 RunMain(isolate); 740 RunMain(isolate);
705 741
706 Service::FinishedInitializing(); 742 Service::FinishedInitializing();
707 743
708 isolate->message_handler()->Run(Dart::thread_pool(), 744 isolate->message_handler()->Run(Dart::thread_pool(),
709 NULL, 745 NULL,
710 ShutdownIsolate, 746 ShutdownIsolate,
711 reinterpret_cast<uword>(isolate)); 747 reinterpret_cast<uword>(isolate));
712 } 748 }
713 749
714 protected: 750 protected:
715 void RunMain(Isolate* isolate) { 751 void RunMain(Isolate* isolate) {
716 StartIsolateScope iso_scope(isolate); 752 StartIsolateScope iso_scope(isolate);
717 StackZone zone(isolate); 753 StackZone zone(isolate);
718 HANDLESCOPE(isolate); 754 HANDLESCOPE(isolate);
719 // Invoke main which will return the loadScriptPort. 755 // Invoke main which will return the loadScriptPort.
720 const Library& root_library = 756 const Library& root_library =
721 Library::Handle(isolate, isolate->object_store()->root_library()); 757 Library::Handle(isolate, isolate->object_store()->root_library());
722 if (root_library.IsNull()) { 758 if (root_library.IsNull()) {
759 if (FLAG_trace_service) {
760 OS::Print("vm-service: Embedder did not install a script.");
761 }
723 // Service isolate is not supported by embedder. 762 // Service isolate is not supported by embedder.
724 return; 763 return;
725 } 764 }
726 ASSERT(!root_library.IsNull()); 765 ASSERT(!root_library.IsNull());
727 const String& entry_name = String::Handle(isolate, String::New("main")); 766 const String& entry_name = String::Handle(isolate, String::New("main"));
728 ASSERT(!entry_name.IsNull()); 767 ASSERT(!entry_name.IsNull());
729 const Function& entry = 768 const Function& entry =
730 Function::Handle(isolate, 769 Function::Handle(isolate,
731 root_library.LookupFunctionAllowPrivate(entry_name)); 770 root_library.LookupFunctionAllowPrivate(entry_name));
732 if (entry.IsNull()) { 771 if (entry.IsNull()) {
733 // Service isolate is not supported by embedder. 772 // Service isolate is not supported by embedder.
773 if (FLAG_trace_service) {
774 OS::Print("vm-service: Embedder did not provide a main function.");
775 }
734 return; 776 return;
735 } 777 }
736 ASSERT(!entry.IsNull()); 778 ASSERT(!entry.IsNull());
737 const Object& result = 779 const Object& result =
738 Object::Handle(isolate, 780 Object::Handle(isolate,
739 DartEntry::InvokeFunction(entry, 781 DartEntry::InvokeFunction(entry,
740 Object::empty_array())); 782 Object::empty_array()));
741 ASSERT(!result.IsNull()); 783 ASSERT(!result.IsNull());
742 if (result.IsError()) { 784 if (result.IsError()) {
743 // Service isolate did not initialize properly. 785 // Service isolate did not initialize properly.
786 if (FLAG_trace_service) {
787 const Error& error = Error::Cast(result);
788 OS::Print("vm-service: Calling main resulted in an error: %s",
789 error.ToErrorCString());
790 }
744 return; 791 return;
745 } 792 }
746 ASSERT(result.IsReceivePort()); 793 ASSERT(result.IsReceivePort());
747 const ReceivePort& rp = ReceivePort::Cast(result); 794 const ReceivePort& rp = ReceivePort::Cast(result);
748 Service::SetLoadPort(rp.Id()); 795 Service::SetLoadPort(rp.Id());
749 } 796 }
750 }; 797 };
751 798
752 799
753 void Service::RunService() { 800 void Service::RunService() {
(...skipping 1824 matching lines...) Expand 10 before | Expand all | Expand 10 after
2578 static IsolateMessageHandler FindIsolateMessageHandler(const char* command) { 2625 static IsolateMessageHandler FindIsolateMessageHandler(const char* command) {
2579 intptr_t num_message_handlers = sizeof(isolate_handlers) / 2626 intptr_t num_message_handlers = sizeof(isolate_handlers) /
2580 sizeof(isolate_handlers[0]); 2627 sizeof(isolate_handlers[0]);
2581 for (intptr_t i = 0; i < num_message_handlers; i++) { 2628 for (intptr_t i = 0; i < num_message_handlers; i++) {
2582 const IsolateMessageHandlerEntry& entry = isolate_handlers[i]; 2629 const IsolateMessageHandlerEntry& entry = isolate_handlers[i];
2583 if (strcmp(command, entry.command) == 0) { 2630 if (strcmp(command, entry.command) == 0) {
2584 return entry.handler; 2631 return entry.handler;
2585 } 2632 }
2586 } 2633 }
2587 if (FLAG_trace_service) { 2634 if (FLAG_trace_service) {
2588 OS::Print("Service has no isolate message handler for <%s>\n", command); 2635 OS::Print("vm-service: No isolate message handler for <%s>.\n", command);
2589 } 2636 }
2590 return NULL; 2637 return NULL;
2591 } 2638 }
2592 2639
2593 2640
2594 void Service::HandleRootMessage(const Instance& msg) { 2641 void Service::HandleRootMessage(const Instance& msg) {
2595 Isolate* isolate = Isolate::Current(); 2642 Isolate* isolate = Isolate::Current();
2596 ASSERT(!msg.IsNull()); 2643 ASSERT(!msg.IsNull());
2597 ASSERT(msg.IsArray()); 2644 ASSERT(msg.IsArray());
2598 2645
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
2767 static RootMessageHandler FindRootMessageHandler(const char* command) { 2814 static RootMessageHandler FindRootMessageHandler(const char* command) {
2768 intptr_t num_message_handlers = sizeof(root_handlers) / 2815 intptr_t num_message_handlers = sizeof(root_handlers) /
2769 sizeof(root_handlers[0]); 2816 sizeof(root_handlers[0]);
2770 for (intptr_t i = 0; i < num_message_handlers; i++) { 2817 for (intptr_t i = 0; i < num_message_handlers; i++) {
2771 const RootMessageHandlerEntry& entry = root_handlers[i]; 2818 const RootMessageHandlerEntry& entry = root_handlers[i];
2772 if (strcmp(command, entry.command) == 0) { 2819 if (strcmp(command, entry.command) == 0) {
2773 return entry.handler; 2820 return entry.handler;
2774 } 2821 }
2775 } 2822 }
2776 if (FLAG_trace_service) { 2823 if (FLAG_trace_service) {
2777 OS::Print("Service has no root message handler for <%s>\n", command); 2824 OS::Print("vm-service: No root message handler for <%s>.\n", command);
2778 } 2825 }
2779 return NULL; 2826 return NULL;
2780 } 2827 }
2781 2828
2782 2829
2783 void Service::SendEvent(intptr_t eventId, const Object& eventMessage) { 2830 void Service::SendEvent(intptr_t eventId, const Object& eventMessage) {
2784 if (!IsRunning()) { 2831 if (!IsRunning()) {
2785 return; 2832 return;
2786 } 2833 }
2787 Isolate* isolate = Isolate::Current(); 2834 Isolate* isolate = Isolate::Current();
2788 ASSERT(isolate != NULL); 2835 ASSERT(isolate != NULL);
2789 HANDLESCOPE(isolate); 2836 HANDLESCOPE(isolate);
2790 2837
2791 // Construct a list of the form [eventId, eventMessage]. 2838 // Construct a list of the form [eventId, eventMessage].
2792 const Array& list = Array::Handle(Array::New(2)); 2839 const Array& list = Array::Handle(Array::New(2));
2793 ASSERT(!list.IsNull()); 2840 ASSERT(!list.IsNull());
2794 list.SetAt(0, Integer::Handle(Integer::New(eventId))); 2841 list.SetAt(0, Integer::Handle(Integer::New(eventId)));
2795 list.SetAt(1, eventMessage); 2842 list.SetAt(1, eventMessage);
2796 2843
2797 // Push the event to port_. 2844 // Push the event to port_.
2798 uint8_t* data = NULL; 2845 uint8_t* data = NULL;
2799 MessageWriter writer(&data, &allocator, false); 2846 MessageWriter writer(&data, &allocator, false);
2800 writer.WriteMessage(list); 2847 writer.WriteMessage(list);
2801 intptr_t len = writer.BytesWritten(); 2848 intptr_t len = writer.BytesWritten();
2802 if (FLAG_trace_service) { 2849 if (FLAG_trace_service) {
2803 OS::Print("Pushing event of type %" Pd ", len %" Pd "\n", eventId, len); 2850 OS::Print("vm-service: Pushing event of type %" Pd ", len %" Pd "\n",
2851 eventId, len);
2804 } 2852 }
2805 // TODO(turnidge): For now we ignore failure to send an event. Revisit? 2853 // TODO(turnidge): For now we ignore failure to send an event. Revisit?
2806 PortMap::PostMessage( 2854 PortMap::PostMessage(
2807 new Message(port_, data, len, Message::kNormalPriority)); 2855 new Message(service_port_, data, len, Message::kNormalPriority));
2808 } 2856 }
2809 2857
2810 2858
2811 void Service::SendEvent(intptr_t eventId, 2859 void Service::SendEvent(intptr_t eventId,
2812 const String& meta, 2860 const String& meta,
2813 const uint8_t* data, 2861 const uint8_t* data,
2814 intptr_t size) { 2862 intptr_t size) {
2815 // Bitstream: [meta data size (big-endian 64 bit)] [meta data (UTF-8)] [data] 2863 // Bitstream: [meta data size (big-endian 64 bit)] [meta data (UTF-8)] [data]
2816 const intptr_t meta_bytes = Utf8::Length(meta); 2864 const intptr_t meta_bytes = Utf8::Length(meta);
2817 const intptr_t total_bytes = sizeof(uint64_t) + meta_bytes + size; 2865 const intptr_t total_bytes = sizeof(uint64_t) + meta_bytes + size;
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
2943 while (current != NULL) { 2991 while (current != NULL) {
2944 if (strcmp(name, current->name()) == 0) { 2992 if (strcmp(name, current->name()) == 0) {
2945 return current; 2993 return current;
2946 } 2994 }
2947 current = current->next(); 2995 current = current->next();
2948 } 2996 }
2949 return NULL; 2997 return NULL;
2950 } 2998 }
2951 2999
2952 } // namespace dart 3000 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698