V8 Project
mksnapshot.cc File Reference
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include "src/v8.h"
#include "include/libplatform/libplatform.h"
#include "src/assembler.h"
#include "src/base/platform/platform.h"
#include "src/bootstrapper.h"
#include "src/flags.h"
#include "src/list.h"
#include "src/natives.h"
#include "src/serialize.h"
+ Include dependency graph for mksnapshot.cc:

Go to the source code of this file.

Classes

class  Compressor
 
class  SnapshotWriter
 

Functions

void DumpException (Handle< Message > message)
 
int main (int argc, char **argv)
 

Function Documentation

◆ DumpException()

void DumpException ( Handle< Message message)

Definition at line 294 of file mksnapshot.cc.

294  {
295  String::Utf8Value message_string(message->Get());
296  String::Utf8Value message_line(message->GetSourceLine());
297  fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber());
298  fprintf(stderr, "%s\n", *message_line);
299  for (int i = 0; i <= message->GetEndColumn(); ++i) {
300  fprintf(stderr, "%c", i < message->GetStartColumn() ? ' ' : '^');
301  }
302  fprintf(stderr, "\n");
303 }
Converts an object to a UTF-8-encoded character array.
Definition: v8.h:2048

Referenced by main().

+ Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 306 of file mksnapshot.cc.

306  {
307  // By default, log code create information in the snapshot.
308  i::FLAG_log_code = true;
309 
310  // Print the usage if an error occurs when parsing the command line
311  // flags or if the help flag is set.
312  int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
313  if (result > 0 || argc != 2 || i::FLAG_help) {
314  ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
316  return !i::FLAG_help;
317  }
318 
319  i::CpuFeatures::Probe(true);
322  v8::V8::InitializePlatform(platform);
324 
325 #ifdef COMPRESS_STARTUP_DATA_BZ2
326  BZip2Decompressor natives_decompressor;
327  int bz2_result = natives_decompressor.Decompress();
328  if (bz2_result != BZ_OK) {
329  fprintf(stderr, "bzip error code: %d\n", bz2_result);
330  exit(1);
331  }
332 #endif
333  i::FLAG_logfile_per_isolate = false;
334 
335  Isolate::CreateParams params;
336  params.enable_serializer = true;
337  Isolate* isolate = v8::Isolate::New(params);
338  { Isolate::Scope isolate_scope(isolate);
339  i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
340 
341  Persistent<Context> context;
342  {
343  HandleScope handle_scope(isolate);
344  context.Reset(isolate, Context::New(isolate));
345  }
346 
347  if (context.IsEmpty()) {
348  fprintf(stderr,
349  "\nException thrown while compiling natives - see above.\n\n");
350  exit(1);
351  }
352  if (i::FLAG_extra_code != NULL) {
353  // Capture 100 frames if anything happens.
354  V8::SetCaptureStackTraceForUncaughtExceptions(true, 100);
355  HandleScope scope(isolate);
356  v8::Context::Scope cscope(v8::Local<v8::Context>::New(isolate, context));
357  const char* name = i::FLAG_extra_code;
358  FILE* file = base::OS::FOpen(name, "rb");
359  if (file == NULL) {
360  fprintf(stderr, "Failed to open '%s': errno %d\n", name, errno);
361  exit(1);
362  }
363 
364  fseek(file, 0, SEEK_END);
365  int size = ftell(file);
366  rewind(file);
367 
368  char* chars = new char[size + 1];
369  chars[size] = '\0';
370  for (int i = 0; i < size;) {
371  int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
372  if (read < 0) {
373  fprintf(stderr, "Failed to read '%s': errno %d\n", name, errno);
374  exit(1);
375  }
376  i += read;
377  }
378  fclose(file);
379  Local<String> source = String::NewFromUtf8(isolate, chars);
380  TryCatch try_catch;
381  Local<Script> script = Script::Compile(source);
382  if (try_catch.HasCaught()) {
383  fprintf(stderr, "Failure compiling '%s'\n", name);
384  DumpException(try_catch.Message());
385  exit(1);
386  }
387  script->Run();
388  if (try_catch.HasCaught()) {
389  fprintf(stderr, "Failure running '%s'\n", name);
390  DumpException(try_catch.Message());
391  exit(1);
392  }
393  }
394  // Make sure all builtin scripts are cached.
395  { HandleScope scope(isolate);
396  for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
397  internal_isolate->bootstrapper()->NativesSourceLookup(i);
398  }
399  }
400  // If we don't do this then we end up with a stray root pointing at the
401  // context even after we have disposed of the context.
402  internal_isolate->heap()->CollectAllGarbage(
403  i::Heap::kNoGCFlags, "mksnapshot");
404  i::Object* raw_context = *v8::Utils::OpenPersistent(context);
405  context.Reset();
406 
407  // This results in a somewhat smaller snapshot, probably because it gets
408  // rid of some things that are cached between garbage collections.
409  i::List<i::byte> snapshot_data;
410  i::ListSnapshotSink snapshot_sink(&snapshot_data);
411  i::StartupSerializer ser(internal_isolate, &snapshot_sink);
412  ser.SerializeStrongReferences();
413 
414  i::List<i::byte> context_data;
415  i::ListSnapshotSink contex_sink(&context_data);
416  i::PartialSerializer context_ser(internal_isolate, &ser, &contex_sink);
417  context_ser.Serialize(&raw_context);
418  ser.SerializeWeakReferences();
419 
420  {
421  SnapshotWriter writer(argv[1]);
422  if (i::FLAG_raw_file && i::FLAG_raw_context_file)
423  writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file);
424  if (i::FLAG_startup_blob)
425  writer.SetStartupBlobFile(i::FLAG_startup_blob);
426  #ifdef COMPRESS_STARTUP_DATA_BZ2
427  BZip2Compressor bzip2;
428  writer.SetCompressor(&bzip2);
429  #endif
430  writer.WriteSnapshot(snapshot_data, ser, context_data, context_ser);
431  }
432  }
433 
434  isolate->Dispose();
435  V8::Dispose();
436  V8::ShutdownPlatform();
437  delete platform;
438  return 0;
439 }
Stack-allocated class which sets the execution context for all operations executed within a local sco...
Definition: v8.h:5579
A stack-allocated class that governs a number of local handles.
Definition: v8.h:802
Stack-allocated class which sets the isolate for all operations executed within a local scope.
Definition: v8.h:4398
Isolate represents an isolated instance of the V8 engine.
Definition: v8.h:4356
void Dispose()
Disposes the isolate.
Definition: api.cc:6609
static Isolate * New(const CreateParams &params=CreateParams())
Creates a new isolate.
Definition: api.cc:6583
A light-weight stack-allocated object handle.
Definition: v8.h:334
void Reset()
If non-empty, destroy the underlying storage cell IsEmpty() will return true after this call.
Definition: v8.h:6082
bool IsEmpty() const
Definition: v8.h:469
A PersistentBase which allows copy and assignment.
Definition: v8.h:627
V8 Platform abstraction layer.
Definition: v8-platform.h:28
An external exception handler.
Definition: v8.h:5271
Local< v8::Message > Message() const
Returns the message associated with this exception.
Definition: api.cc:2012
bool HasCaught() const
Returns true if an exception has been caught by this try/catch block.
Definition: api.cc:1954
static v8::internal::Handle< v8::internal::Object > OpenPersistent(const v8::Persistent< T > &persistent)
Definition: api.h:275
static void InitializePlatform(Platform *platform)
Sets the v8::Platform to use.
Definition: api.cc:5048
static bool Initialize()
Initializes V8.
Definition: api.cc:5058
static void Probe(bool cross_compile)
Definition: assembler.h:172
static int SetFlagsFromCommandLine(int *argc, char **argv, bool remove_flags)
Definition: flags.cc:334
static void PrintHelp()
Definition: flags.cc:516
static const int kNoGCFlags
Definition: heap.h:716
void CollectAllGarbage(int flags, const char *gc_reason=NULL, const GCCallbackFlags gc_callback_flags=kNoGCCallbackFlags)
Definition: heap.cc:724
Bootstrapper * bootstrapper()
Definition: isolate.h:856
enable harmony numeric enable harmony object literal extensions Optimize object size
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in name
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in only print modified registers Trace simulator debug messages Implied by trace sim abort randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot A filename with extra code to be included in the A file to write the raw snapshot bytes A file to write the raw context snapshot bytes Write V8 startup blob file(mksnapshot only)") DEFINE_BOOL(profile_hydrogen_code_stub_compilation
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
void DumpException(Handle< Message > message)
Definition: mksnapshot.cc:294
bool InitializeICU(const char *icu_data_file)
Definition: icu_util.cc:41
v8::Platform * CreateDefaultPlatform(int thread_pool_size)
Returns a new instance of the default v8::Platform implementation.
static FILE * FOpen(const char *path, const char *mode)
Definition: d8.cc:1056
Initial configuration parameters for a new Isolate.
Definition: v8.h:4361
bool enable_serializer
This flag currently renders the Isolate unusable.
Definition: v8.h:4390

References v8::internal::Isolate::bootstrapper(), v8::internal::Heap::CollectAllGarbage(), v8::Script::Compile(), v8::platform::CreateDefaultPlatform(), v8::Isolate::Dispose(), v8::V8::Dispose(), DumpException(), v8::Isolate::CreateParams::enable_serializer, file(), v8::base::OS::FOpen(), v8::internal::NativesCollection< type >::GetBuiltinsCount(), v8::TryCatch::HasCaught(), v8::internal::Isolate::heap(), v8::V8::Initialize(), v8::V8::InitializeICU(), v8::V8::InitializePlatform(), v8::PersistentBase< T >::IsEmpty(), v8::internal::Heap::kNoGCFlags, v8::TryCatch::Message(), name, v8::Isolate::New(), v8::Context::New(), v8::String::NewFromUtf8(), NULL, v8::Utils::OpenPersistent(), v8::internal::FlagList::PrintHelp(), v8::internal::CpuFeatures::Probe(), v8::PersistentBase< T >::Reset(), v8::internal::PartialSerializer::Serialize(), v8::internal::StartupSerializer::SerializeStrongReferences(), v8::internal::StartupSerializer::SerializeWeakReferences(), v8::V8::SetCaptureStackTraceForUncaughtExceptions(), SnapshotWriter::SetCompressor(), v8::internal::FlagList::SetFlagsFromCommandLine(), SnapshotWriter::SetRawFiles(), SnapshotWriter::SetStartupBlobFile(), v8::V8::ShutdownPlatform(), size, and SnapshotWriter::WriteSnapshot().

+ Here is the call graph for this function: