Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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_debugger_api.h" | 10 #include "include/dart_debugger_api.h" |
| 11 | 11 |
| 12 #include "bin/builtin.h" | 12 #include "bin/builtin.h" |
| 13 #include "bin/dartutils.h" | 13 #include "bin/dartutils.h" |
| 14 #include "bin/file.h" | 14 #include "bin/file.h" |
| 15 #include "bin/globals.h" | 15 #include "bin/globals.h" |
| 16 #include "bin/platform.h" | 16 #include "bin/platform.h" |
| 17 | 17 |
| 18 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | 18 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 19 // it is initialized to NULL. | 19 // it is initialized to NULL. |
| 20 extern const uint8_t* snapshot_buffer; | 20 extern const uint8_t* snapshot_buffer; |
| 21 | 21 |
| 22 | 22 |
| 23 // Global state that stores a pointer to the application script file. | 23 // Global state that stores a pointer to the application script file. |
| 24 static char* canonical_script_name = NULL; | 24 static char* canonical_script_name = NULL; |
| 25 | 25 |
| 26 | 26 |
| 27 // Global state that stores a pointer to the application script snapshot. | |
| 28 static bool use_script_snapshot = false; | |
| 29 static uint8_t* script_snapshot_buffer = NULL; | |
| 30 | |
| 31 | |
| 27 // Global state that indicates whether pprof symbol information is | 32 // Global state that indicates whether pprof symbol information is |
| 28 // to be generated or not. | 33 // to be generated or not. |
| 29 static const char* generate_pprof_symbols_filename = NULL; | 34 static const char* generate_pprof_symbols_filename = NULL; |
| 30 | 35 |
| 31 | 36 |
| 32 // Global state that indicates whether there is a debug breakpoint. | 37 // Global state that indicates whether there is a debug breakpoint. |
| 33 // This pointer points into an argv buffer and does not need to be | 38 // This pointer points into an argv buffer and does not need to be |
| 34 // free'd. | 39 // free'd. |
| 35 static const char* breakpoint_at = NULL; | 40 static const char* breakpoint_at = NULL; |
| 36 | 41 |
| 37 | 42 |
| 38 static bool IsValidFlag(const char* name, | 43 static bool IsValidFlag(const char* name, |
| 39 const char* prefix, | 44 const char* prefix, |
| 40 intptr_t prefix_length) { | 45 intptr_t prefix_length) { |
| 41 intptr_t name_length = strlen(name); | 46 intptr_t name_length = strlen(name); |
| 42 return ((name_length > prefix_length) && | 47 return ((name_length > prefix_length) && |
| 43 (strncmp(name, prefix, prefix_length) == 0)); | 48 (strncmp(name, prefix, prefix_length) == 0)); |
| 44 } | 49 } |
| 45 | 50 |
| 46 | 51 |
| 47 static const char* ProcessOption(const char* option, const char* name) { | 52 static void ProcessPprofOption(const char* filename) { |
| 48 const intptr_t length = strlen(name); | 53 ASSERT(filename != NULL); |
| 49 if (strncmp(option, name, length) == 0) { | 54 generate_pprof_symbols_filename = filename; |
| 50 return (option + length); | |
| 51 } | |
| 52 return NULL; | |
| 53 } | 55 } |
| 54 | 56 |
| 55 | 57 |
| 56 static bool ProcessPprofOption(const char* option) { | 58 static void ProcessBreakpointOption(const char* funcname) { |
| 57 const char* kProfOption = "--generate_pprof_symbols="; | 59 ASSERT(funcname != NULL); |
| 58 const char* filename = ProcessOption(option, kProfOption); | 60 breakpoint_at = funcname; |
| 59 if (filename != NULL) { | |
| 60 generate_pprof_symbols_filename = filename; | |
| 61 } | |
| 62 return filename != NULL; | |
| 63 } | 61 } |
| 64 | 62 |
| 65 | 63 |
| 66 static bool ProcessDebugOption(const char* option) { | 64 static void ProcessSnapshotOption(const char* snapshot) { |
| 67 const char* kDebugOption = "--break_at="; | 65 ASSERT(snapshot != NULL); |
| 68 const char* func_name = ProcessOption(option, kDebugOption); | 66 use_script_snapshot = true; |
| 69 if (func_name != NULL) { | |
| 70 breakpoint_at = func_name; | |
| 71 } | |
| 72 return func_name != NULL; | |
| 73 } | 67 } |
| 74 | 68 |
| 75 | 69 |
| 70 static struct { | |
| 71 const char* option_name; | |
| 72 void (*process)(const char* option); | |
| 73 } main_options[] = { | |
| 74 { "--generate_pprof_symbols=", ProcessPprofOption }, | |
| 75 { "--break_at=", ProcessBreakpointOption }, | |
| 76 { "--use_script_snapshot", ProcessSnapshotOption }, | |
|
Ivan Posva
2011/12/19 21:32:42
What about compile-all? In general we should figur
siva
2011/12/19 23:50:00
Added --compile_all to this list.
As discussed off
| |
| 77 { NULL, NULL } | |
| 78 }; | |
| 79 | |
| 80 | |
| 81 static bool ProcessMainOptions(const char* option) { | |
| 82 int i = 0; | |
| 83 const char* name = main_options[0].option_name; | |
| 84 while (name != NULL) { | |
| 85 int length = strlen(name); | |
| 86 if (strncmp(option, name, length) == 0) { | |
| 87 main_options[i].process(option + length); | |
| 88 return true; | |
| 89 } | |
| 90 i += 1; | |
| 91 name = main_options[i].option_name; | |
| 92 } | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 | |
| 76 // Parse out the command line arguments. Returns -1 if the arguments | 97 // Parse out the command line arguments. Returns -1 if the arguments |
| 77 // are incorrect, 0 otherwise. | 98 // are incorrect, 0 otherwise. |
| 78 static int ParseArguments(int argc, | 99 static int ParseArguments(int argc, |
| 79 char** argv, | 100 char** argv, |
| 80 CommandLineOptions* vm_options, | 101 CommandLineOptions* vm_options, |
| 81 char** script_name, | 102 char** script_name, |
| 82 CommandLineOptions* dart_options) { | 103 CommandLineOptions* dart_options) { |
| 83 const char* kPrefix = "--"; | 104 const char* kPrefix = "--"; |
| 84 const intptr_t kPrefixLen = strlen(kPrefix); | 105 const intptr_t kPrefixLen = strlen(kPrefix); |
| 85 | 106 |
| 86 // Skip the binary name. | 107 // Skip the binary name. |
| 87 int i = 1; | 108 int i = 1; |
| 88 | 109 |
| 89 // Parse out the vm options. | 110 // Parse out the vm options. |
| 90 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { | 111 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { |
| 91 if (ProcessPprofOption(argv[i])) { | 112 if (ProcessMainOptions(argv[i])) { |
| 92 i += 1; | |
| 93 } else if (ProcessDebugOption(argv[i])) { | |
| 94 i += 1; | 113 i += 1; |
| 95 } else { | 114 } else { |
| 96 vm_options->AddArgument(argv[i]); | 115 vm_options->AddArgument(argv[i]); |
| 97 i += 1; | 116 i += 1; |
| 98 } | 117 } |
| 99 } | 118 } |
| 100 if (generate_pprof_symbols_filename != NULL) { | 119 if (generate_pprof_symbols_filename != NULL) { |
| 101 Dart_InitPprofSupport(); | 120 Dart_InitPprofSupport(); |
| 102 } | 121 } |
| 103 | 122 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 static bool CreateIsolateAndSetup(void* data, char** error) { | 249 static bool CreateIsolateAndSetup(void* data, char** error) { |
| 231 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, data, error); | 250 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, data, error); |
| 232 if (isolate == NULL) { | 251 if (isolate == NULL) { |
| 233 return false; | 252 return false; |
| 234 } | 253 } |
| 235 | 254 |
| 236 Dart_Handle library; | 255 Dart_Handle library; |
| 237 Dart_EnterScope(); | 256 Dart_EnterScope(); |
| 238 | 257 |
| 239 // Load the specified application script into the newly created isolate. | 258 // Load the specified application script into the newly created isolate. |
| 240 library = LoadScript(canonical_script_name); | 259 if (script_snapshot_buffer != NULL) { |
| 260 library = Dart_LoadScriptFromSnapshot(script_snapshot_buffer); | |
| 261 } else { | |
| 262 library = LoadScript(canonical_script_name); | |
| 263 } | |
| 241 if (Dart_IsError(library)) { | 264 if (Dart_IsError(library)) { |
| 242 *error = strdup(Dart_GetError(library)); | 265 *error = strdup(Dart_GetError(library)); |
| 243 Dart_ExitScope(); | 266 Dart_ExitScope(); |
| 244 Dart_ShutdownIsolate(); | 267 Dart_ShutdownIsolate(); |
| 245 return false; | 268 return false; |
| 246 } | 269 } |
| 247 if (!Dart_IsLibrary(library)) { | 270 if (!Dart_IsLibrary(library)) { |
| 248 char errbuf[256]; | 271 char errbuf[256]; |
| 249 snprintf(errbuf, sizeof(errbuf), | 272 snprintf(errbuf, sizeof(errbuf), |
| 250 "Expected a library when loading script: %s", | 273 "Expected a library when loading script: %s", |
| 251 canonical_script_name); | 274 canonical_script_name); |
| 252 *error = strdup(errbuf); | 275 *error = strdup(errbuf); |
| 253 Dart_ExitScope(); | 276 Dart_ExitScope(); |
| 254 Dart_ShutdownIsolate(); | 277 Dart_ShutdownIsolate(); |
| 255 return false; | 278 return false; |
| 256 } | 279 } |
| 257 Builtin::ImportLibrary(library); // Implicitly import builtin into app. | 280 if (script_snapshot_buffer == NULL) { |
| 281 Builtin::ImportLibrary(library); // Implicitly import builtin into app. | |
| 282 } | |
| 258 if (snapshot_buffer != NULL) { | 283 if (snapshot_buffer != NULL) { |
| 259 // Setup the native resolver as the snapshot does not carry it. | 284 // Setup the native resolver as the snapshot does not carry it. |
| 260 Builtin::SetNativeResolver(); | 285 Builtin::SetNativeResolver(); |
| 261 } | 286 } |
| 262 Dart_ExitScope(); | 287 Dart_ExitScope(); |
| 263 return true; | 288 return true; |
| 264 } | 289 } |
| 265 | 290 |
| 266 | 291 |
| 292 static bool CaptureScriptSnapshot() { | |
| 293 char* error = NULL; | |
| 294 Dart_Handle result; | |
| 295 | |
| 296 // First create an isolate and load up the specified script in it. | |
| 297 if (!CreateIsolateAndSetup(NULL, &error)) { | |
| 298 fprintf(stderr, "%s\n", error); | |
| 299 free(canonical_script_name); | |
| 300 free(error); | |
| 301 return false; // Indicates we encountered an error. | |
| 302 } | |
| 303 | |
| 304 Dart_EnterScope(); | |
| 305 | |
| 306 // Lookup the library of the main script. | |
| 307 Dart_Handle script_url = Dart_NewString(canonical_script_name); | |
| 308 Dart_Handle library = Dart_LookupLibrary(script_url); | |
| 309 if (Dart_IsError(library)) { | |
| 310 fprintf(stderr, "%s\n", Dart_GetError(library)); | |
| 311 Dart_ExitScope(); | |
| 312 Dart_ShutdownIsolate(); | |
| 313 free(canonical_script_name); | |
| 314 return false; // Indicates we encountered an error. | |
| 315 } | |
| 316 | |
| 317 // Now create the script snapshot and save into a buffer. | |
| 318 uint8_t* buffer; | |
| 319 intptr_t size; | |
| 320 result = Dart_CreateScriptSnapshot(library, &buffer, &size); | |
| 321 if (Dart_IsError(result)) { | |
| 322 fprintf(stderr, "%s\n", Dart_GetError(result)); | |
| 323 Dart_ExitScope(); | |
| 324 Dart_ShutdownIsolate(); | |
| 325 return false; // Indicates we encountered an error. | |
| 326 } | |
| 327 | |
| 328 // Save the script snapshot as we are about to shutdown isolate. | |
|
Ivan Posva
2011/12/19 21:32:42
shutdown the isolate.
siva
2011/12/19 23:50:00
Done.
| |
| 329 script_snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); | |
| 330 ASSERT(script_snapshot_buffer != NULL); | |
| 331 memmove(script_snapshot_buffer, buffer, size); | |
| 332 | |
| 333 // Shutdown this isolate. | |
| 334 Dart_ExitScope(); | |
| 335 Dart_ShutdownIsolate(); | |
| 336 return true; | |
| 337 } | |
| 338 | |
| 339 | |
| 267 static void PrintUsage() { | 340 static void PrintUsage() { |
| 268 fprintf(stderr, | 341 fprintf(stderr, |
| 269 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); | 342 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); |
| 270 } | 343 } |
| 271 | 344 |
| 272 | 345 |
| 273 static bool HasCompileAll(const CommandLineOptions& options) { | 346 static bool HasCompileAll(const CommandLineOptions& options) { |
| 274 for (int i = 0; i < options.count(); i++) { | 347 for (int i = 0; i < options.count(); i++) { |
| 275 if (strcmp(options.GetArgument(i), "--compile_all") == 0) { | 348 if (strcmp(options.GetArgument(i), "--compile_all") == 0) { |
| 276 return true; | 349 return true; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 304 | 377 |
| 305 // Initialize the Dart VM. | 378 // Initialize the Dart VM. |
| 306 Dart_Initialize(CreateIsolateAndSetup, NULL); | 379 Dart_Initialize(CreateIsolateAndSetup, NULL); |
| 307 | 380 |
| 308 canonical_script_name = File::GetCanonicalPath(script_name); | 381 canonical_script_name = File::GetCanonicalPath(script_name); |
| 309 if (canonical_script_name == NULL) { | 382 if (canonical_script_name == NULL) { |
| 310 fprintf(stderr, "Unable to find '%s'\n", script_name); | 383 fprintf(stderr, "Unable to find '%s'\n", script_name); |
| 311 return 255; // Indicates we encountered an error. | 384 return 255; // Indicates we encountered an error. |
| 312 } | 385 } |
| 313 | 386 |
| 387 // If application snapshot option is specified, first create the | |
| 388 // application snapshot and then load the script using the snapshot | |
| 389 // created. | |
| 390 if (use_script_snapshot) { | |
| 391 if (!CaptureScriptSnapshot()) { | |
| 392 return 255; // Error capturing script snapshot, error already reported. | |
| 393 } | |
| 394 } | |
| 395 | |
| 314 // Call CreateIsolateAndSetup which creates an isolate and loads up | 396 // Call CreateIsolateAndSetup which creates an isolate and loads up |
| 315 // the specified application script. | 397 // the specified application script. |
| 316 char* error = NULL; | 398 char* error = NULL; |
| 317 if (!CreateIsolateAndSetup(NULL, &error)) { | 399 if (!CreateIsolateAndSetup(NULL, &error)) { |
| 318 fprintf(stderr, "%s\n", error); | 400 fprintf(stderr, "%s\n", error); |
| 319 free(canonical_script_name); | 401 free(canonical_script_name); |
| 320 free(error); | 402 free(error); |
| 403 free(script_snapshot_buffer); | |
| 321 return 255; // Indicates we encountered an error. | 404 return 255; // Indicates we encountered an error. |
| 322 } | 405 } |
| 323 | 406 |
| 407 free(script_snapshot_buffer); // Don't need it anymore. | |
| 324 Dart_Isolate isolate = Dart_CurrentIsolate(); | 408 Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 325 ASSERT(isolate != NULL); | 409 ASSERT(isolate != NULL); |
| 326 Dart_Handle result; | 410 Dart_Handle result; |
| 327 | 411 |
| 328 Dart_EnterScope(); | 412 Dart_EnterScope(); |
| 329 | 413 |
| 330 if (HasCompileAll(vm_options)) { | 414 if (HasCompileAll(vm_options)) { |
| 331 result = Dart_CompileAll(); | 415 result = Dart_CompileAll(); |
| 332 if (Dart_IsError(result)) { | 416 if (Dart_IsError(result)) { |
| 333 fprintf(stderr, "%s\n", Dart_GetError(result)); | 417 fprintf(stderr, "%s\n", Dart_GetError(result)); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 return 255; // Indicates we encountered an error. | 493 return 255; // Indicates we encountered an error. |
| 410 } | 494 } |
| 411 free(canonical_script_name); | 495 free(canonical_script_name); |
| 412 Dart_ExitScope(); | 496 Dart_ExitScope(); |
| 413 // Dump symbol information for the profiler. | 497 // Dump symbol information for the profiler. |
| 414 DumpPprofSymbolInfo(); | 498 DumpPprofSymbolInfo(); |
| 415 // Shutdown the isolate. | 499 // Shutdown the isolate. |
| 416 Dart_ShutdownIsolate(); | 500 Dart_ShutdownIsolate(); |
| 417 return 0; | 501 return 0; |
| 418 } | 502 } |
| OLD | NEW |