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

Unified Diff: runtime/bin/main.cc

Issue 1670243002: Add command line options (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: self-review-comments Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/main.cc
diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc
index 77625d80cb43adcef525d7e9007e74761c9d2d7b..14a0b6805f3996dad95ed76ff6485c5593fc8aa9 100644
--- a/runtime/bin/main.cc
+++ b/runtime/bin/main.cc
@@ -41,7 +41,8 @@ extern const uint8_t* isolate_snapshot_buffer;
// Global state that stores a pointer to the application script snapshot.
static bool generate_script_snapshot = false;
-static bool generate_script_snapshot_after_run = false;
+static bool generate_full_snapshot_after_run = false;
+static bool run_full_snapshot = false;
static const char* snapshot_filename = NULL;
@@ -58,17 +59,17 @@ static const char* commandline_packages_file = NULL;
// Global flag that is used to indicate that we want to compile all the
// dart functions and not run anything.
-static bool has_compile_all = false;
+static bool compile_all = false;
// Global flag that is used to indicate that we want to compile all the
// dart functions before running main and not compile anything thereafter.
-static bool has_gen_precompiled_snapshot = false;
+static bool gen_precompiled_snapshot = false;
// Global flag that is used to indicate that we want to run from a precompiled
// snapshot.
-static bool has_run_precompiled_snapshot = false;
+static bool run_precompiled_snapshot = false;
// Value of the --gen/run_precompiled_snapshot flag.
@@ -80,7 +81,7 @@ static const char* precompiled_snapshot_directory = NULL;
// Global flag that is used to indicate that we want to compile everything in
// the same way as precompilation before main, then continue running in the
// same process.
-static bool has_noopt = false;
+static bool noopt = false;
srdjan 2016/02/05 17:35:02 I would add a verb to this, as done with all other
siva 2016/02/05 23:14:18 Done.
extern const char* kPrecompiledLibraryName;
@@ -88,11 +89,12 @@ extern const char* kPrecompiledSymbolName;
static const char* kPrecompiledVmIsolateName = "precompiled.vmisolate";
static const char* kPrecompiledIsolateName = "precompiled.isolate";
static const char* kPrecompiledInstructionsName = "precompiled.S";
-
+static const char* kVMIsolateSuffix = "vmisolate";
+static const char* kIsolateSuffix = "isolate";
// Global flag that is used to indicate that we want to trace resolution of
// URIs and the loading of libraries, parts and scripts.
-static bool has_trace_loading = false;
+static bool trace_loading = false;
static const char* DEFAULT_VM_SERVICE_SERVER_IP = "127.0.0.1";
@@ -155,34 +157,34 @@ static bool IsValidFlag(const char* name,
}
-static bool has_version_option = false;
+static bool version_option = false;
static bool ProcessVersionOption(const char* arg,
CommandLineOptions* vm_options) {
if (*arg != '\0') {
return false;
}
- has_version_option = true;
+ version_option = true;
return true;
}
-static bool has_help_option = false;
+static bool help_option = false;
static bool ProcessHelpOption(const char* arg, CommandLineOptions* vm_options) {
if (*arg != '\0') {
return false;
}
- has_help_option = true;
+ help_option = true;
return true;
}
-static bool has_verbose_option = false;
+static bool verbose_option = false;
static bool ProcessVerboseOption(const char* arg,
CommandLineOptions* vm_options) {
if (*arg != '\0') {
return false;
}
- has_verbose_option = true;
+ verbose_option = true;
return true;
}
@@ -302,7 +304,7 @@ static bool ProcessCompileAllOption(const char* arg,
if (*arg != '\0') {
return false;
}
- has_compile_all = true;
+ compile_all = true;
return true;
}
@@ -322,7 +324,7 @@ static bool ProcessGenPrecompiledSnapshotOption(
} else {
precompiled_snapshot_directory = arg;
}
- has_gen_precompiled_snapshot = true;
+ gen_precompiled_snapshot = true;
vm_options->AddArgument("--precompilation");
return true;
}
@@ -337,45 +339,67 @@ static bool ProcessRunPrecompiledSnapshotOption(
(precompiled_snapshot_directory[0] == ':')) {
precompiled_snapshot_directory = &precompiled_snapshot_directory[1];
}
- has_run_precompiled_snapshot = true;
+ run_precompiled_snapshot = true;
vm_options->AddArgument("--precompilation");
return true;
}
-static bool ProcessScriptSnapshotOptionHelper(const char* filename,
- bool* snapshot_option) {
- *snapshot_option = false;
- if ((filename != NULL) && (strlen(filename) != 0)) {
- // Ensure that we are already running using a full snapshot.
- if (isolate_snapshot_buffer == NULL) {
- Log::PrintErr("Script snapshots cannot be generated in this version of"
- " dart\n");
- return false;
- }
- snapshot_filename = filename;
- *snapshot_option = true;
- if (generate_script_snapshot && generate_script_snapshot_after_run) {
- Log::PrintErr("--snapshot and --snapshot-after-run options"
- " cannot be specified at the same time\n");
- return false;
- }
- return true;
+static bool ProcessSnapshotOptionHelper(const char* filename,
+ bool* snapshot_option) {
+ ASSERT((filename != NULL) && (strlen(filename) != 0));
+ snapshot_filename = filename;
+ *snapshot_option = true;
+ if (generate_script_snapshot && generate_full_snapshot_after_run) {
+ Log::PrintErr("--snapshot and --snapshot-after-run options"
+ " cannot be specified at the same time\n");
+ *snapshot_option = false;
+ return false;
}
- return false;
+ return true;
}
static bool ProcessScriptSnapshotOption(const char* filename,
CommandLineOptions* vm_options) {
- return ProcessScriptSnapshotOptionHelper(filename, &generate_script_snapshot);
+ if ((filename == NULL) || (strlen(filename) == 0)) {
+ return false;
+ }
+ // Ensure that we are already running using a full snapshot.
+ if (isolate_snapshot_buffer == NULL) {
+ Log::PrintErr("Script snapshots cannot be generated in this version of"
+ " dart\n");
srdjan 2016/02/05 17:35:02 s/dart/Dart/
siva 2016/02/05 23:14:18 Done.
+ return false;
+ }
+ return ProcessSnapshotOptionHelper(filename, &generate_script_snapshot);
}
-static bool ProcessScriptSnapshotAfterRunOption(
+static bool ProcessFullSnapshotAfterRunOption(
const char* filename, CommandLineOptions* vm_options) {
- return ProcessScriptSnapshotOptionHelper(filename,
- &generate_script_snapshot_after_run);
+ if ((filename == NULL) || (strlen(filename) == 0)) {
+ return false;
+ }
+ // Ensure that we are running 'dart_no_snapshot'.
+ if (isolate_snapshot_buffer != NULL) {
+ Log::PrintErr("Full Application snapshots must be generated with"
+ " dart_no_snapshot.\n");
+ return false;
+ }
+ return ProcessSnapshotOptionHelper(filename,
+ &generate_full_snapshot_after_run);
+}
+
+
+static bool ProcessRunFullSnapshotOption(
+ const char* filename, CommandLineOptions* vm_options) {
+ // Ensure that we are not running 'dart_no_snapshot'.
+ if (isolate_snapshot_buffer == NULL) {
+ Log::PrintErr("Full Application snapshots cannot be run with"
+ " dart_no_snapshot.\n");
+ return false;
+ }
+ return ProcessSnapshotOptionHelper(filename, &run_full_snapshot);
}
@@ -423,7 +447,7 @@ static bool ProcessTraceLoadingOption(const char* arg,
if (*arg != '\0') {
return false;
}
- has_trace_loading = true;
+ trace_loading = true;
return true;
}
@@ -478,7 +502,8 @@ static struct {
{ "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption },
{ "--shutdown", ProcessShutdownOption },
{ "--snapshot=", ProcessScriptSnapshotOption },
- { "--snapshot-after-run=", ProcessScriptSnapshotAfterRunOption },
+ { "--full-snapshot-after-run=", ProcessFullSnapshotAfterRunOption },
+ { "--run-full-snapshot=", ProcessRunFullSnapshotOption },
{ "--trace-loading", ProcessTraceLoadingOption },
{ NULL, NULL }
};
@@ -595,23 +620,29 @@ static int ParseArguments(int argc,
"file is invalid.\n");
return -1;
}
- if (has_noopt) {
- if (has_gen_precompiled_snapshot) {
+ if (noopt) {
+ if (gen_precompiled_snapshot) {
Log::PrintErr("Specifying --noopt and --gen_precompiled_snapshot"
" is invalid.\n");
return -1;
}
- if (has_run_precompiled_snapshot) {
+ if (run_precompiled_snapshot) {
Log::PrintErr("Specifying --noopt and --run_precompiled_snapshot"
" is invalid.\n");
return -1;
}
}
- if (has_gen_precompiled_snapshot && has_run_precompiled_snapshot) {
- Log::PrintErr("Specifying --gen_precompiled_snapshot and"
+ if (run_full_snapshot && run_precompiled_snapshot) {
+ Log::PrintErr("Specifying --run_full_snapshot and"
" --run_precompiled_snapshot is invalid.\n");
return -1;
}
+ if ((generate_full_snapshot_after_run || gen_precompiled_snapshot) &&
+ (run_full_snapshot || run_precompiled_snapshot)) {
+ Log::PrintErr("Specifying an option to generate a snapshot and"
+ " run using a snapshot is invalid.\n");
+ return -1;
+ }
return 0;
}
@@ -697,6 +728,12 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
char** error,
int* exit_code) {
ASSERT(script_uri != NULL);
+ if (run_full_snapshot &&
+ !strcmp(script_uri, DART_VM_SERVICE_ISOLATE_NAME)) {
+ // We do not create a service isolate when running a full application
+ // snapshot.
+ return NULL;
+ }
IsolateData* isolate_data = new IsolateData(script_uri,
package_root,
packages_config);
@@ -730,11 +767,11 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
// If this is the service isolate, load embedder specific bits and return.
if (!VmService::Setup(vm_service_server_ip,
vm_service_server_port,
- has_run_precompiled_snapshot)) {
+ run_precompiled_snapshot)) {
*error = strdup(VmService::GetErrorMessage());
return NULL;
}
- if (has_compile_all) {
+ if (compile_all) {
result = Dart_CompileAll();
CHECK_RESULT(result);
}
@@ -746,13 +783,15 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
// Prepare builtin and other core libraries for use to resolve URIs.
// Set up various closures, e.g: printing, timers etc.
// Set up 'package root' for URI resolution.
- result = DartUtils::PrepareForScriptLoading(false, has_trace_loading);
+ result = DartUtils::PrepareForScriptLoading(false, trace_loading);
CHECK_RESULT(result);
- // Set up the load port provided by the service isolate so that we can
- // load scripts.
- result = DartUtils::SetupServiceLoadPort();
- CHECK_RESULT(result);
+ if (!run_full_snapshot) {
+ // Set up the load port provided by the service isolate so that we can
+ // load scripts.
+ result = DartUtils::SetupServiceLoadPort();
+ CHECK_RESULT(result);
+ }
// Setup package root if specified.
result = DartUtils::SetupPackageRoot(package_root, packages_config);
@@ -761,10 +800,8 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
result = Dart_SetEnvironmentCallback(EnvironmentCallback);
CHECK_RESULT(result);
- if (!has_run_precompiled_snapshot) {
+ if (!run_precompiled_snapshot && !run_full_snapshot) {
// Load the specified application script into the newly created isolate.
-
- // Load the script.
result = DartUtils::LoadScript(script_uri);
CHECK_RESULT(result);
@@ -778,6 +815,9 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
result = DartUtils::SetupIOLibrary(script_uri);
CHECK_RESULT(result);
+ } else if (run_full_snapshot) {
+ result = DartUtils::SetupIOLibrary(script_uri);
+ CHECK_RESULT(result);
}
// Make the isolate runnable so that it is ready to handle messages.
@@ -834,7 +874,7 @@ static void PrintUsage() {
"\n"
"Executes the Dart script passed as <dart-script-file>.\n"
"\n");
- if (!has_verbose_option) {
+ if (!verbose_option) {
Log::PrintErr(
"Common options:\n"
"--checked or -c\n"
@@ -1003,17 +1043,17 @@ static void ServiceStreamCancelCallback(const char* stream_id) {
}
-static void WritePrecompiledSnapshotFile(const char* filename,
- const uint8_t* buffer,
- const intptr_t size) {
+static void WriteSnapshotFile(const char* snapshot_directory,
+ const char* filename,
+ bool write_magic_number,
+ const uint8_t* buffer,
+ const intptr_t size) {
char* concat = NULL;
const char* qualified_filename;
- if (strlen(precompiled_snapshot_directory) > 0) {
- intptr_t len = snprintf(NULL, 0, "%s/%s",
- precompiled_snapshot_directory, filename);
+ if ((snapshot_directory != NULL) && strlen(snapshot_directory) > 0) {
+ intptr_t len = snprintf(NULL, 0, "%s/%s", snapshot_directory, filename);
concat = new char[len + 1];
- snprintf(concat, len + 1, "%s/%s",
- precompiled_snapshot_directory, filename);
+ snprintf(concat, len + 1, "%s/%s", snapshot_directory, filename);
qualified_filename = concat;
} else {
qualified_filename = filename;
@@ -1021,6 +1061,12 @@ static void WritePrecompiledSnapshotFile(const char* filename,
File* file = File::Open(qualified_filename, File::kWriteTruncate);
ASSERT(file != NULL);
+
+ if (write_magic_number) {
+ // Write the magic number to indicate file is a script snapshot.
+ DartUtils::WriteMagicNumber(file);
+ }
+
if (!file->WriteFully(buffer, size)) {
ErrorExit(kErrorExitCode,
"Unable to open file %s for writing snapshot\n",
@@ -1033,16 +1079,15 @@ static void WritePrecompiledSnapshotFile(const char* filename,
}
-static void ReadPrecompiledSnapshotFile(const char* filename,
- const uint8_t** buffer) {
+static void ReadSnapshotFile(const char* snapshot_directory,
+ const char* filename,
+ const uint8_t** buffer) {
char* concat = NULL;
const char* qualified_filename;
- if (strlen(precompiled_snapshot_directory) > 0) {
- intptr_t len = snprintf(NULL, 0, "%s/%s",
- precompiled_snapshot_directory, filename);
+ if ((snapshot_directory != NULL) && strlen(snapshot_directory) > 0) {
+ intptr_t len = snprintf(NULL, 0, "%s/%s", snapshot_directory, filename);
concat = new char[len + 1];
- snprintf(concat, len + 1, "%s/%s",
- precompiled_snapshot_directory, filename);
+ snprintf(concat, len + 1, "%s/%s", snapshot_directory, filename);
qualified_filename = concat;
} else {
qualified_filename = filename;
@@ -1091,22 +1136,54 @@ static void GenerateScriptSnapshot() {
ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result));
}
- // Open the snapshot file.
- File* snapshot_file = File::Open(snapshot_filename, File::kWriteTruncate);
- if (snapshot_file == NULL) {
- ErrorExit(kErrorExitCode,
- "Unable to open file %s for writing the snapshot\n",
- snapshot_filename);
- }
+ WriteSnapshotFile(NULL, snapshot_filename, true, buffer, size);
+}
+
+
+static void ComputeSnapshotFilenames(const char* filename,
+ char** vm_snapshot_fname,
+ char** isolate_snapshot_fname) {
+ intptr_t len = snprintf(NULL, 0, "%s.%s", filename, kVMIsolateSuffix);
+ *vm_snapshot_fname = new char[len + 1];
+ snprintf(*vm_snapshot_fname, len + 1, "%s.%s", filename, kVMIsolateSuffix);
+
+ len = snprintf(NULL, 0, "%s.%s", filename, kIsolateSuffix);
+ *isolate_snapshot_fname = new char[len + 1];
+ snprintf(*isolate_snapshot_fname, len + 1, "%s.%s", filename, kIsolateSuffix);
+}
- // Write the magic number to indicate file is a script snapshot.
- DartUtils::WriteMagicNumber(snapshot_file);
+static void GenerateFullSnapshot() {
+ // Create a full snapshot of the script.
+ Dart_Handle result;
+ uint8_t* vm_isolate_buffer = NULL;
+ intptr_t vm_isolate_size = 0;
+ uint8_t* isolate_buffer = NULL;
+ intptr_t isolate_size = 0;
+ char* vm_snapshot_fname = NULL;
+ char* isolate_snapshot_fname = NULL;
+
+ result = Dart_CreateSnapshot(&vm_isolate_buffer,
+ &vm_isolate_size,
+ &isolate_buffer,
+ &isolate_size);
+ if (Dart_IsError(result)) {
+ ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result));
+ }
- // Now write the snapshot out to specified file.
- bool bytes_written = snapshot_file->WriteFully(buffer, size);
- ASSERT(bytes_written);
- delete snapshot_file;
- snapshot_file = NULL;
+ // Compute snapshot file names and write out the snapshot files.
+ ComputeSnapshotFilenames(snapshot_filename,
+ &vm_snapshot_fname,
+ &isolate_snapshot_fname);
+ WriteSnapshotFile(NULL,
+ vm_snapshot_fname,
+ false,
+ vm_isolate_buffer,
+ vm_isolate_size);
+ WriteSnapshotFile(NULL,
+ isolate_snapshot_fname,
+ false,
+ isolate_buffer,
+ isolate_size);
}
@@ -1177,7 +1254,7 @@ bool RunMainIsolate(const char* script_name,
reinterpret_cast<IsolateData*>(Dart_IsolateData(isolate));
result = Dart_LibraryImportLibrary(
isolate_data->builtin_lib(), root_lib, Dart_Null());
- if (has_gen_precompiled_snapshot) {
+ if (gen_precompiled_snapshot) {
// Load the embedder's portion of the VM service's Dart code so it will
// be included in the precompiled snapshot.
if (!VmService::LoadForGenPrecompiled()) {
@@ -1189,12 +1266,12 @@ bool RunMainIsolate(const char* script_name,
}
}
- if (has_compile_all) {
+ if (compile_all) {
result = Dart_CompileAll();
CHECK_RESULT(result);
}
- if (has_noopt || has_gen_precompiled_snapshot) {
+ if (noopt || gen_precompiled_snapshot) {
Dart_QualifiedFunctionName standalone_entry_points[] = {
{ "dart:_builtin", "::", "_getMainClosure" },
{ "dart:_builtin", "::", "_getPrintClosure" },
@@ -1228,12 +1305,12 @@ bool RunMainIsolate(const char* script_name,
{ NULL, NULL, NULL } // Must be terminated with NULL entries.
};
- const bool reset_fields = has_gen_precompiled_snapshot;
+ const bool reset_fields = gen_precompiled_snapshot;
result = Dart_Precompile(standalone_entry_points, reset_fields);
CHECK_RESULT(result);
}
- if (has_gen_precompiled_snapshot) {
+ if (gen_precompiled_snapshot) {
uint8_t* vm_isolate_buffer = NULL;
intptr_t vm_isolate_size = 0;
uint8_t* isolate_buffer = NULL;
@@ -1247,15 +1324,21 @@ bool RunMainIsolate(const char* script_name,
&instructions_buffer,
&instructions_size);
CHECK_RESULT(result);
- WritePrecompiledSnapshotFile(kPrecompiledVmIsolateName,
- vm_isolate_buffer,
- vm_isolate_size);
- WritePrecompiledSnapshotFile(kPrecompiledIsolateName,
- isolate_buffer,
- isolate_size);
- WritePrecompiledSnapshotFile(kPrecompiledInstructionsName,
- instructions_buffer,
- instructions_size);
+ WriteSnapshotFile(precompiled_snapshot_directory,
+ kPrecompiledVmIsolateName,
+ false,
+ vm_isolate_buffer,
+ vm_isolate_size);
+ WriteSnapshotFile(precompiled_snapshot_directory,
+ kPrecompiledIsolateName,
+ false,
+ isolate_buffer,
+ isolate_size);
+ WriteSnapshotFile(precompiled_snapshot_directory,
+ kPrecompiledInstructionsName,
+ false,
+ instructions_buffer,
+ instructions_size);
} else {
if (Dart_IsNull(root_lib)) {
ErrorExit(kErrorExitCode,
@@ -1288,9 +1371,9 @@ bool RunMainIsolate(const char* script_name,
result = Dart_RunLoop();
CHECK_RESULT(result);
- // Generate a script snapshot after execution if specified.
- if (generate_script_snapshot_after_run) {
- GenerateScriptSnapshot();
+ // Generate a full snapshot after execution if specified.
+ if (generate_full_snapshot_after_run) {
+ GenerateFullSnapshot();
}
}
}
@@ -1414,10 +1497,10 @@ void main(int argc, char** argv) {
&dart_options,
&print_flags_seen,
&verbose_debug_seen) < 0) {
- if (has_help_option) {
+ if (help_option) {
PrintUsage();
Platform::Exit(0);
- } else if (has_version_option) {
+ } else if (version_option) {
PrintVersion();
Platform::Exit(0);
} else if (print_flags_seen) {
@@ -1440,16 +1523,15 @@ void main(int argc, char** argv) {
Platform::Exit(kErrorExitCode);
}
- if (generate_script_snapshot) {
+ if (generate_script_snapshot || generate_full_snapshot_after_run) {
vm_options.AddArgument("--load_deferred_eagerly");
}
#if defined(DART_PRECOMPILER) && !defined(DART_NO_SNAPSHOT)
// Always set --precompilation with dart_noopt, unless already set by these
// command line options.
- if (!has_gen_precompiled_snapshot &&
- !has_run_precompiled_snapshot) {
- has_noopt = true;
+ if (!gen_precompiled_snapshot && !run_precompiled_snapshot) {
+ noopt = true;
vm_options.AddArgument("--precompilation");
}
#endif
@@ -1461,13 +1543,28 @@ void main(int argc, char** argv) {
EventHandler::Start();
const uint8_t* instructions_snapshot = NULL;
- if (has_run_precompiled_snapshot) {
+ if (run_precompiled_snapshot) {
instructions_snapshot = reinterpret_cast<const uint8_t*>(
LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName));
- ReadPrecompiledSnapshotFile(kPrecompiledVmIsolateName,
- &vm_isolate_snapshot_buffer);
- ReadPrecompiledSnapshotFile(kPrecompiledIsolateName,
- &isolate_snapshot_buffer);
+ ReadSnapshotFile(precompiled_snapshot_directory,
+ kPrecompiledVmIsolateName,
+ &vm_isolate_snapshot_buffer);
+ ReadSnapshotFile(precompiled_snapshot_directory,
+ kPrecompiledIsolateName,
+ &isolate_snapshot_buffer);
+ } else if (run_full_snapshot) {
+ char* vm_snapshot_fname;
+ char* isolate_snapshot_fname;
+
+ // Compute file names.
+ ComputeSnapshotFilenames(snapshot_filename,
+ &vm_snapshot_fname,
+ &isolate_snapshot_fname);
+
+ ReadSnapshotFile(NULL, vm_snapshot_fname, &vm_isolate_snapshot_buffer);
+ ReadSnapshotFile(NULL, isolate_snapshot_fname, &isolate_snapshot_buffer);
+ delete vm_snapshot_fname;
+ delete isolate_snapshot_fname;
}
// Initialize the Dart VM.
« 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