Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1579)

Side by Side Diff: runtime/bin/main.cc

Issue 1413753002: Added option --snapshot-after-run to enable an application snapshot to be taken after running the a… (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 20 matching lines...) Expand all
31 // vm_isolate_snapshot_buffer points to a snapshot for the vm isolate if we 31 // vm_isolate_snapshot_buffer points to a snapshot for the vm isolate if we
32 // link in a snapshot otherwise it is initialized to NULL. 32 // link in a snapshot otherwise it is initialized to NULL.
33 extern const uint8_t* vm_isolate_snapshot_buffer; 33 extern const uint8_t* vm_isolate_snapshot_buffer;
34 34
35 // isolate_snapshot_buffer points to a snapshot for an isolate if we link in a 35 // isolate_snapshot_buffer points to a snapshot for an isolate if we link in a
36 // snapshot otherwise it is initialized to NULL. 36 // snapshot otherwise it is initialized to NULL.
37 extern const uint8_t* isolate_snapshot_buffer; 37 extern const uint8_t* isolate_snapshot_buffer;
38 38
39 // Global state that stores a pointer to the application script snapshot. 39 // Global state that stores a pointer to the application script snapshot.
40 static bool generate_script_snapshot = false; 40 static bool generate_script_snapshot = false;
41 static bool generate_script_snapshot_after_run = false;
41 static const char* snapshot_filename = NULL; 42 static const char* snapshot_filename = NULL;
42 43
43 44
44 // Global state that indicates whether there is a debug breakpoint. 45 // Global state that indicates whether there is a debug breakpoint.
45 // This pointer points into an argv buffer and does not need to be 46 // This pointer points into an argv buffer and does not need to be
46 // free'd. 47 // free'd.
47 static const char* breakpoint_at = NULL; 48 static const char* breakpoint_at = NULL;
48 49
49 50
50 // Global state that indicates whether we should open a connection 51 // Global state that indicates whether we should open a connection
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 } 377 }
377 378
378 breakpoint_at = "main"; 379 breakpoint_at = "main";
379 start_debugger = true; 380 start_debugger = true;
380 return true; 381 return true;
381 } 382 }
382 383
383 384
384 static bool ProcessGenScriptSnapshotOption(const char* filename, 385 static bool ProcessGenScriptSnapshotOption(const char* filename,
385 CommandLineOptions* vm_options) { 386 CommandLineOptions* vm_options) {
387 if (generate_script_snapshot_after_run) {
388 Log::PrintErr("--snapshot and --snapshot-after-run options"
389 " cannot be specified at the same time\n");
390 return false;
391 }
386 if (filename != NULL && strlen(filename) != 0) { 392 if (filename != NULL && strlen(filename) != 0) {
387 // Ensure that we are already running using a full snapshot. 393 // Ensure that we are already running using a full snapshot.
388 if (isolate_snapshot_buffer == NULL) { 394 if (isolate_snapshot_buffer == NULL) {
389 Log::PrintErr("Script snapshots cannot be generated in this version of" 395 Log::PrintErr("Script snapshots cannot be generated in this version of"
390 " dart\n"); 396 " dart\n");
391 return false; 397 return false;
392 } 398 }
393 snapshot_filename = filename; 399 snapshot_filename = filename;
394 generate_script_snapshot = true; 400 generate_script_snapshot = true;
401 generate_script_snapshot_after_run = false;
395 return true; 402 return true;
396 } 403 }
397 return false; 404 return false;
405 }
406
407
408 static bool ProcessGenScriptSnapshotAfterRunOption(
409 const char* filename, CommandLineOptions* vm_options) {
410 if (generate_script_snapshot) {
411 Log::PrintErr("--snapshot and --snapshot-after-run options"
412 " cannot be specified at the same time\n");
413 return false;
414 }
415 if (filename != NULL && strlen(filename) != 0) {
srdjan 2015/10/16 21:26:56 Add parens please
416 // Ensure that we are already running using a full snapshot.
417 if (isolate_snapshot_buffer == NULL) {
418 Log::PrintErr("Script snapshots cannot be generated in this version of"
419 " dart\n");
420 return false;
421 }
422 snapshot_filename = filename;
423 generate_script_snapshot = false;
424 generate_script_snapshot_after_run = true;
425 return true;
426 }
427 return false;
398 } 428 }
399 429
400 430
401 static bool ProcessEnableVmServiceOption(const char* option_value, 431 static bool ProcessEnableVmServiceOption(const char* option_value,
402 CommandLineOptions* vm_options) { 432 CommandLineOptions* vm_options) {
403 ASSERT(option_value != NULL); 433 ASSERT(option_value != NULL);
404 434
405 if (!ExtractPortAndIP(option_value, 435 if (!ExtractPortAndIP(option_value,
406 &vm_service_server_port, 436 &vm_service_server_port,
407 &vm_service_server_ip, 437 &vm_service_server_ip,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 { "--break-at=", ProcessBreakpointOption }, 532 { "--break-at=", ProcessBreakpointOption },
503 { "--compile_all", ProcessCompileAllOption }, 533 { "--compile_all", ProcessCompileAllOption },
504 { "--debug", ProcessDebugOption }, 534 { "--debug", ProcessDebugOption },
505 { "--enable-vm-service", ProcessEnableVmServiceOption }, 535 { "--enable-vm-service", ProcessEnableVmServiceOption },
506 { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption }, 536 { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption },
507 { "--noopt", ProcessNooptOption }, 537 { "--noopt", ProcessNooptOption },
508 { "--observe", ProcessObserveOption }, 538 { "--observe", ProcessObserveOption },
509 { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption }, 539 { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption },
510 { "--shutdown", ProcessShutdownOption }, 540 { "--shutdown", ProcessShutdownOption },
511 { "--snapshot=", ProcessGenScriptSnapshotOption }, 541 { "--snapshot=", ProcessGenScriptSnapshotOption },
542 { "--snapshot-after-run=", ProcessGenScriptSnapshotAfterRunOption },
512 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption }, 543 { "--trace-debug-protocol", ProcessTraceDebugProtocolOption },
513 { "--trace-loading", ProcessTraceLoadingOption }, 544 { "--trace-loading", ProcessTraceLoadingOption },
514 { NULL, NULL } 545 { NULL, NULL }
515 }; 546 };
516 547
517 548
518 static bool ProcessMainOptions(const char* option, 549 static bool ProcessMainOptions(const char* option,
519 CommandLineOptions* vm_options) { 550 CommandLineOptions* vm_options) {
520 int i = 0; 551 int i = 0;
521 const char* name = main_options[0].option_name; 552 const char* name = main_options[0].option_name;
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 } 1130 }
1100 void* symbol = Extensions::ResolveSymbol(library, symname); 1131 void* symbol = Extensions::ResolveSymbol(library, symname);
1101 if (symbol == NULL) { 1132 if (symbol == NULL) {
1102 Log::PrintErr("Error: Failed to load symbol '%s'\n", symname); 1133 Log::PrintErr("Error: Failed to load symbol '%s'\n", symname);
1103 exit(kErrorExitCode); 1134 exit(kErrorExitCode);
1104 } 1135 }
1105 return symbol; 1136 return symbol;
1106 } 1137 }
1107 1138
1108 1139
1140 static void GenerateScriptSnapshot() {
1141 // First create a snapshot.
1142 uint8_t* buffer = NULL;
1143 intptr_t size = 0;
1144 Dart_Handle result = Dart_CreateScriptSnapshot(&buffer, &size);
1145 if (Dart_IsError(result)) {
1146 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result));
1147 }
1148
1149 // Open the snapshot file.
1150 File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate);
1151 if (snapshot_file == NULL) {
1152 ErrorExit(kErrorExitCode,
1153 "Unable to open file %s for writing the snapshot\n",
1154 snapshot_filename);
1155 }
1156
1157 // Write the magic number to indicate file is a script snapshot.
1158 DartUtils::WriteMagicNumber(snapshot_file);
1159
1160 // Now write the snapshot out to specified file.
1161 bool bytes_written = snapshot_file->WriteFully(buffer, size);
1162 ASSERT(bytes_written);
1163 delete snapshot_file;
1164 snapshot_file = NULL;
1165 }
1166
1167
1109 #define CHECK_RESULT(result) \ 1168 #define CHECK_RESULT(result) \
1110 if (Dart_IsError(result)) { \ 1169 if (Dart_IsError(result)) { \
1111 if (Dart_IsVMRestartRequest(result)) { \ 1170 if (Dart_IsVMRestartRequest(result)) { \
1112 Dart_ExitScope(); \ 1171 Dart_ExitScope(); \
1113 Dart_ShutdownIsolate(); \ 1172 Dart_ShutdownIsolate(); \
1114 return true; \ 1173 return true; \
1115 } \ 1174 } \
1116 const int exit_code = Dart_IsCompilationError(result) ? \ 1175 const int exit_code = Dart_IsCompilationError(result) ? \
1117 kCompilationErrorExitCode : kErrorExitCode; \ 1176 kCompilationErrorExitCode : kErrorExitCode; \
1118 ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \ 1177 ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \
1119 } 1178 }
1120 1179
1180
1121 bool RunMainIsolate(const char* script_name, 1181 bool RunMainIsolate(const char* script_name,
1122 CommandLineOptions* dart_options) { 1182 CommandLineOptions* dart_options) {
1123 // Call CreateIsolateAndSetup which creates an isolate and loads up 1183 // Call CreateIsolateAndSetup which creates an isolate and loads up
1124 // the specified application script. 1184 // the specified application script.
1125 char* error = NULL; 1185 char* error = NULL;
1126 int exit_code = 0; 1186 int exit_code = 0;
1127 char* isolate_name = BuildIsolateName(script_name, "main"); 1187 char* isolate_name = BuildIsolateName(script_name, "main");
1128 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, 1188 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name,
1129 "main", 1189 "main",
1130 commandline_package_root, 1190 commandline_package_root,
(...skipping 26 matching lines...) Expand all
1157 delete [] isolate_name; 1217 delete [] isolate_name;
1158 1218
1159 Dart_EnterIsolate(isolate); 1219 Dart_EnterIsolate(isolate);
1160 ASSERT(isolate == Dart_CurrentIsolate()); 1220 ASSERT(isolate == Dart_CurrentIsolate());
1161 ASSERT(isolate != NULL); 1221 ASSERT(isolate != NULL);
1162 Dart_Handle result; 1222 Dart_Handle result;
1163 1223
1164 Dart_EnterScope(); 1224 Dart_EnterScope();
1165 1225
1166 if (generate_script_snapshot) { 1226 if (generate_script_snapshot) {
1167 // First create a snapshot. 1227 GenerateScriptSnapshot();
1168 Dart_Handle result;
1169 uint8_t* buffer = NULL;
1170 intptr_t size = 0;
1171 result = Dart_CreateScriptSnapshot(&buffer, &size);
1172 CHECK_RESULT(result);
1173
1174 // Open the snapshot file.
1175 File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate);
1176 if (snapshot_file == NULL) {
1177 ErrorExit(kErrorExitCode,
1178 "Unable to open file %s for writing the snapshot\n",
1179 snapshot_filename);
1180 }
1181
1182 // Write the magic number to indicate file is a script snapshot.
1183 DartUtils::WriteMagicNumber(snapshot_file);
1184
1185 // Now write the snapshot out to specified file.
1186 bool bytes_written = snapshot_file->WriteFully(buffer, size);
1187 ASSERT(bytes_written);
1188 delete snapshot_file;
1189 snapshot_file = NULL;
1190 } else { 1228 } else {
1191 // Lookup the library of the root script. 1229 // Lookup the library of the root script.
1192 Dart_Handle root_lib = Dart_RootLibrary(); 1230 Dart_Handle root_lib = Dart_RootLibrary();
1193 // Import the root library into the builtin library so that we can easily 1231 // Import the root library into the builtin library so that we can easily
1194 // lookup the main entry point exported from the root library. 1232 // lookup the main entry point exported from the root library.
1195 Dart_Handle builtin_lib = 1233 Dart_Handle builtin_lib =
1196 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); 1234 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
1197 ASSERT(!Dart_IsError(builtin_lib)); 1235 ASSERT(!Dart_IsError(builtin_lib));
1198 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null()); 1236 result = Dart_LibraryImportLibrary(builtin_lib, root_lib, Dart_Null());
1199 1237
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1292 Dart_Handle isolate_lib = 1330 Dart_Handle isolate_lib =
1293 Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate")); 1331 Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate"));
1294 result = Dart_Invoke(isolate_lib, 1332 result = Dart_Invoke(isolate_lib,
1295 Dart_NewStringFromCString("_startMainIsolate"), 1333 Dart_NewStringFromCString("_startMainIsolate"),
1296 kNumIsolateArgs, isolate_args); 1334 kNumIsolateArgs, isolate_args);
1297 CHECK_RESULT(result); 1335 CHECK_RESULT(result);
1298 1336
1299 // Keep handling messages until the last active receive port is closed. 1337 // Keep handling messages until the last active receive port is closed.
1300 result = Dart_RunLoop(); 1338 result = Dart_RunLoop();
1301 CHECK_RESULT(result); 1339 CHECK_RESULT(result);
1340
1341 // Generate a script snapshot after execution if specified.
1342 if (generate_script_snapshot_after_run) {
1343 GenerateScriptSnapshot();
1344 }
1302 } 1345 }
1303 } 1346 }
1304 1347
1305 Dart_ExitScope(); 1348 Dart_ExitScope();
1306 // Shutdown the isolate. 1349 // Shutdown the isolate.
1307 Dart_ShutdownIsolate(); 1350 Dart_ShutdownIsolate();
1308 1351
1309 // No restart. 1352 // No restart.
1310 return false; 1353 return false;
1311 } 1354 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1456 exit(Process::GlobalExitCode()); 1499 exit(Process::GlobalExitCode());
1457 } 1500 }
1458 1501
1459 } // namespace bin 1502 } // namespace bin
1460 } // namespace dart 1503 } // namespace dart
1461 1504
1462 int main(int argc, char** argv) { 1505 int main(int argc, char** argv) {
1463 dart::bin::main(argc, argv); 1506 dart::bin::main(argc, argv);
1464 UNREACHABLE(); 1507 UNREACHABLE();
1465 } 1508 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698