2015-02-12 19:10:55 +00:00
|
|
|
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "vm/service_isolate.h"
|
|
|
|
|
2017-09-04 14:18:37 +00:00
|
|
|
#include "vm/compiler/jit/compiler.h"
|
2015-02-12 19:10:55 +00:00
|
|
|
#include "vm/dart_api_impl.h"
|
2018-02-14 00:28:01 +00:00
|
|
|
#include "vm/dart_api_message.h"
|
2015-02-12 19:10:55 +00:00
|
|
|
#include "vm/dart_entry.h"
|
|
|
|
#include "vm/isolate.h"
|
|
|
|
#include "vm/lockers.h"
|
|
|
|
#include "vm/message.h"
|
|
|
|
#include "vm/message_handler.h"
|
|
|
|
#include "vm/native_arguments.h"
|
2017-07-13 15:08:33 +00:00
|
|
|
#include "vm/native_entry.h"
|
2015-02-12 19:10:55 +00:00
|
|
|
#include "vm/object.h"
|
|
|
|
#include "vm/object_store.h"
|
|
|
|
#include "vm/port.h"
|
|
|
|
#include "vm/service.h"
|
|
|
|
#include "vm/symbols.h"
|
|
|
|
#include "vm/thread_pool.h"
|
2016-03-16 19:18:46 +00:00
|
|
|
#include "vm/timeline.h"
|
2015-02-12 19:10:55 +00:00
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
2015-09-02 23:07:48 +00:00
|
|
|
#define Z (T->zone())
|
|
|
|
|
2015-02-12 19:10:55 +00:00
|
|
|
DEFINE_FLAG(bool, trace_service, false, "Trace VM service requests.");
|
2016-11-08 21:54:47 +00:00
|
|
|
DEFINE_FLAG(bool,
|
|
|
|
trace_service_pause_events,
|
|
|
|
false,
|
2015-02-12 19:10:55 +00:00
|
|
|
"Trace VM service isolate pause events.");
|
2016-11-08 21:54:47 +00:00
|
|
|
DEFINE_FLAG(bool,
|
|
|
|
trace_service_verbose,
|
|
|
|
false,
|
2016-02-12 21:26:16 +00:00
|
|
|
"Provide extra service tracing information.");
|
2015-02-12 19:10:55 +00:00
|
|
|
|
|
|
|
// These must be kept in sync with service/constants.dart
|
2015-03-25 15:50:19 +00:00
|
|
|
#define VM_SERVICE_ISOLATE_EXIT_MESSAGE_ID 0
|
2015-02-12 19:10:55 +00:00
|
|
|
#define VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID 1
|
|
|
|
#define VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID 2
|
|
|
|
|
2016-10-31 19:32:23 +00:00
|
|
|
#define VM_SERVICE_WEB_SERVER_CONTROL_MESSAGE_ID 3
|
|
|
|
#define VM_SERVICE_SERVER_INFO_MESSAGE_ID 4
|
|
|
|
|
2018-07-25 12:06:55 +00:00
|
|
|
#define VM_SERVICE_METHOD_CALL_FROM_NATIVE 5
|
|
|
|
|
2016-11-08 21:54:47 +00:00
|
|
|
static RawArray* MakeServiceControlMessage(Dart_Port port_id,
|
|
|
|
intptr_t code,
|
2015-02-12 19:10:55 +00:00
|
|
|
const String& name) {
|
|
|
|
const Array& list = Array::Handle(Array::New(4));
|
|
|
|
ASSERT(!list.IsNull());
|
|
|
|
const Integer& code_int = Integer::Handle(Integer::New(code));
|
|
|
|
const Integer& port_int = Integer::Handle(Integer::New(port_id));
|
|
|
|
const SendPort& send_port = SendPort::Handle(SendPort::New(port_id));
|
|
|
|
list.SetAt(0, code_int);
|
|
|
|
list.SetAt(1, port_int);
|
|
|
|
list.SetAt(2, send_port);
|
|
|
|
list.SetAt(3, name);
|
|
|
|
return list.raw();
|
|
|
|
}
|
|
|
|
|
2016-10-31 19:32:23 +00:00
|
|
|
static RawArray* MakeServerControlMessage(const SendPort& sp,
|
|
|
|
intptr_t code,
|
|
|
|
bool enable = false) {
|
|
|
|
const Array& list = Array::Handle(Array::New(3));
|
|
|
|
ASSERT(!list.IsNull());
|
|
|
|
list.SetAt(0, Integer::Handle(Integer::New(code)));
|
|
|
|
list.SetAt(1, sp);
|
|
|
|
list.SetAt(2, Bool::Get(enable));
|
|
|
|
return list.raw();
|
|
|
|
}
|
|
|
|
|
2019-04-10 22:55:20 +00:00
|
|
|
const char* ServiceIsolate::kName = DART_VM_SERVICE_ISOLATE_NAME;
|
2019-07-08 23:49:05 +00:00
|
|
|
Dart_IsolateGroupCreateCallback ServiceIsolate::create_group_callback_ = NULL;
|
2019-04-10 22:55:20 +00:00
|
|
|
Monitor* ServiceIsolate::monitor_ = new Monitor();
|
|
|
|
ServiceIsolate::State ServiceIsolate::state_ = ServiceIsolate::kStopped;
|
2015-02-12 19:10:55 +00:00
|
|
|
Isolate* ServiceIsolate::isolate_ = NULL;
|
2015-05-19 18:41:42 +00:00
|
|
|
Dart_Port ServiceIsolate::port_ = ILLEGAL_PORT;
|
|
|
|
Dart_Port ServiceIsolate::load_port_ = ILLEGAL_PORT;
|
|
|
|
Dart_Port ServiceIsolate::origin_ = ILLEGAL_PORT;
|
2016-01-29 15:43:21 +00:00
|
|
|
char* ServiceIsolate::server_address_ = NULL;
|
2019-08-28 13:18:52 +00:00
|
|
|
char* ServiceIsolate::startup_failure_reason_ = nullptr;
|
2016-01-29 15:43:21 +00:00
|
|
|
|
2016-10-31 19:32:23 +00:00
|
|
|
void ServiceIsolate::RequestServerInfo(const SendPort& sp) {
|
2016-11-08 21:54:47 +00:00
|
|
|
const Array& message = Array::Handle(MakeServerControlMessage(
|
|
|
|
sp, VM_SERVICE_SERVER_INFO_MESSAGE_ID, false /* ignored */));
|
2016-10-31 19:32:23 +00:00
|
|
|
ASSERT(!message.IsNull());
|
2018-02-14 00:28:01 +00:00
|
|
|
MessageWriter writer(false);
|
|
|
|
PortMap::PostMessage(
|
|
|
|
writer.WriteMessage(message, port_, Message::kNormalPriority));
|
2016-10-31 19:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceIsolate::ControlWebServer(const SendPort& sp, bool enable) {
|
2016-11-08 21:54:47 +00:00
|
|
|
const Array& message = Array::Handle(MakeServerControlMessage(
|
|
|
|
sp, VM_SERVICE_WEB_SERVER_CONTROL_MESSAGE_ID, enable));
|
2016-10-31 19:32:23 +00:00
|
|
|
ASSERT(!message.IsNull());
|
2018-02-14 00:28:01 +00:00
|
|
|
MessageWriter writer(false);
|
|
|
|
PortMap::PostMessage(
|
|
|
|
writer.WriteMessage(message, port_, Message::kNormalPriority));
|
2016-10-31 19:32:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 15:43:21 +00:00
|
|
|
void ServiceIsolate::SetServerAddress(const char* address) {
|
|
|
|
if (server_address_ != NULL) {
|
|
|
|
free(server_address_);
|
|
|
|
server_address_ = NULL;
|
|
|
|
}
|
|
|
|
if (address == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
server_address_ = strdup(address);
|
|
|
|
}
|
2015-02-12 19:10:55 +00:00
|
|
|
|
|
|
|
bool ServiceIsolate::NameEquals(const char* name) {
|
|
|
|
ASSERT(name != NULL);
|
|
|
|
return strcmp(name, kName) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ServiceIsolate::Exists() {
|
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
return isolate_ != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ServiceIsolate::IsRunning() {
|
|
|
|
MonitorLocker ml(monitor_);
|
2015-05-19 18:41:42 +00:00
|
|
|
return (port_ != ILLEGAL_PORT) && (isolate_ != NULL);
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 19:19:06 +00:00
|
|
|
bool ServiceIsolate::IsServiceIsolate(const Isolate* isolate) {
|
2015-02-12 19:10:55 +00:00
|
|
|
MonitorLocker ml(monitor_);
|
2019-08-28 13:18:52 +00:00
|
|
|
return isolate != nullptr && isolate == isolate_;
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 19:19:06 +00:00
|
|
|
bool ServiceIsolate::IsServiceIsolateDescendant(const Isolate* isolate) {
|
2015-04-01 20:23:42 +00:00
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
return isolate->origin_id() == origin_;
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:10:55 +00:00
|
|
|
Dart_Port ServiceIsolate::Port() {
|
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
return port_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dart_Port ServiceIsolate::WaitForLoadPort() {
|
2018-12-17 23:35:24 +00:00
|
|
|
VMTagScope tagScope(Thread::Current(), VMTag::kLoadWaitTagId);
|
2019-08-05 10:07:39 +00:00
|
|
|
return WaitForLoadPortInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
Dart_Port ServiceIsolate::WaitForLoadPortInternal() {
|
2015-02-12 19:10:55 +00:00
|
|
|
MonitorLocker ml(monitor_);
|
2019-04-10 22:55:20 +00:00
|
|
|
while (state_ == kStarting && (load_port_ == ILLEGAL_PORT)) {
|
2015-02-12 19:10:55 +00:00
|
|
|
ml.Wait();
|
|
|
|
}
|
|
|
|
return load_port_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dart_Port ServiceIsolate::LoadPort() {
|
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
return load_port_;
|
|
|
|
}
|
|
|
|
|
2018-07-25 12:06:55 +00:00
|
|
|
bool ServiceIsolate::SendServiceRpc(uint8_t* request_json,
|
|
|
|
intptr_t request_json_length,
|
2019-08-28 13:18:52 +00:00
|
|
|
Dart_Port reply_port,
|
|
|
|
char** error) {
|
2018-07-25 12:06:55 +00:00
|
|
|
// Keep in sync with "sdk/lib/vmservice/vmservice.dart:_handleNativeRpcCall".
|
|
|
|
Dart_CObject opcode;
|
|
|
|
opcode.type = Dart_CObject_kInt32;
|
|
|
|
opcode.value.as_int32 = VM_SERVICE_METHOD_CALL_FROM_NATIVE;
|
|
|
|
|
|
|
|
Dart_CObject message;
|
|
|
|
message.type = Dart_CObject_kTypedData;
|
|
|
|
message.value.as_typed_data.type = Dart_TypedData_kUint8;
|
|
|
|
message.value.as_typed_data.length = request_json_length;
|
|
|
|
message.value.as_typed_data.values = request_json;
|
|
|
|
|
|
|
|
Dart_CObject send_port;
|
|
|
|
send_port.type = Dart_CObject_kSendPort;
|
|
|
|
send_port.value.as_send_port.id = reply_port;
|
|
|
|
send_port.value.as_send_port.origin_id = ILLEGAL_PORT;
|
|
|
|
|
|
|
|
Dart_CObject* request_array[] = {
|
|
|
|
&opcode,
|
|
|
|
&message,
|
|
|
|
&send_port,
|
|
|
|
};
|
|
|
|
|
|
|
|
Dart_CObject request;
|
|
|
|
request.type = Dart_CObject_kArray;
|
|
|
|
request.value.as_array.values = request_array;
|
|
|
|
request.value.as_array.length = ARRAY_SIZE(request_array);
|
|
|
|
|
2019-08-05 10:07:39 +00:00
|
|
|
ServiceIsolate::WaitForLoadPortInternal();
|
2019-08-28 13:18:52 +00:00
|
|
|
Dart_Port service_port = ServiceIsolate::Port();
|
|
|
|
|
|
|
|
const bool success = Dart_PostCObject(service_port, &request);
|
|
|
|
|
|
|
|
if (!success && error != nullptr) {
|
|
|
|
if (service_port == ILLEGAL_PORT) {
|
|
|
|
if (startup_failure_reason_ != nullptr) {
|
|
|
|
*error = OS::SCreate(/*zone=*/nullptr,
|
|
|
|
"Service isolate failed to start up: %s.",
|
|
|
|
startup_failure_reason_);
|
|
|
|
} else {
|
|
|
|
*error = strdup("No service isolate port was found.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*error = strdup("Was unable to post message to service isolate.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2018-07-25 12:06:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 19:10:55 +00:00
|
|
|
bool ServiceIsolate::SendIsolateStartupMessage() {
|
|
|
|
if (!IsRunning()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-02 00:18:55 +00:00
|
|
|
Thread* thread = Thread::Current();
|
|
|
|
Isolate* isolate = thread->isolate();
|
2018-08-16 22:01:42 +00:00
|
|
|
if (Isolate::IsVMInternalIsolate(isolate)) {
|
2015-02-12 19:10:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ASSERT(isolate != NULL);
|
2015-09-02 00:18:55 +00:00
|
|
|
HANDLESCOPE(thread);
|
2015-02-12 19:10:55 +00:00
|
|
|
const String& name = String::Handle(String::New(isolate->name()));
|
|
|
|
ASSERT(!name.IsNull());
|
2016-11-08 21:54:47 +00:00
|
|
|
const Array& list = Array::Handle(MakeServiceControlMessage(
|
|
|
|
Dart_GetMainPortId(), VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID, name));
|
2015-02-12 19:10:55 +00:00
|
|
|
ASSERT(!list.IsNull());
|
2018-02-14 00:28:01 +00:00
|
|
|
MessageWriter writer(false);
|
2015-02-12 19:10:55 +00:00
|
|
|
if (FLAG_trace_service) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME ": Isolate %s %" Pd64
|
|
|
|
" registered.\n",
|
2017-08-09 20:06:11 +00:00
|
|
|
name.ToCString(), Dart_GetMainPortId());
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
return PortMap::PostMessage(
|
2018-02-14 00:28:01 +00:00
|
|
|
writer.WriteMessage(list, port_, Message::kNormalPriority));
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ServiceIsolate::SendIsolateShutdownMessage() {
|
|
|
|
if (!IsRunning()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-02 00:18:55 +00:00
|
|
|
Thread* thread = Thread::Current();
|
|
|
|
Isolate* isolate = thread->isolate();
|
2018-08-16 22:01:42 +00:00
|
|
|
if (Isolate::IsVMInternalIsolate(isolate)) {
|
2015-02-12 19:10:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ASSERT(isolate != NULL);
|
2015-09-02 00:18:55 +00:00
|
|
|
HANDLESCOPE(thread);
|
2015-02-12 19:10:55 +00:00
|
|
|
const String& name = String::Handle(String::New(isolate->name()));
|
|
|
|
ASSERT(!name.IsNull());
|
2016-11-08 21:54:47 +00:00
|
|
|
const Array& list = Array::Handle(MakeServiceControlMessage(
|
|
|
|
Dart_GetMainPortId(), VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID, name));
|
2015-02-12 19:10:55 +00:00
|
|
|
ASSERT(!list.IsNull());
|
2018-02-14 00:28:01 +00:00
|
|
|
MessageWriter writer(false);
|
2015-02-12 19:10:55 +00:00
|
|
|
if (FLAG_trace_service) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME ": Isolate %s %" Pd64
|
|
|
|
" deregistered.\n",
|
2017-08-09 20:06:11 +00:00
|
|
|
name.ToCString(), Dart_GetMainPortId());
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
return PortMap::PostMessage(
|
2018-02-14 00:28:01 +00:00
|
|
|
writer.WriteMessage(list, port_, Message::kNormalPriority));
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:50:19 +00:00
|
|
|
void ServiceIsolate::SendServiceExitMessage() {
|
|
|
|
if (!IsRunning()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (FLAG_trace_service) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME
|
|
|
|
": sending service exit message.\n");
|
2015-03-25 15:50:19 +00:00
|
|
|
}
|
2018-02-14 00:28:01 +00:00
|
|
|
|
|
|
|
Dart_CObject code;
|
|
|
|
code.type = Dart_CObject_kInt32;
|
|
|
|
code.value.as_int32 = VM_SERVICE_ISOLATE_EXIT_MESSAGE_ID;
|
|
|
|
Dart_CObject* values[1] = {&code};
|
|
|
|
|
|
|
|
Dart_CObject message;
|
|
|
|
message.type = Dart_CObject_kArray;
|
|
|
|
message.value.as_array.length = 1;
|
|
|
|
message.value.as_array.values = values;
|
|
|
|
|
|
|
|
ApiMessageWriter writer;
|
|
|
|
PortMap::PostMessage(
|
|
|
|
writer.WriteCMessage(&message, port_, Message::kNormalPriority));
|
2015-03-25 15:50:19 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 19:10:55 +00:00
|
|
|
void ServiceIsolate::SetServicePort(Dart_Port port) {
|
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
port_ = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceIsolate::SetServiceIsolate(Isolate* isolate) {
|
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
isolate_ = isolate;
|
|
|
|
if (isolate_ != NULL) {
|
2017-08-11 19:38:22 +00:00
|
|
|
isolate_->set_is_service_isolate(true);
|
2015-04-01 20:23:42 +00:00
|
|
|
origin_ = isolate_->origin_id();
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceIsolate::SetLoadPort(Dart_Port port) {
|
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
load_port_ = port;
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:58:14 +00:00
|
|
|
void ServiceIsolate::MaybeMakeServiceIsolate(Isolate* I) {
|
2015-09-02 23:07:48 +00:00
|
|
|
Thread* T = Thread::Current();
|
|
|
|
ASSERT(I == T->isolate());
|
|
|
|
ASSERT(I != NULL);
|
|
|
|
ASSERT(I->name() != NULL);
|
|
|
|
if (!ServiceIsolate::NameEquals(I->name())) {
|
2015-02-12 19:10:55 +00:00
|
|
|
// Not service isolate.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Exists()) {
|
|
|
|
// Service isolate already exists.
|
|
|
|
return;
|
|
|
|
}
|
2015-09-02 23:07:48 +00:00
|
|
|
SetServiceIsolate(I);
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:50:19 +00:00
|
|
|
void ServiceIsolate::FinishedExiting() {
|
|
|
|
MonitorLocker ml(monitor_);
|
2019-04-10 22:55:20 +00:00
|
|
|
ASSERT(state_ == kStarted || state_ == kStopping);
|
|
|
|
state_ = kStopped;
|
2015-03-25 15:50:19 +00:00
|
|
|
ml.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:10:55 +00:00
|
|
|
void ServiceIsolate::FinishedInitializing() {
|
|
|
|
MonitorLocker ml(monitor_);
|
2019-04-10 22:55:20 +00:00
|
|
|
ASSERT(state_ == kStarting);
|
|
|
|
state_ = kStarted;
|
|
|
|
ml.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2019-08-28 13:18:52 +00:00
|
|
|
void ServiceIsolate::InitializingFailed(char* error) {
|
2019-04-10 22:55:20 +00:00
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
ASSERT(state_ == kStarting);
|
|
|
|
state_ = kStopped;
|
2019-08-28 13:18:52 +00:00
|
|
|
startup_failure_reason_ = error;
|
2015-02-12 19:10:55 +00:00
|
|
|
ml.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
class RunServiceTask : public ThreadPool::Task {
|
|
|
|
public:
|
|
|
|
virtual void Run() {
|
|
|
|
ASSERT(Isolate::Current() == NULL);
|
2019-01-18 00:06:10 +00:00
|
|
|
#if defined(SUPPORT_TIMELINE)
|
2016-11-08 21:54:47 +00:00
|
|
|
TimelineDurationScope tds(Timeline::GetVMStream(), "ServiceIsolateStartup");
|
2019-01-18 00:06:10 +00:00
|
|
|
#endif // SUPPORT_TIMELINE
|
2015-02-12 19:10:55 +00:00
|
|
|
char* error = NULL;
|
|
|
|
Isolate* isolate = NULL;
|
|
|
|
|
2019-07-08 23:49:05 +00:00
|
|
|
const auto create_group_callback = ServiceIsolate::create_group_callback();
|
|
|
|
ASSERT(create_group_callback != NULL);
|
2015-02-12 19:10:55 +00:00
|
|
|
|
2015-06-07 15:57:34 +00:00
|
|
|
Dart_IsolateFlags api_flags;
|
2016-02-26 02:06:56 +00:00
|
|
|
Isolate::FlagsInitialize(&api_flags);
|
2015-06-07 15:57:34 +00:00
|
|
|
|
2019-04-10 19:50:29 +00:00
|
|
|
isolate = reinterpret_cast<Isolate*>(
|
2019-07-08 23:49:05 +00:00
|
|
|
create_group_callback(ServiceIsolate::kName, ServiceIsolate::kName,
|
|
|
|
NULL, NULL, &api_flags, NULL, &error));
|
2015-02-12 19:10:55 +00:00
|
|
|
if (isolate == NULL) {
|
2016-08-02 23:19:24 +00:00
|
|
|
if (FLAG_trace_service) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME
|
|
|
|
": Isolate creation error: %s\n",
|
|
|
|
error);
|
2016-08-02 23:19:24 +00:00
|
|
|
}
|
2019-08-28 13:18:52 +00:00
|
|
|
|
|
|
|
char* formatted_error = OS::SCreate(
|
|
|
|
/*zone=*/nullptr, "Invoking the 'create_group' failed with: '%s'",
|
|
|
|
error);
|
|
|
|
|
2019-04-24 12:10:46 +00:00
|
|
|
free(error);
|
|
|
|
error = nullptr;
|
2015-10-26 22:43:27 +00:00
|
|
|
ServiceIsolate::SetServiceIsolate(NULL);
|
2019-08-28 13:18:52 +00:00
|
|
|
ServiceIsolate::InitializingFailed(formatted_error);
|
2015-02-12 19:10:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-04 16:27:48 +00:00
|
|
|
bool got_unwind;
|
2015-10-29 16:57:27 +00:00
|
|
|
{
|
|
|
|
ASSERT(Isolate::Current() == NULL);
|
|
|
|
StartIsolateScope start_scope(isolate);
|
2016-04-04 16:27:48 +00:00
|
|
|
got_unwind = RunMain(isolate);
|
|
|
|
}
|
|
|
|
|
2018-02-03 00:10:23 +00:00
|
|
|
// FinishedInitializing should be called irrespective of whether
|
|
|
|
// running main caused an error or not. Otherwise, other isolates
|
|
|
|
// waiting for service isolate to come up will deadlock.
|
|
|
|
ServiceIsolate::FinishedInitializing();
|
|
|
|
|
2016-04-04 16:27:48 +00:00
|
|
|
if (got_unwind) {
|
|
|
|
ShutdownIsolate(reinterpret_cast<uword>(isolate));
|
|
|
|
return;
|
2015-10-29 16:57:27 +00:00
|
|
|
}
|
2015-02-12 19:10:55 +00:00
|
|
|
|
2016-11-08 21:54:47 +00:00
|
|
|
isolate->message_handler()->Run(Dart::thread_pool(), NULL, ShutdownIsolate,
|
2015-02-12 19:10:55 +00:00
|
|
|
reinterpret_cast<uword>(isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void ShutdownIsolate(uword parameter) {
|
2016-06-06 21:15:01 +00:00
|
|
|
if (FLAG_trace_service) {
|
2017-08-09 20:06:11 +00:00
|
|
|
OS::PrintErr("vm-service: ShutdownIsolate\n");
|
2016-06-06 21:15:01 +00:00
|
|
|
}
|
2015-09-02 23:07:48 +00:00
|
|
|
Isolate* I = reinterpret_cast<Isolate*>(parameter);
|
|
|
|
ASSERT(ServiceIsolate::IsServiceIsolate(I));
|
2015-02-12 19:10:55 +00:00
|
|
|
{
|
|
|
|
// Print the error if there is one. This may execute dart code to
|
|
|
|
// print the exception object, so we need to use a StartIsolateScope.
|
2015-10-29 16:57:27 +00:00
|
|
|
ASSERT(Isolate::Current() == NULL);
|
2015-09-02 23:07:48 +00:00
|
|
|
StartIsolateScope start_scope(I);
|
|
|
|
Thread* T = Thread::Current();
|
|
|
|
ASSERT(I == T->isolate());
|
2019-08-01 19:04:11 +00:00
|
|
|
I->WaitForOutstandingSpawns();
|
2015-09-02 23:07:48 +00:00
|
|
|
StackZone zone(T);
|
|
|
|
HandleScope handle_scope(T);
|
|
|
|
Error& error = Error::Handle(Z);
|
2016-02-05 23:46:55 +00:00
|
|
|
error = T->sticky_error();
|
2015-09-23 19:13:25 +00:00
|
|
|
if (!error.IsNull() && !error.IsUnwindError()) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME ": Error: %s\n",
|
|
|
|
error.ToErrorCString());
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
2019-01-18 01:41:18 +00:00
|
|
|
error = I->sticky_error();
|
|
|
|
if (!error.IsNull() && !error.IsUnwindError()) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME ": Error: %s\n",
|
|
|
|
error.ToErrorCString());
|
2019-01-18 01:41:18 +00:00
|
|
|
}
|
2015-02-12 19:10:55 +00:00
|
|
|
Dart::RunShutdownCallback();
|
2015-10-30 16:57:29 +00:00
|
|
|
}
|
2018-05-16 00:28:48 +00:00
|
|
|
ASSERT(ServiceIsolate::IsServiceIsolate(I));
|
|
|
|
ServiceIsolate::SetServiceIsolate(NULL);
|
|
|
|
ServiceIsolate::SetServicePort(ILLEGAL_PORT);
|
|
|
|
|
2015-11-02 22:29:16 +00:00
|
|
|
// Shut the isolate down.
|
|
|
|
Dart::ShutdownIsolate(I);
|
2015-02-12 19:10:55 +00:00
|
|
|
if (FLAG_trace_service) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME ": Shutdown.\n");
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
2015-03-25 15:50:19 +00:00
|
|
|
ServiceIsolate::FinishedExiting();
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 16:27:48 +00:00
|
|
|
bool RunMain(Isolate* I) {
|
2015-09-02 23:07:48 +00:00
|
|
|
Thread* T = Thread::Current();
|
|
|
|
ASSERT(I == T->isolate());
|
|
|
|
StackZone zone(T);
|
|
|
|
HANDLESCOPE(T);
|
2015-02-12 19:10:55 +00:00
|
|
|
// Invoke main which will return the loadScriptPort.
|
2016-11-08 21:54:47 +00:00
|
|
|
const Library& root_library =
|
|
|
|
Library::Handle(Z, I->object_store()->root_library());
|
2015-02-12 19:10:55 +00:00
|
|
|
if (root_library.IsNull()) {
|
|
|
|
if (FLAG_trace_service) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME
|
|
|
|
": Embedder did not install a script.");
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
// Service isolate is not supported by embedder.
|
2016-04-04 16:27:48 +00:00
|
|
|
return false;
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
ASSERT(!root_library.IsNull());
|
2015-09-02 23:07:48 +00:00
|
|
|
const String& entry_name = String::Handle(Z, String::New("main"));
|
2015-02-12 19:10:55 +00:00
|
|
|
ASSERT(!entry_name.IsNull());
|
2016-11-08 21:54:47 +00:00
|
|
|
const Function& entry = Function::Handle(
|
|
|
|
Z, root_library.LookupFunctionAllowPrivate(entry_name));
|
2015-02-12 19:10:55 +00:00
|
|
|
if (entry.IsNull()) {
|
|
|
|
// Service isolate is not supported by embedder.
|
|
|
|
if (FLAG_trace_service) {
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME
|
|
|
|
": Embedder did not provide a main function.");
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
2016-04-04 16:27:48 +00:00
|
|
|
return false;
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
ASSERT(!entry.IsNull());
|
2016-11-08 21:54:47 +00:00
|
|
|
const Object& result = Object::Handle(
|
|
|
|
Z, DartEntry::InvokeFunction(entry, Object::empty_array()));
|
2015-02-12 19:10:55 +00:00
|
|
|
ASSERT(!result.IsNull());
|
|
|
|
if (result.IsError()) {
|
|
|
|
// Service isolate did not initialize properly.
|
|
|
|
if (FLAG_trace_service) {
|
|
|
|
const Error& error = Error::Cast(result);
|
2019-04-10 22:55:20 +00:00
|
|
|
OS::PrintErr(DART_VM_SERVICE_ISOLATE_NAME
|
|
|
|
": Calling main resulted in an error: %s",
|
2017-08-09 20:06:11 +00:00
|
|
|
error.ToErrorCString());
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
2016-04-04 16:27:48 +00:00
|
|
|
if (result.IsUnwindError()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
ASSERT(result.IsReceivePort());
|
|
|
|
const ReceivePort& rp = ReceivePort::Cast(result);
|
|
|
|
ServiceIsolate::SetLoadPort(rp.Id());
|
2016-04-04 16:27:48 +00:00
|
|
|
return false;
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void ServiceIsolate::Run() {
|
2019-04-10 22:55:20 +00:00
|
|
|
{
|
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
ASSERT(state_ == kStopped);
|
|
|
|
state_ = kStarting;
|
|
|
|
ml.NotifyAll();
|
|
|
|
}
|
2015-02-12 19:10:55 +00:00
|
|
|
// Grab the isolate create callback here to avoid race conditions with tests
|
|
|
|
// that change this after Dart_Initialize returns.
|
2019-07-08 23:49:05 +00:00
|
|
|
create_group_callback_ = Isolate::CreateGroupCallback();
|
|
|
|
if (create_group_callback_ == NULL) {
|
2019-08-28 13:18:52 +00:00
|
|
|
ServiceIsolate::InitializingFailed(
|
|
|
|
strdup("The 'create_group' callback was not provided"));
|
2017-07-13 22:26:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-06-18 17:51:10 +00:00
|
|
|
bool task_started = Dart::thread_pool()->Run<RunServiceTask>();
|
2019-04-10 22:55:20 +00:00
|
|
|
ASSERT(task_started);
|
2015-02-12 19:10:55 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 19:49:52 +00:00
|
|
|
void ServiceIsolate::KillServiceIsolate() {
|
|
|
|
{
|
|
|
|
MonitorLocker ml(monitor_);
|
2019-04-10 22:55:20 +00:00
|
|
|
if (state_ == kStopped) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT(state_ == kStarted);
|
|
|
|
state_ = kStopping;
|
|
|
|
ml.NotifyAll();
|
2015-09-15 19:49:52 +00:00
|
|
|
}
|
2015-10-06 18:27:26 +00:00
|
|
|
Isolate::KillIfExists(isolate_, Isolate::kInternalKillMsg);
|
2015-09-15 19:49:52 +00:00
|
|
|
{
|
|
|
|
MonitorLocker ml(monitor_);
|
2019-04-10 22:55:20 +00:00
|
|
|
while (state_ == kStopping) {
|
2015-09-15 19:49:52 +00:00
|
|
|
ml.Wait();
|
|
|
|
}
|
2019-04-10 22:55:20 +00:00
|
|
|
ASSERT(state_ == kStopped);
|
2015-09-15 19:49:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:50:19 +00:00
|
|
|
void ServiceIsolate::Shutdown() {
|
2019-04-10 22:55:20 +00:00
|
|
|
{
|
|
|
|
MonitorLocker ml(monitor_);
|
|
|
|
while (state_ == kStarting) {
|
|
|
|
ml.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 16:27:48 +00:00
|
|
|
if (IsRunning()) {
|
|
|
|
{
|
|
|
|
MonitorLocker ml(monitor_);
|
2019-04-10 22:55:20 +00:00
|
|
|
ASSERT(state_ == kStarted);
|
|
|
|
state_ = kStopping;
|
|
|
|
ml.NotifyAll();
|
2016-04-04 16:27:48 +00:00
|
|
|
}
|
|
|
|
SendServiceExitMessage();
|
|
|
|
{
|
|
|
|
MonitorLocker ml(monitor_);
|
2019-04-10 22:55:20 +00:00
|
|
|
while (state_ == kStopping) {
|
2016-04-04 16:27:48 +00:00
|
|
|
ml.Wait();
|
|
|
|
}
|
2019-04-10 22:55:20 +00:00
|
|
|
ASSERT(state_ == kStopped);
|
2016-04-04 16:27:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-15 19:49:52 +00:00
|
|
|
if (isolate_ != NULL) {
|
|
|
|
// TODO(johnmccutchan,turnidge) When it is possible to properly create
|
|
|
|
// the VMService object and set up its shutdown handler in the service
|
|
|
|
// isolate's main() function, this case will no longer be possible and
|
|
|
|
// can be removed.
|
|
|
|
KillServiceIsolate();
|
|
|
|
}
|
2015-03-25 15:50:19 +00:00
|
|
|
}
|
2016-01-29 15:43:21 +00:00
|
|
|
if (server_address_ != NULL) {
|
|
|
|
free(server_address_);
|
|
|
|
server_address_ = NULL;
|
|
|
|
}
|
2019-08-28 13:18:52 +00:00
|
|
|
|
|
|
|
if (startup_failure_reason_ != nullptr) {
|
|
|
|
free(startup_failure_reason_);
|
|
|
|
startup_failure_reason_ = nullptr;
|
|
|
|
}
|
2015-03-25 15:50:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 16:16:39 +00:00
|
|
|
void ServiceIsolate::BootVmServiceLibrary() {
|
2016-05-05 17:42:28 +00:00
|
|
|
Thread* thread = Thread::Current();
|
2015-10-06 16:16:39 +00:00
|
|
|
const Library& vmservice_library =
|
2016-05-05 17:42:28 +00:00
|
|
|
Library::Handle(Library::LookupLibrary(thread, Symbols::DartVMService()));
|
2015-10-06 16:16:39 +00:00
|
|
|
ASSERT(!vmservice_library.IsNull());
|
|
|
|
const String& boot_function_name = String::Handle(String::New("boot"));
|
2016-11-08 21:54:47 +00:00
|
|
|
const Function& boot_function = Function::Handle(
|
|
|
|
vmservice_library.LookupFunctionAllowPrivate(boot_function_name));
|
2015-10-06 16:16:39 +00:00
|
|
|
ASSERT(!boot_function.IsNull());
|
2016-11-08 21:54:47 +00:00
|
|
|
const Object& result = Object::Handle(
|
|
|
|
DartEntry::InvokeFunction(boot_function, Object::empty_array()));
|
2015-10-06 16:16:39 +00:00
|
|
|
ASSERT(!result.IsNull());
|
2017-01-19 16:57:08 +00:00
|
|
|
if (result.IsUnwindError() || result.IsUnhandledException()) {
|
|
|
|
Exceptions::PropagateError(Error::Cast(result));
|
|
|
|
}
|
2015-10-06 16:16:39 +00:00
|
|
|
Dart_Port port = ILLEGAL_PORT;
|
|
|
|
if (result.IsReceivePort()) {
|
|
|
|
port = ReceivePort::Cast(result).Id();
|
|
|
|
}
|
|
|
|
ASSERT(port != ILLEGAL_PORT);
|
|
|
|
ServiceIsolate::SetServicePort(port);
|
|
|
|
}
|
|
|
|
|
2016-11-08 21:54:47 +00:00
|
|
|
void ServiceIsolate::VisitObjectPointers(ObjectPointerVisitor* visitor) {}
|
2016-06-10 19:33:50 +00:00
|
|
|
|
2015-02-12 19:10:55 +00:00
|
|
|
} // namespace dart
|