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