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)
115#define BOOL_OPTIONS_LIST(V) \
116 V(compile_all, compile_all) \
118 V(obfuscate, obfuscate) \
120 V(verbose, verbose) \
123#define STRING_OPTION_DEFINITION(flag, variable) \
124 static const char* variable = nullptr; \
125 DEFINE_STRING_OPTION(flag, variable)
127#undef STRING_OPTION_DEFINITION
129#define BOOL_OPTION_DEFINITION(flag, variable) \
130 static bool variable = false; \
131 DEFINE_BOOL_OPTION(flag, variable)
133#undef BOOL_OPTION_DEFINITION
146"Usage: gen_snapshot [<vm-flags>] [<options>] <dart-kernel-file> \n"
150" Display this message (add --verbose for information about all VM options).\n"
152" Print the SDK version. \n"
154"To create a core snapshot: \n"
155"--snapshot_kind=core \n"
156"--vm_snapshot_data=<output-file> \n"
157"--isolate_snapshot_data=<output-file> \n"
158"<dart-kernel-file> \n"
160"To create an AOT application snapshot as assembly suitable for compilation \n"
161"as a static or dynamic library: \n"
162"--snapshot_kind=app-aot-assembly \n"
163"--assembly=<output-file> \n"
166"[--save-debugging-info=<debug-filename>] \n"
167"[--save-obfuscation-map=<map-filename>] \n"
168"<dart-kernel-file> \n"
170"To create an AOT application snapshot as an ELF shared library: \n"
171"--snapshot_kind=app-aot-elf \n"
172"--elf=<output-file> \n"
175"[--save-debugging-info=<debug-filename>] \n"
176"[--save-obfuscation-map=<map-filename>] \n"
177"<dart-kernel-file> \n"
179"AOT snapshots can be obfuscated: that is all identifiers will be renamed \n"
180"during compilation. This mode is enabled with --obfuscate flag. Mapping \n"
181"between original and obfuscated names can be serialized as a JSON array \n"
182"using --save-obfuscation-map=<filename> option. See dartbug.com/30524 \n"
183"for implementation details and limitations of the obfuscation pass. \n"
188"The following options are only used for VM development and may\n"
189"be changed in any future version:\n");
190 const char* print_flags =
"--print_flags";
231 if (
inputs->count() < 1) {
238 if ((vm_snapshot_data_filename ==
nullptr) ||
239 (isolate_snapshot_data_filename ==
nullptr)) {
241 "Building a core snapshot requires specifying output files for "
242 "--vm_snapshot_data and --isolate_snapshot_data.\n\n");
249 if ((load_vm_snapshot_data_filename ==
nullptr) ||
250 (isolate_snapshot_data_filename ==
nullptr) ||
251 (isolate_snapshot_instructions_filename ==
nullptr)) {
253 "Building an app JIT snapshot requires specifying input files for "
254 "--load_vm_snapshot_data and --load_vm_snapshot_instructions, an "
255 " output file for --isolate_snapshot_data, and an output "
256 "file for --isolate_snapshot_instructions.\n\n");
262 if (elf_filename ==
nullptr) {
264 "Building an AOT snapshot as ELF requires specifying "
265 "an output file for --elf.\n\n");
272 if (assembly_filename ==
nullptr) {
274 "Building an AOT snapshot as assembly requires specifying "
275 "an output file for --assembly.\n\n");
282 if (!obfuscate && obfuscation_map_filename !=
nullptr) {
284 "--save-obfuscation_map=<...> should only be specified when "
285 "obfuscation is enabled by the --obfuscate flag.\n\n");
292 "Obfuscation can only be enabled when building an AOT snapshot.\n\n");
296 if (debugging_info_filename !=
nullptr) {
298 "--save-debugging-info=<...> can only be enabled when building an "
299 "AOT snapshot.\n\n");
305 "Stripping can only be enabled when building an AOT snapshot.\n\n");
326 if (
file ==
nullptr) {
327 PrintErrAndExit(
"Error: Unable to write file: %s\n\n", filename);
334 const intptr_t
size) {
338 PrintErrAndExit(
"Error: Unable to write file: %s\n\n", filename);
344 if (
file ==
nullptr) {
345 PrintErrAndExit(
"Error: Unable to read file: %s\n", filename);
351 PrintErrAndExit(
"Error: Unable to read file: %s\n", filename);
360 for (intptr_t
i = 1;
i <
inputs.count();
i++) {
361 uint8_t*
buffer =
nullptr;
381 ASSERT(vm_snapshot_data_filename !=
nullptr);
382 ASSERT(isolate_snapshot_data_filename !=
nullptr);
385 uint8_t* vm_snapshot_data_buffer =
nullptr;
386 intptr_t vm_snapshot_data_size = 0;
387 uint8_t* isolate_snapshot_data_buffer =
nullptr;
388 intptr_t isolate_snapshot_data_size = 0;
392 &isolate_snapshot_data_buffer,
393 &isolate_snapshot_data_size,
399 WriteFile(vm_snapshot_data_filename, vm_snapshot_data_buffer,
400 vm_snapshot_data_size);
401 if (vm_snapshot_instructions_filename !=
nullptr) {
403 WriteFile(vm_snapshot_instructions_filename,
nullptr, 0);
405 WriteFile(isolate_snapshot_data_filename, isolate_snapshot_data_buffer,
406 isolate_snapshot_data_size);
407 if (isolate_snapshot_instructions_filename !=
nullptr) {
409 WriteFile(isolate_snapshot_instructions_filename,
nullptr, 0);
413static std::unique_ptr<MappedMemory>
MapFile(
const char* filename,
417 if (
file ==
nullptr) {
429 if (mapping ==
nullptr) {
433 *
buffer =
reinterpret_cast<const uint8_t*
>(mapping->
address());
434 return std::unique_ptr<MappedMemory>(mapping);
439 ASSERT(isolate_snapshot_data_filename !=
nullptr);
442 uint8_t* isolate_snapshot_data_buffer =
nullptr;
443 intptr_t isolate_snapshot_data_size = 0;
446 &isolate_snapshot_data_size,
false);
449 WriteFile(isolate_snapshot_data_filename, isolate_snapshot_data_buffer,
450 isolate_snapshot_data_size);
451 if (isolate_snapshot_instructions_filename !=
nullptr) {
453 WriteFile(isolate_snapshot_instructions_filename,
nullptr, 0);
459 ASSERT(isolate_snapshot_data_filename !=
nullptr);
460 ASSERT(isolate_snapshot_instructions_filename !=
nullptr);
463 uint8_t* isolate_snapshot_data_buffer =
nullptr;
464 intptr_t isolate_snapshot_data_size = 0;
465 uint8_t* isolate_snapshot_instructions_buffer =
nullptr;
466 intptr_t isolate_snapshot_instructions_size = 0;
469 &isolate_snapshot_data_buffer, &isolate_snapshot_data_size,
470 &isolate_snapshot_instructions_buffer,
471 &isolate_snapshot_instructions_size);
474 WriteFile(isolate_snapshot_data_filename, isolate_snapshot_data_buffer,
475 isolate_snapshot_data_size);
476 WriteFile(isolate_snapshot_instructions_filename,
477 isolate_snapshot_instructions_buffer,
478 isolate_snapshot_instructions_size);
486 PrintErrAndExit(
"Error: Unable to write snapshot file\n\n");
496 File* manifest_file =
OpenFile(loading_unit_manifest_filename);
497 if (!manifest_file->
Print(
"{ \"loadingUnits\": [\n ")) {
498 PrintErrAndExit(
"Error: Unable to write file: %s\n\n",
499 loading_unit_manifest_filename);
501 return manifest_file;
507 const char* debug_path =
nullptr) {
510 line.AddString(
",\n ");
512 line.Printf(
"{\n \"id\": %" Pd ",\n \"path\": \"",
id);
514 if (debug_path !=
nullptr) {
515 line.Printf(
"\",\n \"debugPath\": \"");
516 line.AddEscapedString(debug_path);
518 line.AddString(
"\",\n \"libraries\": [\n ");
527 line.AddString(
",\n ");
529 line.AddString(
"\"");
530 line.AddEscapedString(uri);
531 line.AddString(
"\"");
533 line.AddString(
"\n ]}");
534 if (!manifest_file->
Print(
"%s",
line.buffer())) {
535 PrintErrAndExit(
"Error: Unable to write file: %s\n\n",
536 loading_unit_manifest_filename);
541 if (!manifest_file->
Print(
"]}\n")) {
542 PrintErrAndExit(
"Error: Unable to write file: %s\n\n",
543 loading_unit_manifest_filename);
549 intptr_t loading_unit_id,
550 void** write_callback_data,
551 void** write_debug_callback_data,
552 const char* main_filename,
554 char* filename = loading_unit_id == 1
559 *write_callback_data =
file;
561 char* debug_filename =
nullptr;
562 if (debugging_info_filename !=
nullptr) {
569 *write_debug_callback_data = debug_file;
573 loading_unit_id, filename, debug_filename);
574 free(debug_filename);
580 intptr_t loading_unit_id,
581 void** write_callback_data,
582 void** write_debug_callback_data) {
584 write_debug_callback_data, assembly_filename,
"S");
588 intptr_t loading_unit_id,
589 void** write_callback_data,
590 void** write_debug_callback_data) {
592 write_debug_callback_data, elf_filename,
"so");
605 if (strip && (debugging_info_filename ==
nullptr)) {
607 "Warning: Generating assembly code without DWARF debugging"
610 if (loading_unit_manifest_filename ==
nullptr) {
613 File* debug_file =
nullptr;
614 if (debugging_info_filename !=
nullptr) {
615 debug_file =
OpenFile(debugging_info_filename);
619 if (debug_file !=
nullptr) debug_file->
Release();
629 if (obfuscate && !strip) {
631 "Warning: The generated assembly code contains unobfuscated DWARF "
632 "debugging information.\n"
633 " To avoid this, use --strip to remove it.\n");
636 if (strip && (debugging_info_filename ==
nullptr)) {
638 "Warning: Generating ELF library without DWARF debugging"
641 if (loading_unit_manifest_filename ==
nullptr) {
644 File* debug_file =
nullptr;
645 if (debugging_info_filename !=
nullptr) {
646 debug_file =
OpenFile(debugging_info_filename);
650 if (debug_file !=
nullptr) debug_file->
Release();
660 if (obfuscate && !strip) {
662 "Warning: The generated ELF library contains unobfuscated DWARF "
663 "debugging information.\n"
664 " To avoid this, use --strip to remove it and "
665 "--save-debugging-info=<...> to save it to a separate file.\n");
672 if (obfuscation_map_filename !=
nullptr) {
674 uint8_t*
buffer =
nullptr;
683 uint8_t* kernel_buffer =
nullptr;
684 intptr_t kernel_buffer_size = 0;
685 ReadFile(
inputs.GetArgument(0), &kernel_buffer, &kernel_buffer_size);
693 auto isolate_group_data = std::unique_ptr<IsolateGroupData>(
696 char*
error =
nullptr;
698 bool loading_kernel_failed =
false;
704 nullptr,
nullptr, kernel_buffer, kernel_buffer_size, &isolate_flags,
705 isolate_group_data.get(),
nullptr, &
error);
706 loading_kernel_failed = (isolate ==
nullptr);
710 &isolate_flags, isolate_group_data.get(),
713 if (isolate ==
nullptr) {
790#if !defined(DART_HOST_OS_WINDOWS)
795 const int EXTRA_VM_ARGUMENTS = 7;
803 vm_options.
AddArgument(
"--new_gen_semi_max_size=16");
805 vm_options.
AddArgument(
"--new_gen_semi_max_size=32");
807 vm_options.
AddArgument(
"--new_gen_growth_factor=4");
839#if !defined(TARGET_ARCH_IA32)
845 if (
error !=
nullptr) {
852 memset(&init_params, 0,
sizeof(init_params));
860#if defined(DART_HOST_OS_FUCHSIA)
861 init_params.vmex_resource = Platform::GetVMEXResource();
864 std::unique_ptr<MappedMemory> mapped_vm_snapshot_data;
865 std::unique_ptr<MappedMemory> mapped_vm_snapshot_instructions;
866 std::unique_ptr<MappedMemory> mapped_isolate_snapshot_data;
867 std::unique_ptr<MappedMemory> mapped_isolate_snapshot_instructions;
868 if (load_vm_snapshot_data_filename !=
nullptr) {
869 mapped_vm_snapshot_data =
873 if (load_vm_snapshot_instructions_filename !=
nullptr) {
874 mapped_vm_snapshot_instructions =
878 if (load_isolate_snapshot_data_filename !=
nullptr) {
879 mapped_isolate_snapshot_data =
883 if (load_isolate_snapshot_instructions_filename !=
nullptr) {
884 mapped_isolate_snapshot_instructions =
890 if (
error !=
nullptr) {
902 if (
error !=
nullptr) {
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
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const uint8_t uint32_t uint32_t GError ** error
uint32_t uint32_t * format
#define CHECK_RESULT(result)
int main(int argc, char **argv)
#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)
DEFINE_ENUM_OPTION(snapshot_kind, SnapshotKind, snapshot_kind)
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()
PRINTF_ATTRIBUTE(1, 2) static void PrintErrAndExit(const char *format
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)
DEFINE_CB_OPTION(ProcessEnvironmentOption)
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)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
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