Sam Leffler | f81eaa8 | 2012-03-01 09:50:30 -0800 | [diff] [blame^] | 1 | // 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 | |
| 14 | namespace debugd { |
| 15 | |
| 16 | SystraceTool::SystraceTool() { } |
| 17 | SystraceTool::~SystraceTool() { } |
| 18 | |
| 19 | static std::string getpathname(void) { |
| 20 | char *envvar = getenv("DEBUGD_HELPERS"); |
| 21 | return StringPrintf("%s/systrace.sh", envvar ? envvar |
| 22 | : "/usr/libexec/debugd/helpers"); |
| 23 | } |
| 24 | |
| 25 | static void add_category_args(ProcessWithOutput& p, |
| 26 | const std::string& categories) |
| 27 | { |
| 28 | std::string temp(categories); |
| 29 | std::vector<std::string> pieces; |
| 30 | base::SplitString(temp, ' ', &pieces); |
| 31 | for (std::vector<std::string>::iterator it = pieces.begin(); |
| 32 | it < pieces.end(); |
| 33 | it++) |
| 34 | p.AddArg(*it); |
| 35 | } |
| 36 | |
| 37 | std::string SystraceTool::Start(const std::string& categories, |
| 38 | DBus::Error& error) { |
| 39 | ProcessWithOutput p; |
| 40 | p.Init(); |
| 41 | p.AddArg(getpathname()); |
| 42 | p.AddArg("start"); |
| 43 | add_category_args(p, categories); |
| 44 | p.Run(); |
| 45 | std::string out; |
| 46 | p.GetOutput(&out); |
| 47 | return out; |
| 48 | } |
| 49 | |
| 50 | void SystraceTool::Stop(const DBus::FileDescriptor& outfd, |
| 51 | DBus::Error& error) { |
| 52 | ProcessWithOutput p; |
| 53 | p.Init(); |
| 54 | p.AddArg(getpathname()); |
| 55 | p.AddArg("stop"); |
| 56 | // trace data is sent to stdout and not across dbus |
| 57 | p.BindFd(outfd.get(), STDOUT_FILENO); |
| 58 | p.Run(); |
| 59 | } |
| 60 | |
| 61 | std::string SystraceTool::Status(DBus::Error& error) { |
| 62 | ProcessWithOutput p; |
| 63 | p.Init(); |
| 64 | p.AddArg(getpathname()); |
| 65 | p.AddArg("status"); |
| 66 | p.Run(); |
| 67 | std::string out; |
| 68 | p.GetOutput(&out); |
| 69 | return out; |
| 70 | } |
| 71 | |
| 72 | }; // namespace debugd |