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 (rd == -1) {
51 PERFETTO_DPLOG("read of %zu at offset %" PRIu64, size, addr);
52 return 0;
53 }
54 return static_cast<size_t>(rd);
55 }
56
FDMaps(base::ScopedFile fd)57 FDMaps::FDMaps(base::ScopedFile fd) : fd_(std::move(fd)) {}
58
Parse()59 bool FDMaps::Parse() {
60 // If the process has already exited, lseek or ReadFileDescriptor will
61 // return false.
62 if (lseek(*fd_, 0, SEEK_SET) == -1)
63 return false;
64
65 std::string content;
66 if (!base::ReadFileDescriptor(*fd_, &content))
67 return false;
68
69 unwindstack::MapInfo* prev_map = nullptr;
70 unwindstack::MapInfo* prev_real_map = nullptr;
71 return android::procinfo::ReadMapFileContent(
72 &content[0], [&](uint64_t start, uint64_t end, uint16_t flags,
73 uint64_t pgoff, ino_t, const char* name) {
74 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
75 if (strncmp(name, "/dev/", 5) == 0 &&
76 strncmp(name + 5, "ashmem/", 7) != 0) {
77 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
78 }
79 maps_.emplace_back(new unwindstack::MapInfo(
80 prev_map, prev_real_map, start, end, pgoff, flags, name));
81 prev_map = maps_.back().get();
82 if (!prev_map->IsBlank()) {
83 prev_real_map = prev_map;
84 }
85 });
86 }
87
Reset()88 void FDMaps::Reset() {
89 maps_.clear();
90 }
91
UnwindingMetadata(base::ScopedFile maps_fd,base::ScopedFile mem_fd)92 UnwindingMetadata::UnwindingMetadata(base::ScopedFile maps_fd,
93 base::ScopedFile mem_fd)
94 : fd_maps(std::move(maps_fd)),
95 fd_mem(std::make_shared<FDMemory>(std::move(mem_fd)))
96 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
97 ,
98 jit_debug(std::unique_ptr<unwindstack::JitDebug>(
99 new unwindstack::JitDebug(fd_mem))),
100 dex_files(std::unique_ptr<unwindstack::DexFiles>(
101 new unwindstack::DexFiles(fd_mem)))
102 #endif
103 {
104 if (!fd_maps.Parse())
105 PERFETTO_DLOG("Failed initial maps parse");
106 }
107
ReparseMaps()108 void UnwindingMetadata::ReparseMaps() {
109 reparses++;
110 fd_maps.Reset();
111 fd_maps.Parse();
112 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
113 jit_debug =
114 std::unique_ptr<unwindstack::JitDebug>(new unwindstack::JitDebug(fd_mem));
115 dex_files =
116 std::unique_ptr<unwindstack::DexFiles>(new unwindstack::DexFiles(fd_mem));
117 #endif
118 }
119
AnnotateFrame(unwindstack::FrameData frame)120 FrameData UnwindingMetadata::AnnotateFrame(unwindstack::FrameData frame) {
121 std::string build_id;
122 if (!frame.map_name.empty()) {
123 unwindstack::MapInfo* map_info = fd_maps.Find(frame.pc);
124 if (map_info)
125 build_id = map_info->GetBuildID();
126 }
127
128 return FrameData{std::move(frame), std::move(build_id)};
129 }
130
StringifyLibUnwindstackError(unwindstack::ErrorCode e)131 std::string StringifyLibUnwindstackError(unwindstack::ErrorCode e) {
132 switch (e) {
133 case unwindstack::ERROR_NONE:
134 return "NONE";
135 case unwindstack::ERROR_MEMORY_INVALID:
136 return "MEMORY_INVALID";
137 case unwindstack::ERROR_UNWIND_INFO:
138 return "UNWIND_INFO";
139 case unwindstack::ERROR_UNSUPPORTED:
140 return "UNSUPPORTED";
141 case unwindstack::ERROR_INVALID_MAP:
142 return "INVALID_MAP";
143 case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
144 return "MAX_FRAME_EXCEEDED";
145 case unwindstack::ERROR_REPEATED_FRAME:
146 return "REPEATED_FRAME";
147 case unwindstack::ERROR_INVALID_ELF:
148 return "INVALID_ELF";
149 }
150 }
151
152 } // namespace profiling
153 } // namespace perfetto
154