blob: cc351f5b8f3997ff5f1441301bf1d236fdc2eb3e [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
Ben Chanaf125862017-02-08 23:11:18 -080013#include "debugd/src/constants.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070014#include "debugd/src/process_with_output.h"
Ricky Liang1ef73e52016-05-24 16:32:34 +080015#include "debugd/src/sandboxed_process.h"
Sam Lefflerf81eaa82012-03-01 09:50:30 -080016
17namespace debugd {
18
Ben Chan297c3c22013-07-17 17:34:12 -070019namespace {
20
21const char kSystraceHelper[] = "systrace.sh";
22
Ben Chana0011d82014-05-13 00:19:29 -070023void AddCategoryArgs(ProcessWithOutput* p, const std::string& categories) {
Alex Vakulenkoe50371c2016-01-20 16:06:19 -080024 std::vector<std::string> pieces =
25 base::SplitString(categories, " ", base::KEEP_WHITESPACE,
26 base::SPLIT_WANT_ALL);
Ben Chane2fa3572017-02-08 22:46:18 -080027 for (const auto& category : pieces)
28 p->AddArg(category);
Ben Chana0011d82014-05-13 00:19:29 -070029}
30
Ben Chan297c3c22013-07-17 17:34:12 -070031} // namespace
32
Sam Lefflerf81eaa82012-03-01 09:50:30 -080033std::string SystraceTool::Start(const std::string& categories,
Ben Chana0011d82014-05-13 00:19:29 -070034 DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070035 std::string path;
36 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
37 return "";
38
Sam Lefflerf81eaa82012-03-01 09:50:30 -080039 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040040 // this tool needs to reach into /sys/kernel/debug to enable/disable tracing
41 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080042 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070043 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080044 p.AddArg("start");
Ben Chana0011d82014-05-13 00:19:29 -070045 AddCategoryArgs(&p, categories);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080046 p.Run();
47 std::string out;
48 p.GetOutput(&out);
49 return out;
50}
51
Ben Chana0011d82014-05-13 00:19:29 -070052void SystraceTool::Stop(const DBus::FileDescriptor& outfd, DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070053 std::string path;
54 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
55 return;
56
Ricky Liang1ef73e52016-05-24 16:32:34 +080057 SandboxedProcess p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040058 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080059 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070060 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080061 p.AddArg("stop");
62 // trace data is sent to stdout and not across dbus
63 p.BindFd(outfd.get(), STDOUT_FILENO);
64 p.Run();
65}
66
Ben Chana0011d82014-05-13 00:19:29 -070067std::string SystraceTool::Status(DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070068 std::string path;
69 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
70 return "";
71
Sam Lefflerf81eaa82012-03-01 09:50:30 -080072 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040073 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080074 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070075 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080076 p.AddArg("status");
77 p.Run();
78 std::string out;
79 p.GetOutput(&out);
80 return out;
81}
82
Ben Chana0011d82014-05-13 00:19:29 -070083} // namespace debugd