blob: 7046dd6913e12b2ccf67b4a4d7ae54d1c10f60a5 [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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/systrace_tool.h"
Sam Lefflerf81eaa82012-03-01 09:50:30 -08006
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>
Alex Vakulenkoe7696532015-10-16 16:27:29 -070011#include <brillo/process.h>
Sam Lefflerf81eaa82012-03-01 09:50:30 -080012
Alex Vakulenko262be3f2014-07-30 15:25:50 -070013#include "debugd/src/process_with_output.h"
Sam Lefflerf81eaa82012-03-01 09:50:30 -080014
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) {
Alex Vakulenkoe50371c2016-01-20 16:06:19 -080022 std::vector<std::string> pieces =
23 base::SplitString(categories, " ", base::KEEP_WHITESPACE,
24 base::SPLIT_WANT_ALL);
Ben Chana0011d82014-05-13 00:19:29 -070025 for (std::vector<std::string>::iterator it = pieces.begin();
26 it != pieces.end();
27 it++)
28 p->AddArg(*it);
29}
30
Ben Chan297c3c22013-07-17 17:34:12 -070031} // namespace
32
Jonathan Backer7b806fd2013-04-18 15:00:58 -040033extern const char *kDebugfsGroup;
34
Sam Lefflerf81eaa82012-03-01 09:50:30 -080035std::string SystraceTool::Start(const std::string& categories,
Ben Chana0011d82014-05-13 00:19:29 -070036 DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070037 std::string path;
38 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
39 return "";
40
Sam Lefflerf81eaa82012-03-01 09:50:30 -080041 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040042 // this tool needs to reach into /sys/kernel/debug to enable/disable tracing
43 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080044 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070045 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080046 p.AddArg("start");
Ben Chana0011d82014-05-13 00:19:29 -070047 AddCategoryArgs(&p, categories);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080048 p.Run();
49 std::string out;
50 p.GetOutput(&out);
51 return out;
52}
53
Ben Chana0011d82014-05-13 00:19:29 -070054void SystraceTool::Stop(const DBus::FileDescriptor& outfd, DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070055 std::string path;
56 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
57 return;
58
Sam Lefflerf81eaa82012-03-01 09:50:30 -080059 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040060 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080061 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070062 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080063 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 Chana0011d82014-05-13 00:19:29 -070069std::string SystraceTool::Status(DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070070 std::string path;
71 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
72 return "";
73
Sam Lefflerf81eaa82012-03-01 09:50:30 -080074 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040075 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080076 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070077 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080078 p.AddArg("status");
79 p.Run();
80 std::string out;
81 p.GetOutput(&out);
82 return out;
83}
84
Ben Chana0011d82014-05-13 00:19:29 -070085} // namespace debugd