| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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_isolate.h" | 5 #include "vm/service_isolate.h" |
| 6 | 6 |
| 7 #include "vm/compiler.h" | 7 #include "vm/compiler.h" |
| 8 #include "vm/dart_api_impl.h" | 8 #include "vm/dart_api_impl.h" |
| 9 #include "vm/dart_entry.h" | 9 #include "vm/dart_entry.h" |
| 10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 const String& name = String::Handle(String::New(isolate->name())); | 192 const String& name = String::Handle(String::New(isolate->name())); |
| 193 ASSERT(!name.IsNull()); | 193 ASSERT(!name.IsNull()); |
| 194 const Array& list = Array::Handle(MakeServiceControlMessage( | 194 const Array& list = Array::Handle(MakeServiceControlMessage( |
| 195 Dart_GetMainPortId(), VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID, name)); | 195 Dart_GetMainPortId(), VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID, name)); |
| 196 ASSERT(!list.IsNull()); | 196 ASSERT(!list.IsNull()); |
| 197 uint8_t* data = NULL; | 197 uint8_t* data = NULL; |
| 198 MessageWriter writer(&data, &malloc_allocator, &malloc_deallocator, false); | 198 MessageWriter writer(&data, &malloc_allocator, &malloc_deallocator, false); |
| 199 writer.WriteMessage(list); | 199 writer.WriteMessage(list); |
| 200 intptr_t len = writer.BytesWritten(); | 200 intptr_t len = writer.BytesWritten(); |
| 201 if (FLAG_trace_service) { | 201 if (FLAG_trace_service) { |
| 202 OS::Print("vm-service: Isolate %s %" Pd64 " registered.\n", | 202 OS::PrintErr("vm-service: Isolate %s %" Pd64 " registered.\n", |
| 203 name.ToCString(), Dart_GetMainPortId()); | 203 name.ToCString(), Dart_GetMainPortId()); |
| 204 } | 204 } |
| 205 return PortMap::PostMessage( | 205 return PortMap::PostMessage( |
| 206 new Message(port_, data, len, Message::kNormalPriority)); | 206 new Message(port_, data, len, Message::kNormalPriority)); |
| 207 } | 207 } |
| 208 | 208 |
| 209 bool ServiceIsolate::SendIsolateShutdownMessage() { | 209 bool ServiceIsolate::SendIsolateShutdownMessage() { |
| 210 if (!IsRunning()) { | 210 if (!IsRunning()) { |
| 211 return false; | 211 return false; |
| 212 } | 212 } |
| 213 Thread* thread = Thread::Current(); | 213 Thread* thread = Thread::Current(); |
| 214 Isolate* isolate = thread->isolate(); | 214 Isolate* isolate = thread->isolate(); |
| 215 if (IsServiceIsolateDescendant(isolate)) { | 215 if (IsServiceIsolateDescendant(isolate)) { |
| 216 return false; | 216 return false; |
| 217 } | 217 } |
| 218 ASSERT(isolate != NULL); | 218 ASSERT(isolate != NULL); |
| 219 HANDLESCOPE(thread); | 219 HANDLESCOPE(thread); |
| 220 const String& name = String::Handle(String::New(isolate->name())); | 220 const String& name = String::Handle(String::New(isolate->name())); |
| 221 ASSERT(!name.IsNull()); | 221 ASSERT(!name.IsNull()); |
| 222 const Array& list = Array::Handle(MakeServiceControlMessage( | 222 const Array& list = Array::Handle(MakeServiceControlMessage( |
| 223 Dart_GetMainPortId(), VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID, name)); | 223 Dart_GetMainPortId(), VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID, name)); |
| 224 ASSERT(!list.IsNull()); | 224 ASSERT(!list.IsNull()); |
| 225 uint8_t* data = NULL; | 225 uint8_t* data = NULL; |
| 226 MessageWriter writer(&data, &malloc_allocator, &malloc_deallocator, false); | 226 MessageWriter writer(&data, &malloc_allocator, &malloc_deallocator, false); |
| 227 writer.WriteMessage(list); | 227 writer.WriteMessage(list); |
| 228 intptr_t len = writer.BytesWritten(); | 228 intptr_t len = writer.BytesWritten(); |
| 229 if (FLAG_trace_service) { | 229 if (FLAG_trace_service) { |
| 230 OS::Print("vm-service: Isolate %s %" Pd64 " deregistered.\n", | 230 OS::PrintErr("vm-service: Isolate %s %" Pd64 " deregistered.\n", |
| 231 name.ToCString(), Dart_GetMainPortId()); | 231 name.ToCString(), Dart_GetMainPortId()); |
| 232 } | 232 } |
| 233 return PortMap::PostMessage( | 233 return PortMap::PostMessage( |
| 234 new Message(port_, data, len, Message::kNormalPriority)); | 234 new Message(port_, data, len, Message::kNormalPriority)); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void ServiceIsolate::SendServiceExitMessage() { | 237 void ServiceIsolate::SendServiceExitMessage() { |
| 238 if (!IsRunning()) { | 238 if (!IsRunning()) { |
| 239 return; | 239 return; |
| 240 } | 240 } |
| 241 if ((exit_message_ == NULL) || (exit_message_length_ == 0)) { | 241 if ((exit_message_ == NULL) || (exit_message_length_ == 0)) { |
| 242 return; | 242 return; |
| 243 } | 243 } |
| 244 if (FLAG_trace_service) { | 244 if (FLAG_trace_service) { |
| 245 OS::Print("vm-service: sending service exit message.\n"); | 245 OS::PrintErr("vm-service: sending service exit message.\n"); |
| 246 } | 246 } |
| 247 PortMap::PostMessage(new Message(port_, exit_message_, exit_message_length_, | 247 PortMap::PostMessage(new Message(port_, exit_message_, exit_message_length_, |
| 248 Message::kNormalPriority)); | 248 Message::kNormalPriority)); |
| 249 } | 249 } |
| 250 | 250 |
| 251 void ServiceIsolate::SetServicePort(Dart_Port port) { | 251 void ServiceIsolate::SetServicePort(Dart_Port port) { |
| 252 MonitorLocker ml(monitor_); | 252 MonitorLocker ml(monitor_); |
| 253 port_ = port; | 253 port_ = port; |
| 254 } | 254 } |
| 255 | 255 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 | 359 |
| 360 ServiceIsolate::FinishedInitializing(); | 360 ServiceIsolate::FinishedInitializing(); |
| 361 | 361 |
| 362 isolate->message_handler()->Run(Dart::thread_pool(), NULL, ShutdownIsolate, | 362 isolate->message_handler()->Run(Dart::thread_pool(), NULL, ShutdownIsolate, |
| 363 reinterpret_cast<uword>(isolate)); | 363 reinterpret_cast<uword>(isolate)); |
| 364 } | 364 } |
| 365 | 365 |
| 366 protected: | 366 protected: |
| 367 static void ShutdownIsolate(uword parameter) { | 367 static void ShutdownIsolate(uword parameter) { |
| 368 if (FLAG_trace_service) { | 368 if (FLAG_trace_service) { |
| 369 OS::Print("vm-service: ShutdownIsolate\n"); | 369 OS::PrintErr("vm-service: ShutdownIsolate\n"); |
| 370 } | 370 } |
| 371 Isolate* I = reinterpret_cast<Isolate*>(parameter); | 371 Isolate* I = reinterpret_cast<Isolate*>(parameter); |
| 372 ASSERT(ServiceIsolate::IsServiceIsolate(I)); | 372 ASSERT(ServiceIsolate::IsServiceIsolate(I)); |
| 373 ServiceIsolate::SetServiceIsolate(NULL); | 373 ServiceIsolate::SetServiceIsolate(NULL); |
| 374 ServiceIsolate::SetServicePort(ILLEGAL_PORT); | 374 ServiceIsolate::SetServicePort(ILLEGAL_PORT); |
| 375 I->WaitForOutstandingSpawns(); | 375 I->WaitForOutstandingSpawns(); |
| 376 { | 376 { |
| 377 // Print the error if there is one. This may execute dart code to | 377 // Print the error if there is one. This may execute dart code to |
| 378 // print the exception object, so we need to use a StartIsolateScope. | 378 // print the exception object, so we need to use a StartIsolateScope. |
| 379 ASSERT(Isolate::Current() == NULL); | 379 ASSERT(Isolate::Current() == NULL); |
| 380 StartIsolateScope start_scope(I); | 380 StartIsolateScope start_scope(I); |
| 381 Thread* T = Thread::Current(); | 381 Thread* T = Thread::Current(); |
| 382 ASSERT(I == T->isolate()); | 382 ASSERT(I == T->isolate()); |
| 383 StackZone zone(T); | 383 StackZone zone(T); |
| 384 HandleScope handle_scope(T); | 384 HandleScope handle_scope(T); |
| 385 Error& error = Error::Handle(Z); | 385 Error& error = Error::Handle(Z); |
| 386 error = T->sticky_error(); | 386 error = T->sticky_error(); |
| 387 if (!error.IsNull() && !error.IsUnwindError()) { | 387 if (!error.IsNull() && !error.IsUnwindError()) { |
| 388 OS::PrintErr("vm-service: Error: %s\n", error.ToErrorCString()); | 388 OS::PrintErr("vm-service: Error: %s\n", error.ToErrorCString()); |
| 389 } | 389 } |
| 390 error = I->sticky_error(); | 390 error = I->sticky_error(); |
| 391 if (!error.IsNull() && !error.IsUnwindError()) { | 391 if (!error.IsNull() && !error.IsUnwindError()) { |
| 392 OS::PrintErr("vm-service: Error: %s\n", error.ToErrorCString()); | 392 OS::PrintErr("vm-service: Error: %s\n", error.ToErrorCString()); |
| 393 } | 393 } |
| 394 Dart::RunShutdownCallback(); | 394 Dart::RunShutdownCallback(); |
| 395 } | 395 } |
| 396 // Shut the isolate down. | 396 // Shut the isolate down. |
| 397 Dart::ShutdownIsolate(I); | 397 Dart::ShutdownIsolate(I); |
| 398 if (FLAG_trace_service) { | 398 if (FLAG_trace_service) { |
| 399 OS::Print("vm-service: Shutdown.\n"); | 399 OS::PrintErr("vm-service: Shutdown.\n"); |
| 400 } | 400 } |
| 401 ServiceIsolate::FinishedExiting(); | 401 ServiceIsolate::FinishedExiting(); |
| 402 } | 402 } |
| 403 | 403 |
| 404 bool RunMain(Isolate* I) { | 404 bool RunMain(Isolate* I) { |
| 405 Thread* T = Thread::Current(); | 405 Thread* T = Thread::Current(); |
| 406 ASSERT(I == T->isolate()); | 406 ASSERT(I == T->isolate()); |
| 407 StackZone zone(T); | 407 StackZone zone(T); |
| 408 HANDLESCOPE(T); | 408 HANDLESCOPE(T); |
| 409 // Invoke main which will return the loadScriptPort. | 409 // Invoke main which will return the loadScriptPort. |
| 410 const Library& root_library = | 410 const Library& root_library = |
| 411 Library::Handle(Z, I->object_store()->root_library()); | 411 Library::Handle(Z, I->object_store()->root_library()); |
| 412 if (root_library.IsNull()) { | 412 if (root_library.IsNull()) { |
| 413 if (FLAG_trace_service) { | 413 if (FLAG_trace_service) { |
| 414 OS::Print("vm-service: Embedder did not install a script."); | 414 OS::PrintErr("vm-service: Embedder did not install a script."); |
| 415 } | 415 } |
| 416 // Service isolate is not supported by embedder. | 416 // Service isolate is not supported by embedder. |
| 417 return false; | 417 return false; |
| 418 } | 418 } |
| 419 ASSERT(!root_library.IsNull()); | 419 ASSERT(!root_library.IsNull()); |
| 420 const String& entry_name = String::Handle(Z, String::New("main")); | 420 const String& entry_name = String::Handle(Z, String::New("main")); |
| 421 ASSERT(!entry_name.IsNull()); | 421 ASSERT(!entry_name.IsNull()); |
| 422 const Function& entry = Function::Handle( | 422 const Function& entry = Function::Handle( |
| 423 Z, root_library.LookupFunctionAllowPrivate(entry_name)); | 423 Z, root_library.LookupFunctionAllowPrivate(entry_name)); |
| 424 if (entry.IsNull()) { | 424 if (entry.IsNull()) { |
| 425 // Service isolate is not supported by embedder. | 425 // Service isolate is not supported by embedder. |
| 426 if (FLAG_trace_service) { | 426 if (FLAG_trace_service) { |
| 427 OS::Print("vm-service: Embedder did not provide a main function."); | 427 OS::PrintErr("vm-service: Embedder did not provide a main function."); |
| 428 } | 428 } |
| 429 return false; | 429 return false; |
| 430 } | 430 } |
| 431 ASSERT(!entry.IsNull()); | 431 ASSERT(!entry.IsNull()); |
| 432 const Object& result = Object::Handle( | 432 const Object& result = Object::Handle( |
| 433 Z, DartEntry::InvokeFunction(entry, Object::empty_array())); | 433 Z, DartEntry::InvokeFunction(entry, Object::empty_array())); |
| 434 ASSERT(!result.IsNull()); | 434 ASSERT(!result.IsNull()); |
| 435 if (result.IsError()) { | 435 if (result.IsError()) { |
| 436 // Service isolate did not initialize properly. | 436 // Service isolate did not initialize properly. |
| 437 if (FLAG_trace_service) { | 437 if (FLAG_trace_service) { |
| 438 const Error& error = Error::Cast(result); | 438 const Error& error = Error::Cast(result); |
| 439 OS::Print("vm-service: Calling main resulted in an error: %s", | 439 OS::PrintErr("vm-service: Calling main resulted in an error: %s", |
| 440 error.ToErrorCString()); | 440 error.ToErrorCString()); |
| 441 } | 441 } |
| 442 if (result.IsUnwindError()) { | 442 if (result.IsUnwindError()) { |
| 443 return true; | 443 return true; |
| 444 } | 444 } |
| 445 return false; | 445 return false; |
| 446 } | 446 } |
| 447 ASSERT(result.IsReceivePort()); | 447 ASSERT(result.IsReceivePort()); |
| 448 const ReceivePort& rp = ReceivePort::Cast(result); | 448 const ReceivePort& rp = ReceivePort::Cast(result); |
| 449 ServiceIsolate::SetLoadPort(rp.Id()); | 449 ServiceIsolate::SetLoadPort(rp.Id()); |
| 450 return false; | 450 return false; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 if (result.IsReceivePort()) { | 523 if (result.IsReceivePort()) { |
| 524 port = ReceivePort::Cast(result).Id(); | 524 port = ReceivePort::Cast(result).Id(); |
| 525 } | 525 } |
| 526 ASSERT(port != ILLEGAL_PORT); | 526 ASSERT(port != ILLEGAL_PORT); |
| 527 ServiceIsolate::SetServicePort(port); | 527 ServiceIsolate::SetServicePort(port); |
| 528 } | 528 } |
| 529 | 529 |
| 530 void ServiceIsolate::VisitObjectPointers(ObjectPointerVisitor* visitor) {} | 530 void ServiceIsolate::VisitObjectPointers(ObjectPointerVisitor* visitor) {} |
| 531 | 531 |
| 532 } // namespace dart | 532 } // namespace dart |
| OLD | NEW |