Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 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 | #ifndef SIMPLE_PERF_JIT_DEBUG_READER_H_ |
| 18 | #define SIMPLE_PERF_JIT_DEBUG_READER_H_ |
| 19 | |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <functional> |
| 23 | #include <memory> |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 24 | #include <queue> |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 25 | #include <stack> |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 27 | #include <unordered_set> |
| 28 | #include <vector> |
| 29 | |
Mark Salyzyn | 499a017 | 2018-11-12 12:58:06 -0800 | [diff] [blame] | 30 | #include <android-base/file.h> |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 32 | |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 33 | #include "IOEventLoop.h" |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 34 | #include "environment.h" |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 35 | #include "record.h" |
| 36 | |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 37 | namespace simpleperf { |
| 38 | |
Yabin Cui | 075dd18 | 2020-08-05 19:51:36 +0000 | [diff] [blame] | 39 | inline constexpr const char* kJITAppCacheFile = "jit_app_cache"; |
| 40 | inline constexpr const char* kJITZygoteCacheFile = "jit_zygote_cache"; |
| 41 | |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 42 | // JITDebugInfo represents the debug info of a JITed Java method or a dex file. |
| 43 | struct JITDebugInfo { |
| 44 | enum { |
| 45 | JIT_DEBUG_JIT_CODE, |
| 46 | JIT_DEBUG_DEX_FILE, |
| 47 | } type; |
| 48 | pid_t pid; // Process of the debug info |
| 49 | uint64_t timestamp; // Monotonic timestamp for the creation of the debug info |
| 50 | union { |
| 51 | struct { |
| 52 | uint64_t jit_code_addr; // The start addr of the JITed code |
| 53 | uint64_t jit_code_len; // The end addr of the JITed code |
| 54 | }; |
| 55 | uint64_t dex_file_offset; // The offset of the dex file in the file containing it |
| 56 | }; |
| 57 | // For JITed code, it is the path of a temporary ELF file storing its debug info. |
| 58 | // For dex file, it is the path of the file containing the dex file. |
| 59 | std::string file_path; |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 60 | uint64_t file_offset; |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 61 | |
Yabin Cui | 400892e | 2019-05-31 10:39:56 -0700 | [diff] [blame] | 62 | // The map for dex file extracted in memory. On Android Q, ART extracts dex files in apk files |
| 63 | // directly into memory, and names it using prctl(). The kernel doesn't generate a new mmap |
| 64 | // record for it. So we need to dump it manually. |
| 65 | std::shared_ptr<ThreadMmap> extracted_dex_file_map; |
| 66 | |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 67 | JITDebugInfo(pid_t pid, uint64_t timestamp, uint64_t jit_code_addr, uint64_t jit_code_len, |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 68 | const std::string& file_path, uint64_t file_offset) |
| 69 | : type(JIT_DEBUG_JIT_CODE), |
| 70 | pid(pid), |
| 71 | timestamp(timestamp), |
| 72 | jit_code_addr(jit_code_addr), |
| 73 | jit_code_len(jit_code_len), |
| 74 | file_path(file_path), |
| 75 | file_offset(file_offset) {} |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 76 | |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 77 | JITDebugInfo(pid_t pid, uint64_t timestamp, uint64_t dex_file_offset, |
Yabin Cui | 400892e | 2019-05-31 10:39:56 -0700 | [diff] [blame] | 78 | const std::string& file_path, |
| 79 | const std::shared_ptr<ThreadMmap>& extracted_dex_file_map) |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 80 | : type(JIT_DEBUG_DEX_FILE), |
| 81 | pid(pid), |
| 82 | timestamp(timestamp), |
| 83 | dex_file_offset(dex_file_offset), |
| 84 | file_path(file_path), |
| 85 | file_offset(0), |
| 86 | extracted_dex_file_map(extracted_dex_file_map) {} |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 87 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 88 | bool operator>(const JITDebugInfo& other) const { return timestamp > other.timestamp; } |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 91 | class TempSymFile; |
| 92 | |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 93 | // JITDebugReader reads debug info of JIT code and dex files of processes using ART. The |
| 94 | // corresponding debug interface in ART is at art/runtime/jit/debugger_interface.cc. |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 95 | class JITDebugReader { |
| 96 | public: |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 97 | enum class SymFileOption { |
| 98 | kDropSymFiles, // JIT symfiles are dropped after recording. |
| 99 | kKeepSymFiles, // JIT symfiles are kept after recording, usually for debug unwinding. |
| 100 | }; |
| 101 | |
| 102 | enum class SyncOption { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 103 | kNoSync, // Don't sync debug info with records. |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 104 | kSyncWithRecords, // Sync debug info with records based on monotonic timestamp. |
| 105 | }; |
| 106 | |
| 107 | // symfile_prefix: JITDebugReader creates temporary file to store symfiles for JIT code. Add this |
| 108 | // prefix to avoid conflicts. |
| 109 | JITDebugReader(const std::string& symfile_prefix, SymFileOption symfile_option, |
| 110 | SyncOption sync_option); |
| 111 | |
| 112 | ~JITDebugReader(); |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 113 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 114 | bool SyncWithRecords() const { return sync_option_ == SyncOption::kSyncWithRecords; } |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 115 | |
| 116 | typedef std::function<bool(const std::vector<JITDebugInfo>&, bool)> debug_info_callback_t; |
| 117 | bool RegisterDebugInfoCallback(IOEventLoop* loop, const debug_info_callback_t& callback); |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 118 | |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 119 | // There are two ways to select which processes to monitor. One is using MonitorProcess(), the |
| 120 | // other is finding all processes having libart.so using records. |
| 121 | bool MonitorProcess(pid_t pid); |
| 122 | bool UpdateRecord(const Record* record); |
| 123 | |
| 124 | // Read new debug info from all monitored processes. |
| 125 | bool ReadAllProcesses(); |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 126 | // Read new debug info from one process. |
| 127 | bool ReadProcess(pid_t pid); |
| 128 | |
| 129 | // Flush all debug info registered before timestamp. |
| 130 | bool FlushDebugInfo(uint64_t timestamp); |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 131 | |
Yabin Cui | 9ba4d94 | 2020-09-08 16:12:46 -0700 | [diff] [blame] | 132 | static bool IsPathInJITSymFile(const std::string& path) { |
| 133 | return path.find(std::string("_") + kJITAppCacheFile + ":") != path.npos || |
| 134 | path.find(std::string("_") + kJITZygoteCacheFile + ":") != path.npos; |
| 135 | } |
| 136 | |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 137 | private: |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 138 | enum class DescriptorType { |
| 139 | kDEX, |
| 140 | kJIT, |
| 141 | }; |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 142 | |
| 143 | // An arch-independent representation of JIT/dex debug descriptor. |
| 144 | struct Descriptor { |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 145 | DescriptorType type; |
Yabin Cui | 62fa6ee | 2019-10-07 15:15:12 -0700 | [diff] [blame] | 146 | int version = 0; |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 147 | uint32_t action_seqlock = 0; // incremented before and after any modification |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 148 | uint64_t action_timestamp = 0; // CLOCK_MONOTONIC time of last action |
| 149 | uint64_t first_entry_addr = 0; |
| 150 | }; |
| 151 | |
| 152 | // An arch-independent representation of JIT/dex code entry. |
| 153 | struct CodeEntry { |
| 154 | uint64_t addr; |
| 155 | uint64_t symfile_addr; |
| 156 | uint64_t symfile_size; |
| 157 | uint64_t timestamp; // CLOCK_MONOTONIC time of last action |
| 158 | }; |
| 159 | |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 160 | struct Process { |
| 161 | pid_t pid = -1; |
| 162 | bool initialized = false; |
| 163 | bool died = false; |
| 164 | bool is_64bit = false; |
Yabin Cui | e5defe1 | 2022-03-23 15:55:50 -0700 | [diff] [blame] | 165 | // remote addr of jit descriptor |
| 166 | uint64_t jit_descriptor_addr = 0; |
| 167 | // remote addr of dex descriptor |
| 168 | uint64_t dex_descriptor_addr = 0; |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 169 | |
| 170 | // The state we know about the remote jit debug descriptor. |
| 171 | Descriptor last_jit_descriptor; |
| 172 | // The state we know about the remote dex debug descriptor. |
| 173 | Descriptor last_dex_descriptor; |
Yabin Cui | 7f94b47 | 2020-08-03 21:11:32 +0000 | [diff] [blame] | 174 | |
| 175 | // memory space for /memfd:jit-zygote-cache |
| 176 | std::vector<std::pair<uint64_t, uint64_t>> jit_zygote_cache_ranges_; |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | // The location of descriptors in libart.so. |
| 180 | struct DescriptorsLocation { |
Yabin Cui | e5defe1 | 2022-03-23 15:55:50 -0700 | [diff] [blame] | 181 | bool is_64bit = false; |
| 182 | uint64_t jit_descriptor_addr = 0; |
| 183 | uint64_t dex_descriptor_addr = 0; |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 186 | bool ReadProcess(Process& process, std::vector<JITDebugInfo>* debug_info); |
| 187 | bool ReadDebugInfo(Process& process, Descriptor& new_descriptor, |
| 188 | std::vector<JITDebugInfo>* debug_info); |
| 189 | bool IsDescriptorChanged(Process& process, Descriptor& old_descriptor); |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 190 | bool InitializeProcess(Process& process); |
Yabin Cui | e5defe1 | 2022-03-23 15:55:50 -0700 | [diff] [blame] | 191 | const DescriptorsLocation* GetDescriptorsLocation(const std::string& art_lib_path); |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 192 | bool ReadRemoteMem(Process& process, uint64_t remote_addr, uint64_t size, void* data); |
| 193 | bool ReadDescriptors(Process& process, Descriptor* jit_descriptor, Descriptor* dex_descriptor); |
Yabin Cui | 62fa6ee | 2019-10-07 15:15:12 -0700 | [diff] [blame] | 194 | template <typename DescriptorT> |
Yabin Cui | e5defe1 | 2022-03-23 15:55:50 -0700 | [diff] [blame] | 195 | bool ReadDescriptorsImpl(Process& process, Descriptor* jit_descriptor, |
| 196 | Descriptor* dex_descriptor); |
| 197 | template <typename DescriptorT> |
| 198 | bool ParseDescriptor(const DescriptorT& raw_descriptor, Descriptor* descriptor); |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 199 | |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 200 | bool ReadNewCodeEntries(Process& process, const Descriptor& descriptor, |
| 201 | uint64_t last_action_timestamp, uint32_t read_entry_limit, |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 202 | std::vector<CodeEntry>* new_code_entries); |
Yabin Cui | 62fa6ee | 2019-10-07 15:15:12 -0700 | [diff] [blame] | 203 | template <typename CodeEntryT> |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 204 | bool ReadNewCodeEntriesImpl(Process& process, const Descriptor& descriptor, |
| 205 | uint64_t last_action_timestamp, uint32_t read_entry_limit, |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 206 | std::vector<CodeEntry>* new_code_entries); |
| 207 | |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 208 | bool ReadJITCodeDebugInfo(Process& process, const std::vector<CodeEntry>& jit_entries, |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 209 | std::vector<JITDebugInfo>* debug_info); |
Yabin Cui | 7f94b47 | 2020-08-03 21:11:32 +0000 | [diff] [blame] | 210 | TempSymFile* GetTempSymFile(Process& process, const CodeEntry& jit_entry); |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 211 | void ReadDexFileDebugInfo(Process& process, const std::vector<CodeEntry>& dex_entries, |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 212 | std::vector<JITDebugInfo>* debug_info); |
Yabin Cui | 400892e | 2019-05-31 10:39:56 -0700 | [diff] [blame] | 213 | bool AddDebugInfo(const std::vector<JITDebugInfo>& debug_info, bool sync_kernel_records); |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 214 | |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 215 | const std::string symfile_prefix_; |
| 216 | SymFileOption symfile_option_; |
| 217 | SyncOption sync_option_; |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 218 | IOEventRef read_event_ = nullptr; |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 219 | debug_info_callback_t debug_info_callback_; |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 220 | |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 221 | // Keys are pids of processes having libart.so, values show whether a process has been monitored. |
| 222 | std::unordered_map<pid_t, bool> pids_with_art_lib_; |
| 223 | |
| 224 | // All monitored processes |
| 225 | std::unordered_map<pid_t, Process> processes_; |
| 226 | std::unordered_map<std::string, DescriptorsLocation> descriptors_location_cache_; |
Yabin Cui | 5d5c01a | 2018-08-27 10:30:20 -0700 | [diff] [blame] | 227 | |
| 228 | std::priority_queue<JITDebugInfo, std::vector<JITDebugInfo>, std::greater<JITDebugInfo>> |
| 229 | debug_info_q_; |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 230 | |
| 231 | // temporary files used to store jit symfiles created by the app process and the zygote process. |
| 232 | std::unique_ptr<TempSymFile> app_symfile_; |
Yabin Cui | 7f94b47 | 2020-08-03 21:11:32 +0000 | [diff] [blame] | 233 | std::unique_ptr<TempSymFile> zygote_symfile_; |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 234 | }; |
| 235 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 236 | } // namespace simpleperf |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 237 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 238 | #endif // SIMPLE_PERF_JIT_DEBUG_READER_H_ |