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 | 11 |
11 #include "bin/builtin.h" | 12 #include "bin/builtin.h" |
12 #include "bin/dartutils.h" | 13 #include "bin/dartutils.h" |
13 #include "bin/file.h" | 14 #include "bin/file.h" |
14 #include "bin/globals.h" | 15 #include "bin/globals.h" |
15 #include "bin/platform.h" | 16 #include "bin/platform.h" |
16 | 17 |
17 // 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 |
18 // it is initialized to NULL. | 19 // it is initialized to NULL. |
19 extern const uint8_t* snapshot_buffer; | 20 extern const uint8_t* snapshot_buffer; |
20 | 21 |
21 | 22 |
22 // Global state that stores a pointer to the application script file. | 23 // Global state that stores a pointer to the application script file. |
23 static char* canonical_script_name = NULL; | 24 static char* canonical_script_name = NULL; |
24 | 25 |
25 | 26 |
26 // Global state that indicates whether pprof symbol information is | 27 // Global state that indicates whether pprof symbol information is |
27 // to be generated or not. | 28 // to be generated or not. |
28 static const char* generate_pprof_symbols_filename = NULL; | 29 static const char* generate_pprof_symbols_filename = NULL; |
29 | 30 |
30 | 31 |
| 32 // Global state that indicates whether there is a debug breakpoint. |
| 33 // This pointer points into an argv buffer and does not need to be |
| 34 // free'd. |
| 35 static const char* breakpoint_at = NULL; |
| 36 |
| 37 |
31 static bool IsValidFlag(const char* name, | 38 static bool IsValidFlag(const char* name, |
32 const char* prefix, | 39 const char* prefix, |
33 intptr_t prefix_length) { | 40 intptr_t prefix_length) { |
34 intptr_t name_length = strlen(name); | 41 intptr_t name_length = strlen(name); |
35 return ((name_length > prefix_length) && | 42 return ((name_length > prefix_length) && |
36 (strncmp(name, prefix, prefix_length) == 0)); | 43 (strncmp(name, prefix, prefix_length) == 0)); |
37 } | 44 } |
38 | 45 |
39 | 46 |
40 static const char* ProcessOption(const char* option, const char* name) { | 47 static const char* ProcessOption(const char* option, const char* name) { |
41 const intptr_t length = strlen(name); | 48 const intptr_t length = strlen(name); |
42 if (strncmp(option, name, length) == 0) { | 49 if (strncmp(option, name, length) == 0) { |
43 return (option + length); | 50 return (option + length); |
44 } | 51 } |
45 return NULL; | 52 return NULL; |
46 } | 53 } |
47 | 54 |
48 | 55 |
49 static bool ProcessPprofOption(const char* option) { | 56 static bool ProcessPprofOption(const char* option) { |
50 const char* kProfOption = "--generate_pprof_symbols="; | 57 const char* kProfOption = "--generate_pprof_symbols="; |
51 const char* filename = ProcessOption(option, kProfOption); | 58 const char* filename = ProcessOption(option, kProfOption); |
52 if (filename != NULL) { | 59 if (filename != NULL) { |
53 generate_pprof_symbols_filename = filename; | 60 generate_pprof_symbols_filename = filename; |
54 } | 61 } |
55 return filename != NULL; | 62 return filename != NULL; |
56 } | 63 } |
57 | 64 |
58 | 65 |
| 66 static bool ProcessDebugOption(const char* option) { |
| 67 const char* kDebugOption = "--break_at="; |
| 68 const char* func_name = ProcessOption(option, kDebugOption); |
| 69 if (func_name != NULL) { |
| 70 breakpoint_at = func_name; |
| 71 } |
| 72 return func_name != NULL; |
| 73 } |
| 74 |
| 75 |
59 // Parse out the command line arguments. Returns -1 if the arguments | 76 // Parse out the command line arguments. Returns -1 if the arguments |
60 // are incorrect, 0 otherwise. | 77 // are incorrect, 0 otherwise. |
61 static int ParseArguments(int argc, | 78 static int ParseArguments(int argc, |
62 char** argv, | 79 char** argv, |
63 CommandLineOptions* vm_options, | 80 CommandLineOptions* vm_options, |
64 char** script_name, | 81 char** script_name, |
65 CommandLineOptions* dart_options) { | 82 CommandLineOptions* dart_options) { |
66 const char* kPrefix = "--"; | 83 const char* kPrefix = "--"; |
67 const intptr_t kPrefixLen = strlen(kPrefix); | 84 const intptr_t kPrefixLen = strlen(kPrefix); |
68 | 85 |
69 // Skip the binary name. | 86 // Skip the binary name. |
70 int i = 1; | 87 int i = 1; |
71 | 88 |
72 // Parse out the vm options. | 89 // Parse out the vm options. |
73 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { | 90 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { |
74 if (ProcessPprofOption(argv[i])) { | 91 if (ProcessPprofOption(argv[i])) { |
75 i += 1; | 92 i += 1; |
76 continue; | 93 } else if (ProcessDebugOption(argv[i])) { |
| 94 i += 1; |
| 95 } else { |
| 96 vm_options->AddArgument(argv[i]); |
| 97 i += 1; |
77 } | 98 } |
78 vm_options->AddArgument(argv[i]); | |
79 i += 1; | |
80 } | 99 } |
81 if (generate_pprof_symbols_filename != NULL) { | 100 if (generate_pprof_symbols_filename != NULL) { |
82 Dart_InitPprofSupport(); | 101 Dart_InitPprofSupport(); |
83 } | 102 } |
84 | 103 |
85 // Get the script name. | 104 // Get the script name. |
86 if (i < argc) { | 105 if (i < argc) { |
87 *script_name = argv[i]; | 106 *script_name = argv[i]; |
88 i += 1; | 107 i += 1; |
89 } else { | 108 } else { |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 // Create a dart options object that can be accessed from dart code. | 341 // Create a dart options object that can be accessed from dart code. |
323 Dart_Handle options_result = SetupRuntimeOptions(&dart_options); | 342 Dart_Handle options_result = SetupRuntimeOptions(&dart_options); |
324 if (Dart_IsError(options_result)) { | 343 if (Dart_IsError(options_result)) { |
325 fprintf(stderr, "%s\n", Dart_GetError(options_result)); | 344 fprintf(stderr, "%s\n", Dart_GetError(options_result)); |
326 Dart_ExitScope(); | 345 Dart_ExitScope(); |
327 Dart_ShutdownIsolate(); | 346 Dart_ShutdownIsolate(); |
328 free(canonical_script_name); | 347 free(canonical_script_name); |
329 return 255; // Indicates we encountered an error. | 348 return 255; // Indicates we encountered an error. |
330 } | 349 } |
331 | 350 |
332 // Lookup and invoke the top level main function. | 351 // Lookup the library of the main script. |
333 Dart_Handle script_url = Dart_NewString(canonical_script_name); | 352 Dart_Handle script_url = Dart_NewString(canonical_script_name); |
334 Dart_Handle library = Dart_LookupLibrary(script_url); | 353 Dart_Handle library = Dart_LookupLibrary(script_url); |
335 if (Dart_IsError(library)) { | 354 if (Dart_IsError(library)) { |
336 fprintf(stderr, "%s\n", Dart_GetError(library)); | 355 fprintf(stderr, "%s\n", Dart_GetError(library)); |
337 Dart_ExitScope(); | 356 Dart_ExitScope(); |
338 Dart_ShutdownIsolate(); | 357 Dart_ShutdownIsolate(); |
339 free(canonical_script_name); | 358 free(canonical_script_name); |
340 return 255; // Indicates we encountered an error. | 359 return 255; // Indicates we encountered an error. |
341 } | 360 } |
| 361 // Set debug breakpoint if specified on the command line. |
| 362 if (breakpoint_at != NULL) { |
| 363 char* bpt_function = strdup(breakpoint_at); |
| 364 Dart_Handle class_name; |
| 365 Dart_Handle function_name; |
| 366 char* dot = strchr(bpt_function, '.'); |
| 367 if (dot == NULL) { |
| 368 class_name = Dart_NewString(""); |
| 369 function_name = Dart_NewString(breakpoint_at); |
| 370 } else { |
| 371 *dot = '\0'; |
| 372 class_name = Dart_NewString(bpt_function); |
| 373 function_name = Dart_NewString(dot + 1); |
| 374 } |
| 375 free(bpt_function); |
| 376 Dart_Breakpoint bpt; |
| 377 result = Dart_SetBreakpointAtEntry( |
| 378 library, class_name, function_name, &bpt); |
| 379 if (Dart_IsError(result)) { |
| 380 fprintf(stderr, "Error setting breakpoint at '%s': %s\n", |
| 381 breakpoint_at, |
| 382 Dart_GetError(result)); |
| 383 Dart_ExitScope(); |
| 384 Dart_ShutdownIsolate(); |
| 385 free(canonical_script_name); |
| 386 return 255; // Indicates we encountered an error. |
| 387 } |
| 388 } |
| 389 // Lookup and invoke the top level main function. |
342 result = Dart_InvokeStatic(library, | 390 result = Dart_InvokeStatic(library, |
343 Dart_NewString(""), | 391 Dart_NewString(""), |
344 Dart_NewString("main"), | 392 Dart_NewString("main"), |
345 0, | 393 0, |
346 NULL); | 394 NULL); |
347 if (Dart_IsError(result)) { | 395 if (Dart_IsError(result)) { |
348 fprintf(stderr, "%s\n", Dart_GetError(result)); | 396 fprintf(stderr, "%s\n", Dart_GetError(result)); |
349 Dart_ExitScope(); | 397 Dart_ExitScope(); |
350 Dart_ShutdownIsolate(); | 398 Dart_ShutdownIsolate(); |
351 free(canonical_script_name); | 399 free(canonical_script_name); |
352 return 255; // Indicates we encountered an error. | 400 return 255; // Indicates we encountered an error. |
353 } | 401 } |
354 // Keep handling messages until the last active receive port is closed. | 402 // Keep handling messages until the last active receive port is closed. |
355 result = Dart_RunLoop(); | 403 result = Dart_RunLoop(); |
356 if (Dart_IsError(result)) { | 404 if (Dart_IsError(result)) { |
357 fprintf(stderr, "%s\n", Dart_GetError(result)); | 405 fprintf(stderr, "%s\n", Dart_GetError(result)); |
358 Dart_ExitScope(); | 406 Dart_ExitScope(); |
359 Dart_ShutdownIsolate(); | 407 Dart_ShutdownIsolate(); |
360 free(canonical_script_name); | 408 free(canonical_script_name); |
361 return 255; // Indicates we encountered an error. | 409 return 255; // Indicates we encountered an error. |
362 } | 410 } |
363 free(canonical_script_name); | 411 free(canonical_script_name); |
364 Dart_ExitScope(); | 412 Dart_ExitScope(); |
365 // Dump symbol information for the profiler. | 413 // Dump symbol information for the profiler. |
366 DumpPprofSymbolInfo(); | 414 DumpPprofSymbolInfo(); |
367 // Shutdown the isolate. | 415 // Shutdown the isolate. |
368 Dart_ShutdownIsolate(); | 416 Dart_ShutdownIsolate(); |
369 return 0; | 417 return 0; |
370 } | 418 } |
OLD | NEW |