From d5e67f963caaf0520bbf68a68496cb930696f1f8 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 27 Jan 2015 13:58:29 -0800 Subject: [PATCH] Split skydb trace into start_tracing and stop_tracing Now stop_tracing writes the response to a file instead of spamming the trace data to stdout. R=eseidel@chromium.org, esprehn@chromium.org Review URL: https://codereview.chromium.org/878283002 --- tools/debugger/prompt/prompt.cc | 39 +++++++++++++++++++-------------- tools/skydb | 31 +++++++++++++++++++------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/tools/debugger/prompt/prompt.cc b/tools/debugger/prompt/prompt.cc index c53554c1420..f46aaeee562 100644 --- a/tools/debugger/prompt/prompt.cc +++ b/tools/debugger/prompt/prompt.cc @@ -81,9 +81,7 @@ class Prompt : public mojo::ApplicationDelegate, // FIXME: We should use use a fancier lookup system more like what // services/http_server/http_server.cc does with AddHandler. - if (info.path == "/trace") - ToggleTracing(connection_id); - else if (info.path == "/reload") + if (info.path == "/reload") Load(connection_id, url_); else if (info.path == "/inspect") Inspect(connection_id); @@ -95,9 +93,12 @@ class Prompt : public mojo::ApplicationDelegate, StartProfiling(connection_id); else if (info.path == "/stop_profiling") StopProfiling(connection_id); - else { + else if (info.path == "/start_tracing") + StartTracing(connection_id); + else if (info.path == "/stop_tracing") + StopTracing(connection_id); + else Help(info.path, connection_id); - } } void OnWebSocketRequest( @@ -125,9 +126,7 @@ class Prompt : public mojo::ApplicationDelegate, void Help(std::string path, int connection_id) { std::string help = base::StringPrintf("Sky Debugger running on port %d\n" "Supported URLs:\n" - "/toggle_tracing -- Start/stop tracing\n" "/reload -- Reload the current page\n" - "/inspect -- Start inspector server for current page\n" "/quit -- Quit\n" "/load -- Load a new URL, url in POST body.\n", command_port_); @@ -158,21 +157,29 @@ class Prompt : public mojo::ApplicationDelegate, debugger_->Shutdown(); } - void ToggleTracing(int connection_id) { - bool was_tracing = is_tracing_; - is_tracing_ = !is_tracing_; - - if (was_tracing) { - tracing_->StopAndFlush(); - trace_collector_->GetTrace(base::Bind( - &Prompt::OnTraceAvailable, base::Unretained(this), connection_id)); + void StartTracing(int connection_id) { + if (is_tracing_) { + Error(connection_id, "Already tracing. Use stop_tracing to stop.\n"); return; } + is_tracing_ = true; mojo::DataPipe pipe; tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*")); trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass())); - Respond(connection_id, "Starting trace (type 'trace' to stop tracing)\n"); + Respond(connection_id, "Starting trace (type 'stop_tracing' to stop)\n"); + } + + void StopTracing(int connection_id) { + if (!is_tracing_) { + Error(connection_id, "Not tracing yet. Use start_tracing to start.\n"); + return; + } + + is_tracing_ = false; + tracing_->StopAndFlush(); + trace_collector_->GetTrace(base::Bind( + &Prompt::OnTraceAvailable, base::Unretained(this), connection_id)); } void OnTraceAvailable(int connection_id, std::string trace) { diff --git a/tools/skydb b/tools/skydb index b12d138f2f9..602b0372068 100755 --- a/tools/skydb +++ b/tools/skydb @@ -288,7 +288,7 @@ class SkyDebugger(object): def stop_command(self, args): # TODO(eseidel): mojo_shell crashes when attempting graceful shutdown. - # self._send_command_to_sky('/quit') + # self._run_basic_command('/quit') self._kill_if_exists('sky_server_pid', 'sky_server') @@ -326,7 +326,7 @@ class SkyDebugger(object): self.pids['sky_server_root'], args.url_or_path) else: url = args.url_or_path - self._send_command_to_sky('/load', url) + self._run_basic_command('/load', url) def _read_mojo_map(self): # TODO(eseidel): Does not work for android. @@ -335,8 +335,15 @@ class SkyDebugger(object): lines = maps_file.read().strip().split('\n') return dict(map(lambda line: line.split(' '), lines)) + def stop_tracing_command(self, args): + file_name = args.file_name + trace = self._send_command_to_sky('/stop_tracing').content + with open(file_name, "wb") as trace_file: + trace_file.write(trace) + print "Trace saved in %s" % file_name + def stop_profiling_command(self, args): - self._send_command_to_sky('/stop_profiling') + self._run_basic_command('/stop_profiling') mojo_map = self._read_mojo_map() # TODO(eseidel): We should have a helper for resolving urls, etc. @@ -380,7 +387,10 @@ class SkyDebugger(object): response = requests.post(url, payload) else: response = requests.get(url) - print response.text + return response + + def _run_basic_command(self, command_path, payload=None): + print self._send_command_to_sky(command_path, payload=payload).text # FIXME: These could be made into a context object with __enter__/__exit__. def _load_pid_file(self, path): @@ -401,14 +411,14 @@ class SkyDebugger(object): def _add_basic_command(self, subparsers, name, url_path, help_text): parser = subparsers.add_parser(name, help=help_text) - command = lambda args: self._send_command_to_sky(url_path) + command = lambda args: self._run_basic_command(url_path) parser.set_defaults(func=command) def _wait_for_sky_command_port(self): tries = 0 while True: try: - self._send_command_to_sky('/') + self._run_basic_command('/') return True except: tries += 1 @@ -564,8 +574,8 @@ class SkyDebugger(object): help='launch gdb and attach to gdbserver launched from start --gdb') gdb_attach_parser.set_defaults(func=self.gdb_attach_command) - self._add_basic_command(subparsers, 'trace', '/trace', - 'toggle tracing') + self._add_basic_command(subparsers, 'start_tracing', '/start_tracing', + 'starts tracing the running sky instance') self._add_basic_command(subparsers, 'reload', '/reload', 'reload the current page') self._add_basic_command(subparsers, 'inspect', '/inspect', @@ -573,6 +583,11 @@ class SkyDebugger(object): self._add_basic_command(subparsers, 'start_profiling', '/start_profiling', 'starts profiling the running sky instance (Linux only)') + stop_tracing_parser = subparsers.add_parser('stop_tracing', + help='stops tracing the running sky instance') + stop_tracing_parser.add_argument('file_name', type=str, default='sky_viewer.trace') + stop_tracing_parser.set_defaults(func=self.stop_tracing_command) + stop_profiling_parser = subparsers.add_parser('stop_profiling', help='stops profiling the running sky instance (Linux only)') stop_profiling_parser.set_defaults(func=self.stop_profiling_command)