1 /*
2 * Copyright (C) 2015 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 "utils.h"
18
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include <algorithm>
29 #include <map>
30 #include <string>
31
32 #include <android-base/file.h>
33 #include <android-base/logging.h>
34 #include <android-base/parseint.h>
35 #include <android-base/stringprintf.h>
36 #include <android-base/strings.h>
37 #include <build/version.h>
38
39 #include <7zCrc.h>
40 #include <Xz.h>
41 #include <XzCrc64.h>
42
43 #include "RegEx.h"
44 #include "environment.h"
45
46 namespace simpleperf {
47
48 using android::base::ParseInt;
49 using android::base::Split;
50 using android::base::StringPrintf;
51
Clear()52 void OneTimeFreeAllocator::Clear() {
53 for (auto& p : v_) {
54 delete[] p;
55 }
56 v_.clear();
57 cur_ = nullptr;
58 end_ = nullptr;
59 }
60
AllocateString(std::string_view s)61 const char* OneTimeFreeAllocator::AllocateString(std::string_view s) {
62 size_t size = s.size() + 1;
63 if (cur_ + size > end_) {
64 size_t alloc_size = std::max(size, unit_size_);
65 char* p = new char[alloc_size];
66 v_.push_back(p);
67 cur_ = p;
68 end_ = p + alloc_size;
69 }
70 memcpy(cur_, s.data(), s.size());
71 cur_[s.size()] = '\0';
72 const char* result = cur_;
73 cur_ += size;
74 return result;
75 }
76
OpenReadOnly(const std::string & filename)77 android::base::unique_fd FileHelper::OpenReadOnly(const std::string& filename) {
78 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY));
79 return android::base::unique_fd(fd);
80 }
81
OpenWriteOnly(const std::string & filename)82 android::base::unique_fd FileHelper::OpenWriteOnly(const std::string& filename) {
83 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_BINARY | O_CREAT, 0644));
84 return android::base::unique_fd(fd);
85 }
86
CreateInstance(const std::string & filename)87 std::unique_ptr<ArchiveHelper> ArchiveHelper::CreateInstance(const std::string& filename) {
88 android::base::unique_fd fd = FileHelper::OpenReadOnly(filename);
89 if (fd == -1) {
90 return nullptr;
91 }
92 // Simpleperf relies on ArchiveHelper to check if a file is zip file. We expect much more elf
93 // files than zip files in a process map. In order to detect invalid zip files fast, we add a
94 // check of magic number here. Note that OpenArchiveFd() detects invalid zip files in a thorough
95 // way, but it usually needs reading at least 64K file data.
96 static const char zip_preamble[] = {0x50, 0x4b, 0x03, 0x04};
97 char buf[4];
98 if (!android::base::ReadFully(fd, buf, 4) || memcmp(buf, zip_preamble, 4) != 0) {
99 return nullptr;
100 }
101 if (lseek(fd, 0, SEEK_SET) == -1) {
102 return nullptr;
103 }
104 ZipArchiveHandle handle;
105 int result = OpenArchiveFd(fd.release(), filename.c_str(), &handle);
106 if (result != 0) {
107 LOG(ERROR) << "Failed to open archive " << filename << ": " << ErrorCodeString(result);
108 return nullptr;
109 }
110 return std::unique_ptr<ArchiveHelper>(new ArchiveHelper(handle, filename));
111 }
112
~ArchiveHelper()113 ArchiveHelper::~ArchiveHelper() {
114 CloseArchive(handle_);
115 }
116
IterateEntries(const std::function<bool (ZipEntry &,const std::string &)> & callback)117 bool ArchiveHelper::IterateEntries(
118 const std::function<bool(ZipEntry&, const std::string&)>& callback) {
119 void* iteration_cookie;
120 if (StartIteration(handle_, &iteration_cookie) < 0) {
121 LOG(ERROR) << "Failed to iterate " << filename_;
122 return false;
123 }
124 ZipEntry zentry;
125 std::string zname;
126 int result;
127 while ((result = Next(iteration_cookie, &zentry, &zname)) == 0) {
128 if (!callback(zentry, zname)) {
129 break;
130 }
131 }
132 EndIteration(iteration_cookie);
133 if (result == -2) {
134 LOG(ERROR) << "Failed to iterate " << filename_;
135 return false;
136 }
137 return true;
138 }
139
FindEntry(const std::string & name,ZipEntry * entry)140 bool ArchiveHelper::FindEntry(const std::string& name, ZipEntry* entry) {
141 int result = ::FindEntry(handle_, name, entry);
142 if (result != 0) {
143 LOG(ERROR) << "Failed to find " << name << " in " << filename_;
144 return false;
145 }
146 return true;
147 }
148
GetEntryData(ZipEntry & entry,std::vector<uint8_t> * data)149 bool ArchiveHelper::GetEntryData(ZipEntry& entry, std::vector<uint8_t>* data) {
150 data->resize(entry.uncompressed_length);
151 if (ExtractToMemory(handle_, &entry, data->data(), data->size()) != 0) {
152 LOG(ERROR) << "Failed to extract entry at " << entry.offset << " in " << filename_;
153 return false;
154 }
155 return true;
156 }
157
GetFd()158 int ArchiveHelper::GetFd() {
159 return GetFileDescriptor(handle_);
160 }
161
PrintIndented(size_t indent,const char * fmt,...)162 void PrintIndented(size_t indent, const char* fmt, ...) {
163 va_list ap;
164 va_start(ap, fmt);
165 printf("%*s", static_cast<int>(indent * 2), "");
166 vprintf(fmt, ap);
167 va_end(ap);
168 }
169
FprintIndented(FILE * fp,size_t indent,const char * fmt,...)170 void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...) {
171 va_list ap;
172 va_start(ap, fmt);
173 fprintf(fp, "%*s", static_cast<int>(indent * 2), "");
174 vfprintf(fp, fmt, ap);
175 va_end(ap);
176 }
177
IsPowerOfTwo(uint64_t value)178 bool IsPowerOfTwo(uint64_t value) {
179 return (value != 0 && ((value & (value - 1)) == 0));
180 }
181
GetEntriesInDir(const std::string & dirpath)182 std::vector<std::string> GetEntriesInDir(const std::string& dirpath) {
183 std::vector<std::string> result;
184 DIR* dir = opendir(dirpath.c_str());
185 if (dir == nullptr) {
186 PLOG(DEBUG) << "can't open dir " << dirpath;
187 return result;
188 }
189 dirent* entry;
190 while ((entry = readdir(dir)) != nullptr) {
191 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
192 continue;
193 }
194 result.push_back(entry->d_name);
195 }
196 closedir(dir);
197 return result;
198 }
199
GetSubDirs(const std::string & dirpath)200 std::vector<std::string> GetSubDirs(const std::string& dirpath) {
201 std::vector<std::string> entries = GetEntriesInDir(dirpath);
202 std::vector<std::string> result;
203 for (size_t i = 0; i < entries.size(); ++i) {
204 if (IsDir(dirpath + OS_PATH_SEPARATOR + entries[i])) {
205 result.push_back(std::move(entries[i]));
206 }
207 }
208 return result;
209 }
210
IsDir(const std::string & dirpath)211 bool IsDir(const std::string& dirpath) {
212 struct stat st;
213 if (stat(dirpath.c_str(), &st) == 0) {
214 if (S_ISDIR(st.st_mode)) {
215 return true;
216 }
217 }
218 return false;
219 }
220
IsRegularFile(const std::string & filename)221 bool IsRegularFile(const std::string& filename) {
222 struct stat st;
223 if (stat(filename.c_str(), &st) == 0) {
224 if (S_ISREG(st.st_mode)) {
225 return true;
226 }
227 }
228 return false;
229 }
230
GetFileSize(const std::string & filename)231 uint64_t GetFileSize(const std::string& filename) {
232 struct stat st;
233 if (stat(filename.c_str(), &st) == 0) {
234 return static_cast<uint64_t>(st.st_size);
235 }
236 return 0;
237 }
238
MkdirWithParents(const std::string & path)239 bool MkdirWithParents(const std::string& path) {
240 size_t prev_end = 0;
241 while (prev_end < path.size()) {
242 size_t next_end = path.find('/', prev_end + 1);
243 if (next_end == std::string::npos) {
244 break;
245 }
246 std::string dir_path = path.substr(0, next_end);
247 if (!IsDir(dir_path)) {
248 #if defined(_WIN32)
249 int ret = mkdir(dir_path.c_str());
250 #else
251 int ret = mkdir(dir_path.c_str(), 0755);
252 #endif
253 if (ret != 0) {
254 PLOG(ERROR) << "failed to create dir " << dir_path;
255 return false;
256 }
257 }
258 prev_end = next_end;
259 }
260 return true;
261 }
262
xz_alloc(ISzAllocPtr,size_t size)263 static void* xz_alloc(ISzAllocPtr, size_t size) {
264 return malloc(size);
265 }
266
xz_free(ISzAllocPtr,void * address)267 static void xz_free(ISzAllocPtr, void* address) {
268 free(address);
269 }
270
XzDecompress(const std::string & compressed_data,std::string * decompressed_data)271 bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data) {
272 ISzAlloc alloc;
273 CXzUnpacker state;
274 alloc.Alloc = xz_alloc;
275 alloc.Free = xz_free;
276 XzUnpacker_Construct(&state, &alloc);
277 CrcGenerateTable();
278 Crc64GenerateTable();
279 size_t src_offset = 0;
280 size_t dst_offset = 0;
281 std::string dst(compressed_data.size(), ' ');
282
283 ECoderStatus status = CODER_STATUS_NOT_FINISHED;
284 while (status == CODER_STATUS_NOT_FINISHED) {
285 dst.resize(dst.size() * 2);
286 size_t src_remaining = compressed_data.size() - src_offset;
287 size_t dst_remaining = dst.size() - dst_offset;
288 int res = XzUnpacker_Code(&state, reinterpret_cast<Byte*>(&dst[dst_offset]), &dst_remaining,
289 reinterpret_cast<const Byte*>(&compressed_data[src_offset]),
290 &src_remaining, true, CODER_FINISH_ANY, &status);
291 if (res != SZ_OK) {
292 LOG(ERROR) << "LZMA decompression failed with error " << res;
293 XzUnpacker_Free(&state);
294 return false;
295 }
296 src_offset += src_remaining;
297 dst_offset += dst_remaining;
298 }
299 XzUnpacker_Free(&state);
300 if (!XzUnpacker_IsStreamWasFinished(&state)) {
301 LOG(ERROR) << "LZMA decompresstion failed due to incomplete stream";
302 return false;
303 }
304 dst.resize(dst_offset);
305 *decompressed_data = std::move(dst);
306 return true;
307 }
308
309 static std::map<std::string, android::base::LogSeverity> log_severity_map = {
310 {"verbose", android::base::VERBOSE}, {"debug", android::base::DEBUG},
311 {"info", android::base::INFO}, {"warning", android::base::WARNING},
312 {"error", android::base::ERROR}, {"fatal", android::base::FATAL},
313 };
GetLogSeverity(const std::string & name,android::base::LogSeverity * severity)314 bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity) {
315 auto it = log_severity_map.find(name);
316 if (it != log_severity_map.end()) {
317 *severity = it->second;
318 return true;
319 }
320 return false;
321 }
322
GetLogSeverityName()323 std::string GetLogSeverityName() {
324 android::base::LogSeverity severity = android::base::GetMinimumLogSeverity();
325 for (auto& pair : log_severity_map) {
326 if (severity == pair.second) {
327 return pair.first;
328 }
329 }
330 return "info";
331 }
332
IsRoot()333 bool IsRoot() {
334 static int is_root = -1;
335 if (is_root == -1) {
336 #if defined(__linux__)
337 is_root = (getuid() == 0) ? 1 : 0;
338 #else
339 is_root = 0;
340 #endif
341 }
342 return is_root == 1;
343 }
344
GetPageSize()345 size_t GetPageSize() {
346 #if defined(__linux__)
347 return sysconf(_SC_PAGE_SIZE);
348 #else
349 return 4096;
350 #endif
351 }
352
ConvertBytesToValue(const char * bytes,uint32_t size)353 uint64_t ConvertBytesToValue(const char* bytes, uint32_t size) {
354 if (size > 8) {
355 LOG(FATAL) << "unexpected size " << size << " in ConvertBytesToValue";
356 }
357 uint64_t result = 0;
358 int shift = 0;
359 for (uint32_t i = 0; i < size; ++i) {
360 uint64_t tmp = static_cast<unsigned char>(bytes[i]);
361 result |= tmp << shift;
362 shift += 8;
363 }
364 return result;
365 }
366
SecondToTimeval(double time_in_sec)367 timeval SecondToTimeval(double time_in_sec) {
368 timeval tv;
369 tv.tv_sec = static_cast<time_t>(time_in_sec);
370 tv.tv_usec = static_cast<int>((time_in_sec - tv.tv_sec) * 1000000);
371 return tv;
372 }
373
374 constexpr int SIMPLEPERF_VERSION = 1;
375
GetSimpleperfVersion()376 std::string GetSimpleperfVersion() {
377 return StringPrintf("%d.build.%s", SIMPLEPERF_VERSION, android::build::GetBuildNumber().c_str());
378 }
379
380 // Parse a line like: 0,1-3, 5, 7-8
GetCpusFromString(const std::string & s)381 std::optional<std::set<int>> GetCpusFromString(const std::string& s) {
382 std::string str;
383 for (char c : s) {
384 if (!isspace(c)) {
385 str += c;
386 }
387 }
388 std::set<int> cpus;
389 int cpu1;
390 int cpu2;
391 for (const std::string& p : Split(str, ",")) {
392 size_t split_pos = p.find('-');
393 if (split_pos == std::string::npos) {
394 if (!ParseInt(p, &cpu1, 0)) {
395 LOG(ERROR) << "failed to parse cpu: " << p;
396 return std::nullopt;
397 }
398 cpus.insert(cpu1);
399 } else {
400 if (!ParseInt(p.substr(0, split_pos), &cpu1, 0) ||
401 !ParseInt(p.substr(split_pos + 1), &cpu2, 0) || cpu1 > cpu2) {
402 LOG(ERROR) << "failed to parse cpu: " << p;
403 return std::nullopt;
404 }
405 while (cpu1 <= cpu2) {
406 cpus.insert(cpu1++);
407 }
408 }
409 }
410 return cpus;
411 }
412
GetTidsFromString(const std::string & s,bool check_if_exists)413 std::optional<std::set<pid_t>> GetTidsFromString(const std::string& s, bool check_if_exists) {
414 std::set<pid_t> tids;
415 for (const auto& p : Split(s, ",")) {
416 int tid;
417 if (!ParseInt(p.c_str(), &tid, 0)) {
418 LOG(ERROR) << "Invalid tid '" << p << "'";
419 return std::nullopt;
420 }
421 if (check_if_exists && !IsDir(StringPrintf("/proc/%d", tid))) {
422 LOG(ERROR) << "Non existing thread '" << tid << "'";
423 return std::nullopt;
424 }
425 tids.insert(tid);
426 }
427 return tids;
428 }
429
GetPidsFromStrings(const std::vector<std::string> & strs,bool check_if_exists,bool support_progress_name_regex)430 std::optional<std::set<pid_t>> GetPidsFromStrings(const std::vector<std::string>& strs,
431 bool check_if_exists,
432 bool support_progress_name_regex) {
433 std::set<pid_t> pids;
434 std::vector<std::unique_ptr<RegEx>> regs;
435 for (const auto& s : strs) {
436 for (const auto& p : Split(s, ",")) {
437 int pid;
438 if (ParseInt(p.c_str(), &pid, 0)) {
439 if (check_if_exists && !IsDir(StringPrintf("/proc/%d", pid))) {
440 LOG(ERROR) << "no process with pid " << pid;
441 return std::nullopt;
442 }
443 pids.insert(pid);
444 } else if (support_progress_name_regex) {
445 auto reg = RegEx::Create(p);
446 if (!reg) {
447 return std::nullopt;
448 }
449 regs.emplace_back(std::move(reg));
450 } else {
451 LOG(ERROR) << "invalid pid: " << p;
452 return std::nullopt;
453 }
454 }
455 }
456 if (!regs.empty()) {
457 #if defined(__linux__)
458 for (pid_t pid : GetAllProcesses()) {
459 std::string process_name = GetCompleteProcessName(pid);
460 if (process_name.empty()) {
461 continue;
462 }
463 for (const auto& reg : regs) {
464 if (reg->Search(process_name)) {
465 pids.insert(pid);
466 break;
467 }
468 }
469 }
470 #else // defined(__linux__)
471 LOG(ERROR) << "progress name regex isn't supported";
472 return std::nullopt;
473 #endif // defined(__linux__)
474 }
475 return pids;
476 }
477
SafeStrlen(const char * s,const char * end)478 size_t SafeStrlen(const char* s, const char* end) {
479 const char* p = s;
480 while (p < end && *p != '\0') {
481 p++;
482 }
483 return p - s;
484 }
485
SafeAdd(uint64_t a,uint64_t b)486 OverflowResult SafeAdd(uint64_t a, uint64_t b) {
487 OverflowResult result;
488 if (__builtin_add_overflow(a, b, &result.value)) {
489 result.overflow = true;
490 }
491 return result;
492 }
493
OverflowSafeAdd(uint64_t & dest,uint64_t add)494 void OverflowSafeAdd(uint64_t& dest, uint64_t add) {
495 if (__builtin_add_overflow(dest, add, &dest)) {
496 LOG(WARNING) << "Branch count overflow happened.";
497 dest = UINT64_MAX;
498 }
499 }
500
501 } // namespace simpleperf
502