Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "systrace_tool.h" |
| 6 | |
| 7 | #include <string> |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 8 | #include <base/strings/string_split.h> |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 9 | |
| 10 | #include <chromeos/process.h> |
| 11 | |
| 12 | #include "process_with_output.h" |
| 13 | |
Ben Chan | 55903dd | 2014-04-24 00:29:04 -0700 | [diff] [blame] | 14 | using base::StringPrintf; |
| 15 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 16 | namespace debugd { |
| 17 | |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | const char kSystraceHelper[] = "systrace.sh"; |
| 21 | |
| 22 | } // namespace |
| 23 | |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 24 | extern const char *kDebugfsGroup; |
| 25 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 26 | SystraceTool::SystraceTool() { } |
| 27 | SystraceTool::~SystraceTool() { } |
| 28 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 29 | static void add_category_args(ProcessWithOutput& p, |
| 30 | const std::string& categories) |
| 31 | { |
| 32 | std::string temp(categories); |
| 33 | std::vector<std::string> pieces; |
| 34 | base::SplitString(temp, ' ', &pieces); |
| 35 | for (std::vector<std::string>::iterator it = pieces.begin(); |
| 36 | it < pieces.end(); |
| 37 | it++) |
| 38 | p.AddArg(*it); |
| 39 | } |
| 40 | |
| 41 | std::string SystraceTool::Start(const std::string& categories, |
| 42 | DBus::Error& error) { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 43 | std::string path; |
| 44 | if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path)) |
| 45 | return ""; |
| 46 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 47 | ProcessWithOutput p; |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 48 | // this tool needs to reach into /sys/kernel/debug to enable/disable tracing |
| 49 | p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 50 | p.Init(); |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 51 | p.AddArg(path); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 52 | p.AddArg("start"); |
| 53 | add_category_args(p, categories); |
| 54 | p.Run(); |
| 55 | std::string out; |
| 56 | p.GetOutput(&out); |
| 57 | return out; |
| 58 | } |
| 59 | |
| 60 | void SystraceTool::Stop(const DBus::FileDescriptor& outfd, |
| 61 | DBus::Error& error) { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 62 | std::string path; |
| 63 | if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path)) |
| 64 | return; |
| 65 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 66 | ProcessWithOutput p; |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 67 | p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 68 | p.Init(); |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 69 | p.AddArg(path); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 70 | p.AddArg("stop"); |
| 71 | // trace data is sent to stdout and not across dbus |
| 72 | p.BindFd(outfd.get(), STDOUT_FILENO); |
| 73 | p.Run(); |
| 74 | } |
| 75 | |
| 76 | std::string SystraceTool::Status(DBus::Error& error) { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 77 | std::string path; |
| 78 | if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path)) |
| 79 | return ""; |
| 80 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 81 | ProcessWithOutput p; |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 82 | p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 83 | p.Init(); |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 84 | p.AddArg(path); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 85 | p.AddArg("status"); |
| 86 | p.Run(); |
| 87 | std::string out; |
| 88 | p.GetOutput(&out); |
| 89 | return out; |
| 90 | } |
| 91 | |
| 92 | }; // namespace debugd |