| 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" |
| 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/dbg_connection.h" | 14 #include "bin/dbg_connection.h" |
| 15 #include "bin/directory.h" | 15 #include "bin/directory.h" |
| 16 #include "bin/eventhandler.h" | 16 #include "bin/eventhandler.h" |
| 17 #include "bin/extensions.h" | 17 #include "bin/extensions.h" |
| 18 #include "bin/file.h" | 18 #include "bin/file.h" |
| 19 #include "bin/isolate_data.h" | 19 #include "bin/isolate_data.h" |
| 20 #include "bin/log.h" | 20 #include "bin/log.h" |
| 21 #include "bin/platform.h" | 21 #include "bin/platform.h" |
| 22 #include "bin/process.h" | 22 #include "bin/process.h" |
| 23 #include "bin/vmstats_impl.h" | |
| 24 #include "platform/globals.h" | 23 #include "platform/globals.h" |
| 25 | 24 |
| 26 namespace dart { | 25 namespace dart { |
| 27 namespace bin { | 26 namespace bin { |
| 28 | 27 |
| 29 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | 28 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 30 // it is initialized to NULL. | 29 // it is initialized to NULL. |
| 31 extern const uint8_t* snapshot_buffer; | 30 extern const uint8_t* snapshot_buffer; |
| 32 | 31 |
| 33 // Global state that stores a pointer to the application script snapshot. | 32 // Global state that stores a pointer to the application script snapshot. |
| 34 static bool generate_script_snapshot = false; | 33 static bool generate_script_snapshot = false; |
| 35 static File* snapshot_file = NULL; | 34 static File* snapshot_file = NULL; |
| 36 | 35 |
| 37 | 36 |
| 38 // Global state that indicates whether there is a debug breakpoint. | 37 // Global state that indicates whether there is a debug breakpoint. |
| 39 // 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 |
| 40 // free'd. | 39 // free'd. |
| 41 static const char* breakpoint_at = NULL; | 40 static const char* breakpoint_at = NULL; |
| 42 | 41 |
| 43 | 42 |
| 44 // Global state that indicates whether we should open a connection | 43 // Global state that indicates whether we should open a connection |
| 45 // and listen for a debugger to connect. | 44 // and listen for a debugger to connect. |
| 46 static bool start_debugger = false; | 45 static bool start_debugger = false; |
| 47 static const int DEFAULT_DEBUG_PORT = 5858; | 46 static const int DEFAULT_DEBUG_PORT = 5858; |
| 48 static const char* DEFAULT_DEBUG_IP = "127.0.0.1"; | 47 static const char* DEFAULT_DEBUG_IP = "127.0.0.1"; |
| 49 static const char* debug_ip = DEFAULT_DEBUG_IP; | 48 static const char* debug_ip = DEFAULT_DEBUG_IP; |
| 50 static int debug_port = -1; | 49 static int debug_port = -1; |
| 51 | 50 |
| 52 // Global state that defines the VmStats web server port and root directory. | |
| 53 static int vmstats_port = -1; | |
| 54 static const char* vmstats_root = ""; | |
| 55 | |
| 56 // Value of the --package-root flag. | 51 // Value of the --package-root flag. |
| 57 // (This pointer points into an argv buffer and does not need to be | 52 // (This pointer points into an argv buffer and does not need to be |
| 58 // free'd.) | 53 // free'd.) |
| 59 static const char* package_root = NULL; | 54 static const char* package_root = NULL; |
| 60 | 55 |
| 61 | 56 |
| 62 // Global flag that is used to indicate that we want to compile all the | 57 // Global flag that is used to indicate that we want to compile all the |
| 63 // dart functions and not run anything. | 58 // dart functions and not run anything. |
| 64 static bool has_compile_all = false; | 59 static bool has_compile_all = false; |
| 65 // Global flag that is used to indicate that we want to check function | 60 // Global flag that is used to indicate that we want to check function |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 Log::PrintErr("unrecognized --debug option syntax. " | 161 Log::PrintErr("unrecognized --debug option syntax. " |
| 167 "Use --debug[:<port number>]\n"); | 162 "Use --debug[:<port number>]\n"); |
| 168 return false; | 163 return false; |
| 169 } | 164 } |
| 170 breakpoint_at = "main"; | 165 breakpoint_at = "main"; |
| 171 start_debugger = true; | 166 start_debugger = true; |
| 172 return true; | 167 return true; |
| 173 } | 168 } |
| 174 | 169 |
| 175 | 170 |
| 176 static bool ProcessVmStatsOption(const char* port) { | |
| 177 ASSERT(port != NULL); | |
| 178 if (*port == '\0') { | |
| 179 vmstats_port = 0; // Dynamically assigned port number. | |
| 180 } else { | |
| 181 if ((*port == '=') || (*port == ':')) { | |
| 182 vmstats_port = atoi(port + 1); | |
| 183 } | |
| 184 } | |
| 185 if (vmstats_port < 0) { | |
| 186 Log::PrintErr("unrecognized --stats option syntax. " | |
| 187 "Use --stats[:<port number>]\n"); | |
| 188 return false; | |
| 189 } | |
| 190 return true; | |
| 191 } | |
| 192 | |
| 193 | |
| 194 static bool ProcessPrintScriptOption(const char* arg) { | 171 static bool ProcessPrintScriptOption(const char* arg) { |
| 195 ASSERT(arg != NULL); | 172 ASSERT(arg != NULL); |
| 196 if (*arg != '\0') { | 173 if (*arg != '\0') { |
| 197 return false; | 174 return false; |
| 198 } | 175 } |
| 199 has_print_script = true; | 176 has_print_script = true; |
| 200 return true; | 177 return true; |
| 201 } | 178 } |
| 202 | 179 |
| 203 | 180 |
| 204 static bool ProcessVmStatsRootOption(const char* arg) { | |
| 205 ASSERT(arg != NULL); | |
| 206 if (*arg == '\0') { | |
| 207 return false; | |
| 208 } | |
| 209 vmstats_root = arg; | |
| 210 return true; | |
| 211 } | |
| 212 | |
| 213 | |
| 214 static bool ProcessGenScriptSnapshotOption(const char* filename) { | 181 static bool ProcessGenScriptSnapshotOption(const char* filename) { |
| 215 if (filename != NULL && strlen(filename) != 0) { | 182 if (filename != NULL && strlen(filename) != 0) { |
| 216 // Ensure that are already running using a full snapshot. | 183 // Ensure that are already running using a full snapshot. |
| 217 if (snapshot_buffer == NULL) { | 184 if (snapshot_buffer == NULL) { |
| 218 Log::PrintErr("Script snapshots cannot be generated in this version of" | 185 Log::PrintErr("Script snapshots cannot be generated in this version of" |
| 219 " dart\n"); | 186 " dart\n"); |
| 220 return false; | 187 return false; |
| 221 } | 188 } |
| 222 snapshot_file = File::Open(filename, File::kWriteTruncate); | 189 snapshot_file = File::Open(filename, File::kWriteTruncate); |
| 223 if (snapshot_file == NULL) { | 190 if (snapshot_file == NULL) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 241 { "--help", ProcessHelpOption }, | 208 { "--help", ProcessHelpOption }, |
| 242 { "-h", ProcessHelpOption }, | 209 { "-h", ProcessHelpOption }, |
| 243 { "--verbose", ProcessVerboseOption }, | 210 { "--verbose", ProcessVerboseOption }, |
| 244 { "-v", ProcessVerboseOption }, | 211 { "-v", ProcessVerboseOption }, |
| 245 { "--package-root=", ProcessPackageRootOption }, | 212 { "--package-root=", ProcessPackageRootOption }, |
| 246 // VM specific options to the standalone dart program. | 213 // VM specific options to the standalone dart program. |
| 247 { "--break-at=", ProcessBreakpointOption }, | 214 { "--break-at=", ProcessBreakpointOption }, |
| 248 { "--compile_all", ProcessCompileAllOption }, | 215 { "--compile_all", ProcessCompileAllOption }, |
| 249 { "--debug", ProcessDebugOption }, | 216 { "--debug", ProcessDebugOption }, |
| 250 { "--snapshot=", ProcessGenScriptSnapshotOption }, | 217 { "--snapshot=", ProcessGenScriptSnapshotOption }, |
| 251 { "--stats-root=", ProcessVmStatsRootOption }, | |
| 252 { "--stats", ProcessVmStatsOption }, | |
| 253 { "--print-script", ProcessPrintScriptOption }, | 218 { "--print-script", ProcessPrintScriptOption }, |
| 254 { "--check-function-fingerprints", ProcessFingerprintedFunctions }, | 219 { "--check-function-fingerprints", ProcessFingerprintedFunctions }, |
| 255 { NULL, NULL } | 220 { NULL, NULL } |
| 256 }; | 221 }; |
| 257 | 222 |
| 258 | 223 |
| 259 static bool ProcessMainOptions(const char* option) { | 224 static bool ProcessMainOptions(const char* option) { |
| 260 int i = 0; | 225 int i = 0; |
| 261 const char* name = main_options[0].option_name; | 226 const char* name = main_options[0].option_name; |
| 262 int option_length = strlen(option); | 227 int option_length = strlen(option); |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 Dart_ExitScope(); | 477 Dart_ExitScope(); |
| 513 Dart_ExitIsolate(); | 478 Dart_ExitIsolate(); |
| 514 bool retval = Dart_IsolateMakeRunnable(isolate); | 479 bool retval = Dart_IsolateMakeRunnable(isolate); |
| 515 if (!retval) { | 480 if (!retval) { |
| 516 *error = strdup("Invalid isolate state - Unable to make it runnable"); | 481 *error = strdup("Invalid isolate state - Unable to make it runnable"); |
| 517 Dart_EnterIsolate(isolate); | 482 Dart_EnterIsolate(isolate); |
| 518 Dart_ShutdownIsolate(); | 483 Dart_ShutdownIsolate(); |
| 519 return NULL; | 484 return NULL; |
| 520 } | 485 } |
| 521 | 486 |
| 522 VmStats::AddIsolate(reinterpret_cast<IsolateData*>(data), isolate); | |
| 523 return isolate; | 487 return isolate; |
| 524 } | 488 } |
| 525 | 489 |
| 526 | 490 |
| 527 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, | 491 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, |
| 528 const char* main, | 492 const char* main, |
| 529 void* data, char** error) { | 493 void* data, char** error) { |
| 530 return CreateIsolateAndSetupHelper(script_uri, | 494 return CreateIsolateAndSetupHelper(script_uri, |
| 531 main, | 495 main, |
| 532 new IsolateData(), | 496 new IsolateData(), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 " sets a breakpoint at specified location where <location> is one of :\n" | 542 " sets a breakpoint at specified location where <location> is one of :\n" |
| 579 " url:<line_num> e.g. test.dart:10\n" | 543 " url:<line_num> e.g. test.dart:10\n" |
| 580 " [<class_name>.]<function_name> e.g. B.foo\n" | 544 " [<class_name>.]<function_name> e.g. B.foo\n" |
| 581 "\n" | 545 "\n" |
| 582 "--snapshot=<file_name>\n" | 546 "--snapshot=<file_name>\n" |
| 583 " loads Dart script and generates a snapshot in the specified file\n" | 547 " loads Dart script and generates a snapshot in the specified file\n" |
| 584 "\n" | 548 "\n" |
| 585 "--print-script\n" | 549 "--print-script\n" |
| 586 " generates Dart source code back and prints it after parsing a Dart script\n" | 550 " generates Dart source code back and prints it after parsing a Dart script\n" |
| 587 "\n" | 551 "\n" |
| 588 "--stats[:<port number>]\n" | |
| 589 " enables VM stats service and listens on specified port for HTTP requests\n" | |
| 590 " (default port number is dynamically assigned)\n" | |
| 591 "\n" | |
| 592 "--stats-root=<path>\n" | |
| 593 " where to find static files used by the vmstats application\n" | |
| 594 " (used during vmstats plug-in development)\n" | |
| 595 "\n" | |
| 596 "The following options are only used for VM development and may\n" | 552 "The following options are only used for VM development and may\n" |
| 597 "be changed in any future version:\n"); | 553 "be changed in any future version:\n"); |
| 598 const char* print_flags = "--print_flags"; | 554 const char* print_flags = "--print_flags"; |
| 599 Dart_SetVMFlags(1, &print_flags); | 555 Dart_SetVMFlags(1, &print_flags); |
| 600 } | 556 } |
| 601 } | 557 } |
| 602 | 558 |
| 603 | 559 |
| 604 static Dart_Handle SetBreakpoint(const char* breakpoint_at, | 560 static Dart_Handle SetBreakpoint(const char* breakpoint_at, |
| 605 Dart_Handle library) { | 561 Dart_Handle library) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 | 605 |
| 650 Dart_ExitScope(); | 606 Dart_ExitScope(); |
| 651 Dart_ShutdownIsolate(); | 607 Dart_ShutdownIsolate(); |
| 652 | 608 |
| 653 return kErrorExitCode; | 609 return kErrorExitCode; |
| 654 } | 610 } |
| 655 | 611 |
| 656 | 612 |
| 657 static void ShutdownIsolate(void* callback_data) { | 613 static void ShutdownIsolate(void* callback_data) { |
| 658 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); | 614 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
| 659 VmStats::RemoveIsolate(isolate_data); | |
| 660 EventHandler* handler = isolate_data->event_handler; | 615 EventHandler* handler = isolate_data->event_handler; |
| 661 if (handler != NULL) handler->Shutdown(); | 616 if (handler != NULL) handler->Shutdown(); |
| 662 delete isolate_data; | 617 delete isolate_data; |
| 663 } | 618 } |
| 664 | 619 |
| 665 | 620 |
| 666 static Dart_Handle GenerateScriptSource() { | 621 static Dart_Handle GenerateScriptSource() { |
| 667 Dart_Handle library_url = Dart_LibraryUrl(Dart_RootLibrary()); | 622 Dart_Handle library_url = Dart_LibraryUrl(Dart_RootLibrary()); |
| 668 if (Dart_IsError(library_url)) { | 623 if (Dart_IsError(library_url)) { |
| 669 return library_url; | 624 return library_url; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 757 | 712 |
| 758 // Start the debugger wire protocol handler if necessary. | 713 // Start the debugger wire protocol handler if necessary. |
| 759 if (start_debugger) { | 714 if (start_debugger) { |
| 760 ASSERT(debug_port >= 0); | 715 ASSERT(debug_port >= 0); |
| 761 bool print_msg = verbose_debug_seen || (debug_port == 0); | 716 bool print_msg = verbose_debug_seen || (debug_port == 0); |
| 762 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); | 717 debug_port = DebuggerConnectionHandler::StartHandler(debug_ip, debug_port); |
| 763 if (print_msg) { | 718 if (print_msg) { |
| 764 Log::Print("Debugger listening on port %d\n", debug_port); | 719 Log::Print("Debugger listening on port %d\n", debug_port); |
| 765 } | 720 } |
| 766 } | 721 } |
| 767 VmStats::Start(vmstats_port, vmstats_root, verbose_debug_seen); | |
| 768 | 722 |
| 769 // Call CreateIsolateAndSetup which creates an isolate and loads up | 723 // Call CreateIsolateAndSetup which creates an isolate and loads up |
| 770 // the specified application script. | 724 // the specified application script. |
| 771 char* error = NULL; | 725 char* error = NULL; |
| 772 char* isolate_name = BuildIsolateName(script_name, "main"); | 726 char* isolate_name = BuildIsolateName(script_name, "main"); |
| 773 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, | 727 Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name, |
| 774 "main", | 728 "main", |
| 775 new IsolateData(), | 729 new IsolateData(), |
| 776 &error); | 730 &error); |
| 777 if (isolate == NULL) { | 731 if (isolate == NULL) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 859 | 813 |
| 860 // Keep handling messages until the last active receive port is closed. | 814 // Keep handling messages until the last active receive port is closed. |
| 861 result = Dart_RunLoop(); | 815 result = Dart_RunLoop(); |
| 862 if (Dart_IsError(result)) { | 816 if (Dart_IsError(result)) { |
| 863 return ErrorExit("%s\n", Dart_GetError(result)); | 817 return ErrorExit("%s\n", Dart_GetError(result)); |
| 864 } | 818 } |
| 865 } | 819 } |
| 866 } | 820 } |
| 867 | 821 |
| 868 Dart_ExitScope(); | 822 Dart_ExitScope(); |
| 869 if (vmstats_port >= 0) { | |
| 870 VmStats::Stop(); | |
| 871 } | |
| 872 // Shutdown the isolate. | 823 // Shutdown the isolate. |
| 873 Dart_ShutdownIsolate(); | 824 Dart_ShutdownIsolate(); |
| 874 // Terminate process exit-code handler. | 825 // Terminate process exit-code handler. |
| 875 Process::TerminateExitCodeHandler(); | 826 Process::TerminateExitCodeHandler(); |
| 876 // Free copied argument strings if converted. | 827 // Free copied argument strings if converted. |
| 877 if (argv_converted) { | 828 if (argv_converted) { |
| 878 for (int i = 0; i < argc; i++) free(argv[i]); | 829 for (int i = 0; i < argc; i++) free(argv[i]); |
| 879 } | 830 } |
| 880 | 831 |
| 881 return Process::GlobalExitCode(); | 832 return Process::GlobalExitCode(); |
| 882 } | 833 } |
| 883 | 834 |
| 884 } // namespace bin | 835 } // namespace bin |
| 885 } // namespace dart | 836 } // namespace dart |
| 886 | 837 |
| 887 int main(int argc, char** argv) { | 838 int main(int argc, char** argv) { |
| 888 return dart::bin::main(argc, argv); | 839 return dart::bin::main(argc, argv); |
| 889 } | 840 } |
| OLD | NEW |