• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 "src/profiling/common/unwind_support.h"
18 
19 #include <inttypes.h>
20 
21 #include <procinfo/process_map.h>
22 #include <unwindstack/Maps.h>
23 #include <unwindstack/Memory.h>
24 
25 #include "perfetto/ext/base/file_utils.h"
26 
27 namespace perfetto {
28 namespace profiling {
29 
StackOverlayMemory(std::shared_ptr<unwindstack::Memory> mem,uint64_t sp,const uint8_t * stack,size_t size)30 StackOverlayMemory::StackOverlayMemory(std::shared_ptr<unwindstack::Memory> mem,
31                                        uint64_t sp,
32                                        const uint8_t* stack,
33                                        size_t size)
34     : mem_(std::move(mem)), sp_(sp), stack_end_(sp + size), stack_(stack) {}
35 
Read(uint64_t addr,void * dst,size_t size)36 size_t StackOverlayMemory::Read(uint64_t addr, void* dst, size_t size) {
37   if (addr >= sp_ && addr + size <= stack_end_ && addr + size > sp_) {
38     size_t offset = static_cast<size_t>(addr - sp_);
39     memcpy(dst, stack_ + offset, size);
40     return size;
41   }
42 
43   return mem_->Read(addr, dst, size);
44 }
45 
FDMemory(base::ScopedFile mem_fd)46 FDMemory::FDMemory(base::ScopedFile mem_fd) : mem_fd_(std::move(mem_fd)) {}
47 
Read(uint64_t addr,void * dst,size_t size)48 size_t FDMemory::Read(uint64_t addr, void* dst, size_t size) {
49   ssize_t rd = pread64(*mem_fd_, dst, size, static_cast<off64_t>(addr));
50   if (PERFETTO_UNLIKELY(rd == -1)) {
51     PERFETTO_PLOG("Failed remote pread of %zu bytes at address %" PRIx64, size,
52                   addr);
53     return 0;
54   }
55   return static_cast<size_t>(rd);
56 }
57 
FDMaps(base::ScopedFile fd)58 FDMaps::FDMaps(base::ScopedFile fd) : fd_(std::move(fd)) {}
59 
Parse()60 bool FDMaps::Parse() {
61   // If the process has already exited, lseek or ReadFileDescriptor will
62   // return false.
63   if (lseek(*fd_, 0, SEEK_SET) == -1)
64     return false;
65 
66   std::string content;
67   if (!base::ReadFileDescriptor(*fd_, &content))
68     return false;
69 
70   unwindstack::SharedString name("");
71   unwindstack::MapInfo* prev_map = nullptr;
72   unwindstack::MapInfo* prev_real_map = nullptr;
73   return android::procinfo::ReadMapFileContent(
74       &content[0], [&](const android::procinfo::MapInfo& mapinfo) {
75         // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
76         auto flags = mapinfo.flags;
77         if (strncmp(mapinfo.name.c_str(), "/dev/", 5) == 0 &&
78             strncmp(mapinfo.name.c_str() + 5, "ashmem/", 7) != 0) {
79           flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
80         }
81         // Share the string if it matches for consecutive maps.
82         if (name != mapinfo.name) {
83           name = unwindstack::SharedString(mapinfo.name);
84         }
85         maps_.emplace_back(new unwindstack::MapInfo(
86             prev_map, prev_real_map, mapinfo.start, mapinfo.end, mapinfo.pgoff,
87             flags, name));
88         prev_map = maps_.back().get();
89         if (!prev_map->IsBlank()) {
90           prev_real_map = prev_map;
91         }
92       });
93 }
94 
Reset()95 void FDMaps::Reset() {
96   maps_.clear();
97 }
98 
UnwindingMetadata(base::ScopedFile maps_fd,base::ScopedFile mem_fd)99 UnwindingMetadata::UnwindingMetadata(base::ScopedFile maps_fd,
100                                      base::ScopedFile mem_fd)
101     : fd_maps(std::move(maps_fd)),
102       fd_mem(std::make_shared<FDMemory>(std::move(mem_fd))) {
103   if (!fd_maps.Parse())
104     PERFETTO_DLOG("Failed initial maps parse");
105 }
106 
ReparseMaps()107 void UnwindingMetadata::ReparseMaps() {
108   reparses++;
109   fd_maps.Reset();
110   fd_maps.Parse();
111 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
112   jit_debug.reset();
113   dex_files.reset();
114 #endif
115 }
116 
117 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
GetJitDebug(unwindstack::ArchEnum arch)118 unwindstack::JitDebug* UnwindingMetadata::GetJitDebug(unwindstack::ArchEnum arch) {
119   if (jit_debug.get() == nullptr) {
120     std::vector<std::string> search_libs{"libart.so", "libartd.so"};
121     jit_debug = unwindstack::CreateJitDebug(arch, fd_mem, search_libs);
122   }
123   return jit_debug.get();
124 }
125 
GetDexFiles(unwindstack::ArchEnum arch)126 unwindstack::DexFiles* UnwindingMetadata::GetDexFiles(unwindstack::ArchEnum arch) {
127   if (dex_files.get() == nullptr) {
128     std::vector<std::string> search_libs{"libart.so", "libartd.so"};
129     dex_files = unwindstack::CreateDexFiles(arch, fd_mem, search_libs);
130   }
131   return dex_files.get();
132 }
133 #endif
134 
GetBuildId(const unwindstack::FrameData & frame)135 const std::string& UnwindingMetadata::GetBuildId(
136     const unwindstack::FrameData& frame) {
137   if (!frame.map_name.empty()) {
138     unwindstack::MapInfo* map_info = fd_maps.Find(frame.pc);
139     if (map_info)
140       return map_info->GetBuildID();
141   }
142 
143   return empty_string_;
144 }
145 
StringifyLibUnwindstackError(unwindstack::ErrorCode e)146 std::string StringifyLibUnwindstackError(unwindstack::ErrorCode e) {
147   switch (e) {
148     case unwindstack::ERROR_NONE:
149       return "NONE";
150     case unwindstack::ERROR_MEMORY_INVALID:
151       return "MEMORY_INVALID";
152     case unwindstack::ERROR_UNWIND_INFO:
153       return "UNWIND_INFO";
154     case unwindstack::ERROR_UNSUPPORTED:
155       return "UNSUPPORTED";
156     case unwindstack::ERROR_INVALID_MAP:
157       return "INVALID_MAP";
158     case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
159       return "MAX_FRAME_EXCEEDED";
160     case unwindstack::ERROR_REPEATED_FRAME:
161       return "REPEATED_FRAME";
162     case unwindstack::ERROR_INVALID_ELF:
163       return "INVALID_ELF";
164     case unwindstack::ERROR_SYSTEM_CALL:
165       return "SYSTEM_CALL";
166     case unwindstack::ERROR_THREAD_DOES_NOT_EXIST:
167       return "THREAD_DOES_NOT_EXIST";
168     case unwindstack::ERROR_THREAD_TIMEOUT:
169       return "THREAD_TIMEOUT";
170   }
171 }
172 
173 }  // namespace profiling
174 }  // namespace perfetto
175