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 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/systrace_tool.h" |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 6 | |
| 7 | #include <string> |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 8 | #include <vector> |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 9 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 10 | #include <base/strings/string_split.h> |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame] | 11 | #include <brillo/process.h> |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 12 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 13 | #include "debugd/src/constants.h" |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 14 | #include "debugd/src/process_with_output.h" |
Ricky Liang | 1ef73e5 | 2016-05-24 16:32:34 +0800 | [diff] [blame] | 15 | #include "debugd/src/sandboxed_process.h" |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 16 | |
| 17 | namespace debugd { |
| 18 | |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | const char kSystraceHelper[] = "systrace.sh"; |
| 22 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 23 | void AddCategoryArgs(ProcessWithOutput* p, const std::string& categories) { |
Alex Vakulenko | e50371c | 2016-01-20 16:06:19 -0800 | [diff] [blame] | 24 | std::vector<std::string> pieces = |
| 25 | base::SplitString(categories, " ", base::KEEP_WHITESPACE, |
| 26 | base::SPLIT_WANT_ALL); |
Ben Chan | e2fa357 | 2017-02-08 22:46:18 -0800 | [diff] [blame] | 27 | for (const auto& category : pieces) |
| 28 | p->AddArg(category); |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 31 | } // namespace |
| 32 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 33 | std::string SystraceTool::Start(const std::string& categories) { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 34 | std::string path; |
| 35 | if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path)) |
| 36 | return ""; |
| 37 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 38 | ProcessWithOutput p; |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 39 | // this tool needs to reach into /sys/kernel/debug to enable/disable tracing |
| 40 | p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 41 | p.Init(); |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 42 | p.AddArg(path); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 43 | p.AddArg("start"); |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 44 | AddCategoryArgs(&p, categories); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 45 | p.Run(); |
| 46 | std::string out; |
| 47 | p.GetOutput(&out); |
| 48 | return out; |
| 49 | } |
| 50 | |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 51 | void SystraceTool::Stop(const base::ScopedFD& outfd) { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 52 | std::string path; |
| 53 | if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path)) |
| 54 | return; |
| 55 | |
Ricky Liang | 1ef73e5 | 2016-05-24 16:32:34 +0800 | [diff] [blame] | 56 | SandboxedProcess p; |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 57 | p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 58 | p.Init(); |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 59 | p.AddArg(path); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 60 | p.AddArg("stop"); |
| 61 | // trace data is sent to stdout and not across dbus |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 62 | p.BindFd(outfd.get(), STDOUT_FILENO); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 63 | p.Run(); |
| 64 | } |
| 65 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 66 | std::string SystraceTool::Status() { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 67 | std::string path; |
| 68 | if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path)) |
| 69 | return ""; |
| 70 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 71 | ProcessWithOutput p; |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 72 | p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 73 | p.Init(); |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 74 | p.AddArg(path); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 75 | p.AddArg("status"); |
| 76 | p.Run(); |
| 77 | std::string out; |
| 78 | p.GetOutput(&out); |
| 79 | return out; |
| 80 | } |
| 81 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 82 | } // namespace debugd |