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