Flutter Engine
The Flutter Engine
vmservice_impl.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
6
7#include "include/dart_api.h"
8
9#include "bin/builtin.h"
10#include "bin/dartutils.h"
11#include "bin/isolate_data.h"
12#include "bin/main_options.h"
13#include "bin/platform.h"
14#include "bin/thread.h"
15#include "bin/utils.h"
17#include "platform/utils.h"
18
19namespace dart {
20namespace bin {
21
22#if !defined(PRODUCT)
23
24#define RETURN_ERROR_HANDLE(handle) \
25 if (Dart_IsError(handle)) { \
26 return handle; \
27 }
28
29#define SHUTDOWN_ON_ERROR(handle) \
30 if (Dart_IsError(handle)) { \
31 error_msg_ = Utils::StrDup(Dart_GetError(handle)); \
32 Dart_ExitScope(); \
33 Dart_ShutdownIsolate(); \
34 return false; \
35 }
36
37static constexpr const char* kVMServiceIOLibraryUri = "dart:vmservice_io";
38
41 const char* uri_chars;
43 if (Dart_IsError(uri_arg)) {
44 VmService::SetServerAddress("");
46 return;
47 }
48 Dart_Handle result = Dart_StringToCString(uri_arg, &uri_chars);
49 if (Dart_IsError(result)) {
50 VmService::SetServerAddress("");
52 return;
53 }
54 VmService::SetServerAddress(uri_chars);
56}
57
59 // NO-OP.
60}
61
63 const char* name;
66};
67
69 {"VMServiceIO_NotifyServerState", 1, NotifyServerState},
70 {"VMServiceIO_Shutdown", 0, Shutdown},
71};
72
74 int num_arguments,
75 bool* auto_setup_scope) {
76 const char* function_name = nullptr;
79 ASSERT(function_name != nullptr);
80 *auto_setup_scope = true;
81 intptr_t n =
83 for (intptr_t i = 0; i < n; i++) {
85 if ((strcmp(function_name, entry.name) == 0) &&
86 (num_arguments == entry.num_arguments)) {
87 return entry.function;
88 }
89 }
90 return nullptr;
91}
92
94 intptr_t n =
96 for (intptr_t i = 0; i < n; i++) {
98 if (reinterpret_cast<Dart_NativeFunction>(entry.function) == nf) {
99 return reinterpret_cast<const uint8_t*>(entry.name);
100 }
101 }
102 return nullptr;
103}
104
105const char* VmService::error_msg_ = nullptr;
106char VmService::server_uri_[kServerUriStringBufferSize];
107
110 Dart_Handle library = Dart_LookupLibrary(url);
111 if (!Dart_IsError(library)) {
114 }
115}
116
117bool VmService::Setup(const char* server_ip,
118 intptr_t server_port,
119 bool dev_mode_server,
120 bool auth_codes_disabled,
121 const char* write_service_info_filename,
122 bool trace_loading,
123 bool deterministic,
124 bool enable_service_port_fallback,
125 bool wait_for_dds_to_advertise_service,
126 bool serve_devtools,
127 bool serve_observatory,
128 bool print_dtd) {
130 ASSERT(isolate != nullptr);
131 SetServerAddress("");
132
134
135 // Prepare builtin and its dependent libraries for use to resolve URIs.
136 // Set up various closures, e.g: printing, timers etc.
137 // Set up 'package root' for URI resolution.
138 result = DartUtils::PrepareForScriptLoading(/*is_service_isolate=*/true,
139 trace_loading);
141
143 Dart_Handle library = Dart_LookupLibrary(url);
144 SHUTDOWN_ON_ERROR(library);
145 result = Dart_SetRootLibrary(library);
146 SHUTDOWN_ON_ERROR(library);
150
151 // Make runnable.
154 error_msg_ = Dart_IsolateMakeRunnable(isolate);
155 if (error_msg_ != nullptr) {
156 Dart_EnterIsolate(isolate);
158 return false;
159 }
160 Dart_EnterIsolate(isolate);
162
163 library = Dart_RootLibrary();
164 SHUTDOWN_ON_ERROR(library);
165
166 // Set HTTP server state.
167 // If we have a port specified, start the server immediately.
168 bool auto_start = server_port >= 0;
169 if (server_port < 0) {
170 // Adjust server_port to port 0 which will result in the first available
171 // port when the HTTP server is started.
172 server_port = 0;
173 }
174 if (wait_for_dds_to_advertise_service) {
175 result = DartUtils::SetStringField(library, "_ddsIP", server_ip);
177 result = DartUtils::SetIntegerField(library, "_ddsPort", server_port);
179 result =
182 result = DartUtils::SetIntegerField(library, "_port", 0);
184 } else {
185 result = DartUtils::SetStringField(library, "_ip", server_ip);
187 result = DartUtils::SetIntegerField(library, "_port", server_port);
189 }
190 result = Dart_SetField(library, DartUtils::NewString("_autoStart"),
191 Dart_NewBoolean(auto_start));
193 result = Dart_SetField(library, DartUtils::NewString("_originCheckDisabled"),
194 Dart_NewBoolean(dev_mode_server));
196
197 result = Dart_SetField(library, DartUtils::NewString("_authCodesDisabled"),
198 Dart_NewBoolean(auth_codes_disabled));
200
201 result =
202 Dart_SetField(library, DartUtils::NewString("_enableServicePortFallback"),
203 Dart_NewBoolean(enable_service_port_fallback));
205
206 if (write_service_info_filename != nullptr) {
207 result = DartUtils::SetStringField(library, "_serviceInfoFilename",
208 write_service_info_filename);
210 }
211
212 result = Dart_SetField(library,
213 DartUtils::NewString("_waitForDdsToAdvertiseService"),
214 Dart_NewBoolean(wait_for_dds_to_advertise_service));
216
217 result = Dart_SetField(library, DartUtils::NewString("_serveDevtools"),
218 serve_devtools ? Dart_True() : Dart_False());
220
221 result = Dart_SetField(library, DartUtils::NewString("_serveObservatory"),
222 serve_observatory ? Dart_True() : Dart_False());
224
225 result = Dart_SetField(library, DartUtils::NewString("_printDtd"),
226 print_dtd ? Dart_True() : Dart_False());
228
229// Are we running on Windows?
230#if defined(DART_HOST_OS_WINDOWS)
232#else
234#endif
235 result =
236 Dart_SetField(library, DartUtils::NewString("_isWindows"), is_windows);
238
239// Are we running on Fuchsia?
240#if defined(DART_HOST_OS_FUCHSIA)
241 Dart_Handle is_fuchsia = Dart_True();
242#else
243 Dart_Handle is_fuchsia = Dart_False();
244#endif
245 result =
246 Dart_SetField(library, DartUtils::NewString("_isFuchsia"), is_fuchsia);
248
249 // Get _getWatchSignalInternal from dart:io.
251 SHUTDOWN_ON_ERROR(dart_io_str);
252 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str);
253 SHUTDOWN_ON_ERROR(io_lib);
255 Dart_NewStringFromCString("_getWatchSignalInternal");
257 Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, nullptr);
258 SHUTDOWN_ON_ERROR(signal_watch);
259 Dart_Handle field_name = Dart_NewStringFromCString("_signalWatch");
260 SHUTDOWN_ON_ERROR(field_name);
261 result = Dart_SetField(library, field_name, signal_watch);
262 SHUTDOWN_ON_ERROR(field_name);
263 return true;
264}
265
267 return (error_msg_ == nullptr) ? "No error." : error_msg_;
268}
269
270void VmService::SetServerAddress(const char* server_uri) {
271 if (server_uri == nullptr) {
272 server_uri = "";
273 }
274 const intptr_t server_uri_len = strlen(server_uri);
275 if (server_uri_len >= (kServerUriStringBufferSize - 1)) {
276 FATAL("vm-service: Server URI exceeded length: %s\n", server_uri);
277 }
278 strncpy(server_uri_, server_uri, kServerUriStringBufferSize);
279 server_uri_[kServerUriStringBufferSize - 1] = '\0';
280}
281
282#endif // !defined(PRODUCT)
283
284} // namespace bin
285} // namespace dart
static constexpr const char * kIOLibURL
Definition: dartutils.h:297
static Dart_Handle NewString(const char *str)
Definition: dartutils.h:214
static Dart_Handle PrepareForScriptLoading(bool is_service_isolate, bool trace_loading)
Definition: dartutils.cc:570
static Dart_Handle SetIntegerField(Dart_Handle handle, const char *name, int64_t val)
Definition: dartutils.cc:203
static Dart_Handle SetStringField(Dart_Handle handle, const char *name, const char *val)
Definition: dartutils.cc:209
static const char * GetErrorMessage()
static void SetNativeResolver()
static bool Setup(const char *server_ip, intptr_t server_port, bool dev_mode_server, bool auth_codes_disabled, const char *write_service_info_filename, bool trace_loading, bool deterministic, bool enable_service_port_fallback, bool wait_for_dds_to_advertise_service, bool serve_devtools, bool serve_observatory, bool print_dtd)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_Isolate * Dart_Isolate
Definition: dart_api.h:88
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
void(* Dart_NativeFunction)(Dart_NativeArguments arguments)
Definition: dart_api.h:3207
#define ASSERT(E)
#define FATAL(error)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
static Dart_NativeFunction VmServiceIONativeResolver(Dart_Handle name, int num_arguments, bool *auto_setup_scope)
static constexpr const char * DEFAULT_VM_SERVICE_SERVER_IP
Definition: main_options.h:97
static void Shutdown(Dart_NativeArguments args)
void NotifyServerState(Dart_NativeArguments args)
const uint8_t * VmServiceIONativeSymbol(Dart_NativeFunction nf)
static VmServiceIONativeEntry _VmServiceIONativeEntries[]
static constexpr const char * kVMServiceIOLibraryUri
Dart_ShutdownIsolate()
Definition: dart_vm.cc:33
DART_EXPORT void Dart_EnterScope()
const char *const name
DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate)
DART_EXPORT Dart_Handle Dart_False()
DART_EXPORT Dart_Handle Dart_Invoke(Dart_Handle target, Dart_Handle name, int number_of_arguments, Dart_Handle *arguments)
DART_EXPORT Dart_Handle Dart_True()
DART_EXPORT Dart_Handle Dart_RootLibrary()
DART_EXPORT Dart_Isolate Dart_CurrentIsolate()
DART_EXPORT Dart_Handle Dart_NewBoolean(bool value)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_SetNativeResolver(Dart_Handle library, Dart_NativeEntryResolver resolver, Dart_NativeEntrySymbol symbol)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
DART_EXPORT void Dart_ExitIsolate()
DART_EXPORT char * Dart_IsolateMakeRunnable(Dart_Isolate isolate)
DART_EXPORT Dart_Handle Dart_SetField(Dart_Handle container, Dart_Handle name, Dart_Handle value)
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
const char *const function_name
DART_EXPORT Dart_Handle Dart_SetRootLibrary(Dart_Handle library)
DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, const char **cstr)
DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char *str)
def is_windows()
Definition: git_revision.py:15
#define SHUTDOWN_ON_ERROR(handle)