Yabin Cui | 323e945 | 2015-04-20 18:07:17 -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 "environment.h" |
| 18 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 20 | #include <signal.h> |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Yabin Cui | 2db05b4 | 2018-07-16 14:04:49 -0700 | [diff] [blame] | 23 | #include <string.h> |
Yabin Cui | 7c5fe4e | 2017-12-05 17:44:05 -0800 | [diff] [blame] | 24 | #include <sys/resource.h> |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 25 | #include <sys/utsname.h> |
Yabin Cui | 554f3bb | 2021-05-05 13:34:59 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 27 | |
| 28 | #include <limits> |
Yabin Cui | cb4c17e | 2015-10-26 16:15:29 -0700 | [diff] [blame] | 29 | #include <set> |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 30 | #include <unordered_map> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 33 | #include <android-base/file.h> |
| 34 | #include <android-base/logging.h> |
Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 35 | #include <android-base/parseint.h> |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 36 | #include <android-base/stringprintf.h> |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 37 | #include <android-base/strings.h> |
Yabin Cui | 7e5aa13 | 2016-11-16 22:47:53 +0000 | [diff] [blame] | 38 | #include <procinfo/process.h> |
Yabin Cui | 4f79dc8 | 2018-05-09 18:25:25 -0700 | [diff] [blame] | 39 | #include <procinfo/process_map.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 40 | |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 41 | #if defined(__ANDROID__) |
Yabin Cui | 80a1e12 | 2017-08-11 14:52:51 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
Yabin Cui | 3485af9 | 2021-10-27 10:50:53 -0700 | [diff] [blame] | 43 | #include <cutils/android_filesystem_config.h> |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 46 | #include "IOEventLoop.h" |
Yabin Cui | a556c56 | 2020-02-14 16:50:10 -0800 | [diff] [blame] | 47 | #include "command.h" |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 48 | #include "event_type.h" |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 49 | #include "kallsyms.h" |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 50 | #include "read_elf.h" |
Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 51 | #include "thread_tree.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 52 | #include "utils.h" |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 53 | #include "workload.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 54 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 55 | namespace simpleperf { |
Yabin Cui | a556c56 | 2020-02-14 16:50:10 -0800 | [diff] [blame] | 56 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 57 | std::vector<int> GetOnlineCpus() { |
| 58 | std::vector<int> result; |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 59 | LineReader reader("/sys/devices/system/cpu/online"); |
| 60 | if (!reader.Ok()) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 61 | PLOG(ERROR) << "can't open online cpu information"; |
| 62 | return result; |
| 63 | } |
| 64 | |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 65 | std::string* line; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 66 | if ((line = reader.ReadLine()) != nullptr) { |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 67 | if (auto cpus = GetCpusFromString(*line); cpus) { |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 68 | result.assign(cpus->begin(), cpus->end()); |
| 69 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 70 | } |
| 71 | CHECK(!result.empty()) << "can't get online cpu information"; |
| 72 | return result; |
| 73 | } |
| 74 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 75 | static void GetAllModuleFiles(const std::string& path, |
| 76 | std::unordered_map<std::string, std::string>* module_file_map) { |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 77 | for (const auto& name : GetEntriesInDir(path)) { |
| 78 | std::string entry_path = path + "/" + name; |
| 79 | if (IsRegularFile(entry_path) && android::base::EndsWith(name, ".ko")) { |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 80 | std::string module_name = name.substr(0, name.size() - 3); |
| 81 | std::replace(module_name.begin(), module_name.end(), '-', '_'); |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 82 | module_file_map->insert(std::make_pair(module_name, entry_path)); |
| 83 | } else if (IsDir(entry_path)) { |
| 84 | GetAllModuleFiles(entry_path, module_file_map); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Yabin Cui | 7134f38 | 2016-03-25 17:43:43 -0700 | [diff] [blame] | 89 | static std::vector<KernelMmap> GetModulesInUse() { |
Yabin Cui | b068c10 | 2017-11-01 17:53:10 -0700 | [diff] [blame] | 90 | std::vector<KernelMmap> module_mmaps = GetLoadedModules(); |
| 91 | if (module_mmaps.empty()) { |
| 92 | return std::vector<KernelMmap>(); |
| 93 | } |
| 94 | std::unordered_map<std::string, std::string> module_file_map; |
| 95 | #if defined(__ANDROID__) |
| 96 | // Search directories listed in "File locations" section in |
| 97 | // https://source.android.com/devices/architecture/kernel/modular-kernels. |
| 98 | for (const auto& path : {"/vendor/lib/modules", "/odm/lib/modules", "/lib/modules"}) { |
| 99 | GetAllModuleFiles(path, &module_file_map); |
| 100 | } |
| 101 | #else |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 102 | utsname uname_buf; |
| 103 | if (TEMP_FAILURE_RETRY(uname(&uname_buf)) != 0) { |
| 104 | PLOG(ERROR) << "uname() failed"; |
| 105 | return std::vector<KernelMmap>(); |
| 106 | } |
| 107 | std::string linux_version = uname_buf.release; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 108 | std::string module_dirpath = "/lib/modules/" + linux_version + "/kernel"; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 109 | GetAllModuleFiles(module_dirpath, &module_file_map); |
Yabin Cui | b068c10 | 2017-11-01 17:53:10 -0700 | [diff] [blame] | 110 | #endif |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 111 | for (auto& module : module_mmaps) { |
| 112 | auto it = module_file_map.find(module.name); |
| 113 | if (it != module_file_map.end()) { |
| 114 | module.filepath = it->second; |
| 115 | } |
| 116 | } |
| 117 | return module_mmaps; |
| 118 | } |
| 119 | |
Yabin Cui | 7134f38 | 2016-03-25 17:43:43 -0700 | [diff] [blame] | 120 | void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps) { |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 121 | kernel_mmap->name = DEFAULT_KERNEL_MMAP_NAME; |
Yabin Cui | 7134f38 | 2016-03-25 17:43:43 -0700 | [diff] [blame] | 122 | kernel_mmap->start_addr = 0; |
Yabin Cui | b068c10 | 2017-11-01 17:53:10 -0700 | [diff] [blame] | 123 | kernel_mmap->len = std::numeric_limits<uint64_t>::max(); |
Yabin Cui | c13ff89 | 2020-11-10 13:11:01 -0800 | [diff] [blame] | 124 | if (uint64_t kstart_addr = GetKernelStartAddress(); kstart_addr != 0) { |
| 125 | kernel_mmap->name = std::string(DEFAULT_KERNEL_MMAP_NAME) + "_stext"; |
| 126 | kernel_mmap->start_addr = kstart_addr; |
| 127 | kernel_mmap->len = std::numeric_limits<uint64_t>::max() - kstart_addr; |
| 128 | } |
Yabin Cui | 7134f38 | 2016-03-25 17:43:43 -0700 | [diff] [blame] | 129 | kernel_mmap->filepath = kernel_mmap->name; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 130 | *module_mmaps = GetModulesInUse(); |
Yabin Cui | 7134f38 | 2016-03-25 17:43:43 -0700 | [diff] [blame] | 131 | for (auto& map : *module_mmaps) { |
| 132 | if (map.filepath.empty()) { |
| 133 | map.filepath = "[" + map.name + "]"; |
| 134 | } |
| 135 | } |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 138 | bool ReadThreadNameAndPid(pid_t tid, std::string* comm, pid_t* pid) { |
Yabin Cui | 7e5aa13 | 2016-11-16 22:47:53 +0000 | [diff] [blame] | 139 | android::procinfo::ProcessInfo procinfo; |
| 140 | if (!android::procinfo::GetProcessInfo(tid, &procinfo)) { |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
Yabin Cui | 7e5aa13 | 2016-11-16 22:47:53 +0000 | [diff] [blame] | 143 | if (comm != nullptr) { |
| 144 | *comm = procinfo.name; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 145 | } |
Yabin Cui | 7e5aa13 | 2016-11-16 22:47:53 +0000 | [diff] [blame] | 146 | if (pid != nullptr) { |
| 147 | *pid = procinfo.pid; |
| 148 | } |
| 149 | return true; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 152 | std::vector<pid_t> GetThreadsInProcess(pid_t pid) { |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 153 | std::vector<pid_t> result; |
Yabin Cui | 7e5aa13 | 2016-11-16 22:47:53 +0000 | [diff] [blame] | 154 | android::procinfo::GetProcessTids(pid, &result); |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 155 | return result; |
| 156 | } |
| 157 | |
Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 158 | bool IsThreadAlive(pid_t tid) { |
| 159 | return IsDir(android::base::StringPrintf("/proc/%d", tid)); |
| 160 | } |
| 161 | |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 162 | bool GetProcessForThread(pid_t tid, pid_t* pid) { |
| 163 | return ReadThreadNameAndPid(tid, nullptr, pid); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 166 | bool GetThreadName(pid_t tid, std::string* name) { |
| 167 | return ReadThreadNameAndPid(tid, name, nullptr); |
| 168 | } |
| 169 | |
| 170 | std::vector<pid_t> GetAllProcesses() { |
| 171 | std::vector<pid_t> result; |
| 172 | std::vector<std::string> entries = GetEntriesInDir("/proc"); |
| 173 | for (const auto& entry : entries) { |
| 174 | pid_t pid; |
| 175 | if (!android::base::ParseInt(entry.c_str(), &pid, 0)) { |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 176 | continue; |
| 177 | } |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 178 | result.push_back(pid); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 179 | } |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 180 | return result; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps) { |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 184 | thread_mmaps->clear(); |
Yabin Cui | 58740ff | 2021-03-23 13:34:51 -0700 | [diff] [blame] | 185 | return android::procinfo::ReadProcessMaps(pid, [&](const android::procinfo::MapInfo& mapinfo) { |
| 186 | thread_mmaps->emplace_back(mapinfo.start, mapinfo.end - mapinfo.start, mapinfo.pgoff, |
| 187 | mapinfo.name.c_str(), mapinfo.flags); |
| 188 | }); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 189 | } |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 190 | |
| 191 | bool GetKernelBuildId(BuildId* build_id) { |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 192 | ElfStatus result = GetBuildIdFromNoteFile("/sys/kernel/notes", build_id); |
| 193 | if (result != ElfStatus::NO_ERROR) { |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 194 | LOG(DEBUG) << "failed to read /sys/kernel/notes: " << result; |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 195 | } |
| 196 | return result == ElfStatus::NO_ERROR; |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Yabin Cui | ea105c8 | 2020-07-22 13:01:31 -0700 | [diff] [blame] | 199 | bool GetModuleBuildId(const std::string& module_name, BuildId* build_id, |
| 200 | const std::string& sysfs_dir) { |
| 201 | std::string notefile = sysfs_dir + "/module/" + module_name + "/notes/.note.gnu.build-id"; |
| 202 | return GetBuildIdFromNoteFile(notefile, build_id) == ElfStatus::NO_ERROR; |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 203 | } |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 204 | |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 205 | /* |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 206 | * perf event allow level: |
| 207 | * -1 - everything allowed |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 208 | * 0 - disallow raw tracepoint access for unpriv |
| 209 | * 1 - disallow cpu events for unpriv |
| 210 | * 2 - disallow kernel profiling for unpriv |
| 211 | * 3 - disallow user profiling for unpriv |
| 212 | */ |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 213 | static const char* perf_event_allow_path = "/proc/sys/kernel/perf_event_paranoid"; |
| 214 | |
| 215 | static bool ReadPerfEventAllowStatus(int* value) { |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 216 | std::string s; |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 217 | if (!android::base::ReadFileToString(perf_event_allow_path, &s)) { |
| 218 | PLOG(DEBUG) << "failed to read " << perf_event_allow_path; |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | s = android::base::Trim(s); |
| 222 | if (!android::base::ParseInt(s.c_str(), value)) { |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 223 | PLOG(ERROR) << "failed to parse " << perf_event_allow_path << ": " << s; |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | return true; |
| 227 | } |
| 228 | |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 229 | bool CanRecordRawData() { |
Yabin Cui | c3a9bf1 | 2020-02-27 16:55:38 -0800 | [diff] [blame] | 230 | if (IsRoot()) { |
| 231 | return true; |
Yabin Cui | a569776 | 2020-01-13 14:48:01 -0800 | [diff] [blame] | 232 | } |
Yabin Cui | c3a9bf1 | 2020-02-27 16:55:38 -0800 | [diff] [blame] | 233 | #if defined(__ANDROID__) |
Yabin Cui | c98cb11 | 2020-04-06 16:26:41 -0700 | [diff] [blame] | 234 | // Android R uses selinux to control perf_event_open. Whether raw data can be recorded is hard |
| 235 | // to check unless we really try it. And probably there is no need to record raw data in non-root |
| 236 | // users. |
| 237 | return false; |
| 238 | #else |
| 239 | int value; |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 240 | return ReadPerfEventAllowStatus(&value) && value == -1; |
Yabin Cui | c3a9bf1 | 2020-02-27 16:55:38 -0800 | [diff] [blame] | 241 | #endif |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 244 | static const char* GetLimitLevelDescription(int limit_level) { |
| 245 | switch (limit_level) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 246 | case -1: |
| 247 | return "unlimited"; |
| 248 | case 0: |
| 249 | return "disallowing raw tracepoint access for unpriv"; |
| 250 | case 1: |
| 251 | return "disallowing cpu events for unpriv"; |
| 252 | case 2: |
| 253 | return "disallowing kernel profiling for unpriv"; |
| 254 | case 3: |
| 255 | return "disallowing user profiling for unpriv"; |
| 256 | default: |
| 257 | return "unknown level"; |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | bool CheckPerfEventLimit() { |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 262 | // Root is not limited by perf_event_allow_path. However, the monitored threads |
Yabin Cui | 1137a8c | 2017-06-01 13:23:29 -0700 | [diff] [blame] | 263 | // may create child processes not running as root. To make sure the child processes have |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 264 | // enough permission to create inherited tracepoint events, write -1 to perf_event_allow_path. |
Yabin Cui | 1137a8c | 2017-06-01 13:23:29 -0700 | [diff] [blame] | 265 | // See http://b/62230699. |
Yabin Cui | 5edb7fc | 2018-08-29 14:54:19 -0700 | [diff] [blame] | 266 | if (IsRoot()) { |
Yabin Cui | c765ccb | 2021-02-23 15:53:09 -0800 | [diff] [blame] | 267 | if (android::base::WriteStringToFile("-1", perf_event_allow_path)) { |
| 268 | return true; |
| 269 | } |
| 270 | // On host, we may not be able to write to perf_event_allow_path (like when running in docker). |
| 271 | #if defined(__ANDROID__) |
| 272 | PLOG(ERROR) << "failed to write -1 to " << perf_event_allow_path; |
| 273 | return false; |
| 274 | #endif |
Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 275 | } |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 276 | int limit_level; |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 277 | bool can_read_allow_file = ReadPerfEventAllowStatus(&limit_level); |
| 278 | if (can_read_allow_file && limit_level <= 1) { |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 279 | return true; |
| 280 | } |
| 281 | #if defined(__ANDROID__) |
Yabin Cui | 80a1e12 | 2017-08-11 14:52:51 -0700 | [diff] [blame] | 282 | const std::string prop_name = "security.perf_harden"; |
| 283 | std::string prop_value = android::base::GetProperty(prop_name, ""); |
| 284 | if (prop_value.empty()) { |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 285 | // can't do anything if there is no such property. |
| 286 | return true; |
| 287 | } |
Yabin Cui | 80a1e12 | 2017-08-11 14:52:51 -0700 | [diff] [blame] | 288 | if (prop_value == "0") { |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 289 | return true; |
| 290 | } |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 291 | // Try to enable perf events by setprop security.perf_harden=0. |
Yabin Cui | 80a1e12 | 2017-08-11 14:52:51 -0700 | [diff] [blame] | 292 | if (android::base::SetProperty(prop_name, "0")) { |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 293 | sleep(1); |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 294 | if (can_read_allow_file && ReadPerfEventAllowStatus(&limit_level) && limit_level <= 1) { |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 295 | return true; |
| 296 | } |
Yabin Cui | 80a1e12 | 2017-08-11 14:52:51 -0700 | [diff] [blame] | 297 | if (android::base::GetProperty(prop_name, "") == "0") { |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | } |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 301 | if (can_read_allow_file) { |
| 302 | LOG(WARNING) << perf_event_allow_path << " is " << limit_level << ", " |
| 303 | << GetLimitLevelDescription(limit_level) << "."; |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 304 | } |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 305 | LOG(WARNING) << "Try using `adb shell setprop security.perf_harden 0` to allow profiling."; |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 306 | return false; |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 307 | #else |
Yabin Cui | 17cffbb | 2020-07-28 17:11:39 -0700 | [diff] [blame] | 308 | if (can_read_allow_file) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 309 | LOG(WARNING) << perf_event_allow_path << " is " << limit_level << ", " |
| 310 | << GetLimitLevelDescription(limit_level) << "."; |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 311 | return false; |
| 312 | } |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 313 | #endif |
| 314 | return true; |
| 315 | } |
Yabin Cui | b891549 | 2016-06-22 12:43:09 -0700 | [diff] [blame] | 316 | |
Yabin Cui | 6e173a4 | 2018-08-13 15:58:25 -0700 | [diff] [blame] | 317 | #if defined(__ANDROID__) |
| 318 | static bool SetProperty(const char* prop_name, uint64_t value) { |
| 319 | if (!android::base::SetProperty(prop_name, std::to_string(value))) { |
| 320 | LOG(ERROR) << "Failed to SetProperty " << prop_name << " to " << value; |
Yabin Cui | 0720d48 | 2017-03-06 17:05:50 -0800 | [diff] [blame] | 321 | return false; |
| 322 | } |
| 323 | return true; |
| 324 | } |
| 325 | |
Yabin Cui | 6e173a4 | 2018-08-13 15:58:25 -0700 | [diff] [blame] | 326 | bool SetPerfEventLimits(uint64_t sample_freq, size_t cpu_percent, uint64_t mlock_kb) { |
| 327 | if (!SetProperty("debug.perf_event_max_sample_rate", sample_freq) || |
| 328 | !SetProperty("debug.perf_cpu_time_max_percent", cpu_percent) || |
| 329 | !SetProperty("debug.perf_event_mlock_kb", mlock_kb) || |
| 330 | !SetProperty("security.perf_harden", 0)) { |
| 331 | return false; |
| 332 | } |
| 333 | // Wait for init process to change perf event limits based on properties. |
| 334 | const size_t max_wait_us = 3 * 1000000; |
| 335 | int finish_mask = 0; |
| 336 | for (size_t i = 0; i < max_wait_us && finish_mask != 7; ++i) { |
| 337 | usleep(1); // Wait 1us to avoid busy loop. |
| 338 | if ((finish_mask & 1) == 0) { |
| 339 | uint64_t freq; |
| 340 | if (!GetMaxSampleFrequency(&freq) || freq == sample_freq) { |
| 341 | finish_mask |= 1; |
| 342 | } |
| 343 | } |
| 344 | if ((finish_mask & 2) == 0) { |
| 345 | size_t percent; |
| 346 | if (!GetCpuTimeMaxPercent(&percent) || percent == cpu_percent) { |
| 347 | finish_mask |= 2; |
| 348 | } |
| 349 | } |
| 350 | if ((finish_mask & 4) == 0) { |
| 351 | uint64_t kb; |
| 352 | if (!GetPerfEventMlockKb(&kb) || kb == mlock_kb) { |
| 353 | finish_mask |= 4; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | if (finish_mask != 7) { |
| 358 | LOG(WARNING) << "Wait setting perf event limits timeout"; |
| 359 | } |
| 360 | return true; |
| 361 | } |
| 362 | #else // !defined(__ANDROID__) |
| 363 | bool SetPerfEventLimits(uint64_t, size_t, uint64_t) { |
| 364 | return true; |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | template <typename T> |
| 369 | static bool ReadUintFromProcFile(const std::string& path, T* value) { |
| 370 | std::string s; |
| 371 | if (!android::base::ReadFileToString(path, &s)) { |
| 372 | PLOG(DEBUG) << "failed to read " << path; |
| 373 | return false; |
| 374 | } |
| 375 | s = android::base::Trim(s); |
| 376 | if (!android::base::ParseUint(s.c_str(), value)) { |
| 377 | LOG(ERROR) << "failed to parse " << path << ": " << s; |
| 378 | return false; |
| 379 | } |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | template <typename T> |
| 384 | static bool WriteUintToProcFile(const std::string& path, T value) { |
| 385 | if (IsRoot()) { |
| 386 | return android::base::WriteStringToFile(std::to_string(value), path); |
| 387 | } |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | bool GetMaxSampleFrequency(uint64_t* max_sample_freq) { |
| 392 | return ReadUintFromProcFile("/proc/sys/kernel/perf_event_max_sample_rate", max_sample_freq); |
| 393 | } |
| 394 | |
| 395 | bool SetMaxSampleFrequency(uint64_t max_sample_freq) { |
| 396 | return WriteUintToProcFile("/proc/sys/kernel/perf_event_max_sample_rate", max_sample_freq); |
| 397 | } |
| 398 | |
| 399 | bool GetCpuTimeMaxPercent(size_t* percent) { |
| 400 | return ReadUintFromProcFile("/proc/sys/kernel/perf_cpu_time_max_percent", percent); |
| 401 | } |
| 402 | |
| 403 | bool SetCpuTimeMaxPercent(size_t percent) { |
| 404 | return WriteUintToProcFile("/proc/sys/kernel/perf_cpu_time_max_percent", percent); |
| 405 | } |
| 406 | |
| 407 | bool GetPerfEventMlockKb(uint64_t* mlock_kb) { |
| 408 | return ReadUintFromProcFile("/proc/sys/kernel/perf_event_mlock_kb", mlock_kb); |
| 409 | } |
| 410 | |
| 411 | bool SetPerfEventMlockKb(uint64_t mlock_kb) { |
| 412 | return WriteUintToProcFile("/proc/sys/kernel/perf_event_mlock_kb", mlock_kb); |
| 413 | } |
| 414 | |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 415 | ArchType GetMachineArch() { |
Yabin Cui | f227b6d | 2021-01-06 14:01:53 -0800 | [diff] [blame] | 416 | #if defined(__i386__) |
| 417 | // For 32 bit x86 build, we can't get machine arch by uname(). |
| 418 | ArchType arch = ARCH_UNSUPPORTED; |
| 419 | std::unique_ptr<FILE, decltype(&pclose)> fp(popen("uname -m", "re"), pclose); |
| 420 | if (fp) { |
| 421 | char machine[40]; |
| 422 | if (fgets(machine, sizeof(machine), fp.get()) == machine) { |
| 423 | arch = GetArchType(android::base::Trim(machine)); |
| 424 | } |
| 425 | } |
| 426 | #else |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 427 | utsname uname_buf; |
| 428 | if (TEMP_FAILURE_RETRY(uname(&uname_buf)) != 0) { |
| 429 | PLOG(WARNING) << "uname() failed"; |
Yabin Cui | 3807691 | 2021-08-16 16:59:09 -0700 | [diff] [blame] | 430 | return GetTargetArch(); |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 431 | } |
| 432 | ArchType arch = GetArchType(uname_buf.machine); |
Yabin Cui | f227b6d | 2021-01-06 14:01:53 -0800 | [diff] [blame] | 433 | #endif |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 434 | if (arch != ARCH_UNSUPPORTED) { |
| 435 | return arch; |
| 436 | } |
Yabin Cui | 3807691 | 2021-08-16 16:59:09 -0700 | [diff] [blame] | 437 | return GetTargetArch(); |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 438 | } |
Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 439 | |
| 440 | void PrepareVdsoFile() { |
| 441 | // vdso is an elf file in memory loaded in each process's user space by the kernel. To read |
| 442 | // symbols from it and unwind through it, we need to dump it into a file in storage. |
| 443 | // It doesn't affect much when failed to prepare vdso file, so there is no need to return values. |
| 444 | std::vector<ThreadMmap> thread_mmaps; |
| 445 | if (!GetThreadMmapsInProcess(getpid(), &thread_mmaps)) { |
| 446 | return; |
| 447 | } |
| 448 | const ThreadMmap* vdso_map = nullptr; |
| 449 | for (const auto& map : thread_mmaps) { |
| 450 | if (map.name == "[vdso]") { |
| 451 | vdso_map = ↦ |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | if (vdso_map == nullptr) { |
| 456 | return; |
| 457 | } |
| 458 | std::string s(vdso_map->len, '\0'); |
| 459 | memcpy(&s[0], reinterpret_cast<void*>(static_cast<uintptr_t>(vdso_map->start_addr)), |
| 460 | vdso_map->len); |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 461 | std::unique_ptr<TemporaryFile> tmpfile = ScopedTempFiles::CreateTempFile(); |
| 462 | if (!android::base::WriteStringToFd(s, tmpfile->fd)) { |
Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 463 | return; |
| 464 | } |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 465 | Dso::SetVdsoFile(tmpfile->path, sizeof(size_t) == sizeof(uint64_t)); |
Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 466 | } |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 467 | |
Yabin Cui | 8838769 | 2017-08-28 15:49:33 -0700 | [diff] [blame] | 468 | static bool HasOpenedAppApkFile(int pid) { |
| 469 | std::string fd_path = "/proc/" + std::to_string(pid) + "/fd/"; |
| 470 | std::vector<std::string> files = GetEntriesInDir(fd_path); |
| 471 | for (const auto& file : files) { |
| 472 | std::string real_path; |
| 473 | if (!android::base::Readlink(fd_path + file, &real_path)) { |
| 474 | continue; |
| 475 | } |
| 476 | if (real_path.find("app") != std::string::npos && real_path.find(".apk") != std::string::npos) { |
| 477 | return true; |
| 478 | } |
| 479 | } |
| 480 | return false; |
| 481 | } |
| 482 | |
Yabin Cui | 7cb6f29 | 2017-08-28 14:49:04 -0700 | [diff] [blame] | 483 | std::set<pid_t> WaitForAppProcesses(const std::string& package_name) { |
| 484 | std::set<pid_t> result; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 485 | size_t loop_count = 0; |
| 486 | while (true) { |
| 487 | std::vector<pid_t> pids = GetAllProcesses(); |
| 488 | for (pid_t pid : pids) { |
| 489 | std::string cmdline; |
| 490 | if (!android::base::ReadFileToString("/proc/" + std::to_string(pid) + "/cmdline", &cmdline)) { |
| 491 | // Maybe we don't have permission to read it. |
| 492 | continue; |
| 493 | } |
Yabin Cui | 7cb6f29 | 2017-08-28 14:49:04 -0700 | [diff] [blame] | 494 | std::string process_name = android::base::Basename(cmdline); |
| 495 | // The app may have multiple processes, with process name like |
| 496 | // com.google.android.googlequicksearchbox:search. |
| 497 | size_t split_pos = process_name.find(':'); |
| 498 | if (split_pos != std::string::npos) { |
| 499 | process_name = process_name.substr(0, split_pos); |
| 500 | } |
| 501 | if (process_name != package_name) { |
Yabin Cui | 8838769 | 2017-08-28 15:49:33 -0700 | [diff] [blame] | 502 | continue; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 503 | } |
Yabin Cui | 8838769 | 2017-08-28 15:49:33 -0700 | [diff] [blame] | 504 | // If a debuggable app with wrap.sh runs on Android O, the app will be started with |
| 505 | // logwrapper as below: |
| 506 | // 1. Zygote forks a child process, rename it to package_name. |
| 507 | // 2. The child process execute sh, which starts a child process running |
| 508 | // /system/bin/logwrapper. |
| 509 | // 3. logwrapper starts a child process running sh, which interprets wrap.sh. |
| 510 | // 4. wrap.sh starts a child process running the app. |
| 511 | // The problem here is we want to profile the process started in step 4, but sometimes we |
| 512 | // run into the process started in step 1. To solve it, we can check if the process has |
| 513 | // opened an apk file in some app dirs. |
| 514 | if (!HasOpenedAppApkFile(pid)) { |
| 515 | continue; |
| 516 | } |
| 517 | if (loop_count > 0u) { |
| 518 | LOG(INFO) << "Got process " << pid << " for package " << package_name; |
| 519 | } |
Yabin Cui | 7cb6f29 | 2017-08-28 14:49:04 -0700 | [diff] [blame] | 520 | result.insert(pid); |
| 521 | } |
| 522 | if (!result.empty()) { |
| 523 | return result; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 524 | } |
| 525 | if (++loop_count == 1u) { |
| 526 | LOG(INFO) << "Waiting for process of app " << package_name; |
| 527 | } |
| 528 | usleep(1000); |
| 529 | } |
| 530 | } |
| 531 | |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 532 | namespace { |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 533 | |
Yabin Cui | db19d6d | 2021-05-12 17:36:02 -0700 | [diff] [blame] | 534 | bool IsAppDebuggable(int user_id, const std::string& package_name) { |
| 535 | return Workload::RunCmd({"run-as", package_name, "--user", std::to_string(user_id), "echo", |
| 536 | ">/dev/null", "2>/dev/null"}, |
| 537 | false); |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 540 | class InAppRunner { |
| 541 | public: |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 542 | InAppRunner(int user_id, const std::string& package_name) |
| 543 | : user_id_(std::to_string(user_id)), package_name_(package_name) {} |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 544 | virtual ~InAppRunner() { |
| 545 | if (!tracepoint_file_.empty()) { |
| 546 | unlink(tracepoint_file_.c_str()); |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 547 | } |
| 548 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 549 | virtual bool Prepare() = 0; |
| 550 | bool RunCmdInApp(const std::string& cmd, const std::vector<std::string>& args, |
| 551 | size_t workload_args_size, const std::string& output_filepath, |
| 552 | bool need_tracepoint_events); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 553 | |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 554 | protected: |
| 555 | virtual std::vector<std::string> GetPrefixArgs(const std::string& cmd) = 0; |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 556 | |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 557 | const std::string user_id_; |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 558 | const std::string package_name_; |
| 559 | std::string tracepoint_file_; |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 560 | }; |
| 561 | |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 562 | bool InAppRunner::RunCmdInApp(const std::string& cmd, const std::vector<std::string>& cmd_args, |
| 563 | size_t workload_args_size, const std::string& output_filepath, |
| 564 | bool need_tracepoint_events) { |
| 565 | // 1. Build cmd args running in app's context. |
| 566 | std::vector<std::string> args = GetPrefixArgs(cmd); |
| 567 | args.insert(args.end(), {"--in-app", "--log", GetLogSeverityName()}); |
Yabin Cui | a556c56 | 2020-02-14 16:50:10 -0800 | [diff] [blame] | 568 | if (log_to_android_buffer) { |
| 569 | args.emplace_back("--log-to-android-buffer"); |
| 570 | } |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 571 | if (need_tracepoint_events) { |
| 572 | // Since we can't read tracepoint events from tracefs in app's context, we need to prepare |
| 573 | // them in tracepoint_file in shell's context, and pass the path of tracepoint_file to the |
| 574 | // child process using --tracepoint-events option. |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 575 | const std::string tracepoint_file = "/data/local/tmp/tracepoint_events"; |
Yabin Cui | 16a6ace | 2020-10-01 14:56:32 -0700 | [diff] [blame] | 576 | if (!EventTypeManager::Instance().WriteTracepointsToFile(tracepoint_file)) { |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 577 | PLOG(ERROR) << "Failed to store tracepoint events"; |
| 578 | return false; |
| 579 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 580 | tracepoint_file_ = tracepoint_file; |
| 581 | args.insert(args.end(), {"--tracepoint-events", tracepoint_file_}); |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 582 | } |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 583 | |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 584 | android::base::unique_fd out_fd; |
| 585 | if (!output_filepath.empty()) { |
| 586 | // A process running in app's context can't open a file outside it's data directory to write. |
| 587 | // So pass it a file descriptor to write. |
| 588 | out_fd = FileHelper::OpenWriteOnly(output_filepath); |
| 589 | if (out_fd == -1) { |
| 590 | PLOG(ERROR) << "Failed to open " << output_filepath; |
| 591 | return false; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 592 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 593 | args.insert(args.end(), {"--out-fd", std::to_string(int(out_fd))}); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 594 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 595 | |
| 596 | // We can't send signal to a process running in app's context. So use a pipe file to send stop |
| 597 | // signal. |
| 598 | android::base::unique_fd stop_signal_rfd; |
| 599 | android::base::unique_fd stop_signal_wfd; |
| 600 | if (!android::base::Pipe(&stop_signal_rfd, &stop_signal_wfd, 0)) { |
| 601 | PLOG(ERROR) << "pipe"; |
| 602 | return false; |
| 603 | } |
| 604 | args.insert(args.end(), {"--stop-signal-fd", std::to_string(int(stop_signal_rfd))}); |
| 605 | |
| 606 | for (size_t i = 0; i < cmd_args.size(); ++i) { |
| 607 | if (i < cmd_args.size() - workload_args_size) { |
| 608 | // Omit "-o output_file". It is replaced by "--out-fd fd". |
| 609 | if (cmd_args[i] == "-o" || cmd_args[i] == "--app") { |
| 610 | i++; |
| 611 | continue; |
| 612 | } |
| 613 | } |
| 614 | args.push_back(cmd_args[i]); |
| 615 | } |
| 616 | char* argv[args.size() + 1]; |
| 617 | for (size_t i = 0; i < args.size(); ++i) { |
| 618 | argv[i] = &args[i][0]; |
| 619 | } |
| 620 | argv[args.size()] = nullptr; |
| 621 | |
| 622 | // 2. Run child process in app's context. |
| 623 | auto ChildProcFn = [&]() { |
| 624 | stop_signal_wfd.reset(); |
| 625 | execvp(argv[0], argv); |
| 626 | exit(1); |
| 627 | }; |
| 628 | std::unique_ptr<Workload> workload = Workload::CreateWorkload(ChildProcFn); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 629 | if (!workload) { |
| 630 | return false; |
| 631 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 632 | stop_signal_rfd.reset(); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 633 | |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 634 | // Wait on signals. |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 635 | IOEventLoop loop; |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 636 | bool need_to_stop_child = false; |
Yabin Cui | d5d9c42 | 2018-04-11 17:50:56 -0700 | [diff] [blame] | 637 | std::vector<int> stop_signals = {SIGINT, SIGTERM}; |
| 638 | if (!SignalIsIgnored(SIGHUP)) { |
| 639 | stop_signals.push_back(SIGHUP); |
| 640 | } |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 641 | if (!loop.AddSignalEvents(stop_signals, [&]() { |
| 642 | need_to_stop_child = true; |
| 643 | return loop.ExitLoop(); |
| 644 | })) { |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 645 | return false; |
| 646 | } |
| 647 | if (!loop.AddSignalEvent(SIGCHLD, [&]() { return loop.ExitLoop(); })) { |
| 648 | return false; |
| 649 | } |
| 650 | |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 651 | if (!workload->Start()) { |
| 652 | return false; |
| 653 | } |
| 654 | if (!loop.RunLoop()) { |
| 655 | return false; |
| 656 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 657 | if (need_to_stop_child) { |
| 658 | stop_signal_wfd.reset(); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 659 | } |
| 660 | int exit_code; |
Yabin Cui | 248ef5e | 2021-12-16 14:09:39 -0800 | [diff] [blame] | 661 | if (!workload->WaitChildProcess(true, &exit_code) || exit_code != 0) { |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 662 | return false; |
| 663 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 664 | return true; |
| 665 | } |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 666 | |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 667 | class RunAs : public InAppRunner { |
| 668 | public: |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 669 | RunAs(int user_id, const std::string& package_name) : InAppRunner(user_id, package_name) {} |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 670 | virtual ~RunAs() { |
| 671 | if (simpleperf_copied_in_app_) { |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 672 | Workload::RunCmd({"run-as", package_name_, "--user", user_id_, "rm", "-rf", "simpleperf"}); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 673 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 674 | } |
| 675 | bool Prepare() override; |
| 676 | |
| 677 | protected: |
| 678 | std::vector<std::string> GetPrefixArgs(const std::string& cmd) { |
Yabin Cui | 8729bc6 | 2021-05-11 11:29:38 -0700 | [diff] [blame] | 679 | std::vector<std::string> args = {"run-as", |
| 680 | package_name_, |
| 681 | "--user", |
| 682 | user_id_, |
| 683 | simpleperf_copied_in_app_ ? "./simpleperf" : simpleperf_path_, |
| 684 | cmd, |
| 685 | "--app", |
| 686 | package_name_}; |
| 687 | if (cmd == "record") { |
| 688 | if (simpleperf_copied_in_app_ || GetAndroidVersion() >= kAndroidVersionS) { |
| 689 | args.emplace_back("--add-meta-info"); |
| 690 | args.emplace_back("app_type=debuggable"); |
| 691 | } |
| 692 | } |
| 693 | return args; |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | bool simpleperf_copied_in_app_ = false; |
| 697 | std::string simpleperf_path_; |
| 698 | }; |
| 699 | |
| 700 | bool RunAs::Prepare() { |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 701 | // run-as can't run /data/local/tmp/simpleperf directly. So copy simpleperf binary if needed. |
| 702 | if (!android::base::Readlink("/proc/self/exe", &simpleperf_path_)) { |
| 703 | PLOG(ERROR) << "ReadLink failed"; |
| 704 | return false; |
| 705 | } |
Yabin Cui | a24cf96 | 2019-01-29 17:06:42 -0800 | [diff] [blame] | 706 | if (simpleperf_path_.find("CtsSimpleperfTest") != std::string::npos) { |
| 707 | simpleperf_path_ = "/system/bin/simpleperf"; |
| 708 | return true; |
| 709 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 710 | if (android::base::StartsWith(simpleperf_path_, "/system")) { |
| 711 | return true; |
| 712 | } |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 713 | if (!Workload::RunCmd( |
| 714 | {"run-as", package_name_, "--user", user_id_, "cp", simpleperf_path_, "simpleperf"})) { |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 715 | return false; |
| 716 | } |
| 717 | simpleperf_copied_in_app_ = true; |
| 718 | return true; |
| 719 | } |
| 720 | |
| 721 | class SimpleperfAppRunner : public InAppRunner { |
| 722 | public: |
Greg Kaiser | de124ee | 2021-05-17 12:54:14 -0700 | [diff] [blame] | 723 | SimpleperfAppRunner(int user_id, const std::string& package_name, const std::string& app_type) |
Yabin Cui | db19d6d | 2021-05-12 17:36:02 -0700 | [diff] [blame] | 724 | : InAppRunner(user_id, package_name) { |
| 725 | // On Android < S, the app type is unknown before running simpleperf_app_runner. Assume it's |
| 726 | // profileable. |
| 727 | app_type_ = app_type == "unknown" ? "profileable" : app_type; |
| 728 | } |
Yabin Cui | 8729bc6 | 2021-05-11 11:29:38 -0700 | [diff] [blame] | 729 | bool Prepare() override { return GetAndroidVersion() >= kAndroidVersionQ; } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 730 | |
| 731 | protected: |
| 732 | std::vector<std::string> GetPrefixArgs(const std::string& cmd) { |
Yabin Cui | 92825be | 2020-12-02 17:06:54 -0800 | [diff] [blame] | 733 | std::vector<std::string> args = {"simpleperf_app_runner", package_name_}; |
| 734 | if (user_id_ != "0") { |
| 735 | args.emplace_back("--user"); |
| 736 | args.emplace_back(user_id_); |
| 737 | } |
| 738 | args.emplace_back(cmd); |
Yabin Cui | 8729bc6 | 2021-05-11 11:29:38 -0700 | [diff] [blame] | 739 | if (cmd == "record" && GetAndroidVersion() >= kAndroidVersionS) { |
| 740 | args.emplace_back("--add-meta-info"); |
Yabin Cui | db19d6d | 2021-05-12 17:36:02 -0700 | [diff] [blame] | 741 | args.emplace_back("app_type=" + app_type_); |
Yabin Cui | 8729bc6 | 2021-05-11 11:29:38 -0700 | [diff] [blame] | 742 | } |
Yabin Cui | 92825be | 2020-12-02 17:06:54 -0800 | [diff] [blame] | 743 | return args; |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 744 | } |
Yabin Cui | db19d6d | 2021-05-12 17:36:02 -0700 | [diff] [blame] | 745 | |
| 746 | std::string app_type_; |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 747 | }; |
| 748 | |
| 749 | } // namespace |
| 750 | |
Yabin Cui | 94c148d | 2020-02-18 14:32:33 -0800 | [diff] [blame] | 751 | static bool allow_run_as = true; |
| 752 | static bool allow_simpleperf_app_runner = true; |
| 753 | |
| 754 | void SetRunInAppToolForTesting(bool run_as, bool simpleperf_app_runner) { |
| 755 | allow_run_as = run_as; |
| 756 | allow_simpleperf_app_runner = simpleperf_app_runner; |
| 757 | } |
| 758 | |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 759 | static int GetCurrentUserId() { |
| 760 | std::unique_ptr<FILE, decltype(&pclose)> fd(popen("am get-current-user", "r"), pclose); |
| 761 | if (fd) { |
| 762 | char buf[128]; |
| 763 | if (fgets(buf, sizeof(buf), fd.get()) != nullptr) { |
| 764 | int user_id; |
| 765 | if (android::base::ParseInt(android::base::Trim(buf), &user_id, 0)) { |
| 766 | return user_id; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | return 0; |
| 771 | } |
| 772 | |
Yabin Cui | db19d6d | 2021-05-12 17:36:02 -0700 | [diff] [blame] | 773 | std::string GetAppType(const std::string& app_package_name) { |
| 774 | if (GetAndroidVersion() < kAndroidVersionS) { |
| 775 | return "unknown"; |
| 776 | } |
| 777 | std::string cmd = "simpleperf_app_runner " + app_package_name + " --show-app-type"; |
| 778 | std::unique_ptr<FILE, decltype(&pclose)> fp(popen(cmd.c_str(), "re"), pclose); |
| 779 | if (fp) { |
| 780 | char buf[128]; |
| 781 | if (fgets(buf, sizeof(buf), fp.get()) != nullptr) { |
| 782 | return android::base::Trim(buf); |
| 783 | } |
| 784 | } |
| 785 | // Can't get app_type. It means the app doesn't exist. |
| 786 | return "not_exist"; |
| 787 | } |
| 788 | |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 789 | bool RunInAppContext(const std::string& app_package_name, const std::string& cmd, |
| 790 | const std::vector<std::string>& args, size_t workload_args_size, |
| 791 | const std::string& output_filepath, bool need_tracepoint_events) { |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 792 | int user_id = GetCurrentUserId(); |
Yabin Cui | 94c148d | 2020-02-18 14:32:33 -0800 | [diff] [blame] | 793 | std::unique_ptr<InAppRunner> in_app_runner; |
Yabin Cui | db19d6d | 2021-05-12 17:36:02 -0700 | [diff] [blame] | 794 | |
| 795 | std::string app_type = GetAppType(app_package_name); |
| 796 | if (app_type == "unknown" && IsAppDebuggable(user_id, app_package_name)) { |
| 797 | app_type = "debuggable"; |
| 798 | } |
| 799 | |
| 800 | if (allow_run_as && app_type == "debuggable") { |
Yabin Cui | 902f908 | 2020-10-15 17:32:48 -0700 | [diff] [blame] | 801 | in_app_runner.reset(new RunAs(user_id, app_package_name)); |
Yabin Cui | 94c148d | 2020-02-18 14:32:33 -0800 | [diff] [blame] | 802 | if (!in_app_runner->Prepare()) { |
| 803 | in_app_runner = nullptr; |
| 804 | } |
| 805 | } |
| 806 | if (!in_app_runner && allow_simpleperf_app_runner) { |
Yabin Cui | db19d6d | 2021-05-12 17:36:02 -0700 | [diff] [blame] | 807 | if (app_type == "debuggable" || app_type == "profileable" || app_type == "unknown") { |
| 808 | in_app_runner.reset(new SimpleperfAppRunner(user_id, app_package_name, app_type)); |
| 809 | if (!in_app_runner->Prepare()) { |
| 810 | in_app_runner = nullptr; |
| 811 | } |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
Yabin Cui | 94c148d | 2020-02-18 14:32:33 -0800 | [diff] [blame] | 814 | if (!in_app_runner) { |
| 815 | LOG(ERROR) << "Package " << app_package_name |
| 816 | << " doesn't exist or isn't debuggable/profileable."; |
| 817 | return false; |
| 818 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 819 | return in_app_runner->RunCmdInApp(cmd, args, workload_args_size, output_filepath, |
| 820 | need_tracepoint_events); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 821 | } |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 822 | |
Yabin Cui | 7c5fe4e | 2017-12-05 17:44:05 -0800 | [diff] [blame] | 823 | void AllowMoreOpenedFiles() { |
| 824 | // On Android <= O, the hard limit is 4096, and the soft limit is 1024. |
| 825 | // On Android >= P, both the hard and soft limit are 32768. |
| 826 | rlimit limit; |
| 827 | if (getrlimit(RLIMIT_NOFILE, &limit) == 0) { |
| 828 | limit.rlim_cur = limit.rlim_max; |
| 829 | setrlimit(RLIMIT_NOFILE, &limit); |
| 830 | } |
| 831 | } |
Yabin Cui | 38b6a90 | 2017-12-06 14:15:48 -0800 | [diff] [blame] | 832 | |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 833 | std::string ScopedTempFiles::tmp_dir_; |
| 834 | std::vector<std::string> ScopedTempFiles::files_to_delete_; |
| 835 | |
Yabin Cui | 554f3bb | 2021-05-05 13:34:59 -0700 | [diff] [blame] | 836 | std::unique_ptr<ScopedTempFiles> ScopedTempFiles::Create(const std::string& tmp_dir) { |
| 837 | if (access(tmp_dir.c_str(), W_OK | X_OK) != 0) { |
| 838 | return nullptr; |
| 839 | } |
| 840 | return std::unique_ptr<ScopedTempFiles>(new ScopedTempFiles(tmp_dir)); |
| 841 | } |
| 842 | |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 843 | ScopedTempFiles::ScopedTempFiles(const std::string& tmp_dir) { |
| 844 | CHECK(tmp_dir_.empty()); // No other ScopedTempFiles. |
| 845 | tmp_dir_ = tmp_dir; |
Yabin Cui | 38b6a90 | 2017-12-06 14:15:48 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 848 | ScopedTempFiles::~ScopedTempFiles() { |
| 849 | tmp_dir_.clear(); |
| 850 | for (auto& file : files_to_delete_) { |
| 851 | unlink(file.c_str()); |
| 852 | } |
| 853 | files_to_delete_.clear(); |
| 854 | } |
| 855 | |
| 856 | std::unique_ptr<TemporaryFile> ScopedTempFiles::CreateTempFile(bool delete_in_destructor) { |
| 857 | CHECK(!tmp_dir_.empty()); |
| 858 | std::unique_ptr<TemporaryFile> tmp_file(new TemporaryFile(tmp_dir_)); |
Yabin Cui | a9cfcde | 2020-06-29 14:04:44 -0700 | [diff] [blame] | 859 | CHECK_NE(tmp_file->fd, -1) << "failed to create tmpfile under " << tmp_dir_; |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 860 | if (delete_in_destructor) { |
| 861 | tmp_file->DoNotRemove(); |
| 862 | files_to_delete_.push_back(tmp_file->path); |
| 863 | } |
| 864 | return tmp_file; |
Yabin Cui | 38b6a90 | 2017-12-06 14:15:48 -0800 | [diff] [blame] | 865 | } |
Josh Gao | 30535f2 | 2017-12-14 16:10:28 -0800 | [diff] [blame] | 866 | |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 867 | void ScopedTempFiles::RegisterTempFile(const std::string& path) { |
| 868 | files_to_delete_.emplace_back(path); |
| 869 | } |
| 870 | |
Josh Gao | 30535f2 | 2017-12-14 16:10:28 -0800 | [diff] [blame] | 871 | bool SignalIsIgnored(int signo) { |
| 872 | struct sigaction act; |
| 873 | if (sigaction(signo, nullptr, &act) != 0) { |
| 874 | PLOG(FATAL) << "failed to query signal handler for signal " << signo; |
| 875 | } |
| 876 | |
| 877 | if ((act.sa_flags & SA_SIGINFO)) { |
| 878 | return false; |
| 879 | } |
| 880 | |
| 881 | return act.sa_handler == SIG_IGN; |
| 882 | } |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 883 | |
| 884 | int GetAndroidVersion() { |
| 885 | #if defined(__ANDROID__) |
Yabin Cui | 6e173a4 | 2018-08-13 15:58:25 -0700 | [diff] [blame] | 886 | static int android_version = -1; |
| 887 | if (android_version == -1) { |
| 888 | android_version = 0; |
Yabin Cui | 58740ff | 2021-03-23 13:34:51 -0700 | [diff] [blame] | 889 | std::string s = android::base::GetProperty("ro.build.version.codename", "REL"); |
| 890 | if (s == "REL") { |
| 891 | s = android::base::GetProperty("ro.build.version.release", ""); |
| 892 | } |
Yabin Cui | 6e173a4 | 2018-08-13 15:58:25 -0700 | [diff] [blame] | 893 | // The release string can be a list of numbers (like 8.1.0), a character (like Q) |
| 894 | // or many characters (like OMR1). |
| 895 | if (!s.empty()) { |
| 896 | // Each Android version has a version number: L is 5, M is 6, N is 7, O is 8, etc. |
| 897 | if (s[0] >= 'A' && s[0] <= 'Z') { |
| 898 | android_version = s[0] - 'P' + kAndroidVersionP; |
| 899 | } else if (isdigit(s[0])) { |
| 900 | sscanf(s.c_str(), "%d", &android_version); |
| 901 | } |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 902 | } |
| 903 | } |
Yabin Cui | 6e173a4 | 2018-08-13 15:58:25 -0700 | [diff] [blame] | 904 | return android_version; |
| 905 | #else // defined(__ANDROID__) |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 906 | return 0; |
Yabin Cui | 6e173a4 | 2018-08-13 15:58:25 -0700 | [diff] [blame] | 907 | #endif |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 908 | } |
Yabin Cui | 8faa615 | 2018-06-07 15:52:57 -0700 | [diff] [blame] | 909 | |
| 910 | std::string GetHardwareFromCpuInfo(const std::string& cpu_info) { |
| 911 | for (auto& line : android::base::Split(cpu_info, "\n")) { |
| 912 | size_t pos = line.find(':'); |
| 913 | if (pos != std::string::npos) { |
| 914 | std::string key = android::base::Trim(line.substr(0, pos)); |
| 915 | if (key == "Hardware") { |
| 916 | return android::base::Trim(line.substr(pos + 1)); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | return ""; |
| 921 | } |
Yabin Cui | 2db05b4 | 2018-07-16 14:04:49 -0700 | [diff] [blame] | 922 | |
| 923 | bool MappedFileOnlyExistInMemory(const char* filename) { |
| 924 | // Mapped files only existing in memory: |
| 925 | // empty name |
| 926 | // [anon:???] |
| 927 | // [stack] |
| 928 | // /dev/* |
| 929 | // //anon: generated by kernel/events/core.c. |
Yabin Cui | 739b154 | 2018-10-17 17:19:56 -0700 | [diff] [blame] | 930 | // /memfd: created by memfd_create. |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 931 | return filename[0] == '\0' || (filename[0] == '[' && strcmp(filename, "[vdso]") != 0) || |
| 932 | strncmp(filename, "//", 2) == 0 || strncmp(filename, "/dev/", 5) == 0 || |
| 933 | strncmp(filename, "/memfd:", 7) == 0; |
Yabin Cui | 2db05b4 | 2018-07-16 14:04:49 -0700 | [diff] [blame] | 934 | } |
Yabin Cui | 38335f4 | 2018-07-17 16:06:21 -0700 | [diff] [blame] | 935 | |
| 936 | std::string GetCompleteProcessName(pid_t pid) { |
| 937 | std::string s; |
| 938 | if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid), &s)) { |
| 939 | s.clear(); |
| 940 | } |
| 941 | for (size_t i = 0; i < s.size(); ++i) { |
Yabin Cui | 3939b9d | 2018-07-20 17:12:13 -0700 | [diff] [blame] | 942 | // /proc/pid/cmdline uses 0 to separate arguments. |
| 943 | if (isspace(s[i]) || s[i] == 0) { |
Yabin Cui | 38335f4 | 2018-07-17 16:06:21 -0700 | [diff] [blame] | 944 | s.resize(i); |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | return s; |
| 949 | } |
Yabin Cui | a22efeb | 2020-01-29 13:27:33 -0800 | [diff] [blame] | 950 | |
| 951 | const char* GetTraceFsDir() { |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 952 | static const char* tracefs_dir = nullptr; |
| 953 | if (tracefs_dir == nullptr) { |
| 954 | for (const char* path : {"/sys/kernel/debug/tracing", "/sys/kernel/tracing"}) { |
| 955 | if (IsDir(path)) { |
| 956 | tracefs_dir = path; |
| 957 | break; |
| 958 | } |
Yabin Cui | a22efeb | 2020-01-29 13:27:33 -0800 | [diff] [blame] | 959 | } |
| 960 | } |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 961 | return tracefs_dir; |
Yabin Cui | a22efeb | 2020-01-29 13:27:33 -0800 | [diff] [blame] | 962 | } |
Yabin Cui | 84c55ff | 2020-07-09 14:14:22 -0700 | [diff] [blame] | 963 | |
Yabin Cui | d703bb3 | 2021-01-05 16:45:52 -0800 | [diff] [blame] | 964 | std::optional<std::pair<int, int>> GetKernelVersion() { |
Yabin Cui | 71aec16 | 2021-11-03 15:23:22 -0700 | [diff] [blame] | 965 | static std::optional<std::pair<int, int>> kernel_version; |
| 966 | if (!kernel_version.has_value()) { |
| 967 | utsname uname_buf; |
| 968 | int major; |
| 969 | int minor; |
| 970 | if (TEMP_FAILURE_RETRY(uname(&uname_buf)) != 0 || |
| 971 | sscanf(uname_buf.release, "%d.%d", &major, &minor) != 2) { |
| 972 | return std::nullopt; |
| 973 | } |
| 974 | kernel_version = std::make_pair(major, minor); |
Yabin Cui | 84c55ff | 2020-07-09 14:14:22 -0700 | [diff] [blame] | 975 | } |
Yabin Cui | 71aec16 | 2021-11-03 15:23:22 -0700 | [diff] [blame] | 976 | return kernel_version; |
Yabin Cui | 84c55ff | 2020-07-09 14:14:22 -0700 | [diff] [blame] | 977 | } |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 978 | |
Yabin Cui | 3485af9 | 2021-10-27 10:50:53 -0700 | [diff] [blame] | 979 | #if defined(__ANDROID__) |
| 980 | bool IsInAppUid() { |
| 981 | return getuid() % AID_USER_OFFSET >= AID_APP_START; |
| 982 | } |
| 983 | #endif |
| 984 | |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 985 | std::optional<uid_t> GetProcessUid(pid_t pid) { |
| 986 | std::string status_file = "/proc/" + std::to_string(pid) + "/status"; |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 987 | LineReader reader(status_file); |
| 988 | if (!reader.Ok()) { |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 989 | return std::nullopt; |
| 990 | } |
| 991 | |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 992 | std::string* line; |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 993 | while ((line = reader.ReadLine()) != nullptr) { |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 994 | if (android::base::StartsWith(*line, "Uid:")) { |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 995 | uid_t uid; |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 996 | if (sscanf(line->data() + strlen("Uid:"), "%u", &uid) == 1) { |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 997 | return uid; |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | return std::nullopt; |
| 1002 | } |
| 1003 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 1004 | } // namespace simpleperf |