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