node_debugger.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/node_debugger.h"
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "base/command_line.h"
  9. #include "base/logging.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "libplatform/libplatform.h"
  13. #include "shell/common/gin_helper/dictionary.h"
  14. #include "shell/common/node_includes.h"
  15. namespace electron {
  16. NodeDebugger::NodeDebugger(node::Environment* env) : env_(env) {}
  17. NodeDebugger::~NodeDebugger() = default;
  18. void NodeDebugger::Start() {
  19. auto* inspector = env_->inspector_agent();
  20. if (inspector == nullptr)
  21. return;
  22. std::vector<std::string> args;
  23. for (auto& arg : base::CommandLine::ForCurrentProcess()->argv()) {
  24. #if defined(OS_WIN)
  25. args.push_back(base::UTF16ToUTF8(arg));
  26. #else
  27. args.push_back(arg);
  28. #endif
  29. }
  30. node::DebugOptions options;
  31. std::vector<std::string> exec_args;
  32. std::vector<std::string> v8_args;
  33. std::vector<std::string> errors;
  34. node::options_parser::Parse(&args, &exec_args, &v8_args, &options,
  35. node::options_parser::kDisallowedInEnvironment,
  36. &errors);
  37. if (!errors.empty()) {
  38. // TODO(jeremy): what's the appropriate behaviour here?
  39. LOG(ERROR) << "Error parsing node options: "
  40. << base::JoinString(errors, " ");
  41. }
  42. const char* path = "";
  43. if (inspector->Start(path, options,
  44. std::make_shared<node::HostPort>(options.host_port),
  45. true /* is_main */))
  46. DCHECK(inspector->IsListening());
  47. v8::HandleScope handle_scope(env_->isolate());
  48. node::profiler::StartProfilers(env_);
  49. if (inspector->options().break_node_first_line) {
  50. inspector->PauseOnNextJavascriptStatement("Break at bootstrap");
  51. }
  52. }
  53. void NodeDebugger::Stop() {
  54. auto* inspector = env_->inspector_agent();
  55. if (inspector && inspector->IsListening()) {
  56. inspector->WaitForDisconnect();
  57. inspector->Stop();
  58. }
  59. }
  60. } // namespace electron