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