blob: 86004677c6601a4c8d0effe7327e4bc821fbc6e8 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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
Yabin Cui7d59bb42015-05-04 20:27:57 -070019#include <dirent.h>
Yabin Cui323e9452015-04-20 18:07:17 -070020#include <errno.h>
Yabin Cuib1a885b2016-02-14 19:18:02 -080021#include <fcntl.h>
Yabin Cuib4212972016-05-25 14:08:05 -070022#include <inttypes.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070023#include <stdarg.h>
24#include <stdio.h>
Yabin Cui6afc7e12015-06-17 22:21:12 -070025#include <sys/stat.h>
Yabin Cui323e9452015-04-20 18:07:17 -070026#include <unistd.h>
27
Yabin Cuicc2e59e2015-08-21 14:23:43 -070028#include <algorithm>
Yabin Cui8f680f62016-03-18 18:47:43 -070029#include <map>
Yabin Cuicc2e59e2015-08-21 14:23:43 -070030#include <string>
31
Yabin Cuib1a885b2016-02-14 19:18:02 -080032#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080033#include <android-base/logging.h>
Yabin Cuie3ca9982020-10-16 13:16:26 -070034#include <android-base/parseint.h>
Yabin Cuidf6333c2017-05-03 16:34:02 -070035#include <android-base/stringprintf.h>
Yabin Cuie3ca9982020-10-16 13:16:26 -070036#include <android-base/strings.h>
Yabin Cui4192f462019-01-17 15:10:51 -080037#include <build/version.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070038
Yabin Cui05400532016-03-17 21:18:53 -070039#include <7zCrc.h>
40#include <Xz.h>
41#include <XzCrc64.h>
42
Yabin Cui1c6be752023-02-28 11:46:37 -080043#include "RegEx.h"
44#include "environment.h"
45
Yabin Cuifaa7b922021-01-11 17:35:57 -080046namespace simpleperf {
47
Yabin Cuie3ca9982020-10-16 13:16:26 -070048using android::base::ParseInt;
49using android::base::Split;
50using android::base::StringPrintf;
51
Yabin Cuicc2e59e2015-08-21 14:23:43 -070052void OneTimeFreeAllocator::Clear() {
53 for (auto& p : v_) {
54 delete[] p;
55 }
56 v_.clear();
57 cur_ = nullptr;
58 end_ = nullptr;
59}
60
Martin Stjernholm7c27cc22018-11-28 00:46:00 +000061const char* OneTimeFreeAllocator::AllocateString(std::string_view s) {
Yabin Cuicc2e59e2015-08-21 14:23:43 -070062 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 }
Evgeny Eltsin2c4fbfe2020-08-26 15:01:40 +020070 memcpy(cur_, s.data(), s.size());
71 cur_[s.size()] = '\0';
Yabin Cuicc2e59e2015-08-21 14:23:43 -070072 const char* result = cur_;
73 cur_ += size;
74 return result;
75}
76
Yabin Cui2a53ff32018-05-21 17:37:00 -070077android::base::unique_fd FileHelper::OpenReadOnly(const std::string& filename) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020078 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY));
79 return android::base::unique_fd(fd);
Yabin Cuib1a885b2016-02-14 19:18:02 -080080}
81
Yabin Cui2a53ff32018-05-21 17:37:00 -070082android::base::unique_fd FileHelper::OpenWriteOnly(const std::string& filename) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020083 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_BINARY | O_CREAT, 0644));
84 return android::base::unique_fd(fd);
Yabin Cuib1a885b2016-02-14 19:18:02 -080085}
86
Yabin Cui2a53ff32018-05-21 17:37:00 -070087std::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;
Yabin Cuib1a885b2016-02-14 19:18:02 -080091 }
Yabin Cui2a53ff32018-05-21 17:37:00 -070092 // 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.
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020096 static const char zip_preamble[] = {0x50, 0x4b, 0x03, 0x04};
Yabin Cui2a53ff32018-05-21 17:37:00 -070097 char buf[4];
98 if (!android::base::ReadFully(fd, buf, 4) || memcmp(buf, zip_preamble, 4) != 0) {
99 return nullptr;
Yabin Cuibe7ec662016-03-02 13:56:28 -0800100 }
Yabin Cui2a53ff32018-05-21 17:37:00 -0700101 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));
Yabin Cuibe7ec662016-03-02 13:56:28 -0800111}
112
113ArchiveHelper::~ArchiveHelper() {
Yabin Cui2a53ff32018-05-21 17:37:00 -0700114 CloseArchive(handle_);
115}
116
117bool ArchiveHelper::IterateEntries(
118 const std::function<bool(ZipEntry&, const std::string&)>& callback) {
119 void* iteration_cookie;
Elliott Hughesb389bd72019-05-07 14:57:35 -0700120 if (StartIteration(handle_, &iteration_cookie) < 0) {
Yabin Cui2a53ff32018-05-21 17:37:00 -0700121 LOG(ERROR) << "Failed to iterate " << filename_;
122 return false;
Yabin Cuibe7ec662016-03-02 13:56:28 -0800123 }
Yabin Cui2a53ff32018-05-21 17:37:00 -0700124 ZipEntry zentry;
Elliott Hughes6975eeb2019-05-22 18:59:29 -0700125 std::string zname;
Yabin Cui2a53ff32018-05-21 17:37:00 -0700126 int result;
127 while ((result = Next(iteration_cookie, &zentry, &zname)) == 0) {
Elliott Hughes6975eeb2019-05-22 18:59:29 -0700128 if (!callback(zentry, zname)) {
Yabin Cui2a53ff32018-05-21 17:37:00 -0700129 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
140bool ArchiveHelper::FindEntry(const std::string& name, ZipEntry* entry) {
Elliott Hughes32885662019-05-03 22:37:19 -0700141 int result = ::FindEntry(handle_, name, entry);
Yabin Cui2a53ff32018-05-21 17:37:00 -0700142 if (result != 0) {
143 LOG(ERROR) << "Failed to find " << name << " in " << filename_;
144 return false;
145 }
146 return true;
147}
148
149bool 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
158int ArchiveHelper::GetFd() {
159 return GetFileDescriptor(handle_);
Yabin Cuibe7ec662016-03-02 13:56:28 -0800160}
161
Yabin Cui67d3abd2015-04-16 15:26:31 -0700162void PrintIndented(size_t indent, const char* fmt, ...) {
163 va_list ap;
164 va_start(ap, fmt);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700165 printf("%*s", static_cast<int>(indent * 2), "");
Yabin Cui67d3abd2015-04-16 15:26:31 -0700166 vprintf(fmt, ap);
167 va_end(ap);
168}
Yabin Cui323e9452015-04-20 18:07:17 -0700169
Yabin Cui767dd172016-06-02 21:02:43 -0700170void 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
Yabin Cui9759e1b2015-04-28 15:54:13 -0700178bool IsPowerOfTwo(uint64_t value) {
179 return (value != 0 && ((value & (value - 1)) == 0));
180}
181
Yabin Cui07865862016-08-22 13:39:19 -0700182std::vector<std::string> GetEntriesInDir(const std::string& dirpath) {
183 std::vector<std::string> result;
Yabin Cui7d59bb42015-05-04 20:27:57 -0700184 DIR* dir = opendir(dirpath.c_str());
185 if (dir == nullptr) {
186 PLOG(DEBUG) << "can't open dir " << dirpath;
Yabin Cui07865862016-08-22 13:39:19 -0700187 return result;
Yabin Cui7d59bb42015-05-04 20:27:57 -0700188 }
189 dirent* entry;
190 while ((entry = readdir(dir)) != nullptr) {
191 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
192 continue;
193 }
Yabin Cui07865862016-08-22 13:39:19 -0700194 result.push_back(entry->d_name);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700195 }
196 closedir(dir);
Yabin Cui07865862016-08-22 13:39:19 -0700197 return result;
198}
199
200std::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) {
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700204 if (IsDir(dirpath + OS_PATH_SEPARATOR + entries[i])) {
Yabin Cui07865862016-08-22 13:39:19 -0700205 result.push_back(std::move(entries[i]));
206 }
207 }
208 return result;
Yabin Cui7d59bb42015-05-04 20:27:57 -0700209}
Yabin Cui6afc7e12015-06-17 22:21:12 -0700210
Yabin Cuib032de72015-06-17 21:15:09 -0700211bool 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
Yabin Cui797116b2015-12-08 17:43:15 -0800221bool 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}
Yabin Cuib1a885b2016-02-14 19:18:02 -0800230
231uint64_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}
Yabin Cuibe7ec662016-03-02 13:56:28 -0800238
239bool 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}
Yabin Cui05400532016-03-17 21:18:53 -0700262
Sen Jiangea853b12018-05-04 13:25:02 -0700263static void* xz_alloc(ISzAllocPtr, size_t size) {
Yabin Cui05400532016-03-17 21:18:53 -0700264 return malloc(size);
265}
266
Sen Jiangea853b12018-05-04 13:25:02 -0700267static void xz_free(ISzAllocPtr, void* address) {
Yabin Cui05400532016-03-17 21:18:53 -0700268 free(address);
269}
270
271bool 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]),
Sen Jiangea853b12018-05-04 13:25:02 -0700290 &src_remaining, true, CODER_FINISH_ANY, &status);
Yabin Cui05400532016-03-17 21:18:53 -0700291 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}
Yabin Cui8f680f62016-03-18 18:47:43 -0700308
Yabin Cui750b3732017-12-18 17:46:36 -0800309static std::map<std::string, android::base::LogSeverity> log_severity_map = {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200310 {"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},
Yabin Cui750b3732017-12-18 17:46:36 -0800313};
Yabin Cui8f680f62016-03-18 18:47:43 -0700314bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity) {
Yabin Cui8f680f62016-03-18 18:47:43 -0700315 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}
Yabin Cui56335862016-04-18 13:43:20 -0700322
Yabin Cui750b3732017-12-18 17:46:36 -0800323std::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
Yabin Cui56335862016-04-18 13:43:20 -0700333bool 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}
Yabin Cuib4212972016-05-25 14:08:05 -0700344
Yabin Cui4f41df62016-06-01 17:29:06 -0700345size_t GetPageSize() {
346#if defined(__linux__)
347 return sysconf(_SC_PAGE_SIZE);
348#else
349 return 4096;
350#endif
351}
352
353uint64_t ConvertBytesToValue(const char* bytes, uint32_t size) {
Yabin Cuif79a0082016-11-14 11:23:14 -0800354 if (size > 8) {
355 LOG(FATAL) << "unexpected size " << size << " in ConvertBytesToValue";
Yabin Cui4f41df62016-06-01 17:29:06 -0700356 }
Yabin Cuif79a0082016-11-14 11:23:14 -0800357 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;
Yabin Cui4f41df62016-06-01 17:29:06 -0700365}
Yabin Cui3e4c5952016-07-26 15:03:27 -0700366
367timeval 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}
Yabin Cuidf6333c2017-05-03 16:34:02 -0700373
374constexpr int SIMPLEPERF_VERSION = 1;
375
376std::string GetSimpleperfVersion() {
Yabin Cuie3ca9982020-10-16 13:16:26 -0700377 return StringPrintf("%d.build.%s", SIMPLEPERF_VERSION, android::build::GetBuildNumber().c_str());
Yabin Cuidf6333c2017-05-03 16:34:02 -0700378}
Namhyung Kima5f1d422019-12-17 17:15:02 +0900379
Yabin Cuie3ca9982020-10-16 13:16:26 -0700380// Parse a line like: 0,1-3, 5, 7-8
381std::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;
Namhyung Kima5f1d422019-12-17 17:15:02 +0900386 }
387 }
Yabin Cuie3ca9982020-10-16 13:16:26 -0700388 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
413std::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;
Namhyung Kima5f1d422019-12-17 17:15:02 +0900428}
Yabin Cuifaa7b922021-01-11 17:35:57 -0800429
Yabin Cui1c6be752023-02-28 11:46:37 -0800430std::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
Yabin Cui1cb85802021-11-12 17:56:57 -0800478size_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
Yabin Cui65b8fab2023-01-31 09:50:53 -0800486OverflowResult 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
Yabin Cuida3b6ce2023-04-28 17:42:02 -0700494void 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
Yabin Cuifaa7b922021-01-11 17:35:57 -0800501} // namespace simpleperf