1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: TraceOps implements
16 */
17 #include "trace_ops.h"
18
19 #include <algorithm>
20 #include <cerrno>
21 #include <csignal>
22 #include <cstdio>
23 #include <cstring>
24 #include <fcntl.h>
25 #include <functional>
26 #include <memory>
27 #include <sstream>
28 #include <unistd.h>
29
30 #include "logging.h"
31 #include "process_utils.h"
32 #include "string_utils.h"
33
34 FTRACE_NS_BEGIN
TraceOps(const std::string & path,const std::string & arg0,TraceType type)35 TraceOps::TraceOps(const std::string& path, const std::string& arg0, TraceType type)
36 : arg0_(arg0), bin_(path), type_(type)
37 {
38 }
39
~TraceOps()40 TraceOps::~TraceOps() {}
41
GetCommand() const42 std::string TraceOps::GetCommand() const
43 {
44 return bin_;
45 }
46
GetTraceType() const47 TraceOps::TraceType TraceOps::GetTraceType() const
48 {
49 return type_;
50 }
51
IsSupported()52 bool TraceOps::IsSupported()
53 {
54 return access(bin_.c_str(), F_OK | X_OK) == 0;
55 }
56
HasCategory(const std::string & name)57 bool TraceOps::HasCategory(const std::string& name)
58 {
59 if (supportedCategories_.empty()) {
60 supportedCategories_ = ListCategories();
61 std::sort(supportedCategories_.begin(), supportedCategories_.end());
62 }
63 return std::binary_search(supportedCategories_.begin(), supportedCategories_.end(), name);
64 }
65
ListCategories()66 std::vector<std::string> TraceOps::ListCategories()
67 {
68 std::vector<std::string> result;
69 args_ = {arg0_};
70 CHECK_TRUE(PrepareListCategoriesCmd(), result, "prepare list categories command failed!");
71
72 int retval = ExecuteCommand(true, true);
73 HILOG_INFO(LOG_CORE, "exec %s exit %d!", bin_.c_str(), retval);
74
75 std::string line;
76 std::stringstream stream(output_);
77 while (std::getline(stream, line)) {
78 line = StringUtils::Strip(line);
79
80 // format likes: freq - CPU Frequency
81 auto pos = line.find('-');
82 if (pos == std::string::npos) {
83 continue;
84 }
85
86 auto category = line.substr(0, pos - 1);
87 HILOG_INFO(LOG_CORE, "found category: %s", category.c_str());
88 result.push_back(category);
89 }
90 return result;
91 }
92
PrepareListCategoriesCmd()93 bool TraceOps::PrepareListCategoriesCmd()
94 {
95 return false;
96 }
97
PrepareEnableCategoriesCmd()98 bool TraceOps::PrepareEnableCategoriesCmd()
99 {
100 return false;
101 }
102
PrepareDisableCategoriesCmd()103 bool TraceOps::PrepareDisableCategoriesCmd()
104 {
105 return false;
106 }
107
EnableCategories(const std::vector<std::string> & categories)108 bool TraceOps::EnableCategories(const std::vector<std::string>& categories)
109 {
110 CHECK_TRUE(categories.size() > 0, false, "categories empty!");
111 for (auto& category : categories) {
112 if (HasCategory(category)) {
113 targetCategories_.push_back(category);
114 }
115 }
116
117 args_ = {arg0_};
118 CHECK_TRUE(PrepareEnableCategoriesCmd(), false, "prepare enable categories failed!");
119
120 int retval = ExecuteCommand();
121 HILOG_INFO(LOG_CORE, "exec %s exit %d!", bin_.c_str(), retval);
122 return true;
123 }
124
DisableCategories()125 bool TraceOps::DisableCategories()
126 {
127 args_ = {arg0_};
128 CHECK_TRUE(PrepareDisableCategoriesCmd(), false, "prepare enable categories failed!");
129
130 int retval = ExecuteCommand();
131 HILOG_INFO(LOG_CORE, "exec %s exit %d!", bin_.c_str(), retval);
132
133 targetCategories_.clear();
134 return true;
135 }
136
ExecuteCommand(bool out2pipe,bool err2pipe)137 int TraceOps::ExecuteCommand(bool out2pipe, bool err2pipe)
138 {
139 CHECK_TRUE(bin_.size() > 0, -1, "bin_ empty");
140 CHECK_TRUE(args_.size() > 0, -1, "args_ empty");
141
142 ExecuteArgs execArgs = {};
143 execArgs.bin_ = bin_;
144 execArgs.argv_ = args_;
145 execArgs.out2pipe_ = out2pipe;
146 execArgs.err2pipe_ = err2pipe;
147
148 std::string cmdline = StringUtils::Join(args_, " ");
149 int retval = ProcessUtils::Execute(execArgs, output_);
150 return retval;
151 }
152 FTRACE_NS_END
153