OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 | 73 |
74 static bool IsValidFlag(const char* name, | 74 static bool IsValidFlag(const char* name, |
75 const char* prefix, | 75 const char* prefix, |
76 intptr_t prefix_length) { | 76 intptr_t prefix_length) { |
77 intptr_t name_length = strlen(name); | 77 intptr_t name_length = strlen(name); |
78 return ((name_length > prefix_length) && | 78 return ((name_length > prefix_length) && |
79 (strncmp(name, prefix, prefix_length) == 0)); | 79 (strncmp(name, prefix, prefix_length) == 0)); |
80 } | 80 } |
81 | 81 |
82 | 82 |
83 static void ProcessBreakpointOption(const char* funcname) { | 83 static bool has_help_option = false; |
84 ASSERT(funcname != NULL); | 84 static bool ProcessHelpOption(const char* arg) { |
85 breakpoint_at = funcname; | 85 if (*arg != '\0') { |
86 return false; | |
87 } | |
88 has_help_option = true; | |
89 return true; | |
86 } | 90 } |
87 | 91 |
88 | 92 |
89 static void ProcessPackageRootOption(const char* arg) { | 93 static bool has_verbose_option = false; |
90 ASSERT(arg != NULL); | 94 static bool ProcessVerboseOption(const char* arg) { |
91 package_root = arg; | 95 if (*arg != '\0') { |
96 return false; | |
97 } | |
98 has_verbose_option = true; | |
99 return true; | |
92 } | 100 } |
93 | 101 |
94 | 102 |
95 static void ProcessCompileAllOption(const char* compile_all) { | 103 static bool ProcessBreakpointOption(const char* funcname) { |
96 ASSERT(compile_all != NULL); | 104 ASSERT(funcname != NULL); |
97 has_compile_all = true; | 105 breakpoint_at = funcname; |
106 return true; | |
98 } | 107 } |
99 | 108 |
100 | 109 |
101 static void ProcessDebugOption(const char* port) { | 110 static bool ProcessPackageRootOption(const char* arg) { |
111 ASSERT(arg != NULL); | |
112 package_root = arg; | |
113 return true; | |
114 } | |
115 | |
116 | |
117 static bool ProcessCompileAllOption(const char* arg) { | |
118 ASSERT(arg != NULL); | |
119 if (*arg != '\0') { | |
120 return false; | |
121 } | |
122 has_compile_all = true; | |
123 return true; | |
124 } | |
125 | |
126 | |
127 static bool ProcessDebugOption(const char* port) { | |
102 // TODO(hausner): Add support for specifying an IP address on which | 128 // TODO(hausner): Add support for specifying an IP address on which |
103 // the debugger should listen. | 129 // the debugger should listen. |
104 ASSERT(port != NULL); | 130 ASSERT(port != NULL); |
105 debug_port = 0; | 131 debug_port = 0; |
106 if (*port == '\0') { | 132 if (*port == '\0') { |
107 debug_port = DEFAULT_DEBUG_PORT; | 133 debug_port = DEFAULT_DEBUG_PORT; |
108 } else { | 134 } else { |
109 if ((*port == '=') || (*port == ':')) { | 135 if ((*port == '=') || (*port == ':')) { |
110 debug_port = atoi(port + 1); | 136 debug_port = atoi(port + 1); |
111 } | 137 } |
112 } | 138 } |
113 if (debug_port == 0) { | 139 if (debug_port == 0) { |
114 fprintf(stderr, "unrecognized --debug option syntax. " | 140 fprintf(stderr, "unrecognized --debug option syntax. " |
115 "Use --debug[:<port number>]\n"); | 141 "Use --debug[:<port number>]\n"); |
116 return; | 142 return false; |
117 } | 143 } |
118 breakpoint_at = "main"; | 144 breakpoint_at = "main"; |
119 start_debugger = true; | 145 start_debugger = true; |
146 return true; | |
120 } | 147 } |
121 | 148 |
122 | 149 |
123 static void ProcessPerfEventsOption(const char* option) { | 150 static bool ProcessPerfEventsOption(const char* option) { |
124 ASSERT(option != NULL); | 151 ASSERT(option != NULL); |
125 if (perf_events_symbols_file == NULL) { | 152 if (perf_events_symbols_file == NULL) { |
126 // TODO(cshapiro): eliminate the #ifdef by moving this code to a | 153 // TODO(cshapiro): eliminate the #ifdef by moving this code to a |
127 // Linux specific source file. | 154 // Linux specific source file. |
128 #if defined(TARGET_OS_LINUX) | 155 #if defined(TARGET_OS_LINUX) |
129 const char* format = "/tmp/perf-%ld.map"; | 156 const char* format = "/tmp/perf-%ld.map"; |
130 intptr_t pid = Process::CurrentProcessId(); | 157 intptr_t pid = Process::CurrentProcessId(); |
131 intptr_t len = snprintf(NULL, 0, format, pid); | 158 intptr_t len = snprintf(NULL, 0, format, pid); |
132 char* filename = new char[len + 1]; | 159 char* filename = new char[len + 1]; |
133 snprintf(filename, len + 1, format, pid); | 160 snprintf(filename, len + 1, format, pid); |
134 perf_events_symbols_file = File::Open(filename, File::kWriteTruncate); | 161 perf_events_symbols_file = File::Open(filename, File::kWriteTruncate); |
135 ASSERT(perf_events_symbols_file != NULL); | 162 ASSERT(perf_events_symbols_file != NULL); |
136 delete[] filename; | 163 delete[] filename; |
137 #endif | 164 #endif |
138 } | 165 } |
166 return true; | |
139 } | 167 } |
140 | 168 |
141 | 169 |
142 static void ProcessPprofOption(const char* filename) { | 170 static bool ProcessPprofOption(const char* filename) { |
143 ASSERT(filename != NULL); | 171 ASSERT(filename != NULL); |
144 generate_pprof_symbols_filename = filename; | 172 generate_pprof_symbols_filename = filename; |
173 return true; | |
145 } | 174 } |
146 | 175 |
147 | 176 |
148 static void ProcessScriptSnapshotOption(const char* filename) { | 177 static bool ProcessScriptSnapshotOption(const char* filename) { |
149 if (filename != NULL && strlen(filename) != 0) { | 178 if (filename != NULL && strlen(filename) != 0) { |
150 use_script_snapshot = true; | 179 use_script_snapshot = true; |
151 snapshot_file = File::Open(filename, File::kRead); | 180 snapshot_file = File::Open(filename, File::kRead); |
152 } | 181 } |
182 return true; | |
153 } | 183 } |
154 | 184 |
155 | 185 |
156 static void ProcessFlowGraphOption(const char* flowgraph_option) { | 186 static bool ProcessFlowGraphOption(const char* flowgraph_option) { |
157 ASSERT(flowgraph_option != NULL); | 187 ASSERT(flowgraph_option != NULL); |
158 flow_graph_file = File::Open("flowgraph.cfg", File::kWriteTruncate); | 188 flow_graph_file = File::Open("flowgraph.cfg", File::kWriteTruncate); |
159 ASSERT(flow_graph_file != NULL); | 189 ASSERT(flow_graph_file != NULL); |
190 return true; | |
160 } | 191 } |
161 | 192 |
162 | 193 |
163 static struct { | 194 static struct { |
164 const char* option_name; | 195 const char* option_name; |
165 void (*process)(const char* option); | 196 bool (*process)(const char* option); |
166 } main_options[] = { | 197 } main_options[] = { |
198 // Standard options shared with dart2js. | |
199 { "--help", ProcessHelpOption }, | |
200 { "-h", ProcessHelpOption }, | |
201 { "--verbose", ProcessVerboseOption }, | |
202 { "-v", ProcessVerboseOption }, | |
203 { "--package-root=", ProcessPackageRootOption }, | |
204 { "-p", ProcessPackageRootOption }, | |
205 // VM specific options to the standalone dart program. | |
167 { "--break_at=", ProcessBreakpointOption }, | 206 { "--break_at=", ProcessBreakpointOption }, |
168 { "--compile_all", ProcessCompileAllOption }, | 207 { "--compile_all", ProcessCompileAllOption }, |
169 { "--debug", ProcessDebugOption }, | 208 { "--debug", ProcessDebugOption }, |
170 { "--generate_flow_graph", ProcessFlowGraphOption }, | 209 { "--generate_flow_graph", ProcessFlowGraphOption }, |
171 { "--generate_perf_events_symbols", ProcessPerfEventsOption }, | 210 { "--generate_perf_events_symbols", ProcessPerfEventsOption }, |
172 { "--generate_pprof_symbols=", ProcessPprofOption }, | 211 { "--generate_pprof_symbols=", ProcessPprofOption }, |
173 { "--package-root=", ProcessPackageRootOption }, | |
174 { "--use_script_snapshot=", ProcessScriptSnapshotOption }, | 212 { "--use_script_snapshot=", ProcessScriptSnapshotOption }, |
175 { NULL, NULL } | 213 { NULL, NULL } |
176 }; | 214 }; |
177 | 215 |
178 | 216 |
179 static bool ProcessMainOptions(const char* option) { | 217 static bool ProcessMainOptions(const char* option) { |
180 int i = 0; | 218 int i = 0; |
181 const char* name = main_options[0].option_name; | 219 const char* name = main_options[0].option_name; |
182 while (name != NULL) { | 220 while (name != NULL) { |
183 int length = strlen(name); | 221 int length = strlen(name); |
184 if (strncmp(option, name, length) == 0) { | 222 if (strncmp(option, name, length) == 0) { |
185 main_options[i].process(option + length); | 223 return main_options[i].process(option + length); |
186 return true; | |
187 } | 224 } |
188 i += 1; | 225 i += 1; |
189 name = main_options[i].option_name; | 226 name = main_options[i].option_name; |
190 } | 227 } |
191 return false; | 228 return false; |
192 } | 229 } |
193 | 230 |
194 | 231 |
195 static void WriteToPerfEventsFile(const char* buffer, int64_t num_bytes) { | 232 static void WriteToPerfEventsFile(const char* buffer, int64_t num_bytes) { |
196 ASSERT(perf_events_symbols_file != NULL); | 233 ASSERT(perf_events_symbols_file != NULL); |
(...skipping 19 matching lines...) Expand all Loading... | |
216 const char* kPrefix = "--"; | 253 const char* kPrefix = "--"; |
217 const intptr_t kPrefixLen = strlen(kPrefix); | 254 const intptr_t kPrefixLen = strlen(kPrefix); |
218 | 255 |
219 // Get the executable name. | 256 // Get the executable name. |
220 *executable_name = argv[0]; | 257 *executable_name = argv[0]; |
221 | 258 |
222 // Start the rest after the executable name. | 259 // Start the rest after the executable name. |
223 int i = 1; | 260 int i = 1; |
224 | 261 |
225 // Parse out the vm options. | 262 // Parse out the vm options. |
226 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { | 263 while (i < argc) { |
227 if (ProcessMainOptions(argv[i])) { | 264 if (ProcessMainOptions(argv[i])) { |
228 i++; | 265 i++; |
229 } else { | 266 } else { |
267 // Check if this flag is a potentially valid VM flag. | |
268 if (!IsValidFlag(argv[i], kPrefix, kPrefixLen)) { | |
269 break; | |
270 } | |
230 const char* kPrintFlags1 = "--print-flags"; | 271 const char* kPrintFlags1 = "--print-flags"; |
231 const char* kPrintFlags2 = "--print_flags"; | 272 const char* kPrintFlags2 = "--print_flags"; |
232 if ((strncmp(argv[i], kPrintFlags1, strlen(kPrintFlags1)) == 0) || | 273 if ((strncmp(argv[i], kPrintFlags1, strlen(kPrintFlags1)) == 0) || |
233 (strncmp(argv[i], kPrintFlags2, strlen(kPrintFlags2)) == 0)) { | 274 (strncmp(argv[i], kPrintFlags2, strlen(kPrintFlags2)) == 0)) { |
234 *print_flags_seen = true; | 275 *print_flags_seen = true; |
235 } | 276 } |
236 vm_options->AddArgument(argv[i]); | 277 vm_options->AddArgument(argv[i]); |
237 i++; | 278 i++; |
238 } | 279 } |
239 } | 280 } |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 return CreateIsolateAndSetupHelper(script_uri, | 503 return CreateIsolateAndSetupHelper(script_uri, |
463 main, | 504 main, |
464 false, // script_uri already canonical. | 505 false, // script_uri already canonical. |
465 new IsolateData(), | 506 new IsolateData(), |
466 error); | 507 error); |
467 } | 508 } |
468 | 509 |
469 | 510 |
470 static void PrintUsage() { | 511 static void PrintUsage() { |
471 fprintf(stderr, | 512 fprintf(stderr, |
472 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); | 513 "Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]\n" |
514 "\n" | |
515 "Executes the Dart script passed as <dart-script-file>.\n" | |
516 "\n"); | |
517 if (!has_verbose_option) { | |
518 fprintf(stderr, | |
519 "Common options:\n" | |
520 "--checked Insert runtime type checks and enable assertions (checked mode).\n" | |
521 "--help Display this message (add --verbose for information about all\n" | |
522 " VM options).\n"); | |
523 } else { | |
524 fprintf(stderr, | |
525 "Supported options:\n" | |
526 "--checked \n" | |
527 " Insert runtime type checks and enable assertions (checked mode).\n" | |
528 "\n" | |
529 "--help\n" | |
530 " Display this message (add --verbose for information about all VM options).\n" | |
531 "\n" | |
532 "--package-root=<path>\n" | |
533 " Where to find packages, that is, \"package:...\" imports.\n" | |
534 "\n" | |
535 "The following options are only used for VM development and may\n" | |
536 "be changed in any future version:\n"); | |
537 const char* print_flags = "--print_flags"; | |
538 Dart_SetVMFlags(1, &print_flags); | |
539 } | |
473 } | 540 } |
474 | 541 |
475 | 542 |
476 static Dart_Handle SetBreakpoint(const char* breakpoint_at, | 543 static Dart_Handle SetBreakpoint(const char* breakpoint_at, |
477 Dart_Handle library) { | 544 Dart_Handle library) { |
478 Dart_Handle result; | 545 Dart_Handle result; |
479 if (strchr(breakpoint_at, ':')) { | 546 if (strchr(breakpoint_at, ':')) { |
480 char* bpt_line = strdup(breakpoint_at); | 547 char* bpt_line = strdup(breakpoint_at); |
481 char* colon = strchr(bpt_line, ':'); | 548 char* colon = strchr(bpt_line, ':'); |
482 ASSERT(colon != NULL); | 549 ASSERT(colon != NULL); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
573 // Will set the VM flags, print them out and then we exit as no | 640 // Will set the VM flags, print them out and then we exit as no |
574 // script was specified on the command line. | 641 // script was specified on the command line. |
575 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 642 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
576 return 0; | 643 return 0; |
577 } else { | 644 } else { |
578 PrintUsage(); | 645 PrintUsage(); |
579 return kErrorExitCode; | 646 return kErrorExitCode; |
580 } | 647 } |
581 } | 648 } |
582 | 649 |
650 if (has_help_option) { | |
651 PrintUsage(); | |
siva
2012/09/24 22:25:29
return 0;
Ivan Posva
2012/09/24 22:26:23
Done.
| |
652 } | |
653 | |
583 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 654 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
584 | 655 |
585 // Initialize the Dart VM. | 656 // Initialize the Dart VM. |
586 Dart_Initialize(CreateIsolateAndSetup, | 657 Dart_Initialize(CreateIsolateAndSetup, |
587 NULL, | 658 NULL, |
588 ShutdownIsolate); | 659 ShutdownIsolate); |
589 | 660 |
590 DartUtils::SetOriginalWorkingDirectory(); | 661 DartUtils::SetOriginalWorkingDirectory(); |
591 | 662 |
592 // Call CreateIsolateAndSetup which creates an isolate and loads up | 663 // Call CreateIsolateAndSetup which creates an isolate and loads up |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
660 Dart_ExitScope(); | 731 Dart_ExitScope(); |
661 // Dump symbol information for the profiler. | 732 // Dump symbol information for the profiler. |
662 DumpPprofSymbolInfo(); | 733 DumpPprofSymbolInfo(); |
663 // Shutdown the isolate. | 734 // Shutdown the isolate. |
664 Dart_ShutdownIsolate(); | 735 Dart_ShutdownIsolate(); |
665 // Terminate process exit-code handler. | 736 // Terminate process exit-code handler. |
666 Process::TerminateExitCodeHandler(); | 737 Process::TerminateExitCodeHandler(); |
667 | 738 |
668 return 0; | 739 return 0; |
669 } | 740 } |
OLD | NEW |