blob: fb91337b0f0455377e19e05d3499036366ac08e1 [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 Chana0011d82014-05-13 00:19:29 -07008#include <vector>
Sam Lefflerf81eaa82012-03-01 09:50:30 -08009
Ben Chana0011d82014-05-13 00:19:29 -070010#include <base/strings/string_split.h>
Sam Lefflerf81eaa82012-03-01 09:50:30 -080011#include <chromeos/process.h>
12
13#include "process_with_output.h"
14
15namespace debugd {
16
Ben Chan297c3c22013-07-17 17:34:12 -070017namespace {
18
19const char kSystraceHelper[] = "systrace.sh";
20
Ben Chana0011d82014-05-13 00:19:29 -070021void AddCategoryArgs(ProcessWithOutput* p, const std::string& categories) {
22 std::vector<std::string> pieces;
23 base::SplitString(categories, ' ', &pieces);
24 for (std::vector<std::string>::iterator it = pieces.begin();
25 it != pieces.end();
26 it++)
27 p->AddArg(*it);
28}
29
Ben Chan297c3c22013-07-17 17:34:12 -070030} // namespace
31
Jonathan Backer7b806fd2013-04-18 15:00:58 -040032extern const char *kDebugfsGroup;
33
Sam Lefflerf81eaa82012-03-01 09:50:30 -080034SystraceTool::SystraceTool() { }
35SystraceTool::~SystraceTool() { }
36
Sam Lefflerf81eaa82012-03-01 09:50:30 -080037std::string SystraceTool::Start(const std::string& categories,
Ben Chana0011d82014-05-13 00:19:29 -070038 DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070039 std::string path;
40 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
41 return "";
42
Sam Lefflerf81eaa82012-03-01 09:50:30 -080043 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040044 // this tool needs to reach into /sys/kernel/debug to enable/disable tracing
45 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080046 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070047 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080048 p.AddArg("start");
Ben Chana0011d82014-05-13 00:19:29 -070049 AddCategoryArgs(&p, categories);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080050 p.Run();
51 std::string out;
52 p.GetOutput(&out);
53 return out;
54}
55
Ben Chana0011d82014-05-13 00:19:29 -070056void SystraceTool::Stop(const DBus::FileDescriptor& outfd, DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070057 std::string path;
58 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
59 return;
60
Sam Lefflerf81eaa82012-03-01 09:50:30 -080061 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040062 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080063 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070064 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080065 p.AddArg("stop");
66 // trace data is sent to stdout and not across dbus
67 p.BindFd(outfd.get(), STDOUT_FILENO);
68 p.Run();
69}
70
Ben Chana0011d82014-05-13 00:19:29 -070071std::string SystraceTool::Status(DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070072 std::string path;
73 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
74 return "";
75
Sam Lefflerf81eaa82012-03-01 09:50:30 -080076 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040077 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080078 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070079 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080080 p.AddArg("status");
81 p.Run();
82 std::string out;
83 p.GetOutput(&out);
84 return out;
85}
86
Ben Chana0011d82014-05-13 00:19:29 -070087} // namespace debugd