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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 static int vm_service_server_port = -1; | 103 static int vm_service_server_port = -1; |
104 | 104 |
105 | 105 |
106 // Exit code indicating an API error. | 106 // Exit code indicating an API error. |
107 static const int kApiErrorExitCode = 253; | 107 static const int kApiErrorExitCode = 253; |
108 // Exit code indicating a compilation error. | 108 // Exit code indicating a compilation error. |
109 static const int kCompilationErrorExitCode = 254; | 109 static const int kCompilationErrorExitCode = 254; |
110 // Exit code indicating an unhandled error that is not a compilation error. | 110 // Exit code indicating an unhandled error that is not a compilation error. |
111 static const int kErrorExitCode = 255; | 111 static const int kErrorExitCode = 255; |
112 | 112 |
113 extern bool do_vm_shutdown; // Defined in bin/process.cc | |
113 static void ErrorExit(int exit_code, const char* format, ...) { | 114 static void ErrorExit(int exit_code, const char* format, ...) { |
114 va_list arguments; | 115 va_list arguments; |
115 va_start(arguments, format); | 116 va_start(arguments, format); |
116 Log::VPrintErr(format, arguments); | 117 Log::VPrintErr(format, arguments); |
117 va_end(arguments); | 118 va_end(arguments); |
118 fflush(stderr); | 119 fflush(stderr); |
119 | 120 |
120 Dart_ExitScope(); | 121 Dart_ExitScope(); |
121 Dart_ShutdownIsolate(); | 122 Dart_ShutdownIsolate(); |
122 | 123 |
123 Dart_Cleanup(); | 124 // Terminate process exit-code handler. |
125 Process::TerminateExitCodeHandler(); | |
124 | 126 |
125 DebuggerConnectionHandler::StopHandler(); | 127 char* error = Dart_Cleanup(); |
126 // TODO(zra): Stop the EventHandler once thread shutdown is enabled. | 128 if (error != NULL) { |
127 // EventHandler::Stop(); | 129 Log::PrintErr("VM cleanup failed: %s\n", error); |
130 free(error); | |
131 } | |
132 | |
133 if (do_vm_shutdown) { | |
134 DebuggerConnectionHandler::StopHandler(); | |
135 EventHandler::Stop(); | |
136 } | |
128 exit(exit_code); | 137 exit(exit_code); |
129 } | 138 } |
130 | 139 |
131 | 140 |
132 // The environment provided through the command line using -D options. | 141 // The environment provided through the command line using -D options. |
133 static dart::HashMap* environment = NULL; | 142 static dart::HashMap* environment = NULL; |
134 | 143 |
135 static bool IsValidFlag(const char* name, | 144 static bool IsValidFlag(const char* name, |
136 const char* prefix, | 145 const char* prefix, |
137 intptr_t prefix_length) { | 146 intptr_t prefix_length) { |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
423 static bool ProcessTraceLoadingOption(const char* arg, | 432 static bool ProcessTraceLoadingOption(const char* arg, |
424 CommandLineOptions* vm_options) { | 433 CommandLineOptions* vm_options) { |
425 if (*arg != '\0') { | 434 if (*arg != '\0') { |
426 return false; | 435 return false; |
427 } | 436 } |
428 has_trace_loading = true; | 437 has_trace_loading = true; |
429 return true; | 438 return true; |
430 } | 439 } |
431 | 440 |
432 | 441 |
442 | |
443 static bool ProcessShutdownOption(const char* arg, | |
444 CommandLineOptions* vm_options) { | |
445 ASSERT(arg != NULL); | |
446 if (*arg == '\0') { | |
447 do_vm_shutdown = true; | |
448 vm_options->AddArgument("--shutdown"); | |
449 return true; | |
450 } | |
451 | |
452 if ((*arg != '=') && (*arg != ':')) { | |
453 return false; | |
454 } | |
455 | |
456 if (strcmp(arg + 1, "true") == 0) { | |
457 do_vm_shutdown = true; | |
458 vm_options->AddArgument("--shutdown"); | |
459 return true; | |
460 } else if (strcmp(arg + 1, "false") == 0) { | |
461 do_vm_shutdown = false; | |
462 vm_options->AddArgument("--no-shutdown"); | |
463 return true; | |
464 } | |
465 | |
466 return false; | |
467 } | |
468 | |
469 | |
433 static struct { | 470 static struct { |
434 const char* option_name; | 471 const char* option_name; |
435 bool (*process)(const char* option, CommandLineOptions* vm_options); | 472 bool (*process)(const char* option, CommandLineOptions* vm_options); |
436 } main_options[] = { | 473 } main_options[] = { |
437 // Standard options shared with dart2js. | 474 // Standard options shared with dart2js. |
475 { "-D", ProcessEnvironmentOption }, | |
476 { "-h", ProcessHelpOption }, | |
477 { "--help", ProcessHelpOption }, | |
478 { "--packages=", ProcessPackagesOption }, | |
479 { "--package-root=", ProcessPackageRootOption }, | |
480 { "-v", ProcessVerboseOption }, | |
481 { "--verbose", ProcessVerboseOption }, | |
438 { "--version", ProcessVersionOption }, | 482 { "--version", ProcessVersionOption }, |
439 { "--help", ProcessHelpOption }, | 483 |
440 { "-h", ProcessHelpOption }, | |
441 { "--verbose", ProcessVerboseOption }, | |
442 { "-v", ProcessVerboseOption }, | |
443 { "--package-root=", ProcessPackageRootOption }, | |
444 { "--packages=", ProcessPackagesOption }, | |
445 { "-D", ProcessEnvironmentOption }, | |
446 // VM specific options to the standalone dart program. | 484 // VM specific options to the standalone dart program. |
447 { "--break-at=", ProcessBreakpointOption }, | 485 { "--break-at=", ProcessBreakpointOption }, |
448 { "--compile_all", ProcessCompileAllOption }, | 486 { "--compile_all", ProcessCompileAllOption }, |
487 { "--debug", ProcessDebugOption }, | |
488 { "--enable-vm-service", ProcessEnableVmServiceOption }, | |
449 { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption }, | 489 { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption }, |
490 { "--observe", ProcessObserveOption }, | |
450 { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption }, | 491 { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption }, |
451 { "--debug", ProcessDebugOption }, | 492 { "--shutdown", ProcessShutdownOption }, |
452 { "--snapshot=", ProcessGenScriptSnapshotOption }, | 493 { "--snapshot=", ProcessGenScriptSnapshotOption }, |
453 { "--enable-vm-service", ProcessEnableVmServiceOption }, | |
454 { "--observe", ProcessObserveOption }, | |
455 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, | 494 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, |
456 { "--trace-loading", ProcessTraceLoadingOption}, | 495 { "--trace-loading", ProcessTraceLoadingOption }, |
457 { NULL, NULL } | 496 { NULL, NULL } |
458 }; | 497 }; |
459 | 498 |
460 | 499 |
461 static bool ProcessMainOptions(const char* option, | 500 static bool ProcessMainOptions(const char* option, |
462 CommandLineOptions* vm_options) { | 501 CommandLineOptions* vm_options) { |
463 int i = 0; | 502 int i = 0; |
464 const char* name = main_options[0].option_name; | 503 const char* name = main_options[0].option_name; |
465 int option_length = strlen(option); | 504 int option_length = strlen(option); |
466 while (name != NULL) { | 505 while (name != NULL) { |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
653 Dart_Isolate isolate = NULL; | 692 Dart_Isolate isolate = NULL; |
654 | 693 |
655 isolate = Dart_CreateIsolate(script_uri, | 694 isolate = Dart_CreateIsolate(script_uri, |
656 main, | 695 main, |
657 isolate_snapshot_buffer, | 696 isolate_snapshot_buffer, |
658 flags, | 697 flags, |
659 isolate_data, | 698 isolate_data, |
660 error); | 699 error); |
661 | 700 |
662 if (isolate == NULL) { | 701 if (isolate == NULL) { |
702 delete isolate_data; | |
663 return NULL; | 703 return NULL; |
664 } | 704 } |
665 | 705 |
666 Dart_EnterScope(); | 706 Dart_EnterScope(); |
667 | 707 |
668 if (isolate_snapshot_buffer != NULL) { | 708 if (isolate_snapshot_buffer != NULL) { |
669 // Setup the native resolver as the snapshot does not carry it. | 709 // Setup the native resolver as the snapshot does not carry it. |
670 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 710 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
671 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 711 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
672 } | 712 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
721 // Run event-loop and wait for script loading to complete. | 761 // Run event-loop and wait for script loading to complete. |
722 result = Dart_RunLoop(); | 762 result = Dart_RunLoop(); |
723 CHECK_RESULT(result); | 763 CHECK_RESULT(result); |
724 | 764 |
725 if (isolate_data->load_async_id >= 0) { | 765 if (isolate_data->load_async_id >= 0) { |
726 Dart_TimelineAsyncEnd("LoadScript", isolate_data->load_async_id); | 766 Dart_TimelineAsyncEnd("LoadScript", isolate_data->load_async_id); |
727 } | 767 } |
728 | 768 |
729 Platform::SetPackageRoot(package_root); | 769 Platform::SetPackageRoot(package_root); |
730 | 770 |
731 DartUtils::SetupIOLibrary(script_uri); | 771 result = DartUtils::SetupIOLibrary(script_uri); |
772 CHECK_RESULT(result); | |
732 | 773 |
733 // Make the isolate runnable so that it is ready to handle messages. | 774 // Make the isolate runnable so that it is ready to handle messages. |
734 Dart_ExitScope(); | 775 Dart_ExitScope(); |
735 Dart_ExitIsolate(); | 776 Dart_ExitIsolate(); |
736 bool retval = Dart_IsolateMakeRunnable(isolate); | 777 bool retval = Dart_IsolateMakeRunnable(isolate); |
737 if (!retval) { | 778 if (!retval) { |
738 *error = strdup("Invalid isolate state - Unable to make it runnable"); | 779 *error = strdup("Invalid isolate state - Unable to make it runnable"); |
739 Dart_EnterIsolate(isolate); | 780 Dart_EnterIsolate(isolate); |
740 Dart_ShutdownIsolate(); | 781 Dart_ShutdownIsolate(); |
741 return NULL; | 782 return NULL; |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1114 | 1155 |
1115 const uint8_t* instructions_snapshot = NULL; | 1156 const uint8_t* instructions_snapshot = NULL; |
1116 if (has_run_precompiled_snapshot) { | 1157 if (has_run_precompiled_snapshot) { |
1117 instructions_snapshot = reinterpret_cast<const uint8_t*>( | 1158 instructions_snapshot = reinterpret_cast<const uint8_t*>( |
1118 LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName)); | 1159 LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName)); |
1119 ReadSnapshotFile(kPrecompiledVmIsolateName, &vm_isolate_snapshot_buffer); | 1160 ReadSnapshotFile(kPrecompiledVmIsolateName, &vm_isolate_snapshot_buffer); |
1120 ReadSnapshotFile(kPrecompiledIsolateName, &isolate_snapshot_buffer); | 1161 ReadSnapshotFile(kPrecompiledIsolateName, &isolate_snapshot_buffer); |
1121 } | 1162 } |
1122 | 1163 |
1123 // Initialize the Dart VM. | 1164 // Initialize the Dart VM. |
1124 if (!Dart_Initialize(vm_isolate_snapshot_buffer, instructions_snapshot, | 1165 char* error = Dart_Initialize( |
1125 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, | 1166 vm_isolate_snapshot_buffer, instructions_snapshot, |
1126 DartUtils::OpenFile, | 1167 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, |
1127 DartUtils::ReadFile, | 1168 DartUtils::OpenFile, |
1128 DartUtils::WriteFile, | 1169 DartUtils::ReadFile, |
1129 DartUtils::CloseFile, | 1170 DartUtils::WriteFile, |
1130 DartUtils::EntropySource)) { | 1171 DartUtils::CloseFile, |
1131 fprintf(stderr, "%s", "VM initialization failed\n"); | 1172 DartUtils::EntropySource); |
1173 if (error != NULL) { | |
1174 if (do_vm_shutdown) { | |
1175 DebuggerConnectionHandler::StopHandler(); | |
1176 EventHandler::Stop(); | |
1177 } | |
1178 fprintf(stderr, "VM initialization failed: %s\n", error); | |
1132 fflush(stderr); | 1179 fflush(stderr); |
1133 DebuggerConnectionHandler::StopHandler(); | 1180 free(error); |
1134 // TODO(zra): Stop the EventHandler once thread shutdown is enabled. | |
1135 // EventHandler::Stop(); | |
1136 exit(kErrorExitCode); | 1181 exit(kErrorExitCode); |
1137 } | 1182 } |
1138 | 1183 |
1139 Dart_RegisterIsolateServiceRequestCallback( | 1184 Dart_RegisterIsolateServiceRequestCallback( |
1140 "getIO", &ServiceGetIOHandler, NULL); | 1185 "getIO", &ServiceGetIOHandler, NULL); |
1141 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, | 1186 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, |
1142 &ServiceStreamCancelCallback); | 1187 &ServiceStreamCancelCallback); |
1143 | 1188 |
1144 // Call CreateIsolateAndSetup which creates an isolate and loads up | 1189 // Call CreateIsolateAndSetup which creates an isolate and loads up |
1145 // the specified application script. | 1190 // the specified application script. |
1146 char* error = NULL; | |
1147 int exit_code = 0; | 1191 int exit_code = 0; |
1148 char* isolate_name = BuildIsolateName(script_name, "main"); | 1192 char* isolate_name = BuildIsolateName(script_name, "main"); |
1149 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 1193 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
1150 "main", | 1194 "main", |
1151 commandline_package_root, | 1195 commandline_package_root, |
1152 commandline_packages_file, | 1196 commandline_packages_file, |
1153 NULL, | 1197 NULL, |
1154 &error, | 1198 &error, |
1155 &exit_code); | 1199 &exit_code); |
1156 if (isolate == NULL) { | 1200 if (isolate == NULL) { |
1157 Log::PrintErr("%s\n", error); | 1201 Log::PrintErr("%s\n", error); |
1158 free(error); | 1202 free(error); |
1203 error = NULL; | |
1159 delete [] isolate_name; | 1204 delete [] isolate_name; |
1160 DebuggerConnectionHandler::StopHandler(); | 1205 Process::TerminateExitCodeHandler(); |
Ivan Posva
2015/09/14 21:52:13
Why is this done unconditionally, while the EventH
zra
2015/09/14 22:59:00
Process::TerminateExitCodeHandler() was already be
| |
1161 // TODO(zra): Stop the EventHandler once thread shutdown is enabled. | 1206 error = Dart_Cleanup(); |
1162 // EventHandler::Stop(); | 1207 if (error != NULL) { |
1208 Log::PrintErr("VM cleanup failed: %s\n", error); | |
1209 free(error); | |
1210 } | |
1211 if (do_vm_shutdown) { | |
1212 DebuggerConnectionHandler::StopHandler(); | |
1213 EventHandler::Stop(); | |
1214 } | |
1163 exit((exit_code != 0) ? exit_code : kErrorExitCode); | 1215 exit((exit_code != 0) ? exit_code : kErrorExitCode); |
1164 } | 1216 } |
1165 delete [] isolate_name; | 1217 delete [] isolate_name; |
1166 | 1218 |
1167 Dart_EnterIsolate(isolate); | 1219 Dart_EnterIsolate(isolate); |
1168 ASSERT(isolate == Dart_CurrentIsolate()); | 1220 ASSERT(isolate == Dart_CurrentIsolate()); |
1169 ASSERT(isolate != NULL); | 1221 ASSERT(isolate != NULL); |
1170 Dart_Handle result; | 1222 Dart_Handle result; |
1171 | 1223 |
1172 Dart_EnterScope(); | 1224 Dart_EnterScope(); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1279 result = Dart_RunLoop(); | 1331 result = Dart_RunLoop(); |
1280 DartExitOnError(result); | 1332 DartExitOnError(result); |
1281 } | 1333 } |
1282 | 1334 |
1283 Dart_ExitScope(); | 1335 Dart_ExitScope(); |
1284 // Shutdown the isolate. | 1336 // Shutdown the isolate. |
1285 Dart_ShutdownIsolate(); | 1337 Dart_ShutdownIsolate(); |
1286 // Terminate process exit-code handler. | 1338 // Terminate process exit-code handler. |
1287 Process::TerminateExitCodeHandler(); | 1339 Process::TerminateExitCodeHandler(); |
1288 | 1340 |
1289 Dart_Cleanup(); | 1341 error = Dart_Cleanup(); |
1290 | 1342 if (error != NULL) { |
1291 DebuggerConnectionHandler::StopHandler(); | 1343 Log::PrintErr("VM cleanup failed: %s\n", error); |
1292 // TODO(zra): Stop the EventHandler once thread shutdown is enabled. | 1344 free(error); |
1293 // EventHandler::Stop(); | 1345 } |
1346 if (do_vm_shutdown) { | |
1347 DebuggerConnectionHandler::StopHandler(); | |
1348 EventHandler::Stop(); | |
1349 } | |
1294 | 1350 |
1295 // Free copied argument strings if converted. | 1351 // Free copied argument strings if converted. |
1296 if (argv_converted) { | 1352 if (argv_converted) { |
1297 for (int i = 0; i < argc; i++) free(argv[i]); | 1353 for (int i = 0; i < argc; i++) free(argv[i]); |
1298 } | 1354 } |
1299 | 1355 |
1300 // Free environment if any. | 1356 // Free environment if any. |
1301 if (environment != NULL) { | 1357 if (environment != NULL) { |
1302 for (HashMap::Entry* p = environment->Start(); | 1358 for (HashMap::Entry* p = environment->Start(); |
1303 p != NULL; | 1359 p != NULL; |
1304 p = environment->Next(p)) { | 1360 p = environment->Next(p)) { |
1305 free(p->key); | 1361 free(p->key); |
1306 free(p->value); | 1362 free(p->value); |
1307 } | 1363 } |
1308 delete environment; | 1364 delete environment; |
1309 } | 1365 } |
1310 | 1366 |
1311 exit(Process::GlobalExitCode()); | 1367 exit(Process::GlobalExitCode()); |
1312 } | 1368 } |
1313 | 1369 |
1314 } // namespace bin | 1370 } // namespace bin |
1315 } // namespace dart | 1371 } // namespace dart |
1316 | 1372 |
1317 int main(int argc, char** argv) { | 1373 int main(int argc, char** argv) { |
1318 dart::bin::main(argc, argv); | 1374 dart::bin::main(argc, argv); |
1319 UNREACHABLE(); | 1375 UNREACHABLE(); |
1320 } | 1376 } |
OLD | NEW |