OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
10 #include "include/dart_tools_api.h" | 10 #include "include/dart_tools_api.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 static const char* commandline_packages_file = NULL; | 66 static const char* commandline_packages_file = NULL; |
67 | 67 |
68 | 68 |
69 // Global flag that is used to indicate that we want to compile all the | 69 // Global flag that is used to indicate that we want to compile all the |
70 // dart functions and not run anything. | 70 // dart functions and not run anything. |
71 static bool has_compile_all = false; | 71 static bool has_compile_all = false; |
72 | 72 |
73 | 73 |
74 // Global flag that is used to indicate that we want to compile all the | 74 // Global flag that is used to indicate that we want to compile all the |
75 // dart functions before running main and not compile anything thereafter. | 75 // dart functions before running main and not compile anything thereafter. |
76 static bool has_precompile = false; | 76 static bool has_gen_precompiled_snapshot = false; |
| 77 |
| 78 |
| 79 // Global flag that is used to indicate that we want to run from a precompiled |
| 80 // snapshot. |
| 81 static bool has_run_precompiled_snapshot = false; |
| 82 |
| 83 |
| 84 extern const char* kPrecompiledLibraryName; |
| 85 extern const char* kPrecompiledSymbolName; |
| 86 static const char* kPrecompiledVmIsolateName = "precompiled.vmisolate"; |
| 87 static const char* kPrecompiledIsolateName = "precompiled.isolate"; |
| 88 static const char* kPrecompiledInstructionsName = "precompiled.instructions"; |
77 | 89 |
78 | 90 |
79 // Global flag that is used to indicate that we want to trace resolution of | 91 // Global flag that is used to indicate that we want to trace resolution of |
80 // URIs and the loading of libraries, parts and scripts. | 92 // URIs and the loading of libraries, parts and scripts. |
81 static bool has_trace_loading = false; | 93 static bool has_trace_loading = false; |
82 | 94 |
83 | 95 |
84 static const char* DEFAULT_VM_SERVICE_SERVER_IP = "127.0.0.1"; | 96 static const char* DEFAULT_VM_SERVICE_SERVER_IP = "127.0.0.1"; |
85 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; | 97 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; |
86 // VM Service options. | 98 // VM Service options. |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 CommandLineOptions* vm_options) { | 297 CommandLineOptions* vm_options) { |
286 ASSERT(arg != NULL); | 298 ASSERT(arg != NULL); |
287 if (*arg != '\0') { | 299 if (*arg != '\0') { |
288 return false; | 300 return false; |
289 } | 301 } |
290 has_compile_all = true; | 302 has_compile_all = true; |
291 return true; | 303 return true; |
292 } | 304 } |
293 | 305 |
294 | 306 |
295 static bool ProcessPrecompileOption(const char* arg, | 307 static bool ProcessGenPrecompiledSnapshotOption( |
296 CommandLineOptions* vm_options) { | 308 const char* arg, |
| 309 CommandLineOptions* vm_options) { |
297 ASSERT(arg != NULL); | 310 ASSERT(arg != NULL); |
298 if (*arg != '\0') { | 311 if (*arg != '\0') { |
299 return false; | 312 return false; |
300 } | 313 } |
301 has_precompile = true; | 314 // Ensure that we are not already running using a full snapshot. |
| 315 if (isolate_snapshot_buffer != NULL) { |
| 316 Log::PrintErr("Precompiled snapshots must be generated with" |
| 317 " dart_no_snapshot."); |
| 318 return false; |
| 319 } |
| 320 has_gen_precompiled_snapshot = true; |
302 vm_options->AddArgument("--precompile"); | 321 vm_options->AddArgument("--precompile"); |
303 return true; | 322 return true; |
304 } | 323 } |
| 324 |
| 325 |
| 326 static bool ProcessRunPrecompiledSnapshotOption( |
| 327 const char* arg, |
| 328 CommandLineOptions* vm_options) { |
| 329 ASSERT(arg != NULL); |
| 330 if (*arg != '\0') { |
| 331 return false; |
| 332 } |
| 333 has_run_precompiled_snapshot = true; |
| 334 vm_options->AddArgument("--precompile"); |
| 335 return true; |
| 336 } |
305 | 337 |
306 | 338 |
307 static bool ProcessDebugOption(const char* option_value, | 339 static bool ProcessDebugOption(const char* option_value, |
308 CommandLineOptions* vm_options) { | 340 CommandLineOptions* vm_options) { |
309 ASSERT(option_value != NULL); | 341 ASSERT(option_value != NULL); |
310 if (!ExtractPortAndIP(option_value, &debug_port, &debug_ip, | 342 if (!ExtractPortAndIP(option_value, &debug_port, &debug_ip, |
311 DEFAULT_DEBUG_PORT, DEFAULT_DEBUG_IP)) { | 343 DEFAULT_DEBUG_PORT, DEFAULT_DEBUG_IP)) { |
312 Log::PrintErr("unrecognized --debug option syntax. " | 344 Log::PrintErr("unrecognized --debug option syntax. " |
313 "Use --debug[:<port number>[/<IPv4 address>]]\n"); | 345 "Use --debug[:<port number>[/<IPv4 address>]]\n"); |
314 return false; | 346 return false; |
315 } | 347 } |
316 | 348 |
317 breakpoint_at = "main"; | 349 breakpoint_at = "main"; |
318 start_debugger = true; | 350 start_debugger = true; |
319 return true; | 351 return true; |
320 } | 352 } |
321 | 353 |
322 | 354 |
323 static bool ProcessGenScriptSnapshotOption(const char* filename, | 355 static bool ProcessGenScriptSnapshotOption(const char* filename, |
324 CommandLineOptions* vm_options) { | 356 CommandLineOptions* vm_options) { |
325 if (filename != NULL && strlen(filename) != 0) { | 357 if (filename != NULL && strlen(filename) != 0) { |
326 // Ensure that are already running using a full snapshot. | 358 // Ensure that we are already running using a full snapshot. |
327 if (isolate_snapshot_buffer == NULL) { | 359 if (isolate_snapshot_buffer == NULL) { |
328 Log::PrintErr("Script snapshots cannot be generated in this version of" | 360 Log::PrintErr("Script snapshots cannot be generated in this version of" |
329 " dart\n"); | 361 " dart\n"); |
330 return false; | 362 return false; |
331 } | 363 } |
332 snapshot_filename = filename; | 364 snapshot_filename = filename; |
333 generate_script_snapshot = true; | 365 generate_script_snapshot = true; |
334 return true; | 366 return true; |
335 } | 367 } |
336 return false; | 368 return false; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 { "--help", ProcessHelpOption }, | 439 { "--help", ProcessHelpOption }, |
408 { "-h", ProcessHelpOption }, | 440 { "-h", ProcessHelpOption }, |
409 { "--verbose", ProcessVerboseOption }, | 441 { "--verbose", ProcessVerboseOption }, |
410 { "-v", ProcessVerboseOption }, | 442 { "-v", ProcessVerboseOption }, |
411 { "--package-root=", ProcessPackageRootOption }, | 443 { "--package-root=", ProcessPackageRootOption }, |
412 { "--packages=", ProcessPackagesOption }, | 444 { "--packages=", ProcessPackagesOption }, |
413 { "-D", ProcessEnvironmentOption }, | 445 { "-D", ProcessEnvironmentOption }, |
414 // VM specific options to the standalone dart program. | 446 // VM specific options to the standalone dart program. |
415 { "--break-at=", ProcessBreakpointOption }, | 447 { "--break-at=", ProcessBreakpointOption }, |
416 { "--compile_all", ProcessCompileAllOption }, | 448 { "--compile_all", ProcessCompileAllOption }, |
417 { "--precompile", ProcessPrecompileOption }, | 449 { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption }, |
| 450 { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption }, |
418 { "--debug", ProcessDebugOption }, | 451 { "--debug", ProcessDebugOption }, |
419 { "--snapshot=", ProcessGenScriptSnapshotOption }, | 452 { "--snapshot=", ProcessGenScriptSnapshotOption }, |
420 { "--enable-vm-service", ProcessEnableVmServiceOption }, | 453 { "--enable-vm-service", ProcessEnableVmServiceOption }, |
421 { "--observe", ProcessObserveOption }, | 454 { "--observe", ProcessObserveOption }, |
422 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, | 455 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, |
423 { "--trace-loading", ProcessTraceLoadingOption}, | 456 { "--trace-loading", ProcessTraceLoadingOption}, |
424 { NULL, NULL } | 457 { NULL, NULL } |
425 }; | 458 }; |
426 | 459 |
427 | 460 |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 // Set up the library tag handler for this isolate. | 674 // Set up the library tag handler for this isolate. |
642 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 675 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
643 CHECK_RESULT(result); | 676 CHECK_RESULT(result); |
644 | 677 |
645 if (Dart_IsServiceIsolate(isolate)) { | 678 if (Dart_IsServiceIsolate(isolate)) { |
646 // If this is the service isolate, load embedder specific bits and return. | 679 // If this is the service isolate, load embedder specific bits and return. |
647 if (!VmService::Setup(vm_service_server_ip, vm_service_server_port)) { | 680 if (!VmService::Setup(vm_service_server_ip, vm_service_server_port)) { |
648 *error = strdup(VmService::GetErrorMessage()); | 681 *error = strdup(VmService::GetErrorMessage()); |
649 return NULL; | 682 return NULL; |
650 } | 683 } |
651 if (has_precompile) { | 684 if (has_gen_precompiled_snapshot) { |
652 result = Dart_Precompile(); | 685 result = Dart_Precompile(); |
653 CHECK_RESULT(result); | 686 CHECK_RESULT(result); |
654 } else if (has_compile_all) { | 687 } else if (has_compile_all) { |
655 result = Dart_CompileAll(); | 688 result = Dart_CompileAll(); |
656 CHECK_RESULT(result); | 689 CHECK_RESULT(result); |
657 } | 690 } |
658 Dart_ExitScope(); | 691 Dart_ExitScope(); |
659 Dart_ExitIsolate(); | 692 Dart_ExitIsolate(); |
660 return isolate; | 693 return isolate; |
661 } | 694 } |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 static void ServiceStreamCancelCallback(const char* stream_id) { | 988 static void ServiceStreamCancelCallback(const char* stream_id) { |
956 if (strcmp(stream_id, kStdoutStreamId) == 0) { | 989 if (strcmp(stream_id, kStdoutStreamId) == 0) { |
957 capture_stdout = false; | 990 capture_stdout = false; |
958 } else if (strcmp(stream_id, kStderrStreamId) == 0) { | 991 } else if (strcmp(stream_id, kStderrStreamId) == 0) { |
959 capture_stderr = false; | 992 capture_stderr = false; |
960 } | 993 } |
961 capture_stdio = (capture_stdout || capture_stderr); | 994 capture_stdio = (capture_stdout || capture_stderr); |
962 } | 995 } |
963 | 996 |
964 | 997 |
| 998 static void WriteSnapshotFile(const char* filename, |
| 999 const uint8_t* buffer, |
| 1000 const intptr_t size) { |
| 1001 File* file = File::Open(filename, File::kWriteTruncate); |
| 1002 ASSERT(file != NULL); |
| 1003 if (!file->WriteFully(buffer, size)) { |
| 1004 Log::PrintErr("Error: Failed to write snapshot file.\n\n"); |
| 1005 } |
| 1006 delete file; |
| 1007 } |
| 1008 |
| 1009 |
| 1010 static void ReadSnapshotFile(const char* filename, |
| 1011 const uint8_t** buffer) { |
| 1012 void* file = DartUtils::OpenFile(filename, false); |
| 1013 if (file == NULL) { |
| 1014 Log::PrintErr("Error: Failed to open '%s'.\n\n", filename); |
| 1015 exit(kErrorExitCode); |
| 1016 } |
| 1017 intptr_t len = -1; |
| 1018 DartUtils::ReadFile(buffer, &len, file); |
| 1019 if (*buffer == NULL || len == -1) { |
| 1020 Log::PrintErr("Error: Failed to read '%s'.\n\n", filename); |
| 1021 exit(kErrorExitCode); |
| 1022 } |
| 1023 DartUtils::CloseFile(file); |
| 1024 } |
| 1025 |
| 1026 |
| 1027 static void* LoadLibrarySymbol(const char* libname, const char* symname) { |
| 1028 void* library = Extensions::LoadExtensionLibrary(libname); |
| 1029 if (library == NULL) { |
| 1030 Log::PrintErr("Error: Failed to load library '%s'.\n\n", libname); |
| 1031 exit(kErrorExitCode); |
| 1032 } |
| 1033 void* symbol = Extensions::ResolveSymbol(library, symname); |
| 1034 if (symbol == NULL) { |
| 1035 Log::PrintErr("Failed to load symbol '%s'\n", symname); |
| 1036 exit(kErrorExitCode); |
| 1037 } |
| 1038 return symbol; |
| 1039 } |
| 1040 |
| 1041 |
965 void main(int argc, char** argv) { | 1042 void main(int argc, char** argv) { |
966 char* script_name; | 1043 char* script_name; |
967 const int EXTRA_VM_ARGUMENTS = 2; | 1044 const int EXTRA_VM_ARGUMENTS = 2; |
968 CommandLineOptions vm_options(argc + EXTRA_VM_ARGUMENTS); | 1045 CommandLineOptions vm_options(argc + EXTRA_VM_ARGUMENTS); |
969 CommandLineOptions dart_options(argc); | 1046 CommandLineOptions dart_options(argc); |
970 bool print_flags_seen = false; | 1047 bool print_flags_seen = false; |
971 bool verbose_debug_seen = false; | 1048 bool verbose_debug_seen = false; |
972 | 1049 |
973 vm_options.AddArgument("--no_write_protect_code"); | 1050 vm_options.AddArgument("--no_write_protect_code"); |
974 // Perform platform specific initialization. | 1051 // Perform platform specific initialization. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 ASSERT(debug_port >= 0); | 1105 ASSERT(debug_port >= 0); |
1029 bool print_msg = verbose_debug_seen || (debug_port == 0); | 1106 bool print_msg = verbose_debug_seen || (debug_port == 0); |
1030 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); | 1107 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); |
1031 if (print_msg) { | 1108 if (print_msg) { |
1032 Log::Print("Debugger listening on port %d\n", debug_port); | 1109 Log::Print("Debugger listening on port %d\n", debug_port); |
1033 } | 1110 } |
1034 } else { | 1111 } else { |
1035 DebuggerConnectionHandler::InitForVmService(); | 1112 DebuggerConnectionHandler::InitForVmService(); |
1036 } | 1113 } |
1037 | 1114 |
| 1115 const uint8_t* instructions_snapshot = NULL; |
| 1116 if (has_run_precompiled_snapshot) { |
| 1117 instructions_snapshot = reinterpret_cast<const uint8_t*>( |
| 1118 LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName)); |
| 1119 ReadSnapshotFile(kPrecompiledVmIsolateName, &vm_isolate_snapshot_buffer); |
| 1120 ReadSnapshotFile(kPrecompiledIsolateName, &isolate_snapshot_buffer); |
| 1121 } |
| 1122 |
1038 // Initialize the Dart VM. | 1123 // Initialize the Dart VM. |
1039 if (!Dart_Initialize(vm_isolate_snapshot_buffer, NULL, | 1124 if (!Dart_Initialize(vm_isolate_snapshot_buffer, instructions_snapshot, |
1040 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, | 1125 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, |
1041 DartUtils::OpenFile, | 1126 DartUtils::OpenFile, |
1042 DartUtils::ReadFile, | 1127 DartUtils::ReadFile, |
1043 DartUtils::WriteFile, | 1128 DartUtils::WriteFile, |
1044 DartUtils::CloseFile, | 1129 DartUtils::CloseFile, |
1045 DartUtils::EntropySource)) { | 1130 DartUtils::EntropySource)) { |
1046 fprintf(stderr, "%s", "VM initialization failed\n"); | 1131 fprintf(stderr, "%s", "VM initialization failed\n"); |
1047 fflush(stderr); | 1132 fflush(stderr); |
1048 DebuggerConnectionHandler::StopHandler(); | 1133 DebuggerConnectionHandler::StopHandler(); |
1049 // TODO(zra): Stop the EventHandler once thread shutdown is enabled. | 1134 // TODO(zra): Stop the EventHandler once thread shutdown is enabled. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1113 } else { | 1198 } else { |
1114 // Lookup the library of the root script. | 1199 // Lookup the library of the root script. |
1115 Dart_Handle root_lib = Dart_RootLibrary(); | 1200 Dart_Handle root_lib = Dart_RootLibrary(); |
1116 // Import the root library into the builtin library so that we can easily | 1201 // Import the root library into the builtin library so that we can easily |
1117 // lookup the main entry point exported from the root library. | 1202 // lookup the main entry point exported from the root library. |
1118 Dart_Handle builtin_lib = | 1203 Dart_Handle builtin_lib = |
1119 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 1204 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
1120 ASSERT(!Dart_IsError(builtin_lib)); | 1205 ASSERT(!Dart_IsError(builtin_lib)); |
1121 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null()); | 1206 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null()); |
1122 | 1207 |
1123 if (has_precompile) { | 1208 if (has_gen_precompiled_snapshot) { |
1124 result = Dart_Precompile(); | 1209 result = Dart_Precompile(); |
1125 DartExitOnError(result); | 1210 DartExitOnError(result); |
| 1211 |
| 1212 uint8_t* vm_isolate_buffer = NULL; |
| 1213 intptr_t vm_isolate_size = 0; |
| 1214 uint8_t* isolate_buffer = NULL; |
| 1215 intptr_t isolate_size = 0; |
| 1216 uint8_t* instructions_buffer = NULL; |
| 1217 intptr_t instructions_size = 0; |
| 1218 result = Dart_CreatePrecompiledSnapshot(&vm_isolate_buffer, |
| 1219 &vm_isolate_size, |
| 1220 &isolate_buffer, |
| 1221 &isolate_size, |
| 1222 &instructions_buffer, |
| 1223 &instructions_size); |
| 1224 DartExitOnError(result); |
| 1225 WriteSnapshotFile(kPrecompiledVmIsolateName, |
| 1226 vm_isolate_buffer, |
| 1227 vm_isolate_size); |
| 1228 WriteSnapshotFile(kPrecompiledIsolateName, |
| 1229 isolate_buffer, |
| 1230 isolate_size); |
| 1231 WriteSnapshotFile(kPrecompiledInstructionsName, |
| 1232 instructions_buffer, |
| 1233 instructions_size); |
1126 } else if (has_compile_all) { | 1234 } else if (has_compile_all) { |
1127 result = Dart_CompileAll(); | 1235 result = Dart_CompileAll(); |
1128 DartExitOnError(result); | 1236 DartExitOnError(result); |
1129 } | 1237 } |
1130 | 1238 |
1131 if (Dart_IsNull(root_lib)) { | 1239 if (Dart_IsNull(root_lib)) { |
1132 ErrorExit(kErrorExitCode, | 1240 ErrorExit(kErrorExitCode, |
1133 "Unable to find root library for '%s'\n", | 1241 "Unable to find root library for '%s'\n", |
1134 script_name); | 1242 script_name); |
1135 } | 1243 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1203 exit(Process::GlobalExitCode()); | 1311 exit(Process::GlobalExitCode()); |
1204 } | 1312 } |
1205 | 1313 |
1206 } // namespace bin | 1314 } // namespace bin |
1207 } // namespace dart | 1315 } // namespace dart |
1208 | 1316 |
1209 int main(int argc, char** argv) { | 1317 int main(int argc, char** argv) { |
1210 dart::bin::main(argc, argv); | 1318 dart::bin::main(argc, argv); |
1211 UNREACHABLE(); | 1319 UNREACHABLE(); |
1212 } | 1320 } |
OLD | NEW |