Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
Yabin Cui | 6afc7e1 | 2015-06-17 22:21:12 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 28 | #include <algorithm> |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 29 | #include <map> |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 32 | #include <android-base/file.h> |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 34 | #include <android-base/parseint.h> |
Yabin Cui | df6333c | 2017-05-03 16:34:02 -0700 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 36 | #include <android-base/strings.h> |
Yabin Cui | 4192f46 | 2019-01-17 15:10:51 -0800 | [diff] [blame] | 37 | #include <build/version.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 38 | |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 39 | #include <7zCrc.h> |
| 40 | #include <Xz.h> |
| 41 | #include <XzCrc64.h> |
| 42 | |
Yabin Cui | 1c6be75 | 2023-02-28 11:46:37 -0800 | [diff] [blame] | 43 | #include "RegEx.h" |
| 44 | #include "environment.h" |
| 45 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 46 | namespace simpleperf { |
| 47 | |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 48 | using android::base::ParseInt; |
| 49 | using android::base::Split; |
| 50 | using android::base::StringPrintf; |
| 51 | |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 52 | void OneTimeFreeAllocator::Clear() { |
| 53 | for (auto& p : v_) { |
| 54 | delete[] p; |
| 55 | } |
| 56 | v_.clear(); |
| 57 | cur_ = nullptr; |
| 58 | end_ = nullptr; |
| 59 | } |
| 60 | |
Martin Stjernholm | 7c27cc2 | 2018-11-28 00:46:00 +0000 | [diff] [blame] | 61 | const char* OneTimeFreeAllocator::AllocateString(std::string_view s) { |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 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 | } |
Evgeny Eltsin | 2c4fbfe | 2020-08-26 15:01:40 +0200 | [diff] [blame] | 70 | memcpy(cur_, s.data(), s.size()); |
| 71 | cur_[s.size()] = '\0'; |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 72 | const char* result = cur_; |
| 73 | cur_ += size; |
| 74 | return result; |
| 75 | } |
| 76 | |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 77 | android::base::unique_fd FileHelper::OpenReadOnly(const std::string& filename) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 78 | int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY)); |
| 79 | return android::base::unique_fd(fd); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 82 | android::base::unique_fd FileHelper::OpenWriteOnly(const std::string& filename) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 83 | int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_BINARY | O_CREAT, 0644)); |
| 84 | return android::base::unique_fd(fd); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 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; |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 91 | } |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 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. |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 96 | static const char zip_preamble[] = {0x50, 0x4b, 0x03, 0x04}; |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 97 | char buf[4]; |
| 98 | if (!android::base::ReadFully(fd, buf, 4) || memcmp(buf, zip_preamble, 4) != 0) { |
| 99 | return nullptr; |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 100 | } |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 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)); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | ArchiveHelper::~ArchiveHelper() { |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 114 | CloseArchive(handle_); |
| 115 | } |
| 116 | |
| 117 | bool ArchiveHelper::IterateEntries( |
| 118 | const std::function<bool(ZipEntry&, const std::string&)>& callback) { |
| 119 | void* iteration_cookie; |
Elliott Hughes | b389bd7 | 2019-05-07 14:57:35 -0700 | [diff] [blame] | 120 | if (StartIteration(handle_, &iteration_cookie) < 0) { |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 121 | LOG(ERROR) << "Failed to iterate " << filename_; |
| 122 | return false; |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 123 | } |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 124 | ZipEntry zentry; |
Elliott Hughes | 6975eeb | 2019-05-22 18:59:29 -0700 | [diff] [blame] | 125 | std::string zname; |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 126 | int result; |
| 127 | while ((result = Next(iteration_cookie, &zentry, &zname)) == 0) { |
Elliott Hughes | 6975eeb | 2019-05-22 18:59:29 -0700 | [diff] [blame] | 128 | if (!callback(zentry, zname)) { |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 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 | |
| 140 | bool ArchiveHelper::FindEntry(const std::string& name, ZipEntry* entry) { |
Elliott Hughes | 3288566 | 2019-05-03 22:37:19 -0700 | [diff] [blame] | 141 | int result = ::FindEntry(handle_, name, entry); |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 142 | if (result != 0) { |
| 143 | LOG(ERROR) << "Failed to find " << name << " in " << filename_; |
| 144 | return false; |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
| 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 | |
| 158 | int ArchiveHelper::GetFd() { |
| 159 | return GetFileDescriptor(handle_); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 162 | void PrintIndented(size_t indent, const char* fmt, ...) { |
| 163 | va_list ap; |
| 164 | va_start(ap, fmt); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 165 | printf("%*s", static_cast<int>(indent * 2), ""); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 166 | vprintf(fmt, ap); |
| 167 | va_end(ap); |
| 168 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 169 | |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 178 | bool IsPowerOfTwo(uint64_t value) { |
| 179 | return (value != 0 && ((value & (value - 1)) == 0)); |
| 180 | } |
| 181 | |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 182 | std::vector<std::string> GetEntriesInDir(const std::string& dirpath) { |
| 183 | std::vector<std::string> result; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 184 | DIR* dir = opendir(dirpath.c_str()); |
| 185 | if (dir == nullptr) { |
| 186 | PLOG(DEBUG) << "can't open dir " << dirpath; |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 187 | return result; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 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 | } |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 194 | result.push_back(entry->d_name); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 195 | } |
| 196 | closedir(dir); |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 197 | return result; |
| 198 | } |
| 199 | |
| 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) { |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 204 | if (IsDir(dirpath + OS_PATH_SEPARATOR + entries[i])) { |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 205 | result.push_back(std::move(entries[i])); |
| 206 | } |
| 207 | } |
| 208 | return result; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 209 | } |
Yabin Cui | 6afc7e1 | 2015-06-17 22:21:12 -0700 | [diff] [blame] | 210 | |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 797116b | 2015-12-08 17:43:15 -0800 | [diff] [blame] | 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 | } |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 230 | |
| 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 | } |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 238 | |
| 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 | } |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 262 | |
Sen Jiang | ea853b1 | 2018-05-04 13:25:02 -0700 | [diff] [blame] | 263 | static void* xz_alloc(ISzAllocPtr, size_t size) { |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 264 | return malloc(size); |
| 265 | } |
| 266 | |
Sen Jiang | ea853b1 | 2018-05-04 13:25:02 -0700 | [diff] [blame] | 267 | static void xz_free(ISzAllocPtr, void* address) { |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 268 | free(address); |
| 269 | } |
| 270 | |
| 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]), |
Sen Jiang | ea853b1 | 2018-05-04 13:25:02 -0700 | [diff] [blame] | 290 | &src_remaining, true, CODER_FINISH_ANY, &status); |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 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 | } |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 308 | |
Yabin Cui | 750b373 | 2017-12-18 17:46:36 -0800 | [diff] [blame] | 309 | static std::map<std::string, android::base::LogSeverity> log_severity_map = { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 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}, |
Yabin Cui | 750b373 | 2017-12-18 17:46:36 -0800 | [diff] [blame] | 313 | }; |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 314 | bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity) { |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 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 | } |
Yabin Cui | 5633586 | 2016-04-18 13:43:20 -0700 | [diff] [blame] | 322 | |
Yabin Cui | 750b373 | 2017-12-18 17:46:36 -0800 | [diff] [blame] | 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 | |
Yabin Cui | 5633586 | 2016-04-18 13:43:20 -0700 | [diff] [blame] | 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 | } |
Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 344 | |
Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 345 | size_t GetPageSize() { |
| 346 | #if defined(__linux__) |
| 347 | return sysconf(_SC_PAGE_SIZE); |
| 348 | #else |
| 349 | return 4096; |
| 350 | #endif |
| 351 | } |
| 352 | |
| 353 | uint64_t ConvertBytesToValue(const char* bytes, uint32_t size) { |
Yabin Cui | f79a008 | 2016-11-14 11:23:14 -0800 | [diff] [blame] | 354 | if (size > 8) { |
| 355 | LOG(FATAL) << "unexpected size " << size << " in ConvertBytesToValue"; |
Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 356 | } |
Yabin Cui | f79a008 | 2016-11-14 11:23:14 -0800 | [diff] [blame] | 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; |
Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 365 | } |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 366 | |
| 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 | } |
Yabin Cui | df6333c | 2017-05-03 16:34:02 -0700 | [diff] [blame] | 373 | |
| 374 | constexpr int SIMPLEPERF_VERSION = 1; |
| 375 | |
| 376 | std::string GetSimpleperfVersion() { |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 377 | return StringPrintf("%d.build.%s", SIMPLEPERF_VERSION, android::build::GetBuildNumber().c_str()); |
Yabin Cui | df6333c | 2017-05-03 16:34:02 -0700 | [diff] [blame] | 378 | } |
Namhyung Kim | a5f1d42 | 2019-12-17 17:15:02 +0900 | [diff] [blame] | 379 | |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 380 | // Parse a line like: 0,1-3, 5, 7-8 |
| 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; |
Namhyung Kim | a5f1d42 | 2019-12-17 17:15:02 +0900 | [diff] [blame] | 386 | } |
| 387 | } |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 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 | |
| 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; |
Namhyung Kim | a5f1d42 | 2019-12-17 17:15:02 +0900 | [diff] [blame] | 428 | } |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 429 | |
Yabin Cui | 1c6be75 | 2023-02-28 11:46:37 -0800 | [diff] [blame] | 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 | |
Yabin Cui | 1cb8580 | 2021-11-12 17:56:57 -0800 | [diff] [blame] | 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 | |
Yabin Cui | 65b8fab | 2023-01-31 09:50:53 -0800 | [diff] [blame] | 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 | |
Yabin Cui | da3b6ce | 2023-04-28 17:42:02 -0700 | [diff] [blame] | 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 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 501 | } // namespace simpleperf |