blob: 1eda8965718de27272919b97b19b275740e92f20 [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>
Simon Glass2b1da092020-05-21 12:24:16 -060011#include <brillo/process/process.h>
Sam Lefflerf81eaa82012-03-01 09:50:30 -080012
Ben Chanaf125862017-02-08 23:11:18 -080013#include "debugd/src/constants.h"
Hardik Goyalb09d6b02019-08-13 16:15:50 -070014#include "debugd/src/helper_utils.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070015#include "debugd/src/process_with_output.h"
Ricky Liang1ef73e52016-05-24 16:32:34 +080016#include "debugd/src/sandboxed_process.h"
Sam Lefflerf81eaa82012-03-01 09:50:30 -080017
18namespace debugd {
19
Ben Chan297c3c22013-07-17 17:34:12 -070020namespace {
21
22const char kSystraceHelper[] = "systrace.sh";
23
Ben Chana0011d82014-05-13 00:19:29 -070024void AddCategoryArgs(ProcessWithOutput* p, const std::string& categories) {
Tom Hughesd6c2d392020-08-24 18:12:11 -070025 std::vector<std::string> pieces = base::SplitString(
26 categories, " ", base::KEEP_WHITESPACE, 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
Eric Carusoc93a15c2017-04-24 16:15:12 -070033std::string SystraceTool::Start(const std::string& categories) {
Ben Chan297c3c22013-07-17 17:34:12 -070034 std::string path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070035 if (!GetHelperPath(kSystraceHelper, &path))
Ben Chan297c3c22013-07-17 17:34:12 -070036 return "";
37
Sam Lefflerf81eaa82012-03-01 09:50:30 -080038 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040039 // this tool needs to reach into /sys/kernel/debug to enable/disable tracing
40 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080041 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070042 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080043 p.AddArg("start");
Ben Chana0011d82014-05-13 00:19:29 -070044 AddCategoryArgs(&p, categories);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080045 p.Run();
46 std::string out;
47 p.GetOutput(&out);
48 return out;
49}
50
Eric Caruso0b241882018-04-04 13:43:46 -070051void SystraceTool::Stop(const base::ScopedFD& outfd) {
Ben Chan297c3c22013-07-17 17:34:12 -070052 std::string path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070053 if (!GetHelperPath(kSystraceHelper, &path))
Ben Chan297c3c22013-07-17 17:34:12 -070054 return;
55
Ricky Liang1ef73e52016-05-24 16:32:34 +080056 SandboxedProcess p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040057 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080058 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070059 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080060 p.AddArg("stop");
61 // trace data is sent to stdout and not across dbus
Eric Caruso0b241882018-04-04 13:43:46 -070062 p.BindFd(outfd.get(), STDOUT_FILENO);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080063 p.Run();
64}
65
Eric Carusoc93a15c2017-04-24 16:15:12 -070066std::string SystraceTool::Status() {
Ben Chan297c3c22013-07-17 17:34:12 -070067 std::string path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070068 if (!GetHelperPath(kSystraceHelper, &path))
Ben Chan297c3c22013-07-17 17:34:12 -070069 return "";
70
Sam Lefflerf81eaa82012-03-01 09:50:30 -080071 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040072 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080073 p.Init();
Ben Chan297c3c22013-07-17 17:34:12 -070074 p.AddArg(path);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080075 p.AddArg("status");
76 p.Run();
77 std::string out;
78 p.GetOutput(&out);
79 return out;
80}
81
Ben Chana0011d82014-05-13 00:19:29 -070082} // namespace debugd