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 "utils.h"
18 
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 
28 #include <algorithm>
29 #include <map>
30 #include <string>
31 
32 #include <android-base/file.h>
33 #include <android-base/logging.h>
34 #include <android-base/parseint.h>
35 #include <android-base/stringprintf.h>
36 #include <android-base/strings.h>
37 #include <build/version.h>
38 
39 #include <7zCrc.h>
40 #include <Xz.h>
41 #include <XzCrc64.h>
42 
43 namespace simpleperf {
44 
45 using android::base::ParseInt;
46 using android::base::Split;
47 using android::base::StringPrintf;
48 
Clear()49 void OneTimeFreeAllocator::Clear() {
50   for (auto& p : v_) {
51     delete[] p;
52   }
53   v_.clear();
54   cur_ = nullptr;
55   end_ = nullptr;
56 }
57 
AllocateString(std::string_view s)58 const char* OneTimeFreeAllocator::AllocateString(std::string_view s) {
59   size_t size = s.size() + 1;
60   if (cur_ + size > end_) {
61     size_t alloc_size = std::max(size, unit_size_);
62     char* p = new char[alloc_size];
63     v_.push_back(p);
64     cur_ = p;
65     end_ = p + alloc_size;
66   }
67   memcpy(cur_, s.data(), s.size());
68   cur_[s.size()] = '\0';
69   const char* result = cur_;
70   cur_ += size;
71   return result;
72 }
73 
OpenReadOnly(const std::string & filename)74 android::base::unique_fd FileHelper::OpenReadOnly(const std::string& filename) {
75   int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY));
76   return android::base::unique_fd(fd);
77 }
78 
OpenWriteOnly(const std::string & filename)79 android::base::unique_fd FileHelper::OpenWriteOnly(const std::string& filename) {
80   int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_BINARY | O_CREAT, 0644));
81   return android::base::unique_fd(fd);
82 }
83 
CreateInstance(const std::string & filename)84 std::unique_ptr<ArchiveHelper> ArchiveHelper::CreateInstance(const std::string& filename) {
85   android::base::unique_fd fd = FileHelper::OpenReadOnly(filename);
86   if (fd == -1) {
87     return nullptr;
88   }
89   // Simpleperf relies on ArchiveHelper to check if a file is zip file. We expect much more elf
90   // files than zip files in a process map. In order to detect invalid zip files fast, we add a
91   // check of magic number here. Note that OpenArchiveFd() detects invalid zip files in a thorough
92   // way, but it usually needs reading at least 64K file data.
93   static const char zip_preamble[] = {0x50, 0x4b, 0x03, 0x04};
94   char buf[4];
95   if (!android::base::ReadFully(fd, buf, 4) || memcmp(buf, zip_preamble, 4) != 0) {
96     return nullptr;
97   }
98   if (lseek(fd, 0, SEEK_SET) == -1) {
99     return nullptr;
100   }
101   ZipArchiveHandle handle;
102   int result = OpenArchiveFd(fd.release(), filename.c_str(), &handle);
103   if (result != 0) {
104     LOG(ERROR) << "Failed to open archive " << filename << ": " << ErrorCodeString(result);
105     return nullptr;
106   }
107   return std::unique_ptr<ArchiveHelper>(new ArchiveHelper(handle, filename));
108 }
109 
~ArchiveHelper()110 ArchiveHelper::~ArchiveHelper() {
111   CloseArchive(handle_);
112 }
113 
IterateEntries(const std::function<bool (ZipEntry &,const std::string &)> & callback)114 bool ArchiveHelper::IterateEntries(
115     const std::function<bool(ZipEntry&, const std::string&)>& callback) {
116   void* iteration_cookie;
117   if (StartIteration(handle_, &iteration_cookie) < 0) {
118     LOG(ERROR) << "Failed to iterate " << filename_;
119     return false;
120   }
121   ZipEntry zentry;
122   std::string zname;
123   int result;
124   while ((result = Next(iteration_cookie, &zentry, &zname)) == 0) {
125     if (!callback(zentry, zname)) {
126       break;
127     }
128   }
129   EndIteration(iteration_cookie);
130   if (result == -2) {
131     LOG(ERROR) << "Failed to iterate " << filename_;
132     return false;
133   }
134   return true;
135 }
136 
FindEntry(const std::string & name,ZipEntry * entry)137 bool ArchiveHelper::FindEntry(const std::string& name, ZipEntry* entry) {
138   int result = ::FindEntry(handle_, name, entry);
139   if (result != 0) {
140     LOG(ERROR) << "Failed to find " << name << " in " << filename_;
141     return false;
142   }
143   return true;
144 }
145 
GetEntryData(ZipEntry & entry,std::vector<uint8_t> * data)146 bool ArchiveHelper::GetEntryData(ZipEntry& entry, std::vector<uint8_t>* data) {
147   data->resize(entry.uncompressed_length);
148   if (ExtractToMemory(handle_, &entry, data->data(), data->size()) != 0) {
149     LOG(ERROR) << "Failed to extract entry at " << entry.offset << " in " << filename_;
150     return false;
151   }
152   return true;
153 }
154 
GetFd()155 int ArchiveHelper::GetFd() {
156   return GetFileDescriptor(handle_);
157 }
158 
PrintIndented(size_t indent,const char * fmt,...)159 void PrintIndented(size_t indent, const char* fmt, ...) {
160   va_list ap;
161   va_start(ap, fmt);
162   printf("%*s", static_cast<int>(indent * 2), "");
163   vprintf(fmt, ap);
164   va_end(ap);
165 }
166 
FprintIndented(FILE * fp,size_t indent,const char * fmt,...)167 void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...) {
168   va_list ap;
169   va_start(ap, fmt);
170   fprintf(fp, "%*s", static_cast<int>(indent * 2), "");
171   vfprintf(fp, fmt, ap);
172   va_end(ap);
173 }
174 
IsPowerOfTwo(uint64_t value)175 bool IsPowerOfTwo(uint64_t value) {
176   return (value != 0 && ((value & (value - 1)) == 0));
177 }
178 
GetEntriesInDir(const std::string & dirpath)179 std::vector<std::string> GetEntriesInDir(const std::string& dirpath) {
180   std::vector<std::string> result;
181   DIR* dir = opendir(dirpath.c_str());
182   if (dir == nullptr) {
183     PLOG(DEBUG) << "can't open dir " << dirpath;
184     return result;
185   }
186   dirent* entry;
187   while ((entry = readdir(dir)) != nullptr) {
188     if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
189       continue;
190     }
191     result.push_back(entry->d_name);
192   }
193   closedir(dir);
194   return result;
195 }
196 
GetSubDirs(const std::string & dirpath)197 std::vector<std::string> GetSubDirs(const std::string& dirpath) {
198   std::vector<std::string> entries = GetEntriesInDir(dirpath);
199   std::vector<std::string> result;
200   for (size_t i = 0; i < entries.size(); ++i) {
201     if (IsDir(dirpath + OS_PATH_SEPARATOR + entries[i])) {
202       result.push_back(std::move(entries[i]));
203     }
204   }
205   return result;
206 }
207 
IsDir(const std::string & dirpath)208 bool IsDir(const std::string& dirpath) {
209   struct stat st;
210   if (stat(dirpath.c_str(), &st) == 0) {
211     if (S_ISDIR(st.st_mode)) {
212       return true;
213     }
214   }
215   return false;
216 }
217 
IsRegularFile(const std::string & filename)218 bool IsRegularFile(const std::string& filename) {
219   struct stat st;
220   if (stat(filename.c_str(), &st) == 0) {
221     if (S_ISREG(st.st_mode)) {
222       return true;
223     }
224   }
225   return false;
226 }
227 
GetFileSize(const std::string & filename)228 uint64_t GetFileSize(const std::string& filename) {
229   struct stat st;
230   if (stat(filename.c_str(), &st) == 0) {
231     return static_cast<uint64_t>(st.st_size);
232   }
233   return 0;
234 }
235 
MkdirWithParents(const std::string & path)236 bool MkdirWithParents(const std::string& path) {
237   size_t prev_end = 0;
238   while (prev_end < path.size()) {
239     size_t next_end = path.find('/', prev_end + 1);
240     if (next_end == std::string::npos) {
241       break;
242     }
243     std::string dir_path = path.substr(0, next_end);
244     if (!IsDir(dir_path)) {
245 #if defined(_WIN32)
246       int ret = mkdir(dir_path.c_str());
247 #else
248       int ret = mkdir(dir_path.c_str(), 0755);
249 #endif
250       if (ret != 0) {
251         PLOG(ERROR) << "failed to create dir " << dir_path;
252         return false;
253       }
254     }
255     prev_end = next_end;
256   }
257   return true;
258 }
259 
xz_alloc(ISzAllocPtr,size_t size)260 static void* xz_alloc(ISzAllocPtr, size_t size) {
261   return malloc(size);
262 }
263 
xz_free(ISzAllocPtr,void * address)264 static void xz_free(ISzAllocPtr, void* address) {
265   free(address);
266 }
267 
XzDecompress(const std::string & compressed_data,std::string * decompressed_data)268 bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data) {
269   ISzAlloc alloc;
270   CXzUnpacker state;
271   alloc.Alloc = xz_alloc;
272   alloc.Free = xz_free;
273   XzUnpacker_Construct(&state, &alloc);
274   CrcGenerateTable();
275   Crc64GenerateTable();
276   size_t src_offset = 0;
277   size_t dst_offset = 0;
278   std::string dst(compressed_data.size(), ' ');
279 
280   ECoderStatus status = CODER_STATUS_NOT_FINISHED;
281   while (status == CODER_STATUS_NOT_FINISHED) {
282     dst.resize(dst.size() * 2);
283     size_t src_remaining = compressed_data.size() - src_offset;
284     size_t dst_remaining = dst.size() - dst_offset;
285     int res = XzUnpacker_Code(&state, reinterpret_cast<Byte*>(&dst[dst_offset]), &dst_remaining,
286                               reinterpret_cast<const Byte*>(&compressed_data[src_offset]),
287                               &src_remaining, true, CODER_FINISH_ANY, &status);
288     if (res != SZ_OK) {
289       LOG(ERROR) << "LZMA decompression failed with error " << res;
290       XzUnpacker_Free(&state);
291       return false;
292     }
293     src_offset += src_remaining;
294     dst_offset += dst_remaining;
295   }
296   XzUnpacker_Free(&state);
297   if (!XzUnpacker_IsStreamWasFinished(&state)) {
298     LOG(ERROR) << "LZMA decompresstion failed due to incomplete stream";
299     return false;
300   }
301   dst.resize(dst_offset);
302   *decompressed_data = std::move(dst);
303   return true;
304 }
305 
306 static std::map<std::string, android::base::LogSeverity> log_severity_map = {
307     {"verbose", android::base::VERBOSE}, {"debug", android::base::DEBUG},
308     {"info", android::base::INFO},       {"warning", android::base::WARNING},
309     {"error", android::base::ERROR},     {"fatal", android::base::FATAL},
310 };
GetLogSeverity(const std::string & name,android::base::LogSeverity * severity)311 bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity) {
312   auto it = log_severity_map.find(name);
313   if (it != log_severity_map.end()) {
314     *severity = it->second;
315     return true;
316   }
317   return false;
318 }
319 
GetLogSeverityName()320 std::string GetLogSeverityName() {
321   android::base::LogSeverity severity = android::base::GetMinimumLogSeverity();
322   for (auto& pair : log_severity_map) {
323     if (severity == pair.second) {
324       return pair.first;
325     }
326   }
327   return "info";
328 }
329 
IsRoot()330 bool IsRoot() {
331   static int is_root = -1;
332   if (is_root == -1) {
333 #if defined(__linux__)
334     is_root = (getuid() == 0) ? 1 : 0;
335 #else
336     is_root = 0;
337 #endif
338   }
339   return is_root == 1;
340 }
341 
GetPageSize()342 size_t GetPageSize() {
343 #if defined(__linux__)
344   return sysconf(_SC_PAGE_SIZE);
345 #else
346   return 4096;
347 #endif
348 }
349 
ConvertBytesToValue(const char * bytes,uint32_t size)350 uint64_t ConvertBytesToValue(const char* bytes, uint32_t size) {
351   if (size > 8) {
352     LOG(FATAL) << "unexpected size " << size << " in ConvertBytesToValue";
353   }
354   uint64_t result = 0;
355   int shift = 0;
356   for (uint32_t i = 0; i < size; ++i) {
357     uint64_t tmp = static_cast<unsigned char>(bytes[i]);
358     result |= tmp << shift;
359     shift += 8;
360   }
361   return result;
362 }
363 
SecondToTimeval(double time_in_sec)364 timeval SecondToTimeval(double time_in_sec) {
365   timeval tv;
366   tv.tv_sec = static_cast<time_t>(time_in_sec);
367   tv.tv_usec = static_cast<int>((time_in_sec - tv.tv_sec) * 1000000);
368   return tv;
369 }
370 
371 constexpr int SIMPLEPERF_VERSION = 1;
372 
GetSimpleperfVersion()373 std::string GetSimpleperfVersion() {
374   return StringPrintf("%d.build.%s", SIMPLEPERF_VERSION, android::build::GetBuildNumber().c_str());
375 }
376 
377 // Parse a line like: 0,1-3, 5, 7-8
GetCpusFromString(const std::string & s)378 std::optional<std::set<int>> GetCpusFromString(const std::string& s) {
379   std::string str;
380   for (char c : s) {
381     if (!isspace(c)) {
382       str += c;
383     }
384   }
385   std::set<int> cpus;
386   int cpu1;
387   int cpu2;
388   for (const std::string& p : Split(str, ",")) {
389     size_t split_pos = p.find('-');
390     if (split_pos == std::string::npos) {
391       if (!ParseInt(p, &cpu1, 0)) {
392         LOG(ERROR) << "failed to parse cpu: " << p;
393         return std::nullopt;
394       }
395       cpus.insert(cpu1);
396     } else {
397       if (!ParseInt(p.substr(0, split_pos), &cpu1, 0) ||
398           !ParseInt(p.substr(split_pos + 1), &cpu2, 0) || cpu1 > cpu2) {
399         LOG(ERROR) << "failed to parse cpu: " << p;
400         return std::nullopt;
401       }
402       while (cpu1 <= cpu2) {
403         cpus.insert(cpu1++);
404       }
405     }
406   }
407   return cpus;
408 }
409 
GetTidsFromString(const std::string & s,bool check_if_exists)410 std::optional<std::set<pid_t>> GetTidsFromString(const std::string& s, bool check_if_exists) {
411   std::set<pid_t> tids;
412   for (const auto& p : Split(s, ",")) {
413     int tid;
414     if (!ParseInt(p.c_str(), &tid, 0)) {
415       LOG(ERROR) << "Invalid tid '" << p << "'";
416       return std::nullopt;
417     }
418     if (check_if_exists && !IsDir(StringPrintf("/proc/%d", tid))) {
419       LOG(ERROR) << "Non existing thread '" << tid << "'";
420       return std::nullopt;
421     }
422     tids.insert(tid);
423   }
424   return tids;
425 }
426 
427 }  // namespace simpleperf
428