42#define CHECK_RESULT(result) \
43 if (Dart_IsError(result)) { \
44 intptr_t exit_code = 0; \
45 Syslog::PrintErr("Error: %s\n", Dart_GetError(result)); \
46 if (Dart_IsCompilationError(result)) { \
47 exit_code = kCompilationErrorExitCode; \
48 } else if (Dart_IsApiError(result)) { \
49 exit_code = kApiErrorExitCode; \
51 exit_code = kErrorExitCode; \
54 Dart_ShutdownIsolate(); \
98#define STRING_OPTIONS_LIST(V) \
99 V(load_vm_snapshot_data, load_vm_snapshot_data_filename) \
100 V(load_vm_snapshot_instructions, load_vm_snapshot_instructions_filename) \
101 V(load_isolate_snapshot_data, load_isolate_snapshot_data_filename) \
102 V(load_isolate_snapshot_instructions, \
103 load_isolate_snapshot_instructions_filename) \
104 V(vm_snapshot_data, vm_snapshot_data_filename) \
105 V(vm_snapshot_instructions, vm_snapshot_instructions_filename) \
106 V(isolate_snapshot_data, isolate_snapshot_data_filename) \
107 V(isolate_snapshot_instructions, isolate_snapshot_instructions_filename) \
108 V(blobs_container_filename, blobs_container_filename) \
109 V(assembly, assembly_filename) \
110 V(elf, elf_filename) \
111 V(loading_unit_manifest, loading_unit_manifest_filename) \
112 V(save_debugging_info, debugging_info_filename) \
113 V(save_obfuscation_map, obfuscation_map_filename)
117#define BOOL_OPTIONS_LIST(V) \
118 V(compile_all, compile_all) \
120 V(obfuscate, obfuscate) \
122 V(verbose, verbose) \
123 V(version, version) \
124 V(sound_null_safety, sound_null_safety)
126#define STRING_OPTION_DEFINITION(flag, variable) \
127 static const char* variable = nullptr; \
128 DEFINE_STRING_OPTION(flag, variable)
130#undef STRING_OPTION_DEFINITION
132#define BOOL_OPTION_DEFINITION(flag, variable) \
133 static bool variable = false; \
134 DEFINE_BOOL_OPTION(flag, variable)
136#undef BOOL_OPTION_DEFINITION
149"Usage: gen_snapshot [<vm-flags>] [<options>] <dart-kernel-file> \n"
153" Display this message (add --verbose for information about all VM options).\n"
155" Print the SDK version. \n"
157"To create a core snapshot: \n"
158"--snapshot_kind=core \n"
159"--vm_snapshot_data=<output-file> \n"
160"--isolate_snapshot_data=<output-file> \n"
161"<dart-kernel-file> \n"
163"To create an AOT application snapshot as assembly suitable for compilation \n"
164"as a static or dynamic library: \n"
165"--snapshot_kind=app-aot-assembly \n"
166"--assembly=<output-file> \n"
169"[--save-debugging-info=<debug-filename>] \n"
170"[--save-obfuscation-map=<map-filename>] \n"
171"<dart-kernel-file> \n"
173"To create an AOT application snapshot as an ELF shared library: \n"
174"--snapshot_kind=app-aot-elf \n"
175"--elf=<output-file> \n"
178"[--save-debugging-info=<debug-filename>] \n"
179"[--save-obfuscation-map=<map-filename>] \n"
180"<dart-kernel-file> \n"
182"AOT snapshots can be obfuscated: that is all identifiers will be renamed \n"
183"during compilation. This mode is enabled with --obfuscate flag. Mapping \n"
184"between original and obfuscated names can be serialized as a JSON array \n"
185"using --save-obfuscation-map=<filename> option. See dartbug.com/30524 \n"
186"for implementation details and limitations of the obfuscation pass. \n"
191"The following options are only used for VM development and may\n"
192"be changed in any future version:\n");
193 const char* print_flags =
"--print_flags";
221 inputs->AddArgument(
argv[i]);
228 }
else if (version) {
234 if (inputs->count() < 1) {
241 if ((vm_snapshot_data_filename ==
nullptr) ||
242 (isolate_snapshot_data_filename ==
nullptr)) {
244 "Building a core snapshot requires specifying output files for "
245 "--vm_snapshot_data and --isolate_snapshot_data.\n\n");
252 if ((load_vm_snapshot_data_filename ==
nullptr) ||
253 (isolate_snapshot_data_filename ==
nullptr) ||
254 (isolate_snapshot_instructions_filename ==
nullptr)) {
256 "Building an app JIT snapshot requires specifying input files for "
257 "--load_vm_snapshot_data and --load_vm_snapshot_instructions, an "
258 " output file for --isolate_snapshot_data, and an output "
259 "file for --isolate_snapshot_instructions.\n\n");
265 if (elf_filename ==
nullptr) {
267 "Building an AOT snapshot as ELF requires specifying "
268 "an output file for --elf.\n\n");
275 if (assembly_filename ==
nullptr) {
277 "Building an AOT snapshot as assembly requires specifying "
278 "an output file for --assembly.\n\n");
285 if (!obfuscate && obfuscation_map_filename !=
nullptr) {
287 "--save-obfuscation_map=<...> should only be specified when "
288 "obfuscation is enabled by the --obfuscate flag.\n\n");
295 "Obfuscation can only be enabled when building an AOT snapshot.\n\n");
299 if (debugging_info_filename !=
nullptr) {
301 "--save-debugging-info=<...> can only be enabled when building an "
302 "AOT snapshot.\n\n");
308 "Stripping can only be enabled when building an AOT snapshot.\n\n");
329 if (file ==
nullptr) {
330 PrintErrAndExit(
"Error: Unable to write file: %s\n\n", filename);
337 const intptr_t size) {
340 if (!file->WriteFully(
buffer, size)) {
341 PrintErrAndExit(
"Error: Unable to write file: %s\n\n", filename);
347 if (file ==
nullptr) {
348 PrintErrAndExit(
"Error: Unable to read file: %s\n", filename);
351 *size = file->Length();
353 if (!file->ReadFully(*
buffer, *size)) {
354 PrintErrAndExit(
"Error: Unable to read file: %s\n", filename);
363 for (intptr_t i = 1; i < inputs.count(); i++) {
364 uint8_t*
buffer =
nullptr;
384 ASSERT(vm_snapshot_data_filename !=
nullptr);
385 ASSERT(isolate_snapshot_data_filename !=
nullptr);
388 uint8_t* vm_snapshot_data_buffer =
nullptr;
389 intptr_t vm_snapshot_data_size = 0;
390 uint8_t* isolate_snapshot_data_buffer =
nullptr;
391 intptr_t isolate_snapshot_data_size = 0;
395 &isolate_snapshot_data_buffer,
396 &isolate_snapshot_data_size,
402 WriteFile(vm_snapshot_data_filename, vm_snapshot_data_buffer,
403 vm_snapshot_data_size);
404 if (vm_snapshot_instructions_filename !=
nullptr) {
406 WriteFile(vm_snapshot_instructions_filename,
nullptr, 0);
408 WriteFile(isolate_snapshot_data_filename, isolate_snapshot_data_buffer,
409 isolate_snapshot_data_size);
410 if (isolate_snapshot_instructions_filename !=
nullptr) {
412 WriteFile(isolate_snapshot_instructions_filename,
nullptr, 0);
416static std::unique_ptr<MappedMemory>
MapFile(
const char* filename,
420 if (file ==
nullptr) {
425 intptr_t
length = file->Length();
432 if (mapping ==
nullptr) {
436 *
buffer =
reinterpret_cast<const uint8_t*
>(mapping->
address());
437 return std::unique_ptr<MappedMemory>(mapping);
442 ASSERT(isolate_snapshot_data_filename !=
nullptr);
445 uint8_t* isolate_snapshot_data_buffer =
nullptr;
446 intptr_t isolate_snapshot_data_size = 0;
449 &isolate_snapshot_data_size,
false);
452 WriteFile(isolate_snapshot_data_filename, isolate_snapshot_data_buffer,
453 isolate_snapshot_data_size);
454 if (isolate_snapshot_instructions_filename !=
nullptr) {
456 WriteFile(isolate_snapshot_instructions_filename,
nullptr, 0);
462 ASSERT(isolate_snapshot_data_filename !=
nullptr);
463 ASSERT(isolate_snapshot_instructions_filename !=
nullptr);
466 uint8_t* isolate_snapshot_data_buffer =
nullptr;
467 intptr_t isolate_snapshot_data_size = 0;
468 uint8_t* isolate_snapshot_instructions_buffer =
nullptr;
469 intptr_t isolate_snapshot_instructions_size = 0;
472 &isolate_snapshot_data_buffer, &isolate_snapshot_data_size,
473 &isolate_snapshot_instructions_buffer,
474 &isolate_snapshot_instructions_size);
477 WriteFile(isolate_snapshot_data_filename, isolate_snapshot_data_buffer,
478 isolate_snapshot_data_size);
479 WriteFile(isolate_snapshot_instructions_filename,
480 isolate_snapshot_instructions_buffer,
481 isolate_snapshot_instructions_size);
487 File* file =
reinterpret_cast<File*
>(callback_data);
488 if ((file !=
nullptr) && !file->WriteFully(
buffer, size)) {
489 PrintErrAndExit(
"Error: Unable to write snapshot file\n\n");
494 File* file =
reinterpret_cast<File*
>(callback_data);
499 File* manifest_file =
OpenFile(loading_unit_manifest_filename);
500 if (!manifest_file->
Print(
"{ \"loadingUnits\": [\n ")) {
501 PrintErrAndExit(
"Error: Unable to write file: %s\n\n",
502 loading_unit_manifest_filename);
504 return manifest_file;
510 const char* debug_path =
nullptr) {
513 line.AddString(
",\n ");
515 line.Printf(
"{\n \"id\": %" Pd ",\n \"path\": \"",
id);
516 line.AddEscapedString(path);
517 if (debug_path !=
nullptr) {
518 line.Printf(
"\",\n \"debugPath\": \"");
519 line.AddEscapedString(debug_path);
521 line.AddString(
"\",\n \"libraries\": [\n ");
526 for (intptr_t i = 0; i <
length; i++) {
530 line.AddString(
",\n ");
532 line.AddString(
"\"");
533 line.AddEscapedString(uri);
534 line.AddString(
"\"");
536 line.AddString(
"\n ]}");
537 if (!manifest_file->
Print(
"%s", line.buffer())) {
538 PrintErrAndExit(
"Error: Unable to write file: %s\n\n",
539 loading_unit_manifest_filename);
544 if (!manifest_file->
Print(
"]}\n")) {
545 PrintErrAndExit(
"Error: Unable to write file: %s\n\n",
546 loading_unit_manifest_filename);
552 intptr_t loading_unit_id,
553 void** write_callback_data,
554 void** write_debug_callback_data,
555 const char* main_filename,
556 const char* suffix) {
557 char* filename = loading_unit_id == 1
560 loading_unit_id, suffix);
562 *write_callback_data = file;
564 char* debug_filename =
nullptr;
565 if (debugging_info_filename !=
nullptr) {
572 *write_debug_callback_data = debug_file;
576 loading_unit_id, filename, debug_filename);
577 free(debug_filename);
583 intptr_t loading_unit_id,
584 void** write_callback_data,
585 void** write_debug_callback_data) {
587 write_debug_callback_data, assembly_filename,
"S");
591 intptr_t loading_unit_id,
592 void** write_callback_data,
593 void** write_debug_callback_data) {
595 write_debug_callback_data, elf_filename,
"so");
608 if (strip && (debugging_info_filename ==
nullptr)) {
610 "Warning: Generating assembly code without DWARF debugging"
613 if (loading_unit_manifest_filename ==
nullptr) {
616 File* debug_file =
nullptr;
617 if (debugging_info_filename !=
nullptr) {
618 debug_file =
OpenFile(debugging_info_filename);
622 if (debug_file !=
nullptr) debug_file->
Release();
632 if (obfuscate && !strip) {
634 "Warning: The generated assembly code contains unobfuscated DWARF "
635 "debugging information.\n"
636 " To avoid this, use --strip to remove it.\n");
639 if (strip && (debugging_info_filename ==
nullptr)) {
641 "Warning: Generating ELF library without DWARF debugging"
644 if (loading_unit_manifest_filename ==
nullptr) {
647 File* debug_file =
nullptr;
648 if (debugging_info_filename !=
nullptr) {
649 debug_file =
OpenFile(debugging_info_filename);
653 if (debug_file !=
nullptr) debug_file->
Release();
663 if (obfuscate && !strip) {
665 "Warning: The generated ELF library contains unobfuscated DWARF "
666 "debugging information.\n"
667 " To avoid this, use --strip to remove it and "
668 "--save-debugging-info=<...> to save it to a separate file.\n");
675 if (obfuscation_map_filename !=
nullptr) {
677 uint8_t*
buffer =
nullptr;
686 uint8_t* kernel_buffer =
nullptr;
687 intptr_t kernel_buffer_size = 0;
688 ReadFile(inputs.GetArgument(0), &kernel_buffer, &kernel_buffer_size);
696 auto isolate_group_data = std::unique_ptr<IsolateGroupData>(
699 char*
error =
nullptr;
701 bool loading_kernel_failed =
false;
707 nullptr,
nullptr, kernel_buffer, kernel_buffer_size, &isolate_flags,
708 isolate_group_data.get(),
nullptr, &
error);
709 loading_kernel_failed = (isolate ==
nullptr);
713 &isolate_flags, isolate_group_data.get(),
716 if (isolate ==
nullptr) {
792int main(
int argc,
char** argv) {
793#if !defined(DART_HOST_OS_WINDOWS)
798 const int EXTRA_VM_ARGUMENTS = 7;
806 vm_options.
AddArgument(
"--new_gen_semi_max_size=16");
808 vm_options.
AddArgument(
"--new_gen_semi_max_size=32");
810 vm_options.
AddArgument(
"--new_gen_growth_factor=4");
842#if !defined(TARGET_ARCH_IA32)
848 if (
error !=
nullptr) {
855 memset(&init_params, 0,
sizeof(init_params));
863#if defined(DART_HOST_OS_FUCHSIA)
864 init_params.vmex_resource = Platform::GetVMEXResource();
867 std::unique_ptr<MappedMemory> mapped_vm_snapshot_data;
868 std::unique_ptr<MappedMemory> mapped_vm_snapshot_instructions;
869 std::unique_ptr<MappedMemory> mapped_isolate_snapshot_data;
870 std::unique_ptr<MappedMemory> mapped_isolate_snapshot_instructions;
871 if (load_vm_snapshot_data_filename !=
nullptr) {
872 mapped_vm_snapshot_data =
876 if (load_vm_snapshot_instructions_filename !=
nullptr) {
877 mapped_vm_snapshot_instructions =
881 if (load_isolate_snapshot_data_filename !=
nullptr) {
882 mapped_isolate_snapshot_data =
886 if (load_isolate_snapshot_instructions_filename !=
nullptr) {
887 mapped_isolate_snapshot_instructions =
893 if (
error !=
nullptr) {
905 if (
error !=
nullptr) {
916int main(
int argc,
char** argv) {
static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static char * StrDup(const char *s)
static char * SCreate(const char *format,...) PRINTF_ATTRIBUTE(1
const char ** arguments() const
void AddArgument(const char *argument)
static void ReadFile(uint8_t **data, intptr_t *file_len, void *stream)
static bool SetOriginalWorkingDirectory()
static bool EntropySource(uint8_t *buffer, intptr_t length)
static Dart_Handle EnvironmentCallback(Dart_Handle name)
static void SetEnvironment(dart::SimpleHashMap *environment)
static void CloseFile(void *stream)
static void * OpenFile(const char *name, bool write)
static void WriteFile(const void *buffer, intptr_t num_bytes, void *stream)
static void LoadDartProfilerSymbols(const char *exepath)
static File * Open(Namespace *namespc, const char *path, FileOpenMode mode)
bool Print(const char *format,...) PRINTF_ATTRIBUTE(2
static bool TryProcess(const char *option, CommandLineOptions *options)
static bool IsValidShortFlag(const char *name)
static bool ProcessEnvironmentOption(const char *arg, CommandLineOptions *vm_options, dart::SimpleHashMap **environment)
#define DART_INITIALIZE_PARAMS_CURRENT_VERSION
struct _Dart_Handle * Dart_Handle
struct _Dart_Isolate * Dart_Isolate
#define CHECK_RESULT(result)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
const uint8_t uint32_t uint32_t GError ** error
uint32_t uint32_t * format
#define BOOL_OPTIONS_LIST(V)
#define STRING_OPTION_DEFINITION(flag, variable)
#define BOOL_OPTION_DEFINITION(flag, variable)
#define STRING_OPTIONS_LIST(V)
constexpr int kCompilationErrorExitCode
static void CloseLoadingUnitManifest(File *manifest_file)
void WriteFile(const void *buffer, intptr_t num_bytes, void *stream)
void ReadFile(uint8_t **data, intptr_t *file_len, void *stream)
const uint8_t * isolate_snapshot_data
static bool IsSnapshottingForPrecompilation()
static void CreateAndWritePrecompiledSnapshot()
static int CreateIsolateAndSnapshot(const CommandLineOptions &inputs)
static File * OpenLoadingUnitManifest()
static bool ProcessEnvironmentOption(const char *arg, CommandLineOptions *vm_options)
static void MaybeLoadCode()
static void CreateAndWriteCoreSnapshot()
static void StreamingWriteCallback(void *callback_data, const uint8_t *buffer, intptr_t size)
int main(int argc, char **argv)
constexpr int kErrorExitCode
static void MaybeLoadExtraInputs(const CommandLineOptions &inputs)
static void NextLoadingUnit(void *callback_data, intptr_t loading_unit_id, void **write_callback_data, void **write_debug_callback_data, const char *main_filename, const char *suffix)
static void WriteLoadingUnitManifest(File *manifest_file, intptr_t id, const char *path, const char *debug_path=nullptr)
static SnapshotKind snapshot_kind
static void StreamingCloseCallback(void *callback_data)
static void CreateAndWriteAppJITSnapshot()
static int ParseArguments(int argc, char **argv, CommandLineOptions *vm_options, CommandLineOptions *inputs)
static void CreateAndWriteAppSnapshot()
void * OpenFile(const char *name, bool write)
static void MallocFinalizer(void *isolate_callback_data, void *peer)
static void NextAsmCallback(void *callback_data, intptr_t loading_unit_id, void **write_callback_data, void **write_debug_callback_data)
static void NextElfCallback(void *callback_data, intptr_t loading_unit_id, void **write_callback_data, void **write_debug_callback_data)
static const char *const kSnapshotKindNames[]
static dart::SimpleHashMap * environment
const uint8_t * isolate_snapshot_instructions
static std::unique_ptr< MappedMemory > MapFile(const char *filename, File::MapType type, const uint8_t **buffer)
DART_EXPORT Dart_Handle Dart_ListGetAt(Dart_Handle list, intptr_t index)
DART_EXPORT Dart_Handle Dart_CreateAppAOTSnapshotAsElfs(Dart_CreateLoadingUnitCallback next_callback, void *next_callback_data, bool strip, Dart_StreamingWriteCallback write_callback, Dart_StreamingCloseCallback close_callback)
DART_EXPORT void Dart_EnterScope()
DART_EXPORT Dart_Handle Dart_GetObfuscationMap(uint8_t **buffer, intptr_t *buffer_length)
DART_EXPORT Dart_Handle Dart_CreateAppAOTSnapshotAsAssemblies(Dart_CreateLoadingUnitCallback next_callback, void *next_callback_data, bool strip, Dart_StreamingWriteCallback write_callback, Dart_StreamingCloseCallback close_callback)
void * malloc(size_t size)
DART_EXPORT Dart_Handle Dart_Precompile()
DART_EXPORT void Dart_IsolateFlagsInitialize(Dart_IsolateFlags *flags)
DART_EXPORT Dart_Handle Dart_NewExternalTypedDataWithFinalizer(Dart_TypedData_Type type, void *data, intptr_t length, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT Dart_Handle Dart_CreateAppJITSnapshotAsBlobs(uint8_t **isolate_snapshot_data_buffer, intptr_t *isolate_snapshot_data_size, uint8_t **isolate_snapshot_instructions_buffer, intptr_t *isolate_snapshot_instructions_size)
DART_EXPORT Dart_Handle Dart_CreateAppAOTSnapshotAsElf(Dart_StreamingWriteCallback callback, void *callback_data, bool strip, void *debug_callback_data)
DART_EXPORT char * Dart_Initialize(Dart_InitializeParams *params)
DART_EXPORT const char * Dart_VersionString()
DART_EXPORT char * Dart_Cleanup()
DART_EXPORT Dart_Handle Dart_CreateAppAOTSnapshotAsAssembly(Dart_StreamingWriteCallback callback, void *callback_data, bool strip, void *debug_callback_data)
DART_EXPORT Dart_Isolate Dart_CreateIsolateGroup(const char *script_uri, const char *name, const uint8_t *snapshot_data, const uint8_t *snapshot_instructions, Dart_IsolateFlags *flags, void *isolate_group_data, void *isolate_data, char **error)
DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle kernel_buffer)
DART_EXPORT Dart_Isolate Dart_CreateIsolateGroupFromKernel(const char *script_uri, const char *name, const uint8_t *kernel_buffer, intptr_t kernel_buffer_size, Dart_IsolateFlags *flags, void *isolate_group_data, void *isolate_data, char **error)
DART_EXPORT char * Dart_SetVMFlags(int argc, const char **argv)
DART_EXPORT Dart_Handle Dart_CreateVMAOTSnapshotAsAssembly(Dart_StreamingWriteCallback callback, void *callback_data)
DART_EXPORT Dart_Handle Dart_LoadingUnitLibraryUris(intptr_t loading_unit_id)
DART_EXPORT Dart_Handle Dart_CompileAll()
DART_EXPORT Dart_Handle Dart_ListLength(Dart_Handle list, intptr_t *len)
constexpr intptr_t kWordSize
DART_EXPORT Dart_Handle Dart_SetEnvironmentCallback(Dart_EnvironmentCallback callback)
DART_EXPORT Dart_Handle Dart_SetRootLibrary(Dart_Handle library)
DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, const char **cstr)
DART_EXPORT Dart_Handle Dart_CreateSnapshot(uint8_t **vm_snapshot_data_buffer, intptr_t *vm_snapshot_data_size, uint8_t **isolate_snapshot_data_buffer, intptr_t *isolate_snapshot_data_size, bool is_core)
DART_EXPORT Dart_Handle Dart_LoadLibraryFromKernel(const uint8_t *buffer, intptr_t buffer_size)
#define DEFINE_ENUM_OPTION(name, enum_name, variable)
#define DEFINE_CB_OPTION(callback)
Dart_FileReadCallback file_read
const uint8_t * vm_snapshot_data
bool start_kernel_isolate
Dart_FileOpenCallback file_open
Dart_FileWriteCallback file_write
Dart_EntropySource entropy_source
const uint8_t * vm_snapshot_instructions
Dart_FileCloseCallback file_close
bool load_vmservice_library