blob: 17a4bda608f43da0d530024814ce4d32f32ba3a0 [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
5#include "systrace_tool.h"
6
7#include <string>
8#include <base/string_split.h>
9
10#include <chromeos/process.h>
11
12#include "process_with_output.h"
13
14namespace debugd {
15
Jonathan Backer7b806fd2013-04-18 15:00:58 -040016extern const char *kDebugfsGroup;
17
Sam Lefflerf81eaa82012-03-01 09:50:30 -080018SystraceTool::SystraceTool() { }
19SystraceTool::~SystraceTool() { }
20
21static std::string getpathname(void) {
22 char *envvar = getenv("DEBUGD_HELPERS");
23 return StringPrintf("%s/systrace.sh", envvar ? envvar
24 : "/usr/libexec/debugd/helpers");
25}
26
27static void add_category_args(ProcessWithOutput& p,
28 const std::string& categories)
29{
30 std::string temp(categories);
31 std::vector<std::string> pieces;
32 base::SplitString(temp, ' ', &pieces);
33 for (std::vector<std::string>::iterator it = pieces.begin();
34 it < pieces.end();
35 it++)
36 p.AddArg(*it);
37}
38
39std::string SystraceTool::Start(const std::string& categories,
40 DBus::Error& error) {
41 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();
45 p.AddArg(getpathname());
46 p.AddArg("start");
47 add_category_args(p, categories);
48 p.Run();
49 std::string out;
50 p.GetOutput(&out);
51 return out;
52}
53
54void SystraceTool::Stop(const DBus::FileDescriptor& outfd,
55 DBus::Error& error) {
56 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040057 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080058 p.Init();
59 p.AddArg(getpathname());
60 p.AddArg("stop");
61 // trace data is sent to stdout and not across dbus
62 p.BindFd(outfd.get(), STDOUT_FILENO);
63 p.Run();
64}
65
66std::string SystraceTool::Status(DBus::Error& error) {
67 ProcessWithOutput p;
Jonathan Backer7b806fd2013-04-18 15:00:58 -040068 p.SandboxAs(SandboxedProcess::kDefaultUser, kDebugfsGroup);
Sam Lefflerf81eaa82012-03-01 09:50:30 -080069 p.Init();
70 p.AddArg(getpathname());
71 p.AddArg("status");
72 p.Run();
73 std::string out;
74 p.GetOutput(&out);
75 return out;
76}
77
78}; // namespace debugd