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