1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <time.h>
18
19 #include <android-base/file.h>
20 #include <android-base/properties.h>
21 #include <android-base/stringprintf.h>
22 #include <android-base/strings.h>
23
24 #include <wakelock/wakelock.h>
25 #include <include/simpleperf_profcollect.hpp>
26
27 #include "ETMRecorder.h"
28 #include "command.h"
29 #include "event_attr.h"
30 #include "event_fd.h"
31 #include "event_selection_set.h"
32 #include "event_type.h"
33
34 using namespace simpleperf;
35
36 namespace {
37
38 class CommandRegister {
39 public:
CommandRegister()40 CommandRegister() {
41 RegisterRecordCommand();
42 RegisterInjectCommand();
43 }
44 };
45
46 CommandRegister command_register;
47
48 } // namespace
49
IsETMDriverAvailable()50 bool IsETMDriverAvailable() {
51 bool result = ETMRecorder::GetInstance().IsETMDriverAvailable();
52 LOG(INFO) << "HasDriverSupport result " << result;
53 return result;
54 }
55
IsETMDeviceAvailable()56 bool IsETMDeviceAvailable() {
57 auto result = ETMRecorder::GetInstance().CheckEtmSupport();
58 if (!result.ok()) {
59 LOG(INFO) << "HasDeviceSupport check failed: " << result.error();
60 return false;
61 }
62 const EventType* type = FindEventTypeByName("cs-etm", false);
63 if (type == nullptr) {
64 LOG(INFO) << "HasDeviceSupport check failed: no etm event";
65 return false;
66 }
67 bool ret = IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name);
68 LOG(INFO) << "HasDeviceSupport result " << ret;
69 return ret;
70 }
71
IsLBRAvailable()72 bool IsLBRAvailable() {
73 return IsBranchSamplingSupported();
74 }
75
ConvertArgs(const char ** args,int arg_count)76 static std::vector<std::string> ConvertArgs(const char** args, int arg_count) {
77 std::vector<std::string> cmd_args(arg_count);
78 for (int i = 0; i < arg_count; ++i) {
79 cmd_args[i] = args[i];
80 }
81 return cmd_args;
82 }
83
RunRecordCmd(const char ** args,int arg_count)84 bool RunRecordCmd(const char** args, int arg_count) {
85 std::vector<std::string> cmd_args = ConvertArgs(args, arg_count);
86 LOG(INFO) << "Record " << android::base::Join(cmd_args, " ");
87 // The kernel may panic when trying to hibernate or hotplug CPUs while collecting
88 // ETM data. So get wakelock to keep the CPUs on.
89 auto wakelock = android::wakelock::WakeLock::tryGet("profcollectd");
90 if (!wakelock) {
91 LOG(ERROR) << "Record failed: Failed to request wakelock.";
92 return false;
93 }
94 bool result = CreateCommandInstance("record")->Run(cmd_args);
95 LOG(INFO) << "Record result " << result;
96 return result;
97 }
98
RunInjectCmd(const char ** args,int arg_count)99 bool RunInjectCmd(const char** args, int arg_count) {
100 std::vector<std::string> cmd_args = ConvertArgs(args, arg_count);
101 LOG(INFO) << "Inject " << android::base::Join(cmd_args, " ");
102 bool result = CreateCommandInstance("inject")->Run(cmd_args);
103 LOG(INFO) << "Inject result " << result;
104 return result;
105 }
106
107 static android::base::unique_fd log_fd;
108 static android::base::LogFunction saved_log_func;
109
FileLogger(android::base::LogId id,android::base::LogSeverity severity,const char * tag,const char * file,unsigned int line,const char * message)110 static void FileLogger(android::base::LogId id, android::base::LogSeverity severity,
111 const char* tag, const char* file, unsigned int line, const char* message) {
112 if (log_fd.ok()) {
113 static const char log_characters[] = "VDIWEFF";
114 char severity_char = log_characters[severity];
115 struct tm now;
116 time_t t = time(nullptr);
117 localtime_r(&t, &now);
118 char timestamp[32];
119 strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
120 std::string s = android::base::StringPrintf("%s %c %s %s:%u] %s\n", tag, severity_char,
121 timestamp, file, line, message);
122 WriteStringToFd(s, log_fd);
123 }
124 saved_log_func(id, severity, tag, file, line, message);
125 }
126
SetLogFile(const char * filename)127 void SetLogFile(const char* filename) {
128 int fd = TEMP_FAILURE_RETRY(open(filename, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, 0600));
129 if (fd == -1) {
130 PLOG(ERROR) << "failed to open " << filename;
131 return;
132 }
133 log_fd.reset(fd);
134 saved_log_func = SetLogger(FileLogger);
135 }
136
ResetLogFile()137 void ResetLogFile() {
138 log_fd.reset();
139 SetLogger(std::move(saved_log_func));
140 }
141