David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 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 "debugger_interface.h" |
| 18 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 21 | #include "base/array_ref.h" |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 22 | #include "base/bit_utils.h" |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 23 | #include "base/logging.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 24 | #include "base/mutex.h" |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 25 | #include "base/time_utils.h" |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 26 | #include "base/utils.h" |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 27 | #include "dex/dex_file.h" |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 28 | #include "jit/jit.h" |
| 29 | #include "runtime.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 30 | #include "thread-current-inl.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 31 | #include "thread.h" |
| 32 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 33 | #include <atomic> |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 34 | #include <cstddef> |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 35 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 36 | // |
| 37 | // Debug interface for native tools (gdb, lldb, libunwind, simpleperf). |
| 38 | // |
| 39 | // See http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 40 | // |
| 41 | // There are two ways for native tools to access the debug data safely: |
| 42 | // |
| 43 | // 1) Synchronously, by setting a breakpoint in the __*_debug_register_code |
| 44 | // method, which is called after every modification of the linked list. |
| 45 | // GDB does this, but it is complex to set up and it stops the process. |
| 46 | // |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 47 | // 2) Asynchronously, by monitoring the action_seqlock_. |
| 48 | // * The seqlock is a monotonically increasing counter which is incremented |
| 49 | // before and after every modification of the linked list. Odd value of |
| 50 | // the counter means the linked list is being modified (it is locked). |
| 51 | // * The tool should read the value of the seqlock both before and after |
| 52 | // copying the linked list. If the seqlock values match and are even, |
| 53 | // the copy is consistent. Otherwise, the reader should try again. |
| 54 | // * Note that using the data directly while is it being modified |
| 55 | // might crash the tool. Therefore, the only safe way is to make |
| 56 | // a copy and use the copy only after the seqlock has been checked. |
| 57 | // * Note that the process might even free and munmap the data while |
| 58 | // it is being copied, therefore the reader should either handle |
| 59 | // SEGV or use OS calls to read the memory (e.g. process_vm_readv). |
| 60 | // * The seqlock can be used to determine the number of modifications of |
| 61 | // the linked list, which can be used to intelligently cache the data. |
| 62 | // Note the possible overflow of the seqlock. It is intentionally |
| 63 | // 32-bit, since 64-bit atomics can be tricky on some architectures. |
| 64 | // * The timestamps on the entry record the time when the entry was |
| 65 | // created which is relevant if the unwinding is not live and is |
| 66 | // postponed until much later. All timestamps must be unique. |
| 67 | // * Memory barriers are used to make it possible to reason about |
| 68 | // the data even when it is being modified (e.g. the process crashed |
| 69 | // while that data was locked, and thus it will be never unlocked). |
| 70 | // * In particular, it should be possible to: |
| 71 | // 1) read the seqlock and then the linked list head pointer. |
| 72 | // 2) copy the entry and check that seqlock has not changed. |
| 73 | // 3) copy the symfile and check that seqlock has not changed. |
| 74 | // 4) go back to step 2 using the next pointer (if non-null). |
| 75 | // This safely creates copy of all symfiles, although other data |
| 76 | // might be inconsistent/unusable (e.g. prev_, action_timestamp_). |
| 77 | // * For full conformance with the C++ memory model, all seqlock |
| 78 | // protected accesses should be atomic. We currently do this in the |
| 79 | // more critical cases. The rest will have to be fixed before |
| 80 | // attempting to run TSAN on this code. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 81 | // |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 82 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 83 | namespace art { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 84 | |
| 85 | static Mutex g_jit_debug_lock("JIT native debug entries", kNativeDebugInterfaceLock); |
| 86 | static Mutex g_dex_debug_lock("DEX native debug entries", kNativeDebugInterfaceLock); |
| 87 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 88 | extern "C" { |
Andreas Gampe | c55bb39 | 2018-09-21 00:02:02 +0000 | [diff] [blame] | 89 | enum JITAction { |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 90 | JIT_NOACTION = 0, |
| 91 | JIT_REGISTER_FN, |
| 92 | JIT_UNREGISTER_FN |
Andreas Gampe | c55bb39 | 2018-09-21 00:02:02 +0000 | [diff] [blame] | 93 | }; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 94 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 95 | // Public/stable binary interface. |
| 96 | struct JITCodeEntryPublic { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 97 | // Atomic to ensure the reader can always iterate over the linked list |
| 98 | // (e.g. the process could crash in the middle of writing this field). |
| 99 | std::atomic<JITCodeEntry*> next_; |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 100 | JITCodeEntry* prev_; // For linked list deletion. Unused in readers. |
| 101 | const uint8_t* symfile_addr_; // Address of the in-memory ELF file. |
| 102 | uint64_t symfile_size_; // Beware of the offset (12 on x86; but 16 on ARM32). |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 103 | |
| 104 | // Android-specific fields: |
| 105 | uint64_t register_timestamp_; // CLOCK_MONOTONIC time of entry registration. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 108 | // Implementation-specific fields (which can be used only in this file). |
| 109 | struct JITCodeEntry : public JITCodeEntryPublic { |
| 110 | // Unpacked entries: Code address of the symbol in the ELF file. |
| 111 | // Packed entries: The start address of the covered memory range. |
| 112 | const void* addr_ = nullptr; |
| 113 | // Allow merging of ELF files to save space. |
| 114 | // Packing drops advanced DWARF data, so it is not always desirable. |
| 115 | bool allow_packing_ = false; |
| 116 | // Whether this entry has been LZMA compressed. |
| 117 | // Compression is expensive, so we don't always do it. |
| 118 | bool is_compressed_ = false; |
| 119 | }; |
| 120 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 121 | struct JITDescriptor { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 122 | uint32_t version_ = 1; // NB: GDB supports only version 1. |
| 123 | uint32_t action_flag_ = JIT_NOACTION; // One of the JITAction enum values. |
| 124 | JITCodeEntry* relevant_entry_ = nullptr; // The entry affected by the action. |
| 125 | std::atomic<JITCodeEntry*> head_{nullptr}; // Head of link list of all entries. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 126 | |
| 127 | // Android-specific fields: |
| 128 | uint8_t magic_[8] = {'A', 'n', 'd', 'r', 'o', 'i', 'd', '1'}; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 129 | uint32_t flags_ = 0; // Reserved for future use. Must be 0. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 130 | uint32_t sizeof_descriptor = sizeof(JITDescriptor); |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 131 | uint32_t sizeof_entry = sizeof(JITCodeEntryPublic); |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 132 | std::atomic_uint32_t action_seqlock_{0}; // Incremented before and after any modification. |
| 133 | uint64_t action_timestamp_ = 1; // CLOCK_MONOTONIC time of last action. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 136 | // Check that std::atomic has the expected layout. |
| 137 | static_assert(alignof(std::atomic_uint32_t) == alignof(uint32_t), "Weird alignment"); |
| 138 | static_assert(sizeof(std::atomic_uint32_t) == sizeof(uint32_t), "Weird size"); |
| 139 | static_assert(alignof(std::atomic<void*>) == alignof(void*), "Weird alignment"); |
| 140 | static_assert(sizeof(std::atomic<void*>) == sizeof(void*), "Weird size"); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 141 | |
| 142 | // GDB may set breakpoint here. We must ensure it is not removed or deduplicated. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 143 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 144 | __asm__(""); |
| 145 | } |
| 146 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 147 | // Alternatively, native tools may overwrite this field to execute custom handler. |
David Srbecky | e8b4e85 | 2016-03-15 17:02:41 +0000 | [diff] [blame] | 148 | void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code; |
| 149 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 150 | // The root data structure describing of all JITed methods. |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 151 | JITDescriptor __jit_debug_descriptor GUARDED_BY(g_jit_debug_lock) {}; |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 152 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 153 | // The following globals mirror the ones above, but are used to register dex files. |
| 154 | void __attribute__((noinline)) __dex_debug_register_code() { |
| 155 | __asm__(""); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 156 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 157 | void (*__dex_debug_register_code_ptr)() = __dex_debug_register_code; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 158 | JITDescriptor __dex_debug_descriptor GUARDED_BY(g_dex_debug_lock) {}; |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 159 | } |
| 160 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 161 | ArrayRef<const uint8_t> GetJITCodeEntrySymFile(JITCodeEntry* entry) { |
| 162 | return ArrayRef<const uint8_t>(entry->symfile_addr_, entry->symfile_size_); |
| 163 | } |
| 164 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 165 | // Mark the descriptor as "locked", so native tools know the data is being modified. |
| 166 | static void ActionSeqlock(JITDescriptor& descriptor) { |
| 167 | DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 0u) << "Already locked"; |
| 168 | descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed); |
| 169 | // Ensure that any writes within the locked section cannot be reordered before the increment. |
| 170 | std::atomic_thread_fence(std::memory_order_release); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 171 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 172 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 173 | // Mark the descriptor as "unlocked", so native tools know the data is safe to read. |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 174 | static void ActionSequnlock(JITDescriptor& descriptor) { |
| 175 | DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 1u) << "Already unlocked"; |
| 176 | // Ensure that any writes within the locked section cannot be reordered after the increment. |
| 177 | std::atomic_thread_fence(std::memory_order_release); |
| 178 | descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 179 | } |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 180 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 181 | static JITCodeEntry* CreateJITCodeEntryInternal( |
| 182 | JITDescriptor& descriptor, |
| 183 | void (*register_code_ptr)(), |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 184 | ArrayRef<const uint8_t> symfile, |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 185 | bool copy_symfile, |
| 186 | const void* addr = nullptr, |
| 187 | bool allow_packing = false, |
| 188 | bool is_compressed = false) { |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 189 | // Make a copy of the buffer to shrink it and to pass ownership to JITCodeEntry. |
| 190 | if (copy_symfile) { |
| 191 | uint8_t* copy = new uint8_t[symfile.size()]; |
| 192 | CHECK(copy != nullptr); |
| 193 | memcpy(copy, symfile.data(), symfile.size()); |
| 194 | symfile = ArrayRef<const uint8_t>(copy, symfile.size()); |
| 195 | } |
| 196 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 197 | // Ensure the timestamp is monotonically increasing even in presence of low |
| 198 | // granularity system timer. This ensures each entry has unique timestamp. |
| 199 | uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 200 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 201 | JITCodeEntry* head = descriptor.head_.load(std::memory_order_relaxed); |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 202 | JITCodeEntry* entry = new JITCodeEntry(); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 203 | CHECK(entry != nullptr); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 204 | entry->symfile_addr_ = symfile.data(); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 205 | entry->symfile_size_ = symfile.size(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 206 | entry->prev_ = nullptr; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 207 | entry->next_.store(head, std::memory_order_relaxed); |
| 208 | entry->register_timestamp_ = timestamp; |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 209 | entry->addr_ = addr; |
| 210 | entry->allow_packing_ = allow_packing; |
| 211 | entry->is_compressed_ = is_compressed; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 212 | |
| 213 | // We are going to modify the linked list, so take the seqlock. |
| 214 | ActionSeqlock(descriptor); |
| 215 | if (head != nullptr) { |
| 216 | head->prev_ = entry; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 217 | } |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 218 | descriptor.head_.store(entry, std::memory_order_relaxed); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 219 | descriptor.relevant_entry_ = entry; |
| 220 | descriptor.action_flag_ = JIT_REGISTER_FN; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 221 | descriptor.action_timestamp_ = timestamp; |
| 222 | ActionSequnlock(descriptor); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 223 | |
| 224 | (*register_code_ptr)(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 225 | return entry; |
| 226 | } |
| 227 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 228 | static void DeleteJITCodeEntryInternal( |
| 229 | JITDescriptor& descriptor, |
| 230 | void (*register_code_ptr)(), |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 231 | JITCodeEntry* entry, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 232 | bool free_symfile) { |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 233 | CHECK(entry != nullptr); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 234 | const uint8_t* symfile = entry->symfile_addr_; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 235 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 236 | // Ensure the timestamp is monotonically increasing even in presence of low |
| 237 | // granularity system timer. This ensures each entry has unique timestamp. |
| 238 | uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime()); |
| 239 | |
| 240 | // We are going to modify the linked list, so take the seqlock. |
| 241 | ActionSeqlock(descriptor); |
| 242 | JITCodeEntry* next = entry->next_.load(std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 243 | if (entry->prev_ != nullptr) { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 244 | entry->prev_->next_.store(next, std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 245 | } else { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 246 | descriptor.head_.store(next, std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 247 | } |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 248 | if (next != nullptr) { |
| 249 | next->prev_ = entry->prev_; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 250 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 251 | descriptor.relevant_entry_ = entry; |
| 252 | descriptor.action_flag_ = JIT_UNREGISTER_FN; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 253 | descriptor.action_timestamp_ = timestamp; |
| 254 | ActionSequnlock(descriptor); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 255 | |
| 256 | (*register_code_ptr)(); |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 257 | |
| 258 | // Ensure that clear below can not be reordered above the unlock above. |
| 259 | std::atomic_thread_fence(std::memory_order_release); |
| 260 | |
| 261 | // Aggressively clear the entry as an extra check of the synchronisation. |
| 262 | memset(entry, 0, sizeof(*entry)); |
| 263 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 264 | delete entry; |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 265 | if (free_symfile) { |
| 266 | delete[] symfile; |
| 267 | } |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 268 | } |
| 269 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 270 | void AddNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 271 | MutexLock mu(self, g_dex_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 272 | DCHECK(dexfile != nullptr); |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 273 | const ArrayRef<const uint8_t> symfile(dexfile->Begin(), dexfile->Size()); |
| 274 | CreateJITCodeEntryInternal(__dex_debug_descriptor, |
| 275 | __dex_debug_register_code_ptr, |
| 276 | symfile, |
| 277 | /*copy_symfile=*/ false); |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 278 | } |
| 279 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 280 | void RemoveNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 281 | MutexLock mu(self, g_dex_debug_lock); |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 282 | DCHECK(dexfile != nullptr); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 283 | // We register dex files in the class linker and free them in DexFile_closeDexFile, but |
| 284 | // there might be cases where we load the dex file without using it in the class linker. |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 285 | // On the other hand, single dex file might also be used with different class-loaders. |
| 286 | for (JITCodeEntry* entry = __dex_debug_descriptor.head_; entry != nullptr; ) { |
| 287 | JITCodeEntry* next = entry->next_; // Save next pointer before we free the memory. |
| 288 | if (entry->symfile_addr_ == dexfile->Begin()) { |
| 289 | DeleteJITCodeEntryInternal(__dex_debug_descriptor, |
| 290 | __dex_debug_register_code_ptr, |
| 291 | entry, |
| 292 | /*free_symfile=*/ false); |
| 293 | } |
| 294 | entry = next; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 295 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 296 | } |
| 297 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 298 | // Size of JIT code range covered by each packed JITCodeEntry. |
| 299 | static constexpr uint32_t kJitRepackGroupSize = 64 * KB; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 300 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 301 | // Automatically call the repack method every 'n' new entries. |
| 302 | static constexpr uint32_t kJitRepackFrequency = 64; |
| 303 | static uint32_t g_jit_num_unpacked_entries = 0; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 304 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 305 | // Split the JIT code cache into groups of fixed size and create single JITCodeEntry for each group. |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 306 | // The start address of method's code determines which group it belongs to. The end is irrelevant. |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 307 | // New mini debug infos will be merged if possible, and entries for GCed functions will be removed. |
| 308 | static void RepackEntries(bool compress, ArrayRef<const void*> removed) |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 309 | REQUIRES(g_jit_debug_lock) { |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 310 | DCHECK(std::is_sorted(removed.begin(), removed.end())); |
| 311 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 312 | if (jit == nullptr) { |
| 313 | return; |
| 314 | } |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 315 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 316 | // Collect entries that we want to pack. |
| 317 | std::vector<JITCodeEntry*> entries; |
| 318 | entries.reserve(2 * kJitRepackFrequency); |
| 319 | for (JITCodeEntry* it = __jit_debug_descriptor.head_; it != nullptr; it = it->next_) { |
| 320 | if (it->allow_packing_) { |
| 321 | if (!compress && it->is_compressed_ && removed.empty()) { |
| 322 | continue; // If we are not compressing, also avoid decompressing. |
| 323 | } |
| 324 | entries.push_back(it); |
| 325 | } |
| 326 | } |
| 327 | auto cmp = [](JITCodeEntry* lhs, JITCodeEntry* rhs) { return lhs->addr_ < rhs->addr_; }; |
| 328 | std::sort(entries.begin(), entries.end(), cmp); // Sort by address. |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 329 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 330 | // Process the entries in groups (each spanning memory range of size kJitRepackGroupSize). |
| 331 | for (auto group_it = entries.begin(); group_it != entries.end();) { |
| 332 | const void* group_ptr = AlignDown((*group_it)->addr_, kJitRepackGroupSize); |
| 333 | const void* group_end = reinterpret_cast<const uint8_t*>(group_ptr) + kJitRepackGroupSize; |
| 334 | |
| 335 | // Find all entries in this group (each entry is an in-memory ELF file). |
| 336 | auto begin = group_it; |
| 337 | auto end = std::find_if(begin, entries.end(), [=](auto* e) { return e->addr_ >= group_end; }); |
| 338 | CHECK(end > begin); |
| 339 | ArrayRef<JITCodeEntry*> elfs(&*begin, end - begin); |
| 340 | |
| 341 | // Find all symbols that have been removed in this memory range. |
| 342 | auto removed_begin = std::lower_bound(removed.begin(), removed.end(), group_ptr); |
| 343 | auto removed_end = std::lower_bound(removed.begin(), removed.end(), group_end); |
| 344 | CHECK(removed_end >= removed_begin); |
| 345 | ArrayRef<const void*> removed_subset(&*removed_begin, removed_end - removed_begin); |
| 346 | |
| 347 | // Bail out early if there is nothing to do for this group. |
| 348 | if (elfs.size() == 1 && removed_subset.empty() && (*begin)->is_compressed_ == compress) { |
| 349 | group_it = end; // Go to next group. |
| 350 | continue; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 351 | } |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 352 | |
| 353 | // Create new single JITCodeEntry that covers this memory range. |
| 354 | uint64_t start_time = MicroTime(); |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 355 | size_t live_symbols; |
| 356 | std::vector<uint8_t> packed = jit->GetJitCompiler()->PackElfFileForJIT( |
| 357 | elfs, removed_subset, compress, &live_symbols); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 358 | VLOG(jit) |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 359 | << "JIT mini-debug-info repacked" |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 360 | << " for " << group_ptr |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 361 | << " in " << MicroTime() - start_time << "us" |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 362 | << " elfs=" << elfs.size() |
| 363 | << " dead=" << removed_subset.size() |
| 364 | << " live=" << live_symbols |
| 365 | << " size=" << packed.size() << (compress ? "(lzma)" : ""); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 366 | |
| 367 | // Replace the old entries with the new one (with their lifetime temporally overlapping). |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 368 | CreateJITCodeEntryInternal( |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 369 | __jit_debug_descriptor, |
| 370 | __jit_debug_register_code_ptr, |
| 371 | ArrayRef<const uint8_t>(packed), |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 372 | /*copy_symfile=*/ true, |
| 373 | /*addr_=*/ group_ptr, |
| 374 | /*allow_packing_=*/ true, |
| 375 | /*is_compressed_=*/ compress); |
| 376 | for (auto it : elfs) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 377 | DeleteJITCodeEntryInternal(__jit_debug_descriptor, |
| 378 | __jit_debug_register_code_ptr, |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 379 | /*entry=*/ it, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 380 | /*free_symfile=*/ true); |
| 381 | } |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 382 | group_it = end; // Go to next group. |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 383 | } |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 384 | g_jit_num_unpacked_entries = 0; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 385 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 386 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 387 | void AddNativeDebugInfoForJit(const void* code_ptr, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 388 | const std::vector<uint8_t>& symfile, |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 389 | bool allow_packing) { |
| 390 | MutexLock mu(Thread::Current(), g_jit_debug_lock); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 391 | DCHECK_NE(symfile.size(), 0u); |
| 392 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 393 | CreateJITCodeEntryInternal( |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 394 | __jit_debug_descriptor, |
| 395 | __jit_debug_register_code_ptr, |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 396 | ArrayRef<const uint8_t>(symfile), |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 397 | /*copy_symfile=*/ true, |
| 398 | /*addr=*/ code_ptr, |
| 399 | /*allow_packing=*/ allow_packing, |
| 400 | /*is_compressed=*/ false); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 401 | |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 402 | VLOG(jit) |
| 403 | << "JIT mini-debug-info added" |
| 404 | << " for " << code_ptr |
| 405 | << " size=" << PrettySize(symfile.size()); |
| 406 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 407 | // Automatically repack entries on regular basis to save space. |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 408 | // Pack (but don't compress) recent entries - this is cheap and reduces memory use by ~4x. |
| 409 | // We delay compression until after GC since it is more expensive (and saves further ~4x). |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 410 | if (++g_jit_num_unpacked_entries >= kJitRepackFrequency) { |
| 411 | RepackEntries(/*compress=*/ false, /*removed=*/ ArrayRef<const void*>()); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 415 | void RemoveNativeDebugInfoForJit(ArrayRef<const void*> removed) { |
| 416 | MutexLock mu(Thread::Current(), g_jit_debug_lock); |
| 417 | RepackEntries(/*compress=*/ true, removed); |
| 418 | |
| 419 | // Remove entries which are not allowed to be packed (containing single method each). |
| 420 | for (JITCodeEntry* it = __jit_debug_descriptor.head_; it != nullptr; it = it->next_) { |
| 421 | if (!it->allow_packing_ && std::binary_search(removed.begin(), removed.end(), it->addr_)) { |
| 422 | DeleteJITCodeEntryInternal(__jit_debug_descriptor, |
| 423 | __jit_debug_register_code_ptr, |
| 424 | /*entry=*/ it, |
| 425 | /*free_symfile=*/ true); |
| 426 | } |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | size_t GetJitMiniDebugInfoMemUsage() { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 431 | MutexLock mu(Thread::Current(), g_jit_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 432 | size_t size = 0; |
David Srbecky | 8fc2f95 | 2019-07-31 18:40:09 +0100 | [diff] [blame] | 433 | for (JITCodeEntry* it = __jit_debug_descriptor.head_; it != nullptr; it = it->next_) { |
| 434 | size += sizeof(JITCodeEntry) + it->symfile_size_; |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 435 | } |
| 436 | return size; |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 437 | } |
| 438 | |
David Srbecky | 1ed4515 | 2019-04-09 18:10:26 +0100 | [diff] [blame] | 439 | Mutex* GetNativeDebugInfoLock() { |
| 440 | return &g_jit_debug_lock; |
| 441 | } |
| 442 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 443 | } // namespace art |