Add support for --debug switch.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 19 Aug 2014 15:36:26 +0000 (23:36 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 19 Aug 2014 15:36:26 +0000 (23:36 +0800)
atom.gyp
atom/browser/atom_browser_main_parts.cc
atom/browser/atom_browser_main_parts.h
atom/browser/node_debugger.cc [new file with mode: 0644]
atom/browser/node_debugger.h [new file with mode: 0644]

index 44e5e3e..3a5dc57 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
       'atom/browser/net/atom_url_request_job_factory.h',
       'atom/browser/net/url_request_string_job.cc',
       'atom/browser/net/url_request_string_job.h',
+      'atom/browser/node_debugger.cc',
+      'atom/browser/node_debugger.h',
       'atom/browser/ui/accelerator_util.cc',
       'atom/browser/ui/accelerator_util.h',
       'atom/browser/ui/accelerator_util_mac.mm',
index d507e40..af63aad 100644 (file)
@@ -8,6 +8,7 @@
 #include "atom/browser/atom_browser_context.h"
 #include "atom/browser/browser.h"
 #include "atom/browser/javascript_environment.h"
+#include "atom/browser/node_debugger.h"
 #include "atom/common/api/atom_bindings.h"
 #include "atom/common/node_bindings.h"
 #include "base/command_line.h"
@@ -57,6 +58,9 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
 
   node_bindings_->Initialize();
 
+  // Support the "--debug" switch.
+  node_debugger_.reset(new NodeDebugger);
+
   // Create the global environment.
   global_env = node_bindings_->CreateEnvironment(js_env_->context());
 
index 269e042..a221ea6 100644 (file)
@@ -13,6 +13,7 @@ class AtomBindings;
 class Browser;
 class JavascriptEnvironment;
 class NodeBindings;
+class NodeDebugger;
 
 class AtomBrowserMainParts : public brightray::BrowserMainParts {
  public:
@@ -40,6 +41,7 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
   scoped_ptr<JavascriptEnvironment> js_env_;
   scoped_ptr<NodeBindings> node_bindings_;
   scoped_ptr<AtomBindings> atom_bindings_;
+  scoped_ptr<NodeDebugger> node_debugger_;
 
   static AtomBrowserMainParts* self_;
 
diff --git a/atom/browser/node_debugger.cc b/atom/browser/node_debugger.cc
new file mode 100644 (file)
index 0000000..bc1b647
--- /dev/null
@@ -0,0 +1,69 @@
+// Copyright (c) 2014 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/node_debugger.h"
+
+#include "atom/common/atom_version.h"
+#include "base/command_line.h"
+#include "base/strings/string_number_conversions.h"
+#include "v8/include/v8.h"
+#include "v8/include/v8-debug.h"
+
+#include "atom/common/node_includes.h"
+
+namespace atom {
+
+// static
+uv_async_t NodeDebugger::dispatch_debug_messages_async_;
+
+NodeDebugger::NodeDebugger() {
+  uv_async_init(uv_default_loop(),
+                &dispatch_debug_messages_async_,
+                DispatchDebugMessagesInMainThread);
+  uv_unref(reinterpret_cast<uv_handle_t*>(&dispatch_debug_messages_async_));
+
+  bool use_debug_agent = false;
+  int port = 5858;
+  bool wait_for_connection = false;
+
+  std::string port_str;
+  base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
+  if (cmd->HasSwitch("debug")) {
+    use_debug_agent = true;
+    port_str = cmd->GetSwitchValueASCII("debug");
+  }
+  if (cmd->HasSwitch("debug-brk")) {
+    use_debug_agent = true;
+    wait_for_connection = true;
+    port_str = cmd->GetSwitchValueASCII("debug-brk");
+  }
+
+  if (use_debug_agent) {
+    if (!port_str.empty())
+      base::StringToInt(port_str, &port);
+    v8::Debug::EnableAgent("atom-shell " ATOM_VERSION, port,
+                           wait_for_connection);
+    v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessagesInMsgThread,
+                                              false);
+  }
+}
+
+NodeDebugger::~NodeDebugger() {
+}
+
+// static
+void NodeDebugger::DispatchDebugMessagesInMainThread(uv_async_t* handle) {
+  if (!global_env)
+    return;
+
+  v8::Isolate::Scope isolate_scope(global_env->isolate());
+  v8::Debug::ProcessDebugMessages();
+}
+
+// static
+void NodeDebugger::DispatchDebugMessagesInMsgThread() {
+  uv_async_send(&dispatch_debug_messages_async_);
+}
+
+}  // namespace atom
diff --git a/atom/browser/node_debugger.h b/atom/browser/node_debugger.h
new file mode 100644 (file)
index 0000000..4dfdced
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright (c) 2014 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_NODE_DEBUGGER_H_
+#define ATOM_BROWSER_NODE_DEBUGGER_H_
+
+#include "base/basictypes.h"
+#include "vendor/node/deps/uv/include/uv.h"
+
+namespace atom {
+
+// Add support for node's "--debug" switch.
+class NodeDebugger {
+ public:
+  NodeDebugger();
+  virtual ~NodeDebugger();
+
+ private:
+  static void DispatchDebugMessagesInMainThread(uv_async_t* handle);
+  static void DispatchDebugMessagesInMsgThread();
+
+  static uv_async_t dispatch_debug_messages_async_;
+
+  DISALLOW_COPY_AND_ASSIGN(NodeDebugger);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_NODE_DEBUGGER_H_