blob: de41f49bd2391ea4ad1493276db541ec70acde99 [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) {
Alex Vakulenkoe50371c2016-01-20 16:06:19 -080025 std::vector<std::string> pieces =
26 base::SplitString(categories, " ", base::KEEP_WHITESPACE,
27 base::SPLIT_WANT_ALL);
Ben Chane2fa3572017-02-08 22:46:18 -080028 for (const auto& category : pieces)
29 p->AddArg(category);
Ben Chana0011d82014-05-13 00:19:29 -070030}
31
Ben Chan297c3c22013-07-17 17:34:12 -070032} // namespace
33
Eric Carusoc93a15c2017-04-24 16:15:12 -070034std::string SystraceTool::Start(const std::string& categories) {
Ben Chan297c3c22013-07-17 17:34:12 -070035 std::string path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070036 if (!GetHelperPath(kSystraceHelper, &path))
Ben Chan297c3c22013-07-17 17:34:12 -070037 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
Eric Caruso0b241882018-04-04 13:43:46 -070052void SystraceTool::Stop(const base::ScopedFD& outfd) {
Ben Chan297c3c22013-07-17 17:34:12 -070053 std::string path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070054 if (!GetHelperPath(kSystraceHelper, &path))
Ben Chan297c3c22013-07-17 17:34:12 -070055 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
Eric Caruso0b241882018-04-04 13:43:46 -070063 p.BindFd(outfd.get(), STDOUT_FILENO);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080064 p.Run();
65}
66
Eric Carusoc93a15c2017-04-24 16:15:12 -070067std::string SystraceTool::Status() {
Ben Chan297c3c22013-07-17 17:34:12 -070068 std::string path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070069 if (!GetHelperPath(kSystraceHelper, &path))
Ben Chan297c3c22013-07-17 17:34:12 -070070 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