ager@google.com | 607eea1 | 2012-01-10 16:50:14 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 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 | |
Zachary Anderson | 6cd8a79 | 2017-07-13 08:08:33 -0700 | [diff] [blame] | 5 | #include <stdio.h> |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 8 | |
| 9 | #include "include/dart_api.h" |
John McCutchan | bcf6eb7 | 2015-06-17 14:56:55 -0700 | [diff] [blame] | 10 | #include "include/dart_tools_api.h" |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 11 | |
| 12 | #include "bin/builtin.h" |
asiva@google.com | 0ce8298 | 2011-11-18 18:24:04 +0000 | [diff] [blame] | 13 | #include "bin/dartutils.h" |
Siva Annamalai | 286dd51 | 2017-05-25 15:40:36 -0700 | [diff] [blame] | 14 | #include "bin/dfe.h" |
iposva@google.com | 67b02b4 | 2012-03-07 22:57:57 +0000 | [diff] [blame] | 15 | #include "bin/directory.h" |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 16 | #include "bin/embedded_dart_io.h" |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 17 | #include "bin/error_exit.h" |
sgjesse@google.com | a6071e0 | 2012-01-09 07:38:30 +0000 | [diff] [blame] | 18 | #include "bin/eventhandler.h" |
whesse@google.com | 0e40002 | 2012-02-23 13:25:11 +0000 | [diff] [blame] | 19 | #include "bin/extensions.h" |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 20 | #include "bin/file.h" |
ager@google.com | f791a12 | 2012-06-29 14:40:17 +0000 | [diff] [blame] | 21 | #include "bin/isolate_data.h" |
John McCutchan | 6fd8fd7 | 2016-06-06 14:15:01 -0700 | [diff] [blame] | 22 | #include "bin/loader.h" |
gram@google.com | 3601944 | 2012-11-19 17:38:15 +0000 | [diff] [blame] | 23 | #include "bin/log.h" |
sgjesse@google.com | d7c8c1f | 2011-11-23 14:49:18 +0000 | [diff] [blame] | 24 | #include "bin/platform.h" |
ager@google.com | ed3338d | 2012-02-01 18:16:45 +0000 | [diff] [blame] | 25 | #include "bin/process.h" |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 26 | #include "bin/snapshot_utils.h" |
iposva@google.com | e57e60c | 2014-08-14 05:00:37 +0000 | [diff] [blame] | 27 | #include "bin/thread.h" |
Ivan Posva | 6249c2b | 2015-12-03 17:46:02 -0800 | [diff] [blame] | 28 | #include "bin/utils.h" |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 29 | #include "bin/vmservice_impl.h" |
ager@google.com | c4c92c4 | 2012-01-06 11:28:21 +0000 | [diff] [blame] | 30 | #include "platform/globals.h" |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 31 | #include "platform/growable_array.h" |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 32 | #include "platform/hashmap.h" |
Ivan Posva | 6249c2b | 2015-12-03 17:46:02 -0800 | [diff] [blame] | 33 | #include "platform/text_buffer.h" |
Zachary Anderson | 7308e58 | 2016-03-29 11:22:03 -0700 | [diff] [blame] | 34 | #if !defined(DART_PRECOMPILER) |
Ryan Macnak | 7dfe62f | 2017-08-11 12:18:16 -0700 | [diff] [blame] | 35 | #include "bin/gzip.h" |
Zachary Anderson | 7308e58 | 2016-03-29 11:22:03 -0700 | [diff] [blame] | 36 | #endif |
ager@google.com | c4c92c4 | 2012-01-06 11:28:21 +0000 | [diff] [blame] | 37 | |
Kevin Millikin | 385f8fb | 2017-04-25 20:04:20 +0200 | [diff] [blame] | 38 | #include "vm/kernel.h" |
| 39 | |
Ryan Macnak | 14ec8fa | 2017-05-24 14:38:09 -0700 | [diff] [blame] | 40 | extern "C" { |
| 41 | extern const uint8_t kDartVmSnapshotData[]; |
| 42 | extern const uint8_t kDartVmSnapshotInstructions[]; |
| 43 | extern const uint8_t kDartCoreIsolateSnapshotData[]; |
| 44 | extern const uint8_t kDartCoreIsolateSnapshotInstructions[]; |
| 45 | } |
| 46 | |
smok@google.com | ebbc202 | 2013-04-25 14:22:30 +0000 | [diff] [blame] | 47 | namespace dart { |
| 48 | namespace bin { |
| 49 | |
Ryan Macnak | b46af1e | 2017-01-23 10:25:02 -0800 | [diff] [blame] | 50 | // Snapshot pieces if we link in a snapshot, otherwise initialized to NULL. |
Ryan Macnak | 14ec8fa | 2017-05-24 14:38:09 -0700 | [diff] [blame] | 51 | #if defined(DART_NO_SNAPSHOT) |
| 52 | const uint8_t* vm_snapshot_data = NULL; |
| 53 | const uint8_t* vm_snapshot_instructions = NULL; |
| 54 | const uint8_t* core_isolate_snapshot_data = NULL; |
| 55 | const uint8_t* core_isolate_snapshot_instructions = NULL; |
| 56 | #else |
| 57 | const uint8_t* vm_snapshot_data = kDartVmSnapshotData; |
| 58 | const uint8_t* vm_snapshot_instructions = kDartVmSnapshotInstructions; |
| 59 | const uint8_t* core_isolate_snapshot_data = kDartCoreIsolateSnapshotData; |
Ryan Macnak | 76df24b | 2017-05-31 13:49:09 -0700 | [diff] [blame] | 60 | const uint8_t* core_isolate_snapshot_instructions = |
| 61 | kDartCoreIsolateSnapshotInstructions; |
Ryan Macnak | 14ec8fa | 2017-05-24 14:38:09 -0700 | [diff] [blame] | 62 | #endif |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 63 | |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Global state used to control and store generation of application snapshots |
Siva Annamalai | af4d246 | 2016-10-19 17:42:01 -0700 | [diff] [blame] | 66 | * An application snapshot can be generated and run using the following |
| 67 | * command |
| 68 | * dart --snapshot-kind=app-jit --snapshot=<app_snapshot_filename> |
| 69 | * <script_uri> [<script_options>] |
| 70 | * To Run the application snapshot generated above, use : |
| 71 | * dart <app_snapshot_filename> [<script_options>] |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 72 | */ |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 73 | static bool vm_run_app_snapshot = false; |
zra@google.com | 0f6c47ab | 2014-09-08 20:52:25 +0000 | [diff] [blame] | 74 | static const char* snapshot_filename = NULL; |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 75 | enum SnapshotKind { |
| 76 | kNone, |
| 77 | kScript, |
| 78 | kAppAOT, |
Siva Annamalai | af4d246 | 2016-10-19 17:42:01 -0700 | [diff] [blame] | 79 | kAppJIT, |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 80 | }; |
| 81 | static SnapshotKind gen_snapshot_kind = kNone; |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 82 | static const char* snapshot_deps_filename = NULL; |
Siva Annamalai | 286dd51 | 2017-05-25 15:40:36 -0700 | [diff] [blame] | 83 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 84 | DFE dfe; |
| 85 | #endif |
Kevin Millikin | 385f8fb | 2017-04-25 20:04:20 +0200 | [diff] [blame] | 86 | |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 87 | // Value of the --save-compilation-trace flag. |
| 88 | // (This pointer points into an argv buffer and does not need to be |
| 89 | // free'd.) |
| 90 | static const char* save_compilation_trace_filename = NULL; |
| 91 | |
| 92 | // Value of the --load-compilation-trace flag. |
| 93 | // (This pointer points into an argv buffer and does not need to be |
| 94 | // free'd.) |
| 95 | static const char* load_compilation_trace_filename = NULL; |
| 96 | |
Ryan Macnak | 4607e38 | 2016-12-16 16:00:17 -0800 | [diff] [blame] | 97 | // Value of the --save-feedback flag. |
| 98 | // (This pointer points into an argv buffer and does not need to be |
| 99 | // free'd.) |
| 100 | static const char* save_feedback_filename = NULL; |
| 101 | |
| 102 | // Value of the --load-feedback flag. |
| 103 | // (This pointer points into an argv buffer and does not need to be |
| 104 | // free'd.) |
| 105 | static const char* load_feedback_filename = NULL; |
| 106 | |
mattsh@google.com | cb82991 | 2012-04-27 17:23:00 +0000 | [diff] [blame] | 107 | // Value of the --package-root flag. |
| 108 | // (This pointer points into an argv buffer and does not need to be |
| 109 | // free'd.) |
ajohnsen@google.com | 0491f4e | 2014-09-23 05:46:14 +0000 | [diff] [blame] | 110 | static const char* commandline_package_root = NULL; |
mattsh@google.com | cb82991 | 2012-04-27 17:23:00 +0000 | [diff] [blame] | 111 | |
Ivan Posva | b07a9dd | 2015-07-29 11:30:33 -0700 | [diff] [blame] | 112 | // Value of the --packages flag. |
| 113 | // (This pointer points into an argv buffer and does not need to be |
| 114 | // free'd.) |
| 115 | static const char* commandline_packages_file = NULL; |
| 116 | |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 117 | // Global flag that is used to indicate that we want to compile all the |
| 118 | // dart functions and not run anything. |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 119 | static bool compile_all = false; |
Siva Annamalai | 12f8659 | 2016-09-23 15:20:39 -0700 | [diff] [blame] | 120 | static bool parse_all = false; |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 121 | |
Ryan Macnak | f262fad | 2016-04-25 16:19:34 -0700 | [diff] [blame] | 122 | // Global flag that is used to indicate that we want to use blobs/mmap instead |
| 123 | // of assembly/shared libraries for precompilation. |
| 124 | static bool use_blobs = false; |
| 125 | |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame^] | 126 | // Global flag is used to indicate that we want to obfuscate identifiers. |
| 127 | static bool obfuscate = false; |
| 128 | |
| 129 | // Value of the --save-obfuscation-map= flag. |
| 130 | static const char* obfuscation_map_filename = NULL; |
| 131 | |
iposva@google.com | d0763f9 | 2015-05-11 22:58:32 +0000 | [diff] [blame] | 132 | // Global flag that is used to indicate that we want to trace resolution of |
| 133 | // URIs and the loading of libraries, parts and scripts. |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 134 | static bool trace_loading = false; |
asiva@google.com | 6b8987e | 2013-03-14 09:47:46 +0000 | [diff] [blame] | 135 | |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 136 | static char* app_script_uri = NULL; |
Ryan Macnak | b46af1e | 2017-01-23 10:25:02 -0800 | [diff] [blame] | 137 | static const uint8_t* app_isolate_snapshot_data = NULL; |
| 138 | static const uint8_t* app_isolate_snapshot_instructions = NULL; |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 139 | |
Ryan Macnak | 4d65db8a | 2016-10-31 15:56:25 -0700 | [diff] [blame] | 140 | static Dart_Isolate main_isolate = NULL; |
| 141 | |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 142 | static const char* DEFAULT_VM_SERVICE_SERVER_IP = "localhost"; |
johnmccutchan@google.com | 164a3500 | 2014-06-17 14:26:38 +0000 | [diff] [blame] | 143 | static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 144 | // VM Service options. |
johnmccutchan@google.com | 164a3500 | 2014-06-17 14:26:38 +0000 | [diff] [blame] | 145 | static const char* vm_service_server_ip = DEFAULT_VM_SERVICE_SERVER_IP; |
| 146 | // The 0 port is a magic value which results in the first available port |
| 147 | // being allocated. |
johnmccutchan@google.com | 435c6ad | 2014-09-10 22:38:33 +0000 | [diff] [blame] | 148 | static int vm_service_server_port = -1; |
John McCutchan | af85432 | 2016-07-07 12:14:54 -0700 | [diff] [blame] | 149 | // True when we are running in development mode and cross origin security |
| 150 | // checks are disabled. |
| 151 | static bool vm_service_dev_mode = false; |
iposva@google.com | c859d25 | 2015-02-05 14:30:24 +0000 | [diff] [blame] | 152 | |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 153 | // The environment provided through the command line using -D options. |
| 154 | static dart::HashMap* environment = NULL; |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 155 | |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 156 | static bool IsValidFlag(const char* name, |
| 157 | const char* prefix, |
| 158 | intptr_t prefix_length) { |
| 159 | intptr_t name_length = strlen(name); |
| 160 | return ((name_length > prefix_length) && |
| 161 | (strncmp(name, prefix, prefix_length) == 0)); |
| 162 | } |
| 163 | |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 164 | static bool version_option = false; |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 165 | static bool ProcessVersionOption(const char* arg, |
| 166 | CommandLineOptions* vm_options) { |
iposva@google.com | bc26800 | 2012-10-11 07:17:09 +0000 | [diff] [blame] | 167 | if (*arg != '\0') { |
| 168 | return false; |
| 169 | } |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 170 | version_option = true; |
iposva@google.com | bc26800 | 2012-10-11 07:17:09 +0000 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 174 | static bool help_option = false; |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 175 | static bool ProcessHelpOption(const char* arg, CommandLineOptions* vm_options) { |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 176 | if (*arg != '\0') { |
| 177 | return false; |
| 178 | } |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 179 | help_option = true; |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 183 | static bool verbose_option = false; |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 184 | static bool ProcessVerboseOption(const char* arg, |
| 185 | CommandLineOptions* vm_options) { |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 186 | if (*arg != '\0') { |
| 187 | return false; |
| 188 | } |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 189 | verbose_option = true; |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 193 | static bool ProcessPackageRootOption(const char* arg, |
| 194 | CommandLineOptions* vm_options) { |
mattsh@google.com | cb82991 | 2012-04-27 17:23:00 +0000 | [diff] [blame] | 195 | ASSERT(arg != NULL); |
Matthias Hausner | a71a6fc | 2016-06-20 15:41:54 -0700 | [diff] [blame] | 196 | if (*arg == '-') { |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 197 | return false; |
| 198 | } |
ajohnsen@google.com | 0491f4e | 2014-09-23 05:46:14 +0000 | [diff] [blame] | 199 | commandline_package_root = arg; |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 200 | return true; |
mattsh@google.com | cb82991 | 2012-04-27 17:23:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Ivan Posva | b07a9dd | 2015-07-29 11:30:33 -0700 | [diff] [blame] | 203 | static bool ProcessPackagesOption(const char* arg, |
Matthias Hausner | a71a6fc | 2016-06-20 15:41:54 -0700 | [diff] [blame] | 204 | CommandLineOptions* vm_options) { |
Ivan Posva | b07a9dd | 2015-07-29 11:30:33 -0700 | [diff] [blame] | 205 | ASSERT(arg != NULL); |
Matthias Hausner | a71a6fc | 2016-06-20 15:41:54 -0700 | [diff] [blame] | 206 | if (*arg == '-') { |
Ivan Posva | b07a9dd | 2015-07-29 11:30:33 -0700 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | commandline_packages_file = arg; |
| 210 | return true; |
| 211 | } |
| 212 | |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 213 | static bool ProcessSaveCompilationTraceOption(const char* arg, |
| 214 | CommandLineOptions* vm_options) { |
| 215 | ASSERT(arg != NULL); |
| 216 | if (*arg == '-') { |
| 217 | return false; |
| 218 | } |
| 219 | save_compilation_trace_filename = arg; |
| 220 | return true; |
| 221 | } |
| 222 | |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 223 | static bool ProcessLoadCompilationTraceOption(const char* arg, |
| 224 | CommandLineOptions* vm_options) { |
| 225 | ASSERT(arg != NULL); |
| 226 | if (*arg == '-') { |
| 227 | return false; |
| 228 | } |
| 229 | load_compilation_trace_filename = arg; |
| 230 | return true; |
| 231 | } |
| 232 | |
Ryan Macnak | 4607e38 | 2016-12-16 16:00:17 -0800 | [diff] [blame] | 233 | static bool ProcessSaveFeedbackOption(const char* arg, |
| 234 | CommandLineOptions* vm_options) { |
| 235 | ASSERT(arg != NULL); |
| 236 | if (*arg == '-') { |
| 237 | return false; |
| 238 | } |
| 239 | save_feedback_filename = arg; |
| 240 | return true; |
| 241 | } |
| 242 | |
Ryan Macnak | 4607e38 | 2016-12-16 16:00:17 -0800 | [diff] [blame] | 243 | static bool ProcessLoadFeedbackOption(const char* arg, |
| 244 | CommandLineOptions* vm_options) { |
| 245 | ASSERT(arg != NULL); |
| 246 | if (*arg == '-') { |
| 247 | return false; |
| 248 | } |
| 249 | load_feedback_filename = arg; |
| 250 | return true; |
| 251 | } |
| 252 | |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 253 | static void* GetHashmapKeyFromString(char* key) { |
| 254 | return reinterpret_cast<void*>(key); |
| 255 | } |
| 256 | |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 257 | static bool ExtractPortAndAddress(const char* option_value, |
| 258 | int* out_port, |
| 259 | const char** out_ip, |
| 260 | int default_port, |
| 261 | const char* default_ip) { |
kustermann@google.com | ebd8317 | 2014-04-23 13:09:10 +0000 | [diff] [blame] | 262 | // [option_value] has to be one of the following formats: |
| 263 | // - "" |
| 264 | // - ":8181" |
| 265 | // - "=8181" |
| 266 | // - ":8181/192.168.0.1" |
| 267 | // - "=8181/192.168.0.1" |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 268 | // - "=8181/::1" |
kustermann@google.com | ebd8317 | 2014-04-23 13:09:10 +0000 | [diff] [blame] | 269 | |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 270 | if (*option_value == '\0') { |
kustermann@google.com | ebd8317 | 2014-04-23 13:09:10 +0000 | [diff] [blame] | 271 | *out_ip = default_ip; |
| 272 | *out_port = default_port; |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | if ((*option_value != '=') && (*option_value != ':')) { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | int port = atoi(option_value + 1); |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 281 | const char* slash = strstr(option_value, "/"); |
kustermann@google.com | ebd8317 | 2014-04-23 13:09:10 +0000 | [diff] [blame] | 282 | if (slash == NULL) { |
| 283 | *out_ip = default_ip; |
| 284 | *out_port = port; |
| 285 | return true; |
| 286 | } |
| 287 | |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 288 | *out_ip = slash + 1; |
| 289 | *out_port = port; |
| 290 | return true; |
kustermann@google.com | ebd8317 | 2014-04-23 13:09:10 +0000 | [diff] [blame] | 291 | } |
| 292 | |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 293 | static bool ProcessEnvironmentOption(const char* arg, |
| 294 | CommandLineOptions* vm_options) { |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 295 | ASSERT(arg != NULL); |
| 296 | if (*arg == '\0') { |
| 297 | // Ignore empty -D option. |
Siva Annamalai | aae051e | 2016-10-12 18:42:30 -0700 | [diff] [blame] | 298 | Log::PrintErr("No arguments given to -D option, ignoring it\n"); |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 299 | return true; |
| 300 | } |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 301 | // Split the name=value part of the -Dname=value argument. |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 302 | const char* equals_pos = strchr(arg, '='); |
| 303 | if (equals_pos == NULL) { |
| 304 | // No equal sign (name without value) currently not supported. |
Siva Annamalai | aae051e | 2016-10-12 18:42:30 -0700 | [diff] [blame] | 305 | Log::PrintErr("No value given in -D%s option, ignoring it\n", arg); |
| 306 | return true; |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 307 | } |
Zachary Anderson | 82cb63e | 2016-08-08 12:53:26 -0700 | [diff] [blame] | 308 | |
| 309 | char* name; |
| 310 | char* value = NULL; |
| 311 | int name_len = equals_pos - arg; |
| 312 | if (name_len == 0) { |
Siva Annamalai | aae051e | 2016-10-12 18:42:30 -0700 | [diff] [blame] | 313 | Log::PrintErr("No name given in -D%s option, ignoring it\n", arg); |
| 314 | return true; |
Zachary Anderson | 82cb63e | 2016-08-08 12:53:26 -0700 | [diff] [blame] | 315 | } |
| 316 | // Split name=value into name and value. |
| 317 | name = reinterpret_cast<char*>(malloc(name_len + 1)); |
| 318 | strncpy(name, arg, name_len); |
| 319 | name[name_len] = '\0'; |
| 320 | value = strdup(equals_pos + 1); |
Siva Annamalai | aae051e | 2016-10-12 18:42:30 -0700 | [diff] [blame] | 321 | if (environment == NULL) { |
| 322 | environment = new HashMap(&HashMap::SameStringValue, 4); |
| 323 | } |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 324 | HashMap::Entry* entry = environment->Lookup(GetHashmapKeyFromString(name), |
| 325 | HashMap::StringHash(name), true); |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 326 | ASSERT(entry != NULL); // Lookup adds an entry if key not found. |
Zachary Anderson | 82cb63e | 2016-08-08 12:53:26 -0700 | [diff] [blame] | 327 | if (entry->value != NULL) { |
| 328 | free(name); |
| 329 | free(entry->value); |
| 330 | } |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 331 | entry->value = value; |
| 332 | return true; |
| 333 | } |
| 334 | |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 335 | static bool ProcessCompileAllOption(const char* arg, |
| 336 | CommandLineOptions* vm_options) { |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 337 | ASSERT(arg != NULL); |
| 338 | if (*arg != '\0') { |
| 339 | return false; |
| 340 | } |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 341 | compile_all = true; |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 342 | return true; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Siva Annamalai | 12f8659 | 2016-09-23 15:20:39 -0700 | [diff] [blame] | 345 | static bool ProcessParseAllOption(const char* arg, |
| 346 | CommandLineOptions* vm_options) { |
| 347 | ASSERT(arg != NULL); |
| 348 | if (*arg != '\0') { |
| 349 | return false; |
| 350 | } |
| 351 | parse_all = true; |
| 352 | return true; |
| 353 | } |
| 354 | |
Siva Annamalai | 286dd51 | 2017-05-25 15:40:36 -0700 | [diff] [blame] | 355 | #if !defined(DART_PRECOMPILED_RUNTIME) |
Matthias Hausner | f5ab173 | 2016-12-07 09:11:16 -0800 | [diff] [blame] | 356 | static bool ProcessFrontendOption(const char* filename, |
| 357 | CommandLineOptions* vm_options) { |
| 358 | ASSERT(filename != NULL); |
| 359 | if (filename[0] == '\0') { |
| 360 | return false; |
| 361 | } |
Siva Annamalai | 286dd51 | 2017-05-25 15:40:36 -0700 | [diff] [blame] | 362 | dfe.set_frontend_filename(filename); |
Matthias Hausner | f5ab173 | 2016-12-07 09:11:16 -0800 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
Siva Chandra | bac83e0 | 2017-06-16 12:22:28 -0700 | [diff] [blame] | 366 | static bool ProcessKernelBinariesOption(const char* dirname, |
| 367 | CommandLineOptions* vm_options) { |
| 368 | ASSERT(dirname != NULL); |
| 369 | if (dirname[0] == '\0') { |
Kevin Millikin | 385f8fb | 2017-04-25 20:04:20 +0200 | [diff] [blame] | 370 | return false; |
| 371 | } |
Siva Chandra | bac83e0 | 2017-06-16 12:22:28 -0700 | [diff] [blame] | 372 | dfe.SetKernelBinaries(dirname); |
Kevin Millikin | 385f8fb | 2017-04-25 20:04:20 +0200 | [diff] [blame] | 373 | return true; |
| 374 | } |
Siva Annamalai | 286dd51 | 2017-05-25 15:40:36 -0700 | [diff] [blame] | 375 | #endif |
Kevin Millikin | 385f8fb | 2017-04-25 20:04:20 +0200 | [diff] [blame] | 376 | |
Ryan Macnak | f262fad | 2016-04-25 16:19:34 -0700 | [diff] [blame] | 377 | static bool ProcessUseBlobsOption(const char* arg, |
| 378 | CommandLineOptions* vm_options) { |
| 379 | ASSERT(arg != NULL); |
| 380 | if (*arg != '\0') { |
| 381 | return false; |
| 382 | } |
| 383 | use_blobs = true; |
| 384 | return true; |
| 385 | } |
| 386 | |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame^] | 387 | static bool ProcessObfuscateOption(const char* arg, |
| 388 | CommandLineOptions* vm_options) { |
| 389 | ASSERT(arg != NULL); |
| 390 | if (*arg != '\0') { |
| 391 | return false; |
| 392 | } |
| 393 | obfuscate = true; |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | static bool ProcessObfuscationMapFilenameOption( |
| 398 | const char* filename, |
| 399 | CommandLineOptions* vm_options) { |
| 400 | obfuscation_map_filename = filename; |
| 401 | return true; |
| 402 | } |
| 403 | |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 404 | static bool ProcessSnapshotFilenameOption(const char* filename, |
| 405 | CommandLineOptions* vm_options) { |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 406 | snapshot_filename = filename; |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 407 | if (gen_snapshot_kind == kNone) { |
| 408 | gen_snapshot_kind = kScript; // Default behavior. |
asiva@google.com | 2917199 | 2012-09-04 20:46:30 +0000 | [diff] [blame] | 409 | } |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 410 | return true; |
asiva@google.com | 2917199 | 2012-09-04 20:46:30 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 413 | static bool ProcessSnapshotKindOption(const char* kind, |
| 414 | CommandLineOptions* vm_options) { |
| 415 | if (strcmp(kind, "script") == 0) { |
| 416 | gen_snapshot_kind = kScript; |
| 417 | return true; |
| 418 | } else if (strcmp(kind, "app-aot") == 0) { |
| 419 | gen_snapshot_kind = kAppAOT; |
| 420 | return true; |
Siva Annamalai | af4d246 | 2016-10-19 17:42:01 -0700 | [diff] [blame] | 421 | } else if (strcmp(kind, "app-jit") == 0) { |
| 422 | gen_snapshot_kind = kAppJIT; |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 423 | return true; |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 424 | } |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 425 | Log::PrintErr( |
| 426 | "Unrecognized snapshot kind: '%s'\nValid kinds are: " |
| 427 | "script, app-aot, app-jit\n", |
| 428 | kind); |
Siva Annamalai | 809c205 | 2016-02-22 15:33:34 -0800 | [diff] [blame] | 429 | return false; |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 430 | } |
| 431 | |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 432 | static bool ProcessSnapshotDepsFilenameOption(const char* filename, |
| 433 | CommandLineOptions* vm_options) { |
| 434 | snapshot_deps_filename = filename; |
| 435 | return true; |
| 436 | } |
| 437 | |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 438 | static bool ProcessEnableVmServiceOption(const char* option_value, |
| 439 | CommandLineOptions* vm_options) { |
kustermann@google.com | ebd8317 | 2014-04-23 13:09:10 +0000 | [diff] [blame] | 440 | ASSERT(option_value != NULL); |
| 441 | |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 442 | if (!ExtractPortAndAddress( |
| 443 | option_value, &vm_service_server_port, &vm_service_server_ip, |
| 444 | DEFAULT_VM_SERVICE_SERVER_PORT, DEFAULT_VM_SERVICE_SERVER_IP)) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 445 | Log::PrintErr( |
| 446 | "unrecognized --enable-vm-service option syntax. " |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 447 | "Use --enable-vm-service[=<port number>[/<bind address>]]\n"); |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 448 | return false; |
| 449 | } |
kustermann@google.com | ebd8317 | 2014-04-23 13:09:10 +0000 | [diff] [blame] | 450 | |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 451 | return true; |
| 452 | } |
| 453 | |
John McCutchan | af85432 | 2016-07-07 12:14:54 -0700 | [diff] [blame] | 454 | static bool ProcessDisableServiceOriginCheckOption( |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 455 | const char* option_value, |
| 456 | CommandLineOptions* vm_options) { |
John McCutchan | af85432 | 2016-07-07 12:14:54 -0700 | [diff] [blame] | 457 | ASSERT(option_value != NULL); |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 458 | Log::PrintErr( |
| 459 | "WARNING: You are running with the service protocol in an " |
| 460 | "insecure mode.\n"); |
John McCutchan | af85432 | 2016-07-07 12:14:54 -0700 | [diff] [blame] | 461 | vm_service_dev_mode = true; |
| 462 | return true; |
| 463 | } |
| 464 | |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 465 | static bool ProcessObserveOption(const char* option_value, |
| 466 | CommandLineOptions* vm_options) { |
| 467 | ASSERT(option_value != NULL); |
| 468 | |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 469 | if (!ExtractPortAndAddress( |
| 470 | option_value, &vm_service_server_port, &vm_service_server_ip, |
| 471 | DEFAULT_VM_SERVICE_SERVER_PORT, DEFAULT_VM_SERVICE_SERVER_IP)) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 472 | Log::PrintErr( |
| 473 | "unrecognized --observe option syntax. " |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 474 | "Use --observe[=<port number>[/<bind address>]]\n"); |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 475 | return false; |
| 476 | } |
johnmccutchan@google.com | 2acd1f8 | 2014-05-27 07:40:17 +0000 | [diff] [blame] | 477 | |
Todd Turnidge | bd3cdf4 | 2016-05-13 10:51:51 -0700 | [diff] [blame] | 478 | // These options should also be documented in the help message. |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 479 | vm_options->AddArgument("--pause-isolates-on-exit"); |
Todd Turnidge | 0f1d73f | 2016-01-29 12:19:33 -0800 | [diff] [blame] | 480 | vm_options->AddArgument("--pause-isolates-on-unhandled-exceptions"); |
Ryan Macnak | f8e47a9 | 2017-08-09 11:20:55 -0700 | [diff] [blame] | 481 | vm_options->AddArgument("--profiler"); |
Todd Turnidge | 0f1d73f | 2016-01-29 12:19:33 -0800 | [diff] [blame] | 482 | vm_options->AddArgument("--warn-on-pause-with-no-debugger"); |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 483 | return true; |
| 484 | } |
| 485 | |
iposva@google.com | d0763f9 | 2015-05-11 22:58:32 +0000 | [diff] [blame] | 486 | static bool ProcessTraceLoadingOption(const char* arg, |
| 487 | CommandLineOptions* vm_options) { |
| 488 | if (*arg != '\0') { |
| 489 | return false; |
| 490 | } |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 491 | trace_loading = true; |
iposva@google.com | d0763f9 | 2015-05-11 22:58:32 +0000 | [diff] [blame] | 492 | return true; |
| 493 | } |
| 494 | |
John McCutchan | 621ce4e | 2016-07-01 14:26:25 -0700 | [diff] [blame] | 495 | static bool ProcessHotReloadTestModeOption(const char* arg, |
| 496 | CommandLineOptions* vm_options) { |
| 497 | if (*arg != '\0') { |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | // Identity reload. |
| 502 | vm_options->AddArgument("--identity_reload"); |
| 503 | // Start reloading quickly. |
John McCutchan | d0ebc78 | 2016-07-25 13:34:04 -0700 | [diff] [blame] | 504 | vm_options->AddArgument("--reload_every=4"); |
John McCutchan | 621ce4e | 2016-07-01 14:26:25 -0700 | [diff] [blame] | 505 | // Reload from optimized and unoptimized code. |
| 506 | vm_options->AddArgument("--reload_every_optimized=false"); |
| 507 | // Reload less frequently as time goes on. |
| 508 | vm_options->AddArgument("--reload_every_back_off"); |
John McCutchan | ef71bc1 | 2016-07-11 12:29:13 -0700 | [diff] [blame] | 509 | // Ensure that every isolate has reloaded once before exiting. |
John McCutchan | 621ce4e | 2016-07-01 14:26:25 -0700 | [diff] [blame] | 510 | vm_options->AddArgument("--check_reloaded"); |
| 511 | |
| 512 | return true; |
| 513 | } |
| 514 | |
John McCutchan | 9c83d246 | 2016-08-01 10:57:22 -0700 | [diff] [blame] | 515 | static bool ProcessHotReloadRollbackTestModeOption( |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 516 | const char* arg, |
| 517 | CommandLineOptions* vm_options) { |
John McCutchan | 9c83d246 | 2016-08-01 10:57:22 -0700 | [diff] [blame] | 518 | // Identity reload. |
| 519 | vm_options->AddArgument("--identity_reload"); |
| 520 | // Start reloading quickly. |
| 521 | vm_options->AddArgument("--reload_every=4"); |
| 522 | // Reload from optimized and unoptimized code. |
| 523 | vm_options->AddArgument("--reload_every_optimized=false"); |
| 524 | // Reload less frequently as time goes on. |
| 525 | vm_options->AddArgument("--reload_every_back_off"); |
| 526 | // Ensure that every isolate has reloaded once before exiting. |
| 527 | vm_options->AddArgument("--check_reloaded"); |
| 528 | // Force all reloads to fail and execute the rollback code. |
| 529 | vm_options->AddArgument("--reload_force_rollback"); |
| 530 | |
| 531 | return true; |
| 532 | } |
| 533 | |
Florian Schneider | 021ebb20 | 2016-08-15 18:28:11 -0700 | [diff] [blame] | 534 | extern bool short_socket_read; |
| 535 | |
| 536 | extern bool short_socket_write; |
| 537 | |
| 538 | static bool ProcessShortSocketReadOption(const char* arg, |
| 539 | CommandLineOptions* vm_options) { |
| 540 | short_socket_read = true; |
| 541 | return true; |
| 542 | } |
| 543 | |
Florian Schneider | 021ebb20 | 2016-08-15 18:28:11 -0700 | [diff] [blame] | 544 | static bool ProcessShortSocketWriteOption(const char* arg, |
| 545 | CommandLineOptions* vm_options) { |
| 546 | short_socket_write = true; |
| 547 | return true; |
| 548 | } |
| 549 | |
Zachary Anderson | 139db22 | 2016-09-16 09:08:51 -0700 | [diff] [blame] | 550 | extern const char* commandline_root_certs_file; |
| 551 | extern const char* commandline_root_certs_cache; |
| 552 | |
| 553 | static bool ProcessRootCertsFileOption(const char* arg, |
| 554 | CommandLineOptions* vm_options) { |
| 555 | ASSERT(arg != NULL); |
| 556 | if (*arg == '-') { |
| 557 | return false; |
| 558 | } |
| 559 | if (commandline_root_certs_cache != NULL) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 560 | Log::PrintErr( |
| 561 | "Only one of --root-certs-file and --root-certs-cache " |
| 562 | "may be specified"); |
Zachary Anderson | 139db22 | 2016-09-16 09:08:51 -0700 | [diff] [blame] | 563 | return false; |
| 564 | } |
| 565 | commandline_root_certs_file = arg; |
| 566 | return true; |
| 567 | } |
| 568 | |
Zachary Anderson | 139db22 | 2016-09-16 09:08:51 -0700 | [diff] [blame] | 569 | static bool ProcessRootCertsCacheOption(const char* arg, |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 570 | CommandLineOptions* vm_options) { |
Zachary Anderson | 139db22 | 2016-09-16 09:08:51 -0700 | [diff] [blame] | 571 | ASSERT(arg != NULL); |
| 572 | if (*arg == '-') { |
| 573 | return false; |
| 574 | } |
| 575 | if (commandline_root_certs_file != NULL) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 576 | Log::PrintErr( |
| 577 | "Only one of --root-certs-file and --root-certs-cache " |
| 578 | "may be specified"); |
Zachary Anderson | 139db22 | 2016-09-16 09:08:51 -0700 | [diff] [blame] | 579 | return false; |
| 580 | } |
| 581 | commandline_root_certs_cache = arg; |
| 582 | return true; |
| 583 | } |
Zachary Anderson | 139db22 | 2016-09-16 09:08:51 -0700 | [diff] [blame] | 584 | |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 585 | static struct { |
| 586 | const char* option_name; |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 587 | bool (*process)(const char* option, CommandLineOptions* vm_options); |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 588 | } main_options[] = { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 589 | // Standard options shared with dart2js. |
| 590 | {"-D", ProcessEnvironmentOption}, |
| 591 | {"-h", ProcessHelpOption}, |
| 592 | {"--help", ProcessHelpOption}, |
| 593 | {"--packages=", ProcessPackagesOption}, |
| 594 | {"--package-root=", ProcessPackageRootOption}, |
| 595 | {"-v", ProcessVerboseOption}, |
| 596 | {"--verbose", ProcessVerboseOption}, |
| 597 | {"--version", ProcessVersionOption}, |
Zachary Anderson | 7093f29 | 2015-09-15 12:49:52 -0700 | [diff] [blame] | 598 | |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 599 | // VM specific options to the standalone dart program. |
| 600 | {"--compile_all", ProcessCompileAllOption}, |
| 601 | {"--parse_all", ProcessParseAllOption}, |
Siva Annamalai | 286dd51 | 2017-05-25 15:40:36 -0700 | [diff] [blame] | 602 | #if !defined(DART_PRECOMPILED_RUNTIME) |
Matthias Hausner | f5ab173 | 2016-12-07 09:11:16 -0800 | [diff] [blame] | 603 | {"--dfe=", ProcessFrontendOption}, |
Siva Chandra | bac83e0 | 2017-06-16 12:22:28 -0700 | [diff] [blame] | 604 | {"--kernel-binaries=", ProcessKernelBinariesOption}, |
Siva Annamalai | 286dd51 | 2017-05-25 15:40:36 -0700 | [diff] [blame] | 605 | #endif |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 606 | {"--enable-vm-service", ProcessEnableVmServiceOption}, |
| 607 | {"--disable-service-origin-check", ProcessDisableServiceOriginCheckOption}, |
| 608 | {"--observe", ProcessObserveOption}, |
| 609 | {"--snapshot=", ProcessSnapshotFilenameOption}, |
| 610 | {"--snapshot-kind=", ProcessSnapshotKindOption}, |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 611 | {"--snapshot-depfile=", ProcessSnapshotDepsFilenameOption}, |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 612 | {"--use-blobs", ProcessUseBlobsOption}, |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame^] | 613 | {"--obfuscate", ProcessObfuscateOption}, |
| 614 | {"--save-obfuscation-map=", ProcessObfuscationMapFilenameOption}, |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 615 | {"--save-compilation-trace=", ProcessSaveCompilationTraceOption}, |
| 616 | {"--load-compilation-trace=", ProcessLoadCompilationTraceOption}, |
Ryan Macnak | 4607e38 | 2016-12-16 16:00:17 -0800 | [diff] [blame] | 617 | {"--save-feedback=", ProcessSaveFeedbackOption}, |
| 618 | {"--load-feedback=", ProcessLoadFeedbackOption}, |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 619 | {"--trace-loading", ProcessTraceLoadingOption}, |
| 620 | {"--hot-reload-test-mode", ProcessHotReloadTestModeOption}, |
| 621 | {"--hot-reload-rollback-test-mode", ProcessHotReloadRollbackTestModeOption}, |
| 622 | {"--short_socket_read", ProcessShortSocketReadOption}, |
| 623 | {"--short_socket_write", ProcessShortSocketWriteOption}, |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 624 | {"--root-certs-file=", ProcessRootCertsFileOption}, |
| 625 | {"--root-certs-cache=", ProcessRootCertsCacheOption}, |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 626 | {NULL, NULL}}; |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 627 | |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 628 | static bool ProcessMainOptions(const char* option, |
| 629 | CommandLineOptions* vm_options) { |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 630 | int i = 0; |
| 631 | const char* name = main_options[0].option_name; |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 632 | int option_length = strlen(option); |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 633 | while (name != NULL) { |
| 634 | int length = strlen(name); |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 635 | if ((option_length >= length) && (strncmp(option, name, length) == 0)) { |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 636 | if (main_options[i].process(option + length, vm_options)) { |
ajohnsen@google.com | 8bd5848 | 2013-06-11 11:55:43 +0000 | [diff] [blame] | 637 | return true; |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 638 | } |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 639 | } |
| 640 | i += 1; |
| 641 | name = main_options[i].option_name; |
hausner@google.com | 9159389 | 2011-12-07 22:13:49 +0000 | [diff] [blame] | 642 | } |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 643 | return false; |
hausner@google.com | 9159389 | 2011-12-07 22:13:49 +0000 | [diff] [blame] | 644 | } |
| 645 | |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 646 | // Parse out the command line arguments. Returns -1 if the arguments |
| 647 | // are incorrect, 0 otherwise. |
| 648 | static int ParseArguments(int argc, |
| 649 | char** argv, |
| 650 | CommandLineOptions* vm_options, |
| 651 | char** script_name, |
asiva@google.com | c7f58ad | 2012-06-13 17:48:47 +0000 | [diff] [blame] | 652 | CommandLineOptions* dart_options, |
tball@google.com | b3cae06 | 2013-04-05 20:55:10 +0000 | [diff] [blame] | 653 | bool* print_flags_seen, |
| 654 | bool* verbose_debug_seen) { |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 655 | const char* kPrefix = "--"; |
| 656 | const intptr_t kPrefixLen = strlen(kPrefix); |
| 657 | |
sgjesse@google.com | a1158d6 | 2013-08-08 07:19:21 +0000 | [diff] [blame] | 658 | // Store the executable name. |
| 659 | Platform::SetExecutableName(argv[0]); |
ager@google.com | 16ee3e9 | 2012-05-07 14:27:05 +0000 | [diff] [blame] | 660 | |
| 661 | // Start the rest after the executable name. |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 662 | int i = 1; |
| 663 | |
| 664 | // Parse out the vm options. |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 665 | while (i < argc) { |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 666 | if (ProcessMainOptions(argv[i], vm_options)) { |
ager@google.com | 8a67791 | 2012-05-08 06:41:00 +0000 | [diff] [blame] | 667 | i++; |
hausner@google.com | 9159389 | 2011-12-07 22:13:49 +0000 | [diff] [blame] | 668 | } else { |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 669 | // Check if this flag is a potentially valid VM flag. |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 670 | const char* kChecked = "-c"; |
| 671 | const char* kPackageRoot = "-p"; |
| 672 | if (strncmp(argv[i], kPackageRoot, strlen(kPackageRoot)) == 0) { |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 673 | if (!ProcessPackageRootOption(argv[i] + strlen(kPackageRoot), |
| 674 | vm_options)) { |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 675 | i++; |
johnmccutchan@google.com | c57e2b0 | 2014-05-27 07:26:05 +0000 | [diff] [blame] | 676 | if ((argv[i] == NULL) || |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 677 | !ProcessPackageRootOption(argv[i], vm_options)) { |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 678 | Log::PrintErr("Invalid option specification : '%s'\n", argv[i - 1]); |
| 679 | i++; |
| 680 | break; |
| 681 | } |
| 682 | } |
asiva@google.com | 8573043 | 2014-06-19 21:00:50 +0000 | [diff] [blame] | 683 | i++; |
| 684 | continue; // '-p' is not a VM flag so don't add to vm options. |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 685 | } else if (strncmp(argv[i], kChecked, strlen(kChecked)) == 0) { |
| 686 | vm_options->AddArgument("--checked"); |
asiva@google.com | 8573043 | 2014-06-19 21:00:50 +0000 | [diff] [blame] | 687 | i++; |
| 688 | continue; // '-c' is not a VM flag so don't add to vm options. |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 689 | } else if (!IsValidFlag(argv[i], kPrefix, kPrefixLen)) { |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 690 | break; |
| 691 | } |
asiva@google.com | 8573043 | 2014-06-19 21:00:50 +0000 | [diff] [blame] | 692 | // The following two flags are processed by both the embedder and |
| 693 | // the VM. |
asiva@google.com | c7f58ad | 2012-06-13 17:48:47 +0000 | [diff] [blame] | 694 | const char* kPrintFlags1 = "--print-flags"; |
| 695 | const char* kPrintFlags2 = "--print_flags"; |
asiva@google.com | 8573043 | 2014-06-19 21:00:50 +0000 | [diff] [blame] | 696 | const char* kVerboseDebug1 = "--verbose_debug"; |
| 697 | const char* kVerboseDebug2 = "--verbose-debug"; |
asiva@google.com | c7f58ad | 2012-06-13 17:48:47 +0000 | [diff] [blame] | 698 | if ((strncmp(argv[i], kPrintFlags1, strlen(kPrintFlags1)) == 0) || |
| 699 | (strncmp(argv[i], kPrintFlags2, strlen(kPrintFlags2)) == 0)) { |
| 700 | *print_flags_seen = true; |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 701 | } else if ((strncmp(argv[i], kVerboseDebug1, strlen(kVerboseDebug1)) == |
| 702 | 0) || |
| 703 | (strncmp(argv[i], kVerboseDebug2, strlen(kVerboseDebug2)) == |
| 704 | 0)) { |
tball@google.com | b3cae06 | 2013-04-05 20:55:10 +0000 | [diff] [blame] | 705 | *verbose_debug_seen = true; |
| 706 | } |
hausner@google.com | 9159389 | 2011-12-07 22:13:49 +0000 | [diff] [blame] | 707 | vm_options->AddArgument(argv[i]); |
ager@google.com | 8a67791 | 2012-05-08 06:41:00 +0000 | [diff] [blame] | 708 | i++; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 709 | } |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 710 | } |
cshapiro@google.com | 772a399 | 2012-07-26 00:21:25 +0000 | [diff] [blame] | 711 | |
whesse@google.com | 9de122a | 2013-08-21 15:21:55 +0000 | [diff] [blame] | 712 | // The arguments to the VM are at positions 1 through i-1 in argv. |
| 713 | Platform::SetExecutableArguments(i, argv); |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 714 | |
| 715 | // Get the script name. |
| 716 | if (i < argc) { |
| 717 | *script_name = argv[i]; |
ager@google.com | 8a67791 | 2012-05-08 06:41:00 +0000 | [diff] [blame] | 718 | i++; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 719 | } else { |
| 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | // Parse out options to be passed to dart main. |
| 724 | while (i < argc) { |
| 725 | dart_options->AddArgument(argv[i]); |
ager@google.com | 8a67791 | 2012-05-08 06:41:00 +0000 | [diff] [blame] | 726 | i++; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Ivan Posva | b07a9dd | 2015-07-29 11:30:33 -0700 | [diff] [blame] | 729 | // Verify consistency of arguments. |
| 730 | if ((commandline_package_root != NULL) && |
| 731 | (commandline_packages_file != NULL)) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 732 | Log::PrintErr( |
| 733 | "Specifying both a packages directory and a packages " |
| 734 | "file is invalid.\n"); |
Ryan Macnak | 9328d72 | 2016-01-08 10:19:14 -0800 | [diff] [blame] | 735 | return -1; |
| 736 | } |
Matthias Hausner | a71a6fc | 2016-06-20 15:41:54 -0700 | [diff] [blame] | 737 | if ((commandline_package_root != NULL) && |
| 738 | (strlen(commandline_package_root) == 0)) { |
| 739 | Log::PrintErr("Empty package root specified.\n"); |
| 740 | return -1; |
| 741 | } |
| 742 | if ((commandline_packages_file != NULL) && |
| 743 | (strlen(commandline_packages_file) == 0)) { |
| 744 | Log::PrintErr("Empty package file name specified.\n"); |
| 745 | return -1; |
| 746 | } |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 747 | if (((gen_snapshot_kind != kNone) || (snapshot_deps_filename != NULL)) && |
| 748 | (snapshot_filename == NULL)) { |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 749 | Log::PrintErr("Generating a snapshot requires a filename (--snapshot).\n"); |
| 750 | return -1; |
| 751 | } |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 752 | if ((gen_snapshot_kind != kNone) && vm_run_app_snapshot) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 753 | Log::PrintErr( |
| 754 | "Specifying an option to generate a snapshot and" |
| 755 | " run using a snapshot is invalid.\n"); |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 756 | return -1; |
| 757 | } |
Ivan Posva | b07a9dd | 2015-07-29 11:30:33 -0700 | [diff] [blame] | 758 | |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | |
iposva@google.com | 736d034 | 2013-10-17 21:59:46 +0000 | [diff] [blame] | 762 | static Dart_Handle CreateRuntimeOptions(CommandLineOptions* options) { |
iposva@google.com | 97634fb | 2011-11-03 21:45:03 +0000 | [diff] [blame] | 763 | int options_count = options->count(); |
ager@google.com | 6816666 | 2011-11-04 12:25:53 +0000 | [diff] [blame] | 764 | Dart_Handle dart_arguments = Dart_NewList(options_count); |
turnidge@google.com | 8c64adf | 2011-11-11 19:31:04 +0000 | [diff] [blame] | 765 | if (Dart_IsError(dart_arguments)) { |
iposva@google.com | 97634fb | 2011-11-03 21:45:03 +0000 | [diff] [blame] | 766 | return dart_arguments; |
| 767 | } |
| 768 | for (int i = 0; i < options_count; i++) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 769 | Dart_Handle argument_value = DartUtils::NewString(options->GetArgument(i)); |
turnidge@google.com | 8c64adf | 2011-11-11 19:31:04 +0000 | [diff] [blame] | 770 | if (Dart_IsError(argument_value)) { |
iposva@google.com | 97634fb | 2011-11-03 21:45:03 +0000 | [diff] [blame] | 771 | return argument_value; |
| 772 | } |
ager@google.com | 6816666 | 2011-11-04 12:25:53 +0000 | [diff] [blame] | 773 | Dart_Handle result = Dart_ListSetAt(dart_arguments, i, argument_value); |
turnidge@google.com | 8c64adf | 2011-11-11 19:31:04 +0000 | [diff] [blame] | 774 | if (Dart_IsError(result)) { |
iposva@google.com | 97634fb | 2011-11-03 21:45:03 +0000 | [diff] [blame] | 775 | return result; |
| 776 | } |
| 777 | } |
iposva@google.com | 736d034 | 2013-10-17 21:59:46 +0000 | [diff] [blame] | 778 | return dart_arguments; |
| 779 | } |
| 780 | |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 781 | static Dart_Handle EnvironmentCallback(Dart_Handle name) { |
| 782 | uint8_t* utf8_array; |
| 783 | intptr_t utf8_len; |
| 784 | Dart_Handle result = Dart_Null(); |
| 785 | Dart_Handle handle = Dart_StringToUTF8(name, &utf8_array, &utf8_len); |
| 786 | if (Dart_IsError(handle)) { |
| 787 | handle = Dart_ThrowException( |
| 788 | DartUtils::NewDartArgumentError(Dart_GetError(handle))); |
| 789 | } else { |
| 790 | char* name_chars = reinterpret_cast<char*>(malloc(utf8_len + 1)); |
| 791 | memmove(name_chars, utf8_array, utf8_len); |
| 792 | name_chars[utf8_len] = '\0'; |
| 793 | const char* value = NULL; |
| 794 | if (environment != NULL) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 795 | HashMap::Entry* entry = |
| 796 | environment->Lookup(GetHashmapKeyFromString(name_chars), |
| 797 | HashMap::StringHash(name_chars), false); |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 798 | if (entry != NULL) { |
| 799 | value = reinterpret_cast<char*>(entry->value); |
| 800 | } |
| 801 | } |
| 802 | if (value != NULL) { |
| 803 | result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(value), |
| 804 | strlen(value)); |
| 805 | } |
| 806 | free(name_chars); |
| 807 | } |
| 808 | return result; |
| 809 | } |
| 810 | |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 811 | #define SAVE_ERROR_AND_EXIT(result) \ |
| 812 | *error = strdup(Dart_GetError(result)); \ |
| 813 | if (Dart_IsCompilationError(result)) { \ |
| 814 | *exit_code = kCompilationErrorExitCode; \ |
| 815 | } else if (Dart_IsApiError(result)) { \ |
| 816 | *exit_code = kApiErrorExitCode; \ |
| 817 | } else { \ |
| 818 | *exit_code = kErrorExitCode; \ |
| 819 | } \ |
| 820 | Dart_ExitScope(); \ |
| 821 | Dart_ShutdownIsolate(); \ |
| 822 | return NULL; |
| 823 | |
johnmccutchan@google.com | c76b6eb | 2014-01-14 22:34:43 +0000 | [diff] [blame] | 824 | #define CHECK_RESULT(result) \ |
| 825 | if (Dart_IsError(result)) { \ |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 826 | SAVE_ERROR_AND_EXIT(result); \ |
| 827 | } |
| 828 | |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 829 | #define CHECK_RESULT_CLEANUP(result, cleanup) \ |
| 830 | if (Dart_IsError(result)) { \ |
| 831 | delete (cleanup); \ |
| 832 | SAVE_ERROR_AND_EXIT(result); \ |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 833 | } |
johnmccutchan@google.com | c76b6eb | 2014-01-14 22:34:43 +0000 | [diff] [blame] | 834 | |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 835 | static void SnapshotOnExitHook(int64_t exit_code) { |
| 836 | if (Dart_CurrentIsolate() != main_isolate) { |
| 837 | Log::PrintErr( |
| 838 | "A snapshot was requested, but a secondary isolate " |
| 839 | "performed a hard exit (%" Pd64 ").\n", |
| 840 | exit_code); |
Ryan Macnak | 9eb216a | 2017-01-31 09:47:51 -0800 | [diff] [blame] | 841 | Platform::Exit(kErrorExitCode); |
| 842 | } |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 843 | if (exit_code == 0) { |
| 844 | Snapshot::GenerateAppJIT(snapshot_filename); |
Ryan Macnak | 9eb216a | 2017-01-31 09:47:51 -0800 | [diff] [blame] | 845 | } |
Ryan Macnak | 9eb216a | 2017-01-31 09:47:51 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 848 | static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate, |
| 849 | bool is_main_isolate, |
| 850 | const char* script_uri, |
| 851 | const char* package_root, |
| 852 | const char* packages_config, |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 853 | bool set_native_resolvers, |
| 854 | bool isolate_run_app_snapshot, |
| 855 | char** error, |
| 856 | int* exit_code) { |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 857 | Dart_EnterScope(); |
Siva Annamalai | 6656404 | 2017-06-07 13:42:17 -0700 | [diff] [blame] | 858 | IsolateData* isolate_data = |
| 859 | reinterpret_cast<IsolateData*>(Dart_IsolateData(isolate)); |
| 860 | void* kernel_program = isolate_data->kernel_program; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 861 | |
Vyacheslav Egorov | 23fd1a1 | 2016-11-16 13:56:20 +0100 | [diff] [blame] | 862 | // Set up the library tag handler for this isolate. |
| 863 | Dart_Handle result = Dart_SetLibraryTagHandler(Loader::LibraryTagHandler); |
| 864 | CHECK_RESULT(result); |
| 865 | |
Siva Annamalai | 33c50e9 | 2017-07-19 18:09:17 -0700 | [diff] [blame] | 866 | // Prepare builtin and other core libraries for use to resolve URIs. |
| 867 | // Set up various closures, e.g: printing, timers etc. |
| 868 | // Set up 'package root' for URI resolution. |
| 869 | result = DartUtils::PrepareForScriptLoading(false, trace_loading); |
| 870 | CHECK_RESULT(result); |
| 871 | |
Siva Annamalai | 53cf85a | 2017-07-27 10:06:52 -0700 | [diff] [blame] | 872 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 873 | if (dfe.kernel_file_specified()) { |
Siva Annamalai | 000bec4 | 2017-07-21 08:27:37 -0700 | [diff] [blame] | 874 | ASSERT(kernel_program != NULL); |
| 875 | result = Dart_LoadKernel(kernel_program); |
Jens Johansen | c7faf2d | 2017-08-14 10:38:11 +0200 | [diff] [blame] | 876 | isolate_data->kernel_program = NULL; // Dart_LoadKernel takes ownership. |
Siva Annamalai | 000bec4 | 2017-07-21 08:27:37 -0700 | [diff] [blame] | 877 | } else { |
| 878 | if (kernel_program != NULL) { |
| 879 | Dart_Handle uri = Dart_NewStringFromCString(script_uri); |
| 880 | CHECK_RESULT(uri); |
| 881 | Dart_Handle resolved_script_uri = DartUtils::ResolveScript(uri); |
| 882 | CHECK_RESULT(resolved_script_uri); |
| 883 | result = |
| 884 | Dart_LoadScript(uri, resolved_script_uri, |
| 885 | reinterpret_cast<Dart_Handle>(kernel_program), 0, 0); |
Jens Johansen | c7faf2d | 2017-08-14 10:38:11 +0200 | [diff] [blame] | 886 | isolate_data->kernel_program = NULL; // Dart_LoadScript takes ownership. |
Siva Annamalai | 000bec4 | 2017-07-21 08:27:37 -0700 | [diff] [blame] | 887 | CHECK_RESULT(result); |
| 888 | } |
Vyacheslav Egorov | 23fd1a1 | 2016-11-16 13:56:20 +0100 | [diff] [blame] | 889 | } |
Siva Annamalai | 53cf85a | 2017-07-27 10:06:52 -0700 | [diff] [blame] | 890 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 891 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 892 | if (set_native_resolvers) { |
iposva@google.com | 67b02b4 | 2012-03-07 22:57:57 +0000 | [diff] [blame] | 893 | // Setup the native resolver as the snapshot does not carry it. |
asiva@google.com | 6a0ea0d | 2012-09-08 01:00:28 +0000 | [diff] [blame] | 894 | Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 895 | Builtin::SetNativeResolver(Builtin::kIOLibrary); |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 896 | } |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 897 | if (isolate_run_app_snapshot) { |
Ryan Macnak | 987165f | 2016-11-07 16:17:47 -0800 | [diff] [blame] | 898 | Dart_Handle result = Loader::ReloadNativeExtensions(); |
| 899 | CHECK_RESULT(result); |
| 900 | } |
iposva@google.com | 67b02b4 | 2012-03-07 22:57:57 +0000 | [diff] [blame] | 901 | |
Florian Schneider | bef4967 | 2017-01-11 16:55:55 -0800 | [diff] [blame] | 902 | // Set up the load port provided by the service isolate so that we can |
| 903 | // load scripts. |
| 904 | result = DartUtils::SetupServiceLoadPort(); |
| 905 | CHECK_RESULT(result); |
asiva@google.com | 2917199 | 2012-09-04 20:46:30 +0000 | [diff] [blame] | 906 | |
Siva Annamalai | 1e5eab3 | 2016-02-04 09:18:31 -0800 | [diff] [blame] | 907 | // Setup package root if specified. |
| 908 | result = DartUtils::SetupPackageRoot(package_root, packages_config); |
asiva@google.com | 5858afd | 2013-03-21 18:47:49 +0000 | [diff] [blame] | 909 | CHECK_RESULT(result); |
asiva@google.com | 2917199 | 2012-09-04 20:46:30 +0000 | [diff] [blame] | 910 | |
johnmccutchan@google.com | 172baa0 | 2015-01-29 18:31:27 +0000 | [diff] [blame] | 911 | result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
| 912 | CHECK_RESULT(result); |
| 913 | |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 914 | if (isolate_run_app_snapshot) { |
Ryan Macnak | 257b3a8 | 2016-05-04 18:47:36 -0700 | [diff] [blame] | 915 | result = DartUtils::SetupIOLibrary(script_uri); |
| 916 | CHECK_RESULT(result); |
John McCutchan | bf4b3b3 | 2016-06-07 12:47:44 -0700 | [diff] [blame] | 917 | Loader::InitForSnapshot(script_uri); |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 918 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 919 | if (is_main_isolate) { |
| 920 | // Find the canonical uri of the app snapshot. We'll use this to decide if |
| 921 | // other isolates should use the app snapshot or the core snapshot. |
| 922 | const char* resolved_script_uri = NULL; |
| 923 | result = Dart_StringToCString( |
| 924 | DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)), |
| 925 | &resolved_script_uri); |
| 926 | CHECK_RESULT(result); |
| 927 | ASSERT(app_script_uri == NULL); |
| 928 | app_script_uri = strdup(resolved_script_uri); |
| 929 | } |
| 930 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
Ryan Macnak | 257b3a8 | 2016-05-04 18:47:36 -0700 | [diff] [blame] | 931 | } else { |
Siva Annamalai | 1e5eab3 | 2016-02-04 09:18:31 -0800 | [diff] [blame] | 932 | // Load the specified application script into the newly created isolate. |
John McCutchan | 6fd8fd7 | 2016-06-06 14:15:01 -0700 | [diff] [blame] | 933 | Dart_Handle uri = |
| 934 | DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)); |
| 935 | CHECK_RESULT(uri); |
Kevin Millikin | d3fa66a | 2017-04-26 00:41:38 +0200 | [diff] [blame] | 936 | if (kernel_program == NULL) { |
Vyacheslav Egorov | 23fd1a1 | 2016-11-16 13:56:20 +0100 | [diff] [blame] | 937 | result = Loader::LibraryTagHandler(Dart_kScriptTag, Dart_Null(), uri); |
| 938 | CHECK_RESULT(result); |
Vyacheslav Egorov | 43a0500 | 2017-01-30 20:52:59 +0100 | [diff] [blame] | 939 | } else { |
| 940 | // Various core-library parts will send requests to the Loader to resolve |
| 941 | // relative URIs and perform other related tasks. We need Loader to be |
| 942 | // initialized for this to work because loading from Kernel binary |
| 943 | // bypasses normal source code loading paths that initialize it. |
| 944 | Loader::InitForSnapshot(script_uri); |
Vyacheslav Egorov | 23fd1a1 | 2016-11-16 13:56:20 +0100 | [diff] [blame] | 945 | } |
johnmccutchan@google.com | c76b6eb | 2014-01-14 22:34:43 +0000 | [diff] [blame] | 946 | |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 947 | Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), |
| 948 | Dart_GetMainPortId(), Dart_Timeline_Event_Async_End, 0, |
| 949 | NULL, NULL); |
Ryan Macnak | 9764a9f | 2015-09-16 11:22:57 -0700 | [diff] [blame] | 950 | |
Ryan Macnak | 9764a9f | 2015-09-16 11:22:57 -0700 | [diff] [blame] | 951 | result = DartUtils::SetupIOLibrary(script_uri); |
| 952 | CHECK_RESULT(result); |
John McCutchan | 1ff7144 | 2015-08-18 15:18:02 -0700 | [diff] [blame] | 953 | } |
| 954 | |
iposva@google.com | 0d0fcbe | 2013-04-09 17:51:24 +0000 | [diff] [blame] | 955 | // Make the isolate runnable so that it is ready to handle messages. |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 956 | Dart_ExitScope(); |
iposva@google.com | 0d0fcbe | 2013-04-09 17:51:24 +0000 | [diff] [blame] | 957 | Dart_ExitIsolate(); |
| 958 | bool retval = Dart_IsolateMakeRunnable(isolate); |
| 959 | if (!retval) { |
| 960 | *error = strdup("Invalid isolate state - Unable to make it runnable"); |
| 961 | Dart_EnterIsolate(isolate); |
| 962 | Dart_ShutdownIsolate(); |
| 963 | return NULL; |
| 964 | } |
| 965 | |
iposva@google.com | 0d0fcbe | 2013-04-09 17:51:24 +0000 | [diff] [blame] | 966 | return isolate; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 969 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 970 | // Returns newly created Kernel Isolate on success, NULL on failure. |
| 971 | // For now we only support the kernel isolate coming up from an |
| 972 | // application snapshot or from sources which are compiled by the |
| 973 | // VM parser. |
| 974 | static Dart_Isolate CreateAndSetupKernelIsolate(const char* main, |
| 975 | const char* package_root, |
| 976 | const char* packages_config, |
| 977 | Dart_IsolateFlags* flags, |
| 978 | char** error, |
| 979 | int* exit_code) { |
| 980 | if (!dfe.UseDartFrontend()) { |
| 981 | *error = strdup("Kernel isolate not supported."); |
| 982 | return NULL; |
| 983 | } |
| 984 | const char* script_uri = dfe.frontend_filename(); |
| 985 | if (packages_config == NULL) { |
| 986 | packages_config = commandline_packages_file; |
| 987 | } |
| 988 | |
| 989 | // Kernel isolate uses an app snapshot or the core libraries snapshot. |
| 990 | bool isolate_run_app_snapshot = false; |
| 991 | const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data; |
| 992 | const uint8_t* isolate_snapshot_instructions = |
| 993 | core_isolate_snapshot_instructions; |
| 994 | AppSnapshot* app_snapshot = Snapshot::TryReadAppSnapshot(script_uri); |
| 995 | if (app_snapshot != NULL) { |
| 996 | isolate_run_app_snapshot = true; |
| 997 | const uint8_t* ignore_vm_snapshot_data; |
| 998 | const uint8_t* ignore_vm_snapshot_instructions; |
| 999 | app_snapshot->SetBuffers( |
| 1000 | &ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions, |
| 1001 | &isolate_snapshot_data, &isolate_snapshot_instructions); |
| 1002 | } |
| 1003 | |
| 1004 | IsolateData* isolate_data = |
| 1005 | new IsolateData(script_uri, package_root, packages_config, app_snapshot); |
| 1006 | Dart_Isolate isolate = Dart_CreateIsolate( |
| 1007 | script_uri, main, isolate_snapshot_data, isolate_snapshot_instructions, |
| 1008 | flags, isolate_data, error); |
| 1009 | if (isolate == NULL) { |
| 1010 | delete isolate_data; |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | |
Siva Annamalai | 53cf85a | 2017-07-27 10:06:52 -0700 | [diff] [blame] | 1014 | return IsolateSetupHelper(isolate, false, script_uri, package_root, |
Siva Annamalai | 6656404 | 2017-06-07 13:42:17 -0700 | [diff] [blame] | 1015 | packages_config, isolate_snapshot_data, |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1016 | isolate_run_app_snapshot, error, exit_code); |
| 1017 | } |
| 1018 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 1019 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1020 | // Returns newly created Service Isolate on success, NULL on failure. |
| 1021 | // For now we only support the service isolate coming up from sources |
| 1022 | // which are compiled by the VM parser. |
| 1023 | static Dart_Isolate CreateAndSetupServiceIsolate(const char* script_uri, |
| 1024 | const char* main, |
| 1025 | const char* package_root, |
| 1026 | const char* packages_config, |
| 1027 | Dart_IsolateFlags* flags, |
| 1028 | char** error, |
| 1029 | int* exit_code) { |
| 1030 | ASSERT(script_uri != NULL); |
| 1031 | |
| 1032 | #if defined(DART_PRECOMPILED_RUNTIME) |
| 1033 | // AOT: All isolates start from the app snapshot. |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1034 | bool skip_library_load = true; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1035 | const uint8_t* isolate_snapshot_data = app_isolate_snapshot_data; |
| 1036 | const uint8_t* isolate_snapshot_instructions = |
| 1037 | app_isolate_snapshot_instructions; |
| 1038 | #else |
| 1039 | // JIT: Service isolate uses the core libraries snapshot. |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1040 | bool skip_library_load = false; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1041 | const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data; |
| 1042 | const uint8_t* isolate_snapshot_instructions = |
| 1043 | core_isolate_snapshot_instructions; |
| 1044 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 1045 | |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1046 | Dart_Isolate isolate = NULL; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1047 | IsolateData* isolate_data = |
| 1048 | new IsolateData(script_uri, package_root, packages_config, NULL); |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1049 | #if defined(DART_PRECOMPILED_RUNTIME) |
| 1050 | isolate = Dart_CreateIsolate(script_uri, main, isolate_snapshot_data, |
| 1051 | isolate_snapshot_instructions, flags, |
| 1052 | isolate_data, error); |
| 1053 | #else |
| 1054 | if (dfe.UsePlatformBinary()) { |
| 1055 | isolate = Dart_CreateIsolateFromKernel( |
| 1056 | script_uri, NULL, dfe.kernel_platform(), flags, isolate_data, error); |
| 1057 | } else { |
| 1058 | isolate = Dart_CreateIsolate(script_uri, main, isolate_snapshot_data, |
| 1059 | isolate_snapshot_instructions, flags, |
| 1060 | isolate_data, error); |
| 1061 | } |
| 1062 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1063 | if (isolate == NULL) { |
| 1064 | delete isolate_data; |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | |
| 1068 | Dart_EnterScope(); |
| 1069 | |
| 1070 | Dart_Handle result = Dart_SetLibraryTagHandler(Loader::LibraryTagHandler); |
| 1071 | CHECK_RESULT(result); |
| 1072 | |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1073 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 1074 | if (dfe.UsePlatformBinary()) { |
Alexander Markov | d89b06a | 2017-08-16 12:44:41 -0700 | [diff] [blame] | 1075 | // Read vmservice_io kernel file independently of main thread |
| 1076 | // as Dart_LoadKernel takes ownership. |
| 1077 | void* kernel_vmservice_io = dfe.ReadVMServiceIO(); |
| 1078 | if (kernel_vmservice_io == NULL) { |
| 1079 | Log::PrintErr("Could not read dart:vmservice_io binary file."); |
| 1080 | Platform::Exit(kErrorExitCode); |
| 1081 | } |
| 1082 | // Dart_LoadKernel takes ownership. |
| 1083 | Dart_Handle library = Dart_LoadKernel(kernel_vmservice_io); |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1084 | CHECK_RESULT_CLEANUP(library, isolate_data); |
| 1085 | skip_library_load = true; |
| 1086 | } |
| 1087 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 1088 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1089 | // Load embedder specific bits and return. |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1090 | if (!VmService::Setup(vm_service_server_ip, vm_service_server_port, |
| 1091 | skip_library_load, vm_service_dev_mode, |
| 1092 | trace_loading)) { |
| 1093 | *error = strdup(VmService::GetErrorMessage()); |
| 1094 | return NULL; |
| 1095 | } |
| 1096 | if (compile_all) { |
| 1097 | result = Dart_CompileAll(); |
| 1098 | CHECK_RESULT(result); |
| 1099 | } |
| 1100 | result = Dart_SetEnvironmentCallback(EnvironmentCallback); |
| 1101 | CHECK_RESULT(result); |
| 1102 | Dart_ExitScope(); |
| 1103 | Dart_ExitIsolate(); |
| 1104 | return isolate; |
| 1105 | } |
| 1106 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1107 | // Returns newly created Isolate on success, NULL on failure. |
| 1108 | static Dart_Isolate CreateIsolateAndSetupHelper(bool is_main_isolate, |
| 1109 | const char* script_uri, |
| 1110 | const char* main, |
| 1111 | const char* package_root, |
| 1112 | const char* packages_config, |
| 1113 | Dart_IsolateFlags* flags, |
| 1114 | char** error, |
| 1115 | int* exit_code) { |
| 1116 | ASSERT(script_uri != NULL); |
| 1117 | void* kernel_platform = NULL; |
| 1118 | void* kernel_program = NULL; |
| 1119 | AppSnapshot* app_snapshot = NULL; |
| 1120 | |
Siva Annamalai | 6656404 | 2017-06-07 13:42:17 -0700 | [diff] [blame] | 1121 | IsolateData* isolate_data = |
| 1122 | new IsolateData(script_uri, package_root, packages_config, app_snapshot); |
| 1123 | if (is_main_isolate && (snapshot_deps_filename != NULL)) { |
| 1124 | isolate_data->set_dependencies(new MallocGrowableArray<char*>()); |
| 1125 | } |
| 1126 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1127 | #if defined(DART_PRECOMPILED_RUNTIME) |
| 1128 | // AOT: All isolates start from the app snapshot. |
| 1129 | bool isolate_run_app_snapshot = true; |
| 1130 | const uint8_t* isolate_snapshot_data = app_isolate_snapshot_data; |
| 1131 | const uint8_t* isolate_snapshot_instructions = |
| 1132 | app_isolate_snapshot_instructions; |
| 1133 | #else |
| 1134 | // JIT: Main isolate starts from the app snapshot, if any. Other isolates |
| 1135 | // use the core libraries snapshot. |
| 1136 | bool isolate_run_app_snapshot = false; |
| 1137 | const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data; |
| 1138 | const uint8_t* isolate_snapshot_instructions = |
| 1139 | core_isolate_snapshot_instructions; |
| 1140 | if ((app_isolate_snapshot_data != NULL) && |
| 1141 | (is_main_isolate || ((app_script_uri != NULL) && |
| 1142 | (strcmp(script_uri, app_script_uri) == 0)))) { |
| 1143 | isolate_run_app_snapshot = true; |
| 1144 | isolate_snapshot_data = app_isolate_snapshot_data; |
| 1145 | isolate_snapshot_instructions = app_isolate_snapshot_instructions; |
| 1146 | } else if (!is_main_isolate) { |
| 1147 | app_snapshot = Snapshot::TryReadAppSnapshot(script_uri); |
| 1148 | if (app_snapshot != NULL) { |
| 1149 | isolate_run_app_snapshot = true; |
| 1150 | const uint8_t* ignore_vm_snapshot_data; |
| 1151 | const uint8_t* ignore_vm_snapshot_instructions; |
| 1152 | app_snapshot->SetBuffers( |
| 1153 | &ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions, |
| 1154 | &isolate_snapshot_data, &isolate_snapshot_instructions); |
| 1155 | } |
| 1156 | } |
Siva Annamalai | 6656404 | 2017-06-07 13:42:17 -0700 | [diff] [blame] | 1157 | if (!isolate_run_app_snapshot) { |
| 1158 | kernel_platform = dfe.kernel_platform(); |
| 1159 | kernel_program = dfe.ReadScript(script_uri); |
Siva Annamalai | 000bec4 | 2017-07-21 08:27:37 -0700 | [diff] [blame] | 1160 | if (kernel_program != NULL) { |
| 1161 | // A kernel file was specified on the command line instead of a source |
| 1162 | // file. Load that kernel file directly. |
Siva Annamalai | 53cf85a | 2017-07-27 10:06:52 -0700 | [diff] [blame] | 1163 | dfe.set_kernel_file_specified(true); |
Siva Annamalai | 000bec4 | 2017-07-21 08:27:37 -0700 | [diff] [blame] | 1164 | } else if (dfe.UseDartFrontend()) { |
Siva Annamalai | 6656404 | 2017-06-07 13:42:17 -0700 | [diff] [blame] | 1165 | kernel_program = dfe.CompileAndReadScript(script_uri, error, exit_code); |
| 1166 | if (kernel_program == NULL) { |
| 1167 | return NULL; |
| 1168 | } |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1169 | } |
Siva Annamalai | 6656404 | 2017-06-07 13:42:17 -0700 | [diff] [blame] | 1170 | isolate_data->kernel_program = kernel_program; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1171 | } |
| 1172 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 1173 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1174 | Dart_Isolate isolate = NULL; |
| 1175 | if (kernel_platform != NULL) { |
| 1176 | isolate = Dart_CreateIsolateFromKernel(script_uri, main, kernel_platform, |
| 1177 | flags, isolate_data, error); |
| 1178 | } else if (kernel_program != NULL) { |
| 1179 | isolate = Dart_CreateIsolateFromKernel(script_uri, main, kernel_program, |
| 1180 | flags, isolate_data, error); |
| 1181 | } else { |
| 1182 | isolate = Dart_CreateIsolate(script_uri, main, isolate_snapshot_data, |
| 1183 | isolate_snapshot_instructions, flags, |
| 1184 | isolate_data, error); |
| 1185 | } |
| 1186 | if (isolate == NULL) { |
| 1187 | delete isolate_data; |
| 1188 | return NULL; |
| 1189 | } |
| 1190 | |
Siva Annamalai | 6656404 | 2017-06-07 13:42:17 -0700 | [diff] [blame] | 1191 | bool set_native_resolvers = (kernel_program || isolate_snapshot_data); |
Siva Annamalai | 53cf85a | 2017-07-27 10:06:52 -0700 | [diff] [blame] | 1192 | return IsolateSetupHelper(isolate, is_main_isolate, script_uri, package_root, |
| 1193 | packages_config, set_native_resolvers, |
| 1194 | isolate_run_app_snapshot, error, exit_code); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
johnmccutchan@google.com | c76b6eb | 2014-01-14 22:34:43 +0000 | [diff] [blame] | 1197 | #undef CHECK_RESULT |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1198 | |
iposva@google.com | 0d0fcbe | 2013-04-09 17:51:24 +0000 | [diff] [blame] | 1199 | static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, |
| 1200 | const char* main, |
ajohnsen@google.com | 0491f4e | 2014-09-23 05:46:14 +0000 | [diff] [blame] | 1201 | const char* package_root, |
Ivan Posva | 6d066c7 | 2016-01-12 18:19:04 -0800 | [diff] [blame] | 1202 | const char* package_config, |
Ivan Posva | f5e3f94 | 2015-06-07 17:57:34 +0200 | [diff] [blame] | 1203 | Dart_IsolateFlags* flags, |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1204 | void* data, |
| 1205 | char** error) { |
Ivan Posva | f5e3f94 | 2015-06-07 17:57:34 +0200 | [diff] [blame] | 1206 | // The VM should never call the isolate helper with a NULL flags. |
| 1207 | ASSERT(flags != NULL); |
| 1208 | ASSERT(flags->version == DART_FLAGS_CURRENT_VERSION); |
Ivan Posva | 6d066c7 | 2016-01-12 18:19:04 -0800 | [diff] [blame] | 1209 | if ((package_root != NULL) && (package_config != NULL)) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1210 | *error = strdup( |
| 1211 | "Invalid arguments - Cannot simultaneously specify " |
| 1212 | "package root and package map."); |
Ivan Posva | 60eab65 | 2015-10-12 09:05:37 -0700 | [diff] [blame] | 1213 | return NULL; |
| 1214 | } |
Ivan Posva | 60eab65 | 2015-10-12 09:05:37 -0700 | [diff] [blame] | 1215 | |
| 1216 | int exit_code = 0; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 1217 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 1218 | if (strcmp(script_uri, DART_KERNEL_ISOLATE_NAME) == 0) { |
| 1219 | return CreateAndSetupKernelIsolate(main, package_root, package_config, |
| 1220 | flags, error, &exit_code); |
| 1221 | } |
| 1222 | #endif |
| 1223 | if (strcmp(script_uri, DART_VM_SERVICE_ISOLATE_NAME) == 0) { |
| 1224 | return CreateAndSetupServiceIsolate(script_uri, main, package_root, |
| 1225 | package_config, flags, error, |
| 1226 | &exit_code); |
| 1227 | } |
| 1228 | bool is_main_isolate = false; |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 1229 | return CreateIsolateAndSetupHelper(is_main_isolate, script_uri, main, |
| 1230 | package_root, package_config, flags, error, |
| 1231 | &exit_code); |
turnidge@google.com | 05b5d44 | 2012-05-23 18:32:51 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
iposva@google.com | bc26800 | 2012-10-11 07:17:09 +0000 | [diff] [blame] | 1234 | static void PrintVersion() { |
gram@google.com | 3601944 | 2012-11-19 17:38:15 +0000 | [diff] [blame] | 1235 | Log::PrintErr("Dart VM version: %s\n", Dart_VersionString()); |
iposva@google.com | bc26800 | 2012-10-11 07:17:09 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1238 | // clang-format off |
asiva@google.com | bde808f | 2011-12-20 02:04:24 +0000 | [diff] [blame] | 1239 | static void PrintUsage() { |
gram@google.com | 3601944 | 2012-11-19 17:38:15 +0000 | [diff] [blame] | 1240 | Log::PrintErr( |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 1241 | "Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]\n" |
| 1242 | "\n" |
| 1243 | "Executes the Dart script passed as <dart-script-file>.\n" |
| 1244 | "\n"); |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 1245 | if (!verbose_option) { |
gram@google.com | 3601944 | 2012-11-19 17:38:15 +0000 | [diff] [blame] | 1246 | Log::PrintErr( |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 1247 | "Common options:\n" |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 1248 | "--checked or -c\n" |
| 1249 | " Insert runtime type checks and enable assertions (checked mode).\n" |
| 1250 | "--help or -h\n" |
| 1251 | " Display this message (add -v or --verbose for information about\n" |
| 1252 | " all VM options).\n" |
| 1253 | "--package-root=<path> or -p<path>\n" |
| 1254 | " Where to find packages, that is, \"package:...\" imports.\n" |
Ivan Posva | b07a9dd | 2015-07-29 11:30:33 -0700 | [diff] [blame] | 1255 | "--packages=<path>\n" |
| 1256 | " Where to find a package spec file.\n" |
Ryan Macnak | ba88435 | 2015-08-04 15:14:48 -0700 | [diff] [blame] | 1257 | "--observe[=<port>[/<bind-address>]]\n" |
Todd Turnidge | bd3cdf4 | 2016-05-13 10:51:51 -0700 | [diff] [blame] | 1258 | " The observe flag is a convenience flag used to run a program with a\n" |
| 1259 | " set of options which are often useful for debugging under Observatory.\n" |
Todd Turnidge | a02995c | 2016-05-13 11:07:15 -0700 | [diff] [blame] | 1260 | " These options are currently:\n" |
Todd Turnidge | bd3cdf4 | 2016-05-13 10:51:51 -0700 | [diff] [blame] | 1261 | " --enable-vm-service[=<port>[/<bind-address>]]\n" |
| 1262 | " --pause-isolates-on-exit\n" |
| 1263 | " --pause-isolates-on-unhandled-exceptions\n" |
| 1264 | " --warn-on-pause-with-no-debugger\n" |
Todd Turnidge | a02995c | 2016-05-13 11:07:15 -0700 | [diff] [blame] | 1265 | " This set is subject to change.\n" |
Todd Turnidge | bd3cdf4 | 2016-05-13 10:51:51 -0700 | [diff] [blame] | 1266 | " Please see these options (--help --verbose) for further documentation.\n" |
kdaker | e353655 | 2017-02-18 17:06:51 -0800 | [diff] [blame] | 1267 | "--snapshot-kind=<snapshot_kind>\n" |
Siva Annamalai | 9c737d6 | 2016-10-20 13:54:35 -0700 | [diff] [blame] | 1268 | "--snapshot=<file_name>\n" |
| 1269 | " These snapshot options are used to generate a snapshot of the loaded\n" |
| 1270 | " Dart script:\n" |
| 1271 | " <snapshot-kind> controls the kind of snapshot, it could be\n" |
| 1272 | " script(default), app-aot or app-jit\n" |
| 1273 | " <file_name> specifies the file into which the snapshot is written\n" |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 1274 | "--version\n" |
| 1275 | " Print the VM version.\n"); |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 1276 | } else { |
gram@google.com | 3601944 | 2012-11-19 17:38:15 +0000 | [diff] [blame] | 1277 | Log::PrintErr( |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 1278 | "Supported options:\n" |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 1279 | "--checked or -c\n" |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 1280 | " Insert runtime type checks and enable assertions (checked mode).\n" |
asiva@google.com | a12382d | 2013-06-10 21:57:49 +0000 | [diff] [blame] | 1281 | "--help or -h\n" |
| 1282 | " Display this message (add -v or --verbose for information about\n" |
| 1283 | " all VM options).\n" |
| 1284 | "--package-root=<path> or -p<path>\n" |
| 1285 | " Where to find packages, that is, \"package:...\" imports.\n" |
Ryan Macnak | ba88435 | 2015-08-04 15:14:48 -0700 | [diff] [blame] | 1286 | "--packages=<path>\n" |
| 1287 | " Where to find a package spec file.\n" |
| 1288 | "--observe[=<port>[/<bind-address>]]\n" |
Todd Turnidge | bd3cdf4 | 2016-05-13 10:51:51 -0700 | [diff] [blame] | 1289 | " The observe flag is a convenience flag used to run a program with a\n" |
| 1290 | " set of options which are often useful for debugging under Observatory.\n" |
Todd Turnidge | a02995c | 2016-05-13 11:07:15 -0700 | [diff] [blame] | 1291 | " These options are currently:\n" |
Todd Turnidge | bd3cdf4 | 2016-05-13 10:51:51 -0700 | [diff] [blame] | 1292 | " --enable-vm-service[=<port>[/<bind-address>]]\n" |
| 1293 | " --pause-isolates-on-exit\n" |
| 1294 | " --pause-isolates-on-unhandled-exceptions\n" |
| 1295 | " --warn-on-pause-with-no-debugger\n" |
Todd Turnidge | a02995c | 2016-05-13 11:07:15 -0700 | [diff] [blame] | 1296 | " This set is subject to change.\n" |
Todd Turnidge | bd3cdf4 | 2016-05-13 10:51:51 -0700 | [diff] [blame] | 1297 | " Please see these options for further documentation.\n" |
kdaker | e353655 | 2017-02-18 17:06:51 -0800 | [diff] [blame] | 1298 | "--snapshot-kind=<snapshot_kind>\n" |
Siva Annamalai | 9c737d6 | 2016-10-20 13:54:35 -0700 | [diff] [blame] | 1299 | "--snapshot=<file_name>\n" |
| 1300 | " These snapshot options are used to generate a snapshot of the loaded\n" |
| 1301 | " Dart script:\n" |
| 1302 | " <snapshot-kind> controls the kind of snapshot, it could be\n" |
| 1303 | " script(default), app-aot or app-jit\n" |
| 1304 | " <file_name> specifies the file into which the snapshot is written\n" |
iposva@google.com | bc26800 | 2012-10-11 07:17:09 +0000 | [diff] [blame] | 1305 | "--version\n" |
| 1306 | " Print the VM version.\n" |
| 1307 | "\n" |
iposva@google.com | d0763f9 | 2015-05-11 22:58:32 +0000 | [diff] [blame] | 1308 | "--trace-loading\n" |
| 1309 | " enables tracing of library and script loading\n" |
asiva@google.com | 6b8987e | 2013-03-14 09:47:46 +0000 | [diff] [blame] | 1310 | "\n" |
Todd Turnidge | bd3cdf4 | 2016-05-13 10:51:51 -0700 | [diff] [blame] | 1311 | "--enable-vm-service[=<port>[/<bind-address>]]\n" |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 1312 | " enables the VM service and listens on specified port for connections\n" |
Ryan Macnak | 1b51889 | 2016-11-23 14:24:52 -0800 | [diff] [blame] | 1313 | " (default port number is 8181, default bind address is localhost).\n" |
Ryan Macnak | 8772849 | 2017-03-15 13:11:05 -0700 | [diff] [blame] | 1314 | #if !defined(HOST_OS_MACOS) |
Zachary Anderson | 139db22 | 2016-09-16 09:08:51 -0700 | [diff] [blame] | 1315 | "\n" |
| 1316 | "--root-certs-file=<path>\n" |
| 1317 | " The path to a file containing the trusted root certificates to use for\n" |
| 1318 | " secure socket connections.\n" |
| 1319 | "--root-certs-cache=<path>\n" |
| 1320 | " The path to a cache directory containing the trusted root certificates to\n" |
| 1321 | " use for secure socket connections.\n" |
Ryan Macnak | 8772849 | 2017-03-15 13:11:05 -0700 | [diff] [blame] | 1322 | #endif // !defined(HOST_OS_MACOS) |
iposva@google.com | d0763f9 | 2015-05-11 22:58:32 +0000 | [diff] [blame] | 1323 | "\n" |
iposva@google.com | d3f5b60 | 2012-09-24 22:26:37 +0000 | [diff] [blame] | 1324 | "The following options are only used for VM development and may\n" |
| 1325 | "be changed in any future version:\n"); |
| 1326 | const char* print_flags = "--print_flags"; |
| 1327 | Dart_SetVMFlags(1, &print_flags); |
| 1328 | } |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1329 | } |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1330 | // clang-format on |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1331 | |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1332 | char* BuildIsolateName(const char* script_name, const char* func_name) { |
turnidge@google.com | e62f34c | 2012-01-18 21:46:27 +0000 | [diff] [blame] | 1333 | // Skip past any slashes in the script name. |
| 1334 | const char* last_slash = strrchr(script_name, '/'); |
| 1335 | if (last_slash != NULL) { |
| 1336 | script_name = last_slash + 1; |
| 1337 | } |
| 1338 | |
| 1339 | const char* kFormat = "%s/%s"; |
| 1340 | intptr_t len = strlen(script_name) + strlen(func_name) + 2; |
| 1341 | char* buffer = new char[len]; |
| 1342 | ASSERT(buffer != NULL); |
| 1343 | snprintf(buffer, len, kFormat, script_name, func_name); |
| 1344 | return buffer; |
| 1345 | } |
| 1346 | |
Vyacheslav Egorov | a5e1f89 | 2017-02-28 21:10:04 +0100 | [diff] [blame] | 1347 | static void OnIsolateShutdown(void* callback_data) { |
| 1348 | IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
| 1349 | isolate_data->OnIsolateShutdown(); |
| 1350 | } |
| 1351 | |
Vyacheslav Egorov | a5e1f89 | 2017-02-28 21:10:04 +0100 | [diff] [blame] | 1352 | static void DeleteIsolateData(void* callback_data) { |
ager@google.com | f791a12 | 2012-06-29 14:40:17 +0000 | [diff] [blame] | 1353 | IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
ager@google.com | f791a12 | 2012-06-29 14:40:17 +0000 | [diff] [blame] | 1354 | delete isolate_data; |
| 1355 | } |
| 1356 | |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1357 | static const char* kStdoutStreamId = "Stdout"; |
| 1358 | static const char* kStderrStreamId = "Stderr"; |
| 1359 | |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1360 | static bool ServiceStreamListenCallback(const char* stream_id) { |
| 1361 | if (strcmp(stream_id, kStdoutStreamId) == 0) { |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 1362 | SetCaptureStdout(true); |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1363 | return true; |
| 1364 | } else if (strcmp(stream_id, kStderrStreamId) == 0) { |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 1365 | SetCaptureStderr(true); |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1366 | return true; |
| 1367 | } |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1371 | static void ServiceStreamCancelCallback(const char* stream_id) { |
| 1372 | if (strcmp(stream_id, kStdoutStreamId) == 0) { |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 1373 | SetCaptureStdout(false); |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1374 | } else if (strcmp(stream_id, kStderrStreamId) == 0) { |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 1375 | SetCaptureStderr(false); |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1376 | } |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Todd Turnidge | 1166f83 | 2016-07-29 11:23:18 -0700 | [diff] [blame] | 1379 | static bool FileModifiedCallback(const char* url, int64_t since) { |
| 1380 | if (strncmp(url, "file:///", 8) == 0) { |
| 1381 | // If it isn't a file on local disk, we don't know if it has been |
| 1382 | // modified. |
| 1383 | return true; |
| 1384 | } |
| 1385 | int64_t data[File::kStatSize]; |
| 1386 | File::Stat(url + 7, data); |
| 1387 | if (data[File::kType] == File::kDoesNotExist) { |
| 1388 | return true; |
| 1389 | } |
| 1390 | bool modified = data[File::kModifiedTime] > since; |
| 1391 | return modified; |
| 1392 | } |
| 1393 | |
Carlo Bernaschina | 0cbbeeb | 2017-08-14 17:02:22 -0700 | [diff] [blame] | 1394 | static void EmbedderInformationCallback(Dart_EmbedderInformation* info) { |
Carlo Bernaschina | 0cbbeeb | 2017-08-14 17:02:22 -0700 | [diff] [blame] | 1395 | info->version = DART_EMBEDDER_INFORMATION_CURRENT_VERSION; |
| 1396 | info->name = "Dart VM"; |
Carlo Bernaschina | 491390e | 2017-08-16 10:59:20 -0700 | [diff] [blame] | 1397 | Process::GetRSSInformation(&(info->max_rss), &(info->current_rss)); |
Carlo Bernaschina | 0cbbeeb | 2017-08-14 17:02:22 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Ryan Macnak | b46af1e | 2017-01-23 10:25:02 -0800 | [diff] [blame] | 1400 | static void GenerateAppAOTSnapshot() { |
Ryan Macnak | 257b3a8 | 2016-05-04 18:47:36 -0700 | [diff] [blame] | 1401 | if (use_blobs) { |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 1402 | Snapshot::GenerateAppAOTAsBlobs(snapshot_filename); |
Ryan Macnak | 257b3a8 | 2016-05-04 18:47:36 -0700 | [diff] [blame] | 1403 | } else { |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 1404 | Snapshot::GenerateAppAOTAsAssembly(snapshot_filename); |
Ryan Macnak | 257b3a8 | 2016-05-04 18:47:36 -0700 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1408 | #define CHECK_RESULT(result) \ |
| 1409 | if (Dart_IsError(result)) { \ |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1410 | const int exit_code = Dart_IsCompilationError(result) \ |
| 1411 | ? kCompilationErrorExitCode \ |
| 1412 | : kErrorExitCode; \ |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1413 | ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \ |
| 1414 | } |
| 1415 | |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 1416 | static void WriteFile(const char* filename, |
| 1417 | const uint8_t* buffer, |
| 1418 | const intptr_t size) { |
| 1419 | File* file = File::Open(filename, File::kWriteTruncate); |
| 1420 | if (file == NULL) { |
| 1421 | ErrorExit(kErrorExitCode, "Unable to open file %s\n", filename); |
Ryan Macnak | 4d65db8a | 2016-10-31 15:56:25 -0700 | [diff] [blame] | 1422 | } |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 1423 | if (!file->WriteFully(buffer, size)) { |
| 1424 | ErrorExit(kErrorExitCode, "Unable to write file %s\n", filename); |
Ryan Macnak | 5ee94eb | 2016-10-19 09:37:47 -0700 | [diff] [blame] | 1425 | } |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 1426 | file->Release(); |
Ryan Macnak | 5ee94eb | 2016-10-19 09:37:47 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 1429 | static void ReadFile(const char* filename, uint8_t** buffer, intptr_t* size) { |
| 1430 | File* file = File::Open(filename, File::kRead); |
| 1431 | if (file == NULL) { |
| 1432 | ErrorExit(kErrorExitCode, "Unable to open file %s\n", filename); |
| 1433 | } |
| 1434 | *size = file->Length(); |
| 1435 | *buffer = reinterpret_cast<uint8_t*>(malloc(*size)); |
| 1436 | if (!file->ReadFully(*buffer, *size)) { |
| 1437 | ErrorExit(kErrorExitCode, "Unable to read file %s\n", filename); |
| 1438 | } |
| 1439 | file->Release(); |
| 1440 | } |
| 1441 | |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame^] | 1442 | static Dart_QualifiedFunctionName standalone_entry_points[] = { |
| 1443 | // Functions. |
| 1444 | {"dart:_builtin", "::", "_getPrintClosure"}, |
| 1445 | {"dart:_builtin", "::", "_libraryFilePath"}, |
| 1446 | {"dart:_builtin", "::", "_resolveInWorkingDirectory"}, |
| 1447 | {"dart:_builtin", "::", "_setPackageRoot"}, |
| 1448 | {"dart:_builtin", "::", "_setPackagesMap"}, |
| 1449 | {"dart:_builtin", "::", "_setWorkingDirectory"}, |
| 1450 | {"dart:async", "::", "_setScheduleImmediateClosure"}, |
| 1451 | {"dart:io", "::", "_getWatchSignalInternal"}, |
| 1452 | {"dart:io", "::", "_getUriBaseClosure"}, |
| 1453 | {"dart:io", "::", "_makeDatagram"}, |
| 1454 | {"dart:io", "::", "_makeUint8ListView"}, |
| 1455 | {"dart:io", "::", "_setupHooks"}, |
| 1456 | {"dart:io", "CertificateException", "CertificateException."}, |
| 1457 | {"dart:io", "Directory", "Directory."}, |
| 1458 | {"dart:io", "File", "File."}, |
| 1459 | {"dart:io", "FileSystemException", "FileSystemException."}, |
| 1460 | {"dart:io", "HandshakeException", "HandshakeException."}, |
| 1461 | {"dart:io", "Link", "Link."}, |
| 1462 | {"dart:io", "OSError", "OSError."}, |
| 1463 | {"dart:io", "TlsException", "TlsException."}, |
| 1464 | {"dart:io", "X509Certificate", "X509Certificate._"}, |
| 1465 | {"dart:io", "_ExternalBuffer", "get:end"}, |
| 1466 | {"dart:io", "_ExternalBuffer", "get:start"}, |
| 1467 | {"dart:io", "_ExternalBuffer", "set:data"}, |
| 1468 | {"dart:io", "_ExternalBuffer", "set:end"}, |
| 1469 | {"dart:io", "_ExternalBuffer", "set:start"}, |
| 1470 | {"dart:io", "_Platform", "set:_nativeScript"}, |
| 1471 | {"dart:io", "_ProcessStartStatus", "set:_errorCode"}, |
| 1472 | {"dart:io", "_ProcessStartStatus", "set:_errorMessage"}, |
| 1473 | {"dart:io", "_SecureFilterImpl", "get:ENCRYPTED_SIZE"}, |
| 1474 | {"dart:io", "_SecureFilterImpl", "get:SIZE"}, |
| 1475 | {"dart:io", "_SecureFilterImpl", "get:buffers"}, |
| 1476 | {"dart:isolate", "::", "_getIsolateScheduleImmediateClosure"}, |
| 1477 | {"dart:isolate", "::", "_setupHooks"}, |
| 1478 | {"dart:isolate", "::", "_startMainIsolate"}, |
| 1479 | {"dart:vmservice_io", "::", "main"}, |
| 1480 | // Fields |
| 1481 | {"dart:_builtin", "::", "_isolateId"}, |
| 1482 | {"dart:_builtin", "::", "_loadPort"}, |
| 1483 | {"dart:_internal", "::", "_printClosure"}, |
| 1484 | {"dart:vmservice_io", "::", "_autoStart"}, |
| 1485 | {"dart:vmservice_io", "::", "_ip"}, |
| 1486 | {"dart:vmservice_io", "::", "_isFuchsia"}, |
| 1487 | {"dart:vmservice_io", "::", "_isWindows"}, |
| 1488 | {"dart:vmservice_io", "::", "_originCheckDisabled"}, |
| 1489 | {"dart:vmservice_io", "::", "_port"}, |
| 1490 | {"dart:vmservice_io", "::", "_signalWatch"}, |
| 1491 | {"dart:vmservice_io", "::", "_traceLoading"}, |
| 1492 | {NULL, NULL, NULL} // Must be terminated with NULL entries. |
| 1493 | }; |
| 1494 | |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1495 | bool RunMainIsolate(const char* script_name, CommandLineOptions* dart_options) { |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1496 | // Call CreateIsolateAndSetup which creates an isolate and loads up |
| 1497 | // the specified application script. |
| 1498 | char* error = NULL; |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 1499 | bool is_main_isolate = true; |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1500 | int exit_code = 0; |
| 1501 | char* isolate_name = BuildIsolateName(script_name, "main"); |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame^] | 1502 | Dart_IsolateFlags flags; |
| 1503 | Dart_IsolateFlagsInitialize(&flags); |
| 1504 | |
| 1505 | if (gen_snapshot_kind == kAppAOT) { |
| 1506 | flags.obfuscate = obfuscate; |
| 1507 | flags.entry_points = standalone_entry_points; |
| 1508 | } |
| 1509 | |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1510 | Dart_Isolate isolate = CreateIsolateAndSetupHelper( |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 1511 | is_main_isolate, script_name, "main", commandline_package_root, |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame^] | 1512 | commandline_packages_file, &flags, &error, &exit_code); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1513 | if (isolate == NULL) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1514 | delete[] isolate_name; |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1515 | Log::PrintErr("%s\n", error); |
| 1516 | free(error); |
| 1517 | error = NULL; |
| 1518 | Process::TerminateExitCodeHandler(); |
| 1519 | error = Dart_Cleanup(); |
| 1520 | if (error != NULL) { |
| 1521 | Log::PrintErr("VM cleanup failed: %s\n", error); |
| 1522 | free(error); |
| 1523 | } |
Zachary Anderson | 89dba57 | 2017-03-28 07:44:04 -0700 | [diff] [blame] | 1524 | Process::ClearAllSignalHandlers(); |
Florian Schneider | 63e3f4d | 2016-07-13 09:41:06 -0700 | [diff] [blame] | 1525 | EventHandler::Stop(); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1526 | Platform::Exit((exit_code != 0) ? exit_code : kErrorExitCode); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1527 | } |
Ryan Macnak | 4d65db8a | 2016-10-31 15:56:25 -0700 | [diff] [blame] | 1528 | main_isolate = isolate; |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1529 | delete[] isolate_name; |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1530 | |
| 1531 | Dart_EnterIsolate(isolate); |
| 1532 | ASSERT(isolate == Dart_CurrentIsolate()); |
| 1533 | ASSERT(isolate != NULL); |
| 1534 | Dart_Handle result; |
| 1535 | |
| 1536 | Dart_EnterScope(); |
| 1537 | |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 1538 | if (gen_snapshot_kind == kScript) { |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 1539 | Snapshot::GenerateScript(snapshot_filename); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1540 | } else { |
| 1541 | // Lookup the library of the root script. |
| 1542 | Dart_Handle root_lib = Dart_RootLibrary(); |
| 1543 | // Import the root library into the builtin library so that we can easily |
| 1544 | // lookup the main entry point exported from the root library. |
Siva Annamalai | 1e5eab3 | 2016-02-04 09:18:31 -0800 | [diff] [blame] | 1545 | IsolateData* isolate_data = |
| 1546 | reinterpret_cast<IsolateData*>(Dart_IsolateData(isolate)); |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1547 | result = Dart_LibraryImportLibrary(isolate_data->builtin_lib(), root_lib, |
| 1548 | Dart_Null()); |
Florian Schneider | bef4967 | 2017-01-11 16:55:55 -0800 | [diff] [blame] | 1549 | if ((gen_snapshot_kind == kAppAOT) || (gen_snapshot_kind == kAppJIT)) { |
Alexander Markov | d89b06a | 2017-08-16 12:44:41 -0700 | [diff] [blame] | 1550 | // Load the embedder's portion of the VM service's Dart code so it will |
| 1551 | // be included in the app snapshot. |
| 1552 | void* kernel_vmservice_io = NULL; |
| 1553 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 1554 | if (dfe.UsePlatformBinary()) { |
| 1555 | // Do not cache vmservice_io kernel file as |
| 1556 | // VmService::LoadForGenPrecompiled takes ownership. |
| 1557 | kernel_vmservice_io = dfe.ReadVMServiceIO(); |
| 1558 | if (kernel_vmservice_io == NULL) { |
| 1559 | Log::PrintErr("Could not read dart:vmservice_io binary file."); |
| 1560 | Platform::Exit(kErrorExitCode); |
| 1561 | } |
| 1562 | } |
Siva Chandra | a23178e | 2017-06-23 09:39:15 -0700 | [diff] [blame] | 1563 | #endif // defined(DART_PRECOMPILED_RUNTIME) |
Alexander Markov | d89b06a | 2017-08-16 12:44:41 -0700 | [diff] [blame] | 1564 | if (!VmService::LoadForGenPrecompiled(kernel_vmservice_io)) { |
Ryan Macnak | 5ee71ca | 2016-11-22 09:59:06 -0800 | [diff] [blame] | 1565 | Log::PrintErr("VM service loading failed: %s\n", |
| 1566 | VmService::GetErrorMessage()); |
Alexander Markov | d89b06a | 2017-08-16 12:44:41 -0700 | [diff] [blame] | 1567 | Platform::Exit(kErrorExitCode); |
Ryan Macnak | 37dd876 | 2015-10-29 15:15:46 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 1571 | if (compile_all) { |
Ryan Macnak | 9328d72 | 2016-01-08 10:19:14 -0800 | [diff] [blame] | 1572 | result = Dart_CompileAll(); |
| 1573 | CHECK_RESULT(result); |
| 1574 | } |
| 1575 | |
Siva Annamalai | 12f8659 | 2016-09-23 15:20:39 -0700 | [diff] [blame] | 1576 | if (parse_all) { |
| 1577 | result = Dart_ParseAll(); |
| 1578 | CHECK_RESULT(result); |
| 1579 | Dart_ExitScope(); |
| 1580 | // Shutdown the isolate. |
| 1581 | Dart_ShutdownIsolate(); |
| 1582 | return false; |
| 1583 | } |
| 1584 | |
Florian Schneider | bef4967 | 2017-01-11 16:55:55 -0800 | [diff] [blame] | 1585 | if (gen_snapshot_kind == kAppAOT) { |
Ryan Macnak | 4607e38 | 2016-12-16 16:00:17 -0800 | [diff] [blame] | 1586 | uint8_t* feedback_buffer = NULL; |
| 1587 | intptr_t feedback_length = 0; |
| 1588 | if (load_feedback_filename != NULL) { |
| 1589 | File* file = File::Open(load_feedback_filename, File::kRead); |
| 1590 | if (file == NULL) { |
| 1591 | ErrorExit(kErrorExitCode, "Failed to read JIT feedback.\n"); |
| 1592 | } |
| 1593 | feedback_length = file->Length(); |
| 1594 | feedback_buffer = reinterpret_cast<uint8_t*>(malloc(feedback_length)); |
| 1595 | if (!file->ReadFully(feedback_buffer, feedback_length)) { |
| 1596 | ErrorExit(kErrorExitCode, "Failed to read JIT feedback.\n"); |
| 1597 | } |
| 1598 | file->Release(); |
| 1599 | } |
| 1600 | |
Florian Schneider | bef4967 | 2017-01-11 16:55:55 -0800 | [diff] [blame] | 1601 | result = Dart_Precompile(standalone_entry_points, feedback_buffer, |
| 1602 | feedback_length); |
Ryan Macnak | 4607e38 | 2016-12-16 16:00:17 -0800 | [diff] [blame] | 1603 | if (feedback_buffer != NULL) { |
| 1604 | free(feedback_buffer); |
| 1605 | } |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1606 | CHECK_RESULT(result); |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame^] | 1607 | |
| 1608 | if (obfuscate && obfuscation_map_filename != NULL) { |
| 1609 | uint8_t* buffer = NULL; |
| 1610 | intptr_t size = 0; |
| 1611 | result = Dart_GetObfuscationMap(&buffer, &size); |
| 1612 | CHECK_RESULT(result); |
| 1613 | WriteFile(obfuscation_map_filename, buffer, size); |
| 1614 | } |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 1617 | if (gen_snapshot_kind == kAppAOT) { |
Ryan Macnak | b46af1e | 2017-01-23 10:25:02 -0800 | [diff] [blame] | 1618 | GenerateAppAOTSnapshot(); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1619 | } else { |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1620 | if (Dart_IsNull(root_lib)) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1621 | ErrorExit(kErrorExitCode, "Unable to find root library for '%s'\n", |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1622 | script_name); |
| 1623 | } |
| 1624 | |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 1625 | if (gen_snapshot_kind == kAppJIT) { |
| 1626 | result = Dart_SortClasses(); |
| 1627 | CHECK_RESULT(result); |
| 1628 | } |
| 1629 | |
| 1630 | if (load_compilation_trace_filename != NULL) { |
| 1631 | uint8_t* buffer = NULL; |
| 1632 | intptr_t size = 0; |
| 1633 | ReadFile(load_compilation_trace_filename, &buffer, &size); |
| 1634 | result = Dart_LoadCompilationTrace(buffer, size); |
| 1635 | CHECK_RESULT(result); |
| 1636 | } |
Erik Corry | c6a199e | 2017-02-20 12:14:17 +0100 | [diff] [blame] | 1637 | |
Siva Annamalai | 8b7af73 | 2017-06-15 10:40:43 -0700 | [diff] [blame] | 1638 | // Create a closure for the main entry point which is in the exported |
| 1639 | // namespace of the root library or invoke a getter of the same name |
| 1640 | // in the exported namespace and return the resulting closure. |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1641 | Dart_Handle main_closure = |
Siva Annamalai | 8b7af73 | 2017-06-15 10:40:43 -0700 | [diff] [blame] | 1642 | Dart_GetClosure(root_lib, Dart_NewStringFromCString("main")); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1643 | CHECK_RESULT(main_closure); |
Siva Annamalai | 8b7af73 | 2017-06-15 10:40:43 -0700 | [diff] [blame] | 1644 | if (!Dart_IsClosure(main_closure)) { |
| 1645 | ErrorExit(kErrorExitCode, |
| 1646 | "Unable to find 'main' in root library '%s'\n", script_name); |
| 1647 | } |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1648 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1649 | // Call _startIsolate in the isolate library to enable dispatching the |
| 1650 | // initial startup message. |
| 1651 | const intptr_t kNumIsolateArgs = 2; |
| 1652 | Dart_Handle isolate_args[kNumIsolateArgs]; |
| 1653 | isolate_args[0] = main_closure; // entryPoint |
| 1654 | isolate_args[1] = CreateRuntimeOptions(dart_options); // args |
| 1655 | |
| 1656 | Dart_Handle isolate_lib = |
| 1657 | Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate")); |
| 1658 | result = Dart_Invoke(isolate_lib, |
| 1659 | Dart_NewStringFromCString("_startMainIsolate"), |
| 1660 | kNumIsolateArgs, isolate_args); |
| 1661 | CHECK_RESULT(result); |
| 1662 | |
| 1663 | // Keep handling messages until the last active receive port is closed. |
| 1664 | result = Dart_RunLoop(); |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 1665 | // Generate an app snapshot after execution if specified. |
Siva Annamalai | 36a237c | 2016-10-19 17:48:01 -0700 | [diff] [blame] | 1666 | if (gen_snapshot_kind == kAppJIT) { |
John McCutchan | a4adbffb | 2017-03-20 08:47:39 -0700 | [diff] [blame] | 1667 | if (!Dart_IsCompilationError(result)) { |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 1668 | Snapshot::GenerateAppJIT(snapshot_filename); |
Siva Annamalai | 4f24d17 | 2016-02-12 10:48:43 -0800 | [diff] [blame] | 1669 | } |
Siva Annamalai | 040637a | 2015-10-16 17:10:30 -0700 | [diff] [blame] | 1670 | } |
Siva Annamalai | 4f24d17 | 2016-02-12 10:48:43 -0800 | [diff] [blame] | 1671 | CHECK_RESULT(result); |
Ryan Macnak | 4607e38 | 2016-12-16 16:00:17 -0800 | [diff] [blame] | 1672 | |
| 1673 | if (save_feedback_filename != NULL) { |
| 1674 | uint8_t* buffer = NULL; |
| 1675 | intptr_t size = 0; |
| 1676 | result = Dart_SaveJITFeedback(&buffer, &size); |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 1677 | CHECK_RESULT(result); |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 1678 | WriteFile(save_feedback_filename, buffer, size); |
Ryan Macnak | 4607e38 | 2016-12-16 16:00:17 -0800 | [diff] [blame] | 1679 | } |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 1680 | |
| 1681 | if (save_compilation_trace_filename != NULL) { |
| 1682 | uint8_t* buffer = NULL; |
| 1683 | intptr_t size = 0; |
| 1684 | result = Dart_SaveCompilationTrace(&buffer, &size); |
| 1685 | CHECK_RESULT(result); |
| 1686 | WriteFile(save_compilation_trace_filename, buffer, size); |
| 1687 | } |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 1691 | if (snapshot_deps_filename != NULL) { |
Ryan Macnak | 06080da | 2017-04-26 15:34:29 -0700 | [diff] [blame] | 1692 | Loader::ResolveDependenciesAsFilePaths(); |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 1693 | IsolateData* isolate_data = |
| 1694 | reinterpret_cast<IsolateData*>(Dart_IsolateData(isolate)); |
| 1695 | ASSERT(isolate_data != NULL); |
| 1696 | MallocGrowableArray<char*>* dependencies = isolate_data->dependencies(); |
| 1697 | ASSERT(dependencies != NULL); |
| 1698 | File* file = File::Open(snapshot_deps_filename, File::kWriteTruncate); |
| 1699 | if (file == NULL) { |
| 1700 | ErrorExit(kErrorExitCode, |
| 1701 | "Error: Unable to open snapshot depfile: %s\n\n", |
| 1702 | snapshot_deps_filename); |
| 1703 | } |
| 1704 | bool success = true; |
| 1705 | success &= file->Print("%s: ", snapshot_filename); |
| 1706 | for (intptr_t i = 0; i < dependencies->length(); i++) { |
| 1707 | char* dep = dependencies->At(i); |
| 1708 | success &= file->Print("%s ", dep); |
| 1709 | free(dep); |
| 1710 | } |
| 1711 | success &= file->Print("\n"); |
| 1712 | if (!success) { |
| 1713 | ErrorExit(kErrorExitCode, |
| 1714 | "Error: Unable to write snapshot depfile: %s\n\n", |
| 1715 | snapshot_deps_filename); |
| 1716 | } |
| 1717 | file->Release(); |
| 1718 | isolate_data->set_dependencies(NULL); |
| 1719 | delete dependencies; |
| 1720 | } |
| 1721 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1722 | Dart_ExitScope(); |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 1723 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1724 | // Shutdown the isolate. |
| 1725 | Dart_ShutdownIsolate(); |
| 1726 | |
| 1727 | // No restart. |
| 1728 | return false; |
| 1729 | } |
| 1730 | |
| 1731 | #undef CHECK_RESULT |
| 1732 | |
Ivan Posva | 9535aa6 | 2016-03-04 16:05:54 -0800 | [diff] [blame] | 1733 | // Observatory assets are only needed in the regular dart binary. |
Zachary Anderson | b0503ad | 2016-07-22 14:07:07 -0700 | [diff] [blame] | 1734 | #if !defined(DART_PRECOMPILER) && !defined(NO_OBSERVATORY) |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1735 | extern unsigned int observatory_assets_archive_len; |
John McCutchan | 33814bd | 2015-10-27 08:56:02 -0700 | [diff] [blame] | 1736 | extern const uint8_t* observatory_assets_archive; |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1737 | |
John McCutchan | cc0f297 | 2015-12-16 12:35:58 -0800 | [diff] [blame] | 1738 | |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1739 | Dart_Handle GetVMServiceAssetsArchiveCallback() { |
John McCutchan | cc0f297 | 2015-12-16 12:35:58 -0800 | [diff] [blame] | 1740 | uint8_t* decompressed = NULL; |
Ryan Macnak | 7dfe62f | 2017-08-11 12:18:16 -0700 | [diff] [blame] | 1741 | intptr_t decompressed_len = 0; |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1742 | Decompress(observatory_assets_archive, observatory_assets_archive_len, |
| 1743 | &decompressed, &decompressed_len); |
| 1744 | Dart_Handle tar_file = |
| 1745 | DartUtils::MakeUint8Array(decompressed, decompressed_len); |
John McCutchan | cc0f297 | 2015-12-16 12:35:58 -0800 | [diff] [blame] | 1746 | // Free decompressed memory as it has been copied into a Dart array. |
| 1747 | free(decompressed); |
| 1748 | return tar_file; |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1749 | } |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1750 | #else // !defined(DART_PRECOMPILER) |
Ivan Posva | 9535aa6 | 2016-03-04 16:05:54 -0800 | [diff] [blame] | 1751 | static Dart_GetVMServiceAssetsArchive GetVMServiceAssetsArchiveCallback = NULL; |
| 1752 | #endif // !defined(DART_PRECOMPILER) |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1753 | |
johnmccutchan@google.com | a80d328 | 2013-11-26 18:13:51 +0000 | [diff] [blame] | 1754 | void main(int argc, char** argv) { |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1755 | char* script_name; |
John McCutchan | 24a0988 | 2016-07-11 13:58:26 -0700 | [diff] [blame] | 1756 | const int EXTRA_VM_ARGUMENTS = 8; |
Srdjan Mitrovic | fc3cd69 | 2015-05-27 17:11:35 -0700 | [diff] [blame] | 1757 | CommandLineOptions vm_options(argc + EXTRA_VM_ARGUMENTS); |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1758 | CommandLineOptions dart_options(argc); |
asiva@google.com | c7f58ad | 2012-06-13 17:48:47 +0000 | [diff] [blame] | 1759 | bool print_flags_seen = false; |
tball@google.com | b3cae06 | 2013-04-05 20:55:10 +0000 | [diff] [blame] | 1760 | bool verbose_debug_seen = false; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1761 | |
sgjesse@google.com | d7c8c1f | 2011-11-23 14:49:18 +0000 | [diff] [blame] | 1762 | // Perform platform specific initialization. |
| 1763 | if (!Platform::Initialize()) { |
gram@google.com | 3601944 | 2012-11-19 17:38:15 +0000 | [diff] [blame] | 1764 | Log::PrintErr("Initialization failed\n"); |
sgjesse@google.com | d7c8c1f | 2011-11-23 14:49:18 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
ager@google.com | 28b4314 | 2012-11-13 15:03:23 +0000 | [diff] [blame] | 1767 | // On Windows, the argv strings are code page encoded and not |
| 1768 | // utf8. We need to convert them to utf8. |
Soren Gjesse | 0415d92 | 2015-06-24 10:36:05 +0200 | [diff] [blame] | 1769 | bool argv_converted = ShellUtils::GetUtf8Argv(argc, argv); |
ager@google.com | 28b4314 | 2012-11-13 15:03:23 +0000 | [diff] [blame] | 1770 | |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1771 | // Parse command line arguments. |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1772 | if (ParseArguments(argc, argv, &vm_options, &script_name, &dart_options, |
| 1773 | &print_flags_seen, &verbose_debug_seen) < 0) { |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 1774 | if (help_option) { |
iposva@google.com | bc26800 | 2012-10-11 07:17:09 +0000 | [diff] [blame] | 1775 | PrintUsage(); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1776 | Platform::Exit(0); |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 1777 | } else if (version_option) { |
iposva@google.com | bc26800 | 2012-10-11 07:17:09 +0000 | [diff] [blame] | 1778 | PrintVersion(); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1779 | Platform::Exit(0); |
iposva@google.com | bc26800 | 2012-10-11 07:17:09 +0000 | [diff] [blame] | 1780 | } else if (print_flags_seen) { |
asiva@google.com | c7f58ad | 2012-06-13 17:48:47 +0000 | [diff] [blame] | 1781 | // Will set the VM flags, print them out and then we exit as no |
| 1782 | // script was specified on the command line. |
| 1783 | Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1784 | Platform::Exit(0); |
asiva@google.com | c7f58ad | 2012-06-13 17:48:47 +0000 | [diff] [blame] | 1785 | } else { |
| 1786 | PrintUsage(); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1787 | Platform::Exit(kErrorExitCode); |
asiva@google.com | c7f58ad | 2012-06-13 17:48:47 +0000 | [diff] [blame] | 1788 | } |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
iposva@google.com | e57e60c | 2014-08-14 05:00:37 +0000 | [diff] [blame] | 1791 | Thread::InitOnce(); |
| 1792 | |
Florian Schneider | 63e3f4d | 2016-07-13 09:41:06 -0700 | [diff] [blame] | 1793 | Loader::InitOnce(); |
| 1794 | |
iposva@google.com | 35a8e25 | 2013-09-13 16:40:36 +0000 | [diff] [blame] | 1795 | if (!DartUtils::SetOriginalWorkingDirectory()) { |
| 1796 | OSError err; |
Ryan Macnak | 5ee71ca | 2016-11-22 09:59:06 -0800 | [diff] [blame] | 1797 | Log::PrintErr("Error determining current directory: %s\n", err.message()); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1798 | Platform::Exit(kErrorExitCode); |
iposva@google.com | 35a8e25 | 2013-09-13 16:40:36 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Siva Annamalai | 7aed6c0 | 2017-05-19 13:41:21 -0700 | [diff] [blame] | 1801 | AppSnapshot* app_snapshot = Snapshot::TryReadAppSnapshot(script_name); |
Ryan Macnak | a317b90 | 2017-02-16 13:54:57 -0800 | [diff] [blame] | 1802 | if (app_snapshot != NULL) { |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 1803 | vm_run_app_snapshot = true; |
Ryan Macnak | a317b90 | 2017-02-16 13:54:57 -0800 | [diff] [blame] | 1804 | app_snapshot->SetBuffers(&vm_snapshot_data, &vm_snapshot_instructions, |
| 1805 | &app_isolate_snapshot_data, |
| 1806 | &app_isolate_snapshot_instructions); |
Ryan Macnak | 3d00ecd | 2016-10-14 09:57:54 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 1809 | #if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME) |
| 1810 | // Constant true if PRODUCT or DART_PRECOMPILED_RUNTIME. |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 1811 | if ((gen_snapshot_kind != kNone) || vm_run_app_snapshot) { |
Srdjan Mitrovic | fc3cd69 | 2015-05-27 17:11:35 -0700 | [diff] [blame] | 1812 | vm_options.AddArgument("--load_deferred_eagerly"); |
| 1813 | } |
Florian Schneider | d867796 | 2016-02-25 07:53:39 -0800 | [diff] [blame] | 1814 | #endif |
Srdjan Mitrovic | fc3cd69 | 2015-05-27 17:11:35 -0700 | [diff] [blame] | 1815 | |
Siva Annamalai | af4d246 | 2016-10-19 17:42:01 -0700 | [diff] [blame] | 1816 | if (gen_snapshot_kind == kAppJIT) { |
Ryan Macnak | ad32e93 | 2016-10-07 10:34:57 -0700 | [diff] [blame] | 1817 | vm_options.AddArgument("--fields_may_be_reset"); |
Ryan Macnak | f555cf8 | 2016-11-16 10:02:02 -0800 | [diff] [blame] | 1818 | #if !defined(PRODUCT) |
Ryan Macnak | 13ba4b2 | 2016-11-16 09:10:22 -0800 | [diff] [blame] | 1819 | vm_options.AddArgument("--collect_code=false"); |
Ryan Macnak | f555cf8 | 2016-11-16 10:02:02 -0800 | [diff] [blame] | 1820 | #endif |
Ryan Macnak | ad32e93 | 2016-10-07 10:34:57 -0700 | [diff] [blame] | 1821 | } |
Florian Schneider | bef4967 | 2017-01-11 16:55:55 -0800 | [diff] [blame] | 1822 | if (gen_snapshot_kind == kAppAOT) { |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 1823 | vm_options.AddArgument("--precompilation"); |
| 1824 | } |
| 1825 | #if defined(DART_PRECOMPILED_RUNTIME) |
Florian Schneider | 23dd0b9 | 2016-02-04 16:33:37 -0800 | [diff] [blame] | 1826 | vm_options.AddArgument("--precompilation"); |
Florian Schneider | 0f48690 | 2016-02-02 17:51:15 -0800 | [diff] [blame] | 1827 | #endif |
Ryan Macnak | 4d65db8a | 2016-10-31 15:56:25 -0700 | [diff] [blame] | 1828 | if (gen_snapshot_kind == kAppJIT) { |
| 1829 | Process::SetExitHook(SnapshotOnExitHook); |
| 1830 | } |
Florian Schneider | 0f48690 | 2016-02-02 17:51:15 -0800 | [diff] [blame] | 1831 | |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1832 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 1833 | // If a kernel platform binary file is specified, read it. This |
| 1834 | // step will become redundant once we have the snapshot version |
| 1835 | // of the kernel core/platform libraries. |
| 1836 | if (dfe.UsePlatformBinary()) { |
Alexander Markov | d89b06a | 2017-08-16 12:44:41 -0700 | [diff] [blame] | 1837 | void* kernel_platform = dfe.ReadPlatform(); |
| 1838 | if (kernel_platform == NULL) { |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1839 | Log::PrintErr("The platform binary is not a valid Dart Kernel file."); |
| 1840 | Platform::Exit(kErrorExitCode); |
| 1841 | } |
Alexander Markov | d89b06a | 2017-08-16 12:44:41 -0700 | [diff] [blame] | 1842 | dfe.set_kernel_platform(kernel_platform); |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1843 | } |
| 1844 | #endif |
| 1845 | |
podivilov@google.com | c692646 | 2011-12-01 11:59:35 +0000 | [diff] [blame] | 1846 | Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
| 1847 | |
johnmccutchan@google.com | 172baa0 | 2015-01-29 18:31:27 +0000 | [diff] [blame] | 1848 | // Start event handler. |
Ryan Macnak | 2268380 | 2015-12-18 13:20:57 -0800 | [diff] [blame] | 1849 | TimerUtils::InitOnce(); |
johnmccutchan@google.com | 172baa0 | 2015-01-29 18:31:27 +0000 | [diff] [blame] | 1850 | EventHandler::Start(); |
| 1851 | |
johnmccutchan@google.com | 9b1f39b | 2015-02-12 21:22:34 +0000 | [diff] [blame] | 1852 | // Initialize the Dart VM. |
Adam Barth | fac99f3 | 2016-08-09 12:25:29 -0700 | [diff] [blame] | 1853 | Dart_InitializeParams init_params; |
| 1854 | memset(&init_params, 0, sizeof(init_params)); |
| 1855 | init_params.version = DART_INITIALIZE_PARAMS_CURRENT_VERSION; |
Ryan Macnak | b46af1e | 2017-01-23 10:25:02 -0800 | [diff] [blame] | 1856 | init_params.vm_snapshot_data = vm_snapshot_data; |
| 1857 | init_params.vm_snapshot_instructions = vm_snapshot_instructions; |
Adam Barth | fac99f3 | 2016-08-09 12:25:29 -0700 | [diff] [blame] | 1858 | init_params.create = CreateIsolateAndSetup; |
Vyacheslav Egorov | a5e1f89 | 2017-02-28 21:10:04 +0100 | [diff] [blame] | 1859 | init_params.shutdown = OnIsolateShutdown; |
| 1860 | init_params.cleanup = DeleteIsolateData; |
Adam Barth | fac99f3 | 2016-08-09 12:25:29 -0700 | [diff] [blame] | 1861 | init_params.file_open = DartUtils::OpenFile; |
| 1862 | init_params.file_read = DartUtils::ReadFile; |
| 1863 | init_params.file_write = DartUtils::WriteFile; |
| 1864 | init_params.file_close = DartUtils::CloseFile; |
| 1865 | init_params.entropy_source = DartUtils::EntropySource; |
| 1866 | init_params.get_service_assets = GetVMServiceAssetsArchiveCallback; |
Siva Annamalai | 73d3007 | 2017-08-11 12:38:22 -0700 | [diff] [blame] | 1867 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 1868 | init_params.start_kernel_isolate = dfe.UseDartFrontend(); |
| 1869 | #else |
| 1870 | init_params.start_kernel_isolate = false; |
| 1871 | #endif |
Adam Barth | fac99f3 | 2016-08-09 12:25:29 -0700 | [diff] [blame] | 1872 | |
| 1873 | char* error = Dart_Initialize(&init_params); |
Zachary Anderson | 7093f29 | 2015-09-15 12:49:52 -0700 | [diff] [blame] | 1874 | if (error != NULL) { |
Florian Schneider | 63e3f4d | 2016-07-13 09:41:06 -0700 | [diff] [blame] | 1875 | EventHandler::Stop(); |
Ryan Macnak | 5ee71ca | 2016-11-22 09:59:06 -0800 | [diff] [blame] | 1876 | Log::PrintErr("VM initialization failed: %s\n", error); |
Zachary Anderson | 7093f29 | 2015-09-15 12:49:52 -0700 | [diff] [blame] | 1877 | free(error); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1878 | Platform::Exit(kErrorExitCode); |
johnmccutchan@google.com | 9b1f39b | 2015-02-12 21:22:34 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1881 | Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, |
| 1882 | &ServiceStreamCancelCallback); |
Todd Turnidge | 1166f83 | 2016-07-29 11:23:18 -0700 | [diff] [blame] | 1883 | Dart_SetFileModifiedCallback(&FileModifiedCallback); |
Carlo Bernaschina | 0cbbeeb | 2017-08-14 17:02:22 -0700 | [diff] [blame] | 1884 | Dart_SetEmbedderInformationCallback(&EmbedderInformationCallback); |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 1885 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1886 | // Run the main isolate until we aren't told to restart. |
| 1887 | while (RunMainIsolate(script_name, &dart_options)) { |
| 1888 | Log::PrintErr("Restarting VM\n"); |
hausner@google.com | 9159389 | 2011-12-07 22:13:49 +0000 | [diff] [blame] | 1889 | } |
hausner@google.com | 95be461 | 2012-05-04 15:25:49 +0000 | [diff] [blame] | 1890 | |
ager@google.com | ed3338d | 2012-02-01 18:16:45 +0000 | [diff] [blame] | 1891 | // Terminate process exit-code handler. |
| 1892 | Process::TerminateExitCodeHandler(); |
ajohnsen@google.com | 1e2289e | 2013-07-17 07:26:05 +0000 | [diff] [blame] | 1893 | |
Zachary Anderson | 7093f29 | 2015-09-15 12:49:52 -0700 | [diff] [blame] | 1894 | error = Dart_Cleanup(); |
| 1895 | if (error != NULL) { |
| 1896 | Log::PrintErr("VM cleanup failed: %s\n", error); |
| 1897 | free(error); |
| 1898 | } |
Zachary Anderson | 89dba57 | 2017-03-28 07:44:04 -0700 | [diff] [blame] | 1899 | Process::ClearAllSignalHandlers(); |
Florian Schneider | 63e3f4d | 2016-07-13 09:41:06 -0700 | [diff] [blame] | 1900 | EventHandler::Stop(); |
Zachary Anderson | 9395a5b | 2015-09-01 17:53:03 -0700 | [diff] [blame] | 1901 | |
Ryan Macnak | a317b90 | 2017-02-16 13:54:57 -0800 | [diff] [blame] | 1902 | delete app_snapshot; |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 1903 | free(app_script_uri); |
| 1904 | |
ager@google.com | 28b4314 | 2012-11-13 15:03:23 +0000 | [diff] [blame] | 1905 | // Free copied argument strings if converted. |
| 1906 | if (argv_converted) { |
Zachary Anderson | c75a817 | 2016-03-14 11:08:52 -0700 | [diff] [blame] | 1907 | for (int i = 0; i < argc; i++) { |
| 1908 | free(argv[i]); |
| 1909 | } |
ager@google.com | 28b4314 | 2012-11-13 15:03:23 +0000 | [diff] [blame] | 1910 | } |
ager@google.com | 7031be2 | 2012-02-01 14:19:25 +0000 | [diff] [blame] | 1911 | |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 1912 | // Free environment if any. |
| 1913 | if (environment != NULL) { |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1914 | for (HashMap::Entry* p = environment->Start(); p != NULL; |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 1915 | p = environment->Next(p)) { |
| 1916 | free(p->key); |
| 1917 | free(p->value); |
| 1918 | } |
koda@google.com | 60831f3 | 2014-12-23 18:06:31 +0000 | [diff] [blame] | 1919 | delete environment; |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1922 | Platform::Exit(Process::GlobalExitCode()); |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1923 | } |
smok@google.com | ebbc202 | 2013-04-25 14:22:30 +0000 | [diff] [blame] | 1924 | |
| 1925 | } // namespace bin |
| 1926 | } // namespace dart |
| 1927 | |
| 1928 | int main(int argc, char** argv) { |
johnmccutchan@google.com | a80d328 | 2013-11-26 18:13:51 +0000 | [diff] [blame] | 1929 | dart::bin::main(argc, argv); |
| 1930 | UNREACHABLE(); |
smok@google.com | ebbc202 | 2013-04-25 14:22:30 +0000 | [diff] [blame] | 1931 | } |