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> |
| 8 | #include <base/string_split.h> |
| 9 | |
| 10 | #include <chromeos/process.h> |
| 11 | |
| 12 | #include "process_with_output.h" |
| 13 | |
| 14 | namespace debugd { |
| 15 | |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame^] | 16 | extern const char *kDebugfsGroup; |
| 17 | |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 18 | SystraceTool::SystraceTool() { } |
| 19 | SystraceTool::~SystraceTool() { } |
| 20 | |
| 21 | static std::string getpathname(void) { |
| 22 | char *envvar = getenv("DEBUGD_HELPERS"); |
| 23 | return StringPrintf("%s/systrace.sh", envvar ? envvar |
| 24 | : "/usr/libexec/debugd/helpers"); |
| 25 | } |
| 26 | |
| 27 | static void add_category_args(ProcessWithOutput& p, |
| 28 | const std::string& categories) |
| 29 | { |
| 30 | std::string temp(categories); |
| 31 | std::vector<std::string> pieces; |
| 32 | base::SplitString(temp, ' ', &pieces); |
| 33 | for (std::vector<std::string>::iterator it = pieces.begin(); |
| 34 | it < pieces.end(); |
| 35 | it++) |
| 36 | p.AddArg(*it); |
| 37 | } |
| 38 | |
| 39 | std::string SystraceTool::Start(const std::string& categories, |
| 40 | DBus::Error& error) { |
| 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(); |
| 45 | p.AddArg(getpathname()); |
| 46 | p.AddArg("start"); |
| 47 | add_category_args(p, categories); |
| 48 | p.Run(); |
| 49 | std::string out; |
| 50 | p.GetOutput(&out); |
| 51 | return out; |
| 52 | } |
| 53 | |
| 54 | void SystraceTool::Stop(const DBus::FileDescriptor& outfd, |
| 55 | DBus::Error& error) { |
| 56 | ProcessWithOutput 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(); |
| 59 | p.AddArg(getpathname()); |
| 60 | p.AddArg("stop"); |
| 61 | // trace data is sent to stdout and not across dbus |
| 62 | p.BindFd(outfd.get(), STDOUT_FILENO); |
| 63 | p.Run(); |
| 64 | } |
| 65 | |
| 66 | std::string SystraceTool::Status(DBus::Error& error) { |
| 67 | ProcessWithOutput p; |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame^] | 68 | p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup); |
Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame] | 69 | p.Init(); |
| 70 | p.AddArg(getpathname()); |
| 71 | p.AddArg("status"); |
| 72 | p.Run(); |
| 73 | std::string out; |
| 74 | p.GetOutput(&out); |
| 75 | return out; |
| 76 | } |
| 77 | |
| 78 | }; // namespace debugd |