blob: 10b3ca36493dff73054a723fccea3dd550950955 [file] [log] [blame]
Sam Lefflerf81eaa82012-03-01 09:50:30 -08001// 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 Chan9953a592014-02-05 23:32:00 -08008#include <base/strings/string_split.h>
Sam Lefflerf81eaa82012-03-01 09:50:30 -08009
10#include <chromeos/process.h>
11
12#include "process_with_output.h"
13
Ben Chan55903dd2014-04-24 00:29:04 -070014using base::StringPrintf;
15
Sam Lefflerf81eaa82012-03-01 09:50:30 -080016namespace debugd {
17
Ben Chan297c3c22013-07-17 17:34:12 -070018namespace {
19
20const char kSystraceHelper[] = "systrace.sh";
21
22} // namespace
23
Jonathan Backer7b806fd2013-04-18 15:00:58 -040024extern const char *kDebugfsGroup;
25
Sam Lefflerf81eaa82012-03-01 09:50:30 -080026SystraceTool::SystraceTool() { }
27SystraceTool::~SystraceTool() { }
28
Sam Lefflerf81eaa82012-03-01 09:50:30 -080029static 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
41std::string SystraceTool::Start(const std::string& categories,
42 DBus::Error& error) {
Ben Chan297c3c22013-07-17 17:34:12 -070043 std::string path;
44 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
45 return "";
46
Sam Lefflerf81eaa82012-03-01 09:50:30 -080047 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040048 // this tool needs to reach into /sys/kernel/debug to enable/disable tracing
49 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080050 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070051 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080052 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
60void SystraceTool::Stop(const DBus::FileDescriptor& outfd,
61 DBus::Error& error) {
Ben Chan297c3c22013-07-17 17:34:12 -070062 std::string path;
63 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
64 return;
65
Sam Lefflerf81eaa82012-03-01 09:50:30 -080066 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040067 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080068 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070069 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080070 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
76std::string SystraceTool::Status(DBus::Error& error) {
Ben Chan297c3c22013-07-17 17:34:12 -070077 std::string path;
78 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
79 return "";
80
Sam Lefflerf81eaa82012-03-01 09:50:30 -080081 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040082 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080083 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070084 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080085 p.AddArg("status");
86 p.Run();
87 std::string out;
88 p.GetOutput(&out);
89 return out;
90}
91
92}; // namespace debugd