From 1207dfde4c9a754ac5d30c00824608cbe98f8f6c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 19 Aug 2014 23:36:26 +0800 Subject: [PATCH 1/4] Add support for --debug switch. --- atom.gyp | 2 + atom/browser/atom_browser_main_parts.cc | 4 ++ atom/browser/atom_browser_main_parts.h | 2 + atom/browser/node_debugger.cc | 69 +++++++++++++++++++++++++ atom/browser/node_debugger.h | 30 +++++++++++ 5 files changed, 107 insertions(+) create mode 100644 atom/browser/node_debugger.cc create mode 100644 atom/browser/node_debugger.h diff --git a/atom.gyp b/atom.gyp index 44e5e3e8b48f..3a5dc579b99e 100644 --- a/atom.gyp +++ b/atom.gyp @@ -121,6 +121,8 @@ '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', diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index d507e40c8a89..af63aad23a88 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -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()); diff --git a/atom/browser/atom_browser_main_parts.h b/atom/browser/atom_browser_main_parts.h index 269e042b3768..a221ea62cd7b 100644 --- a/atom/browser/atom_browser_main_parts.h +++ b/atom/browser/atom_browser_main_parts.h @@ -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 js_env_; scoped_ptr node_bindings_; scoped_ptr atom_bindings_; + scoped_ptr node_debugger_; static AtomBrowserMainParts* self_; diff --git a/atom/browser/node_debugger.cc b/atom/browser/node_debugger.cc new file mode 100644 index 000000000000..bc1b647d7330 --- /dev/null +++ b/atom/browser/node_debugger.cc @@ -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(&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 index 000000000000..4dfdced8ee9d --- /dev/null +++ b/atom/browser/node_debugger.h @@ -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_ From c12c0363a0ae0886c44c072f045ad796786c1f48 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 20 Aug 2014 08:15:36 +0800 Subject: [PATCH 2/4] No need to enter isolate. --- atom/browser/node_debugger.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/atom/browser/node_debugger.cc b/atom/browser/node_debugger.cc index bc1b647d7330..f66a2d063896 100644 --- a/atom/browser/node_debugger.cc +++ b/atom/browser/node_debugger.cc @@ -4,6 +4,8 @@ #include "atom/browser/node_debugger.h" +#include + #include "atom/common/atom_version.h" #include "base/command_line.h" #include "base/strings/string_number_conversions.h" @@ -54,10 +56,6 @@ 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(); } From 6e3b3fa5a2a7d887395a4a7143e0f2893089360d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 20 Aug 2014 10:01:43 +0800 Subject: [PATCH 3/4] Add "Debugging browser process" chapter. --- docs/README.md | 1 + docs/tutorial/debugging-browser-process.md | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 docs/tutorial/debugging-browser-process.md diff --git a/docs/README.md b/docs/README.md index 6d2ec76a73db..aaf27d6d95a9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,6 +3,7 @@ * [Quick start](tutorial/quick-start.md) * [Application distribution](tutorial/application-distribution.md) * [Use native node modules](tutorial/use-native-node-modules.md) +* [Debugging browser process](tutorial/debugging-browser-process.md) ## API references diff --git a/docs/tutorial/debugging-browser-process.md b/docs/tutorial/debugging-browser-process.md new file mode 100644 index 000000000000..0e4a3ebbf5f0 --- /dev/null +++ b/docs/tutorial/debugging-browser-process.md @@ -0,0 +1,15 @@ +# Debugging browser process + +The devtools of browser window can only debug the scripts of the web pages +(e.g. the renderer process), in order to provide a way to debug the scripts of +the browser side (e.g. the browser process), atom-shell has provided the +`--debug` and `--debug-brk` switches. + +## `--debug=[port]` + +When this switch is used atom-shell would listen for V8 debugger protocol on +`port`, the `port` is `5858` by default. + +## `debug-brk=[port]` + +Like `--debug` but pauses the script on the first line. From fecc90e67c47c86066bc596d9420fdbe6fd5bb7d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 20 Aug 2014 10:43:41 +0800 Subject: [PATCH 4/4] docs: Add chapter on debugging with node-inspector. --- docs/tutorial/debugging-browser-process.md | 38 ++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/docs/tutorial/debugging-browser-process.md b/docs/tutorial/debugging-browser-process.md index 0e4a3ebbf5f0..34957312b0d3 100644 --- a/docs/tutorial/debugging-browser-process.md +++ b/docs/tutorial/debugging-browser-process.md @@ -5,11 +5,45 @@ The devtools of browser window can only debug the scripts of the web pages the browser side (e.g. the browser process), atom-shell has provided the `--debug` and `--debug-brk` switches. -## `--debug=[port]` +## Command line switches + +### `--debug=[port]` When this switch is used atom-shell would listen for V8 debugger protocol on `port`, the `port` is `5858` by default. -## `debug-brk=[port]` +### `debug-brk=[port]` Like `--debug` but pauses the script on the first line. + +## Use node-inspector for debugging + +__Note:__ Atom Shell uses node v0.11.13, which currently doesn't work very well +with node-inspector, and the browser process would crash if you inspect the +`process` object under node-inspector's console. + +### 1. Start the [node-inspector](node-inspector) server + +```bash +$ node-inspector +``` + +### 2. Enable debug mode for atom-shell + +You can either start atom-shell with a debug flag like: + +```bash +$ atom-shell --debug your/app +``` + +or, to pause your script on the first line: + +```bash +$ atom-shell --debug-brk your/app +``` + +### 3. Load the debugger UI + +Open http://127.0.0.1:8080/debug?port=5858 in the Chrome browser. + +[node-inspector](https://github.com/node-inspector/node-inspector)