2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "environment.h"
22 #include <sys/utsname.h>
26 #include <unordered_map>
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <android-base/parseint.h>
32 #include <android-base/strings.h>
33 #include <android-base/stringprintf.h>
34 #include <procinfo/process.h>
36 #if defined(__ANDROID__)
37 #include <sys/system_properties.h>
41 #include "thread_tree.h"
46 explicit LineReader(FILE* fp) : fp_(fp), buf_(nullptr), bufsize_(0) {
55 if (getline(&buf_, &bufsize_, fp_) != -1) {
61 size_t MaxLineSize() {
71 std::vector<int> GetOnlineCpus() {
72 std::vector<int> result;
73 FILE* fp = fopen("/sys/devices/system/cpu/online", "re");
75 PLOG(ERROR) << "can't open online cpu information";
79 LineReader reader(fp);
81 if ((line = reader.ReadLine()) != nullptr) {
82 result = GetCpusFromString(line);
84 CHECK(!result.empty()) << "can't get online cpu information";
88 std::vector<int> GetCpusFromString(const std::string& s) {
89 std::set<int> cpu_set;
90 bool have_dash = false;
91 const char* p = s.c_str();
95 // Parse line like: 0,1-3, 5, 7-8
96 while ((cpu = static_cast<int>(strtol(p, &endp, 10))) != 0 || endp != p) {
97 if (have_dash && !cpu_set.empty()) {
98 for (int t = last_cpu + 1; t < cpu; ++t) {
106 while (!isdigit(*p) && *p != '\0') {
113 return std::vector<int>(cpu_set.begin(), cpu_set.end());
116 static std::vector<KernelMmap> GetLoadedModules() {
117 std::vector<KernelMmap> result;
118 FILE* fp = fopen("/proc/modules", "re");
120 // There is no /proc/modules on Android devices, so we don't print error if failed to open it.
121 PLOG(DEBUG) << "failed to open file /proc/modules";
124 LineReader reader(fp);
126 while ((line = reader.ReadLine()) != nullptr) {
127 // Parse line like: nf_defrag_ipv6 34768 1 nf_conntrack_ipv6, Live 0xffffffffa0fe5000
128 char name[reader.MaxLineSize()];
130 if (sscanf(line, "%s%*lu%*u%*s%*s 0x%" PRIx64, name, &addr) == 2) {
133 map.start_addr = addr;
134 result.push_back(map);
137 bool all_zero = true;
138 for (const auto& map : result) {
139 if (map.start_addr != 0) {
144 LOG(DEBUG) << "addresses in /proc/modules are all zero, so ignore kernel modules";
145 return std::vector<KernelMmap>();
150 static void GetAllModuleFiles(const std::string& path,
151 std::unordered_map<std::string, std::string>* module_file_map) {
152 for (const auto& name : GetEntriesInDir(path)) {
153 std::string entry_path = path + "/" + name;
154 if (IsRegularFile(entry_path) && android::base::EndsWith(name, ".ko")) {
155 std::string module_name = name.substr(0, name.size() - 3);
156 std::replace(module_name.begin(), module_name.end(), '-', '_');
157 module_file_map->insert(std::make_pair(module_name, entry_path));
158 } else if (IsDir(entry_path)) {
159 GetAllModuleFiles(entry_path, module_file_map);
164 static std::vector<KernelMmap> GetModulesInUse() {
166 if (TEMP_FAILURE_RETRY(uname(&uname_buf)) != 0) {
167 PLOG(ERROR) << "uname() failed";
168 return std::vector<KernelMmap>();
170 std::string linux_version = uname_buf.release;
171 std::string module_dirpath = "/lib/modules/" + linux_version + "/kernel";
172 std::unordered_map<std::string, std::string> module_file_map;
173 GetAllModuleFiles(module_dirpath, &module_file_map);
174 // TODO: There is no /proc/modules or /lib/modules on Android, find methods work on it.
175 std::vector<KernelMmap> module_mmaps = GetLoadedModules();
176 for (auto& module : module_mmaps) {
177 auto it = module_file_map.find(module.name);
178 if (it != module_file_map.end()) {
179 module.filepath = it->second;
185 void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps) {
186 kernel_mmap->name = DEFAULT_KERNEL_MMAP_NAME;
187 kernel_mmap->start_addr = 0;
188 kernel_mmap->filepath = kernel_mmap->name;
189 *module_mmaps = GetModulesInUse();
190 for (auto& map : *module_mmaps) {
191 if (map.filepath.empty()) {
192 map.filepath = "[" + map.name + "]";
196 if (module_mmaps->size() == 0) {
197 kernel_mmap->len = std::numeric_limits<uint64_t>::max() - kernel_mmap->start_addr;
200 module_mmaps->begin(), module_mmaps->end(),
201 [](const KernelMmap& m1, const KernelMmap& m2) { return m1.start_addr < m2.start_addr; });
202 // When not having enough privilege, all addresses are read as 0.
203 if (kernel_mmap->start_addr == (*module_mmaps)[0].start_addr) {
204 kernel_mmap->len = 0;
206 kernel_mmap->len = (*module_mmaps)[0].start_addr - kernel_mmap->start_addr - 1;
208 for (size_t i = 0; i + 1 < module_mmaps->size(); ++i) {
209 if ((*module_mmaps)[i].start_addr == (*module_mmaps)[i + 1].start_addr) {
210 (*module_mmaps)[i].len = 0;
212 (*module_mmaps)[i].len =
213 (*module_mmaps)[i + 1].start_addr - (*module_mmaps)[i].start_addr - 1;
216 module_mmaps->back().len =
217 std::numeric_limits<uint64_t>::max() - module_mmaps->back().start_addr;
221 static bool ReadThreadNameAndPid(pid_t tid, std::string* comm, pid_t* pid) {
222 android::procinfo::ProcessInfo procinfo;
223 if (!android::procinfo::GetProcessInfo(tid, &procinfo)) {
226 if (comm != nullptr) {
227 *comm = procinfo.name;
229 if (pid != nullptr) {
235 std::vector<pid_t> GetThreadsInProcess(pid_t pid) {
236 std::vector<pid_t> result;
237 android::procinfo::GetProcessTids(pid, &result);
241 bool IsThreadAlive(pid_t tid) {
242 return IsDir(android::base::StringPrintf("/proc/%d", tid));
245 bool GetProcessForThread(pid_t tid, pid_t* pid) {
246 return ReadThreadNameAndPid(tid, nullptr, pid);
249 bool GetThreadName(pid_t tid, std::string* name) {
250 return ReadThreadNameAndPid(tid, name, nullptr);
253 std::vector<pid_t> GetAllProcesses() {
254 std::vector<pid_t> result;
255 std::vector<std::string> entries = GetEntriesInDir("/proc");
256 for (const auto& entry : entries) {
258 if (!android::base::ParseInt(entry.c_str(), &pid, 0)) {
261 result.push_back(pid);
266 bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps) {
267 std::string map_file = android::base::StringPrintf("/proc/%d/maps", pid);
268 FILE* fp = fopen(map_file.c_str(), "re");
270 PLOG(DEBUG) << "can't open file " << map_file;
273 thread_mmaps->clear();
274 LineReader reader(fp);
276 while ((line = reader.ReadLine()) != nullptr) {
277 // Parse line like: 00400000-00409000 r-xp 00000000 fc:00 426998 /usr/lib/gvfs/gvfsd-http
278 uint64_t start_addr, end_addr, pgoff;
279 char type[reader.MaxLineSize()];
280 char execname[reader.MaxLineSize()];
281 strcpy(execname, "");
282 if (sscanf(line, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %*x:%*x %*u %s\n", &start_addr,
283 &end_addr, type, &pgoff, execname) < 4) {
286 if (strcmp(execname, "") == 0) {
287 strcpy(execname, DEFAULT_EXECNAME_FOR_THREAD_MMAP);
290 thread.start_addr = start_addr;
291 thread.len = end_addr - start_addr;
292 thread.pgoff = pgoff;
293 thread.name = execname;
294 thread.executable = (type[2] == 'x');
295 thread_mmaps->push_back(thread);
300 bool GetKernelBuildId(BuildId* build_id) {
301 ElfStatus result = GetBuildIdFromNoteFile("/sys/kernel/notes", build_id);
302 if (result != ElfStatus::NO_ERROR) {
303 LOG(DEBUG) << "failed to read /sys/kernel/notes: " << result;
305 return result == ElfStatus::NO_ERROR;
308 bool GetModuleBuildId(const std::string& module_name, BuildId* build_id) {
309 std::string notefile = "/sys/module/" + module_name + "/notes/.note.gnu.build-id";
310 return GetBuildIdFromNoteFile(notefile, build_id);
313 bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set) {
314 std::vector<std::string> strs = android::base::Split(tid_str, ",");
315 for (const auto& s : strs) {
317 if (!android::base::ParseInt(s.c_str(), &tid, 0)) {
318 LOG(ERROR) << "Invalid tid '" << s << "'";
321 if (!IsDir(android::base::StringPrintf("/proc/%d", tid))) {
322 LOG(ERROR) << "Non existing thread '" << tid << "'";
325 tid_set->insert(tid);
331 * perf event paranoia level:
332 * -1 - not paranoid at all
333 * 0 - disallow raw tracepoint access for unpriv
334 * 1 - disallow cpu events for unpriv
335 * 2 - disallow kernel profiling for unpriv
336 * 3 - disallow user profiling for unpriv
338 static bool ReadPerfEventParanoid(int* value) {
340 if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_paranoid", &s)) {
341 PLOG(DEBUG) << "failed to read /proc/sys/kernel/perf_event_paranoid";
344 s = android::base::Trim(s);
345 if (!android::base::ParseInt(s.c_str(), value)) {
346 PLOG(ERROR) << "failed to parse /proc/sys/kernel/perf_event_paranoid: " << s;
352 static const char* GetLimitLevelDescription(int limit_level) {
353 switch (limit_level) {
354 case -1: return "unlimited";
355 case 0: return "disallowing raw tracepoint access for unpriv";
356 case 1: return "disallowing cpu events for unpriv";
357 case 2: return "disallowing kernel profiling for unpriv";
358 case 3: return "disallowing user profiling for unpriv";
359 default: return "unknown level";
363 bool CheckPerfEventLimit() {
364 // root is not limited by /proc/sys/kernel/perf_event_paranoid.
369 bool can_read_paranoid = ReadPerfEventParanoid(&limit_level);
370 if (can_read_paranoid && limit_level <= 1) {
373 #if defined(__ANDROID__)
374 const char* prop_name = "security.perf_harden";
375 char prop_value[PROP_VALUE_MAX];
376 if (__system_property_get(prop_name, prop_value) <= 0) {
377 // can't do anything if there is no such property.
380 if (strcmp(prop_value, "0") == 0) {
383 // Try to enable perf_event_paranoid by setprop security.perf_harden=0.
384 if (__system_property_set(prop_name, "0") == 0) {
386 if (can_read_paranoid && ReadPerfEventParanoid(&limit_level) && limit_level <= 1) {
389 if (__system_property_get(prop_name, prop_value) > 0 && strcmp(prop_value, "0") == 0) {
393 if (can_read_paranoid) {
394 LOG(WARNING) << "/proc/sys/kernel/perf_event_paranoid is " << limit_level
395 << ", " << GetLimitLevelDescription(limit_level) << ".";
397 LOG(WARNING) << "Try using `adb shell setprop security.perf_harden 0` to allow profiling.";
400 if (can_read_paranoid) {
401 LOG(WARNING) << "/proc/sys/kernel/perf_event_paranoid is " << limit_level
402 << ", " << GetLimitLevelDescription(limit_level) << ".";
409 bool GetMaxSampleFrequency(uint64_t* max_sample_freq) {
411 if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_max_sample_rate", &s)) {
412 PLOG(DEBUG) << "failed to read /proc/sys/kernel/perf_event_max_sample_rate";
415 s = android::base::Trim(s);
416 if (!android::base::ParseUint(s.c_str(), max_sample_freq)) {
417 LOG(ERROR) << "failed to parse /proc/sys/kernel/perf_event_max_sample_rate: " << s;
423 bool CheckSampleFrequency(uint64_t sample_freq) {
424 if (sample_freq == 0) {
425 LOG(ERROR) << "Sample frequency can't be zero.";
428 uint64_t max_sample_freq;
429 if (!GetMaxSampleFrequency(&max_sample_freq)) {
430 // Omit the check if can't read perf_event_max_sample_rate.
433 if (sample_freq > max_sample_freq) {
434 LOG(ERROR) << "Sample frequency " << sample_freq << " is out of range [1, "
435 << max_sample_freq << "]";
441 bool CheckKernelSymbolAddresses() {
442 const std::string kptr_restrict_file = "/proc/sys/kernel/kptr_restrict";
444 if (!android::base::ReadFileToString(kptr_restrict_file, &s)) {
445 PLOG(DEBUG) << "failed to read " << kptr_restrict_file;
448 s = android::base::Trim(s);
450 if (!android::base::ParseInt(s.c_str(), &value)) {
451 LOG(ERROR) << "failed to parse " << kptr_restrict_file << ": " << s;
457 if (value == 1 && IsRoot()) {
460 LOG(WARNING) << "Access to kernel symbol addresses is restricted. If "
461 << "possible, please do `echo 0 >/proc/sys/kernel/kptr_restrict` "
466 ArchType GetMachineArch() {
468 if (TEMP_FAILURE_RETRY(uname(&uname_buf)) != 0) {
469 PLOG(WARNING) << "uname() failed";
470 return GetBuildArch();
472 ArchType arch = GetArchType(uname_buf.machine);
473 if (arch != ARCH_UNSUPPORTED) {
476 return GetBuildArch();
479 void PrepareVdsoFile() {
480 // vdso is an elf file in memory loaded in each process's user space by the kernel. To read
481 // symbols from it and unwind through it, we need to dump it into a file in storage.
482 // It doesn't affect much when failed to prepare vdso file, so there is no need to return values.
483 std::vector<ThreadMmap> thread_mmaps;
484 if (!GetThreadMmapsInProcess(getpid(), &thread_mmaps)) {
487 const ThreadMmap* vdso_map = nullptr;
488 for (const auto& map : thread_mmaps) {
489 if (map.name == "[vdso]") {
494 if (vdso_map == nullptr) {
497 std::string s(vdso_map->len, '\0');
498 memcpy(&s[0], reinterpret_cast<void*>(static_cast<uintptr_t>(vdso_map->start_addr)),
500 std::unique_ptr<TemporaryFile> tmpfile(new TemporaryFile);
501 if (!android::base::WriteStringToFile(s, tmpfile->path)) {
504 Dso::SetVdsoFile(std::move(tmpfile), sizeof(size_t) == sizeof(uint64_t));