blob: cd3cc524d6454d3e8b8aa69395486731382faeb1 [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"
Ricky Liang1ef73e52016-05-24 16:32:34 +080014#include "debugd/src/sandboxed_process.h"
Sam Lefflerf81eaa82012-03-01 09:50:30 -080015
16namespace debugd {
17
Ben Chan297c3c22013-07-17 17:34:12 -070018namespace {
19
20const char kSystraceHelper[] = "systrace.sh";
21
Ben Chana0011d82014-05-13 00:19:29 -070022void AddCategoryArgs(ProcessWithOutput* p, const std::string& categories) {
Alex Vakulenkoe50371c2016-01-20 16:06:19 -080023 std::vector<std::string> pieces =
24 base::SplitString(categories, " ", base::KEEP_WHITESPACE,
25 base::SPLIT_WANT_ALL);
Ben Chane2fa3572017-02-08 22:46:18 -080026 for (const auto& category : pieces)
27 p->AddArg(category);
Ben Chana0011d82014-05-13 00:19:29 -070028}
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 -080034std::string SystraceTool::Start(const std::string& categories,
Ben Chana0011d82014-05-13 00:19:29 -070035 DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070036 std::string path;
37 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
38 return "";
39
Sam Lefflerf81eaa82012-03-01 09:50:30 -080040 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040041 // this tool needs to reach into /sys/kernel/debug to enable/disable tracing
42 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080043 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070044 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080045 p.AddArg("start");
Ben Chana0011d82014-05-13 00:19:29 -070046 AddCategoryArgs(&p, categories);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080047 p.Run();
48 std::string out;
49 p.GetOutput(&out);
50 return out;
51}
52
Ben Chana0011d82014-05-13 00:19:29 -070053void SystraceTool::Stop(const DBus::FileDescriptor& outfd, DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070054 std::string path;
55 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
56 return;
57
Ricky Liang1ef73e52016-05-24 16:32:34 +080058 SandboxedProcess p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040059 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080060 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070061 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080062 p.AddArg("stop");
63 // trace data is sent to stdout and not across dbus
64 p.BindFd(outfd.get(), STDOUT_FILENO);
65 p.Run();
66}
67
Ben Chana0011d82014-05-13 00:19:29 -070068std::string SystraceTool::Status(DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070069 std::string path;
70 if (!SandboxedProcess::GetHelperPath(kSystraceHelper, &path))
71 return "";
72
Sam Lefflerf81eaa82012-03-01 09:50:30 -080073 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040074 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080075 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070076 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080077 p.AddArg("status");
78 p.Run();
79 std::string out;
80 p.GetOutput(&out);
81 return out;
82}
83
Ben Chana0011d82014-05-13 00:19:29 -070084} // namespace debugd