servo: Merge #13204 - Implement a WebSocket server for debugging (from ejpbruel:debugger); r=jdm
authorEddy Bruel <ejpbruel@mozilla.com>
Mon, 12 Sep 2016 10:19:17 -0500
changeset 339681 b37cf0deb644fc8fd16654d5cc588ddeac596483
parent 339680 7ae899a83e4dff68a289995c405f0ba2ec1d43c0
child 339682 0d125c00454c7056c1b7c1576d7c75d441754a6e
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
servo: Merge #13204 - Implement a WebSocket server for debugging (from ejpbruel:debugger); r=jdm <!-- Please describe your changes on the following line: --> This pull request adds a very simple WebSocket server to Servo, that we intend to use for debugging. It currently only echoes back messages, but eventually we want this server to implement the Chrome Debugging Protocol. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [X] These changes do not require tests because this is a prototype. <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: f834c893c7cfde631e458564499d85e8895b4482
servo/components/debugger/Cargo.toml
servo/components/debugger/lib.rs
servo/components/servo/Cargo.lock
servo/components/servo/Cargo.toml
servo/components/servo/lib.rs
servo/components/util/opts.rs
servo/ports/cef/Cargo.lock
new file mode 100644
--- /dev/null
+++ b/servo/components/debugger/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "debugger"
+version = "0.0.1"
+authors = ["The Servo Project Developers"]
+license = "MPL-2.0"
+publish = false
+
+[lib]
+name = "debugger"
+path = "lib.rs"
+crate_type = ["rlib"]
+
+[dependencies]
+util = { path = "../util" }
+websocket = "0.17.1"
new file mode 100644
--- /dev/null
+++ b/servo/components/debugger/lib.rs
@@ -0,0 +1,50 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+extern crate util;
+extern crate websocket;
+
+use util::thread::spawn_named;
+use websocket::{Message, Receiver, Sender, Server};
+use websocket::message::Type;
+
+pub fn start_server(port: u16) {
+    println!("Starting debugger server.");
+    spawn_named("debugger-server".to_owned(), move || {
+        run_server(port)
+    });
+}
+
+fn run_server(port: u16) {
+    let server = Server::bind(("127.0.0.1", port)).unwrap();
+    for connection in server {
+        spawn_named("debugger-connection".to_owned(), move || {
+            let connection = connection.unwrap();
+            let request = connection.read_request().unwrap();
+            let response = request.accept();
+            let client = response.send().unwrap();
+            let (mut sender, mut receiver) = client.split();
+            for message in receiver.incoming_messages() {
+                let message: Message = message.unwrap();
+                match message.opcode {
+                    Type::Close => {
+                        let message = Message::close();
+                        sender.send_message(&message).unwrap();
+                        break;
+                    }
+                    Type::Ping => {
+                        let message = Message::pong(message.payload);
+                        sender.send_message(&message).unwrap();
+                    }
+                    Type::Text => {
+                        sender.send_message(&message).unwrap();
+                    }
+                    _ => {
+                        panic!("Unexpected message type.");
+                    }
+                }
+            }
+        });
+    }
+}
--- a/servo/components/servo/Cargo.lock
+++ b/servo/components/servo/Cargo.lock
@@ -5,16 +5,17 @@ dependencies = [
  "android_glue 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "backtrace 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "browserhtml 0.1.17 (git+https://github.com/browserhtml/browserhtml?branch=crate)",
  "canvas 0.0.1",
  "canvas_traits 0.0.1",
  "compiletest_helper 0.0.1",
  "compositing 0.0.1",
  "constellation 0.0.1",
+ "debugger 0.0.1",
  "devtools 0.0.1",
  "devtools_traits 0.0.1",
  "env_logger 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "euclid 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "gaol 0.0.1 (git+https://github.com/servo/gaol)",
  "gfx 0.0.1",
  "gfx_tests 0.0.1",
  "gleam 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -477,16 +478,24 @@ dependencies = [
 name = "debug_unreachable"
 version = "0.1.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
+name = "debugger"
+version = "0.0.1"
+dependencies = [
+ "util 0.0.1",
+ "websocket 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "deque"
 version = "0.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
--- a/servo/components/servo/Cargo.toml
+++ b/servo/components/servo/Cargo.toml
@@ -44,16 +44,17 @@ style_tests = {path = "../../tests/unit/
 util_tests = {path = "../../tests/unit/util"}
 
 [dependencies]
 browserhtml = {git = "https://github.com/browserhtml/browserhtml", branch = "crate"}
 canvas = {path = "../canvas"}
 canvas_traits = {path = "../canvas_traits"}
 compositing = {path = "../compositing"}
 constellation = {path = "../constellation"}
+debugger = {path = "../debugger"}
 devtools = {path = "../devtools"}
 devtools_traits = {path = "../devtools_traits"}
 env_logger = "0.3"
 euclid = "0.10.1"
 gfx = {path = "../gfx"}
 gleam = "0.2"
 glutin_app = {path = "../../ports/glutin"}
 ipc-channel = "0.5"
--- a/servo/components/servo/lib.rs
+++ b/servo/components/servo/lib.rs
@@ -23,16 +23,17 @@ extern crate gaol;
 #[macro_use]
 extern crate gleam;
 extern crate log;
 
 pub extern crate canvas;
 pub extern crate canvas_traits;
 pub extern crate compositing;
 pub extern crate constellation;
+pub extern crate debugger;
 pub extern crate devtools;
 pub extern crate devtools_traits;
 pub extern crate euclid;
 pub extern crate gfx;
 pub extern crate ipc_channel;
 pub extern crate layout_thread;
 pub extern crate msg;
 pub extern crate net;
@@ -120,16 +121,19 @@ impl<Window> Browser<Window> where Windo
         // messages to client may need to pump a platform-specific event loop
         // to deliver the message.
         let (compositor_proxy, compositor_receiver) =
             window.create_compositor_channel();
         let supports_clipboard = window.supports_clipboard();
         let time_profiler_chan = profile_time::Profiler::create(&opts.time_profiling,
                                                                 opts.time_profiler_trace_path.clone());
         let mem_profiler_chan = profile_mem::Profiler::create(opts.mem_profiler_period);
+        if let Some(port) = opts.debugger_port {
+            debugger::start_server(port)
+        }
         let devtools_chan = opts.devtools_port.map(|port| {
             devtools::start_server(port)
         });
 
         let (webrender, webrender_api_sender) = if opts::get().use_webrender {
             if let Ok(mut resource_path) = resources_dir_path() {
                 resource_path.push("shaders");
 
--- a/servo/components/util/opts.rs
+++ b/servo/components/util/opts.rs
@@ -118,16 +118,20 @@ pub struct Opts {
     pub trace_layout: bool,
 
     /// Periodically print out on which events script threads spend their processing time.
     pub profile_script_events: bool,
 
     /// Enable all heartbeats for profiling.
     pub profile_heartbeats: bool,
 
+    /// `None` to disable debugger or `Some` with a port number to start a server to listen to
+    /// remote Firefox debugger connections.
+    pub debugger_port: Option<u16>,
+
     /// `None` to disable devtools or `Some` with a port number to start a server to listen to
     /// remote Firefox devtools connections.
     pub devtools_port: Option<u16>,
 
     /// `None` to disable WebDriver or `Some` with a port number to start a server to listen to
     /// remote WebDriver commands.
     pub webdriver_port: Option<u16>,
 
@@ -497,16 +501,17 @@ pub fn default_opts() -> Opts {
         show_debug_borders: false,
         show_debug_fragment_borders: false,
         show_debug_parallel_paint: false,
         show_debug_parallel_layout: false,
         paint_flashing: false,
         enable_text_antialiasing: false,
         enable_canvas_antialiasing: false,
         trace_layout: false,
+        debugger_port: None,
         devtools_port: None,
         webdriver_port: None,
         initial_window_size: TypedSize2D::new(1024, 740),
         user_agent: default_user_agent_string(DEFAULT_USER_AGENT),
         multiprocess: false,
         random_pipeline_closure_probability: None,
         random_pipeline_closure_seed: None,
         sandbox: false,
@@ -557,16 +562,17 @@ pub fn from_cmdline_args(args: &[String]
     opts.optflag("i", "nonincremental-layout", "Enable to turn off incremental layout.");
     opts.optflagopt("", "userscripts",
                     "Uses userscripts in resources/user-agent-js, or a specified full path", "");
     opts.optmulti("", "user-stylesheet",
                   "A user stylesheet to be added to every document", "file.css");
     opts.optflag("z", "headless", "Headless mode");
     opts.optflag("f", "hard-fail", "Exit on thread failure instead of displaying about:failure");
     opts.optflag("F", "soft-fail", "Display about:failure on thread failure instead of exiting");
+    opts.optflagopt("", "remote-debugging-port", "Start remote debugger server on port", "2794");
     opts.optflagopt("", "devtools", "Start remote devtools server on port", "6000");
     opts.optflagopt("", "webdriver", "Start remote WebDriver server on port", "7000");
     opts.optopt("", "resolution", "Set window resolution.", "1024x740");
     opts.optopt("u",
                 "user-agent",
                 "Set custom user agent string (or android / desktop for platform default)",
                 "NCSA Mosaic/1.0 (X11;SunOS 4.1.4 sun4m)");
     opts.optflag("M", "multiprocess", "Run in multiprocess mode");
@@ -716,16 +722,21 @@ pub fn from_cmdline_args(args: &[String]
 
     let mut bubble_inline_sizes_separately = debug_options.bubble_widths;
     if debug_options.trace_layout {
         paint_threads = 1;
         layout_threads = Some(1);
         bubble_inline_sizes_separately = true;
     }
 
+    let debugger_port = opt_match.opt_default("remote-debugging-port", "2794").map(|port| {
+        port.parse()
+            .unwrap_or_else(|err| args_fail(&format!("Error parsing option: --remote-debugging-port ({})", err)))
+    });
+
     let devtools_port = opt_match.opt_default("devtools", "6000").map(|port| {
         port.parse().unwrap_or_else(|err| args_fail(&format!("Error parsing option: --devtools ({})", err)))
     });
 
     let webdriver_port = opt_match.opt_default("webdriver", "7000").map(|port| {
         port.parse().unwrap_or_else(|err| args_fail(&format!("Error parsing option: --webdriver ({})", err)))
     });
 
@@ -797,16 +808,17 @@ pub fn from_cmdline_args(args: &[String]
         gc_profile: debug_options.gc_profile,
         load_webfonts_synchronously: debug_options.load_webfonts_synchronously,
         headless: opt_match.opt_present("z"),
         hard_fail: opt_match.opt_present("f") && !opt_match.opt_present("F"),
         bubble_inline_sizes_separately: bubble_inline_sizes_separately,
         profile_script_events: debug_options.profile_script_events,
         profile_heartbeats: debug_options.profile_heartbeats,
         trace_layout: debug_options.trace_layout,
+        debugger_port: debugger_port,
         devtools_port: devtools_port,
         webdriver_port: webdriver_port,
         initial_window_size: initial_window_size,
         user_agent: user_agent,
         multiprocess: opt_match.opt_present("M"),
         sandbox: opt_match.opt_present("S"),
         random_pipeline_closure_probability: random_pipeline_closure_probability,
         random_pipeline_closure_seed: random_pipeline_closure_seed,
--- a/servo/ports/cef/Cargo.lock
+++ b/servo/ports/cef/Cargo.lock
@@ -435,16 +435,24 @@ dependencies = [
 name = "debug_unreachable"
 version = "0.1.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
+name = "debugger"
+version = "0.0.1"
+dependencies = [
+ "util 0.0.1",
+ "websocket 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "deque"
 version = "0.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -1966,16 +1974,17 @@ version = "0.0.1"
 dependencies = [
  "android_glue 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "backtrace 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "browserhtml 0.1.17 (git+https://github.com/browserhtml/browserhtml?branch=crate)",
  "canvas 0.0.1",
  "canvas_traits 0.0.1",
  "compositing 0.0.1",
  "constellation 0.0.1",
+ "debugger 0.0.1",
  "devtools 0.0.1",
  "devtools_traits 0.0.1",
  "env_logger 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "euclid 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "gaol 0.0.1 (git+https://github.com/servo/gaol)",
  "gfx 0.0.1",
  "gleam 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
  "glutin_app 0.0.1",