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 | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 22 | #include "base/logging.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 23 | #include "base/mutex.h" |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 24 | #include "base/time_utils.h" |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 25 | #include "base/utils.h" |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 26 | #include "dex/dex_file.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 27 | #include "thread-current-inl.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 28 | #include "thread.h" |
| 29 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 30 | #include <atomic> |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 31 | #include <cstddef> |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 32 | #include <deque> |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 33 | #include <map> |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 34 | #include <set> |
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 | |
| 95 | struct JITCodeEntry { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 96 | // Atomic to ensure the reader can always iterate over the linked list |
| 97 | // (e.g. the process could crash in the middle of writing this field). |
| 98 | std::atomic<JITCodeEntry*> next_; |
| 99 | // Non-atomic. The reader should not use it. It is only used for deletion. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 100 | JITCodeEntry* prev_; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 101 | const uint8_t* symfile_addr_; |
| 102 | uint64_t symfile_size_; // Beware of the offset (12 on x86; but 16 on ARM32). |
| 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 | |
| 108 | struct JITDescriptor { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 109 | uint32_t version_ = 1; // NB: GDB supports only version 1. |
| 110 | uint32_t action_flag_ = JIT_NOACTION; // One of the JITAction enum values. |
| 111 | JITCodeEntry* relevant_entry_ = nullptr; // The entry affected by the action. |
| 112 | std::atomic<JITCodeEntry*> head_{nullptr}; // Head of link list of all entries. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 113 | |
| 114 | // Android-specific fields: |
| 115 | uint8_t magic_[8] = {'A', 'n', 'd', 'r', 'o', 'i', 'd', '1'}; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 116 | uint32_t flags_ = 0; // Reserved for future use. Must be 0. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 117 | uint32_t sizeof_descriptor = sizeof(JITDescriptor); |
| 118 | uint32_t sizeof_entry = sizeof(JITCodeEntry); |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 119 | std::atomic_uint32_t action_seqlock_{0}; // Incremented before and after any modification. |
| 120 | uint64_t action_timestamp_ = 1; // CLOCK_MONOTONIC time of last action. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 123 | // Check that std::atomic has the expected layout. |
| 124 | static_assert(alignof(std::atomic_uint32_t) == alignof(uint32_t), "Weird alignment"); |
| 125 | static_assert(sizeof(std::atomic_uint32_t) == sizeof(uint32_t), "Weird size"); |
| 126 | static_assert(alignof(std::atomic<void*>) == alignof(void*), "Weird alignment"); |
| 127 | static_assert(sizeof(std::atomic<void*>) == sizeof(void*), "Weird size"); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 128 | |
| 129 | // 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] | 130 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 131 | __asm__(""); |
| 132 | } |
| 133 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 134 | // Alternatively, native tools may overwrite this field to execute custom handler. |
David Srbecky | e8b4e85 | 2016-03-15 17:02:41 +0000 | [diff] [blame] | 135 | void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code; |
| 136 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 137 | // The root data structure describing of all JITed methods. |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 138 | JITDescriptor __jit_debug_descriptor GUARDED_BY(g_jit_debug_lock) {}; |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 139 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 140 | // The following globals mirror the ones above, but are used to register dex files. |
| 141 | void __attribute__((noinline)) __dex_debug_register_code() { |
| 142 | __asm__(""); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 143 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 144 | void (*__dex_debug_register_code_ptr)() = __dex_debug_register_code; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 145 | JITDescriptor __dex_debug_descriptor GUARDED_BY(g_dex_debug_lock) {}; |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 146 | } |
| 147 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 148 | // Mark the descriptor as "locked", so native tools know the data is being modified. |
| 149 | static void ActionSeqlock(JITDescriptor& descriptor) { |
| 150 | DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 0u) << "Already locked"; |
| 151 | descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed); |
| 152 | // Ensure that any writes within the locked section cannot be reordered before the increment. |
| 153 | std::atomic_thread_fence(std::memory_order_release); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 154 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 155 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 156 | // 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] | 157 | static void ActionSequnlock(JITDescriptor& descriptor) { |
| 158 | DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 1u) << "Already unlocked"; |
| 159 | // Ensure that any writes within the locked section cannot be reordered after the increment. |
| 160 | std::atomic_thread_fence(std::memory_order_release); |
| 161 | descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 162 | } |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 163 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 164 | static JITCodeEntry* CreateJITCodeEntryInternal( |
| 165 | JITDescriptor& descriptor, |
| 166 | void (*register_code_ptr)(), |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 167 | ArrayRef<const uint8_t> symfile, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 168 | bool copy_symfile) { |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 169 | // Make a copy of the buffer to shrink it and to pass ownership to JITCodeEntry. |
| 170 | if (copy_symfile) { |
| 171 | uint8_t* copy = new uint8_t[symfile.size()]; |
| 172 | CHECK(copy != nullptr); |
| 173 | memcpy(copy, symfile.data(), symfile.size()); |
| 174 | symfile = ArrayRef<const uint8_t>(copy, symfile.size()); |
| 175 | } |
| 176 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 177 | // Ensure the timestamp is monotonically increasing even in presence of low |
| 178 | // granularity system timer. This ensures each entry has unique timestamp. |
| 179 | uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 180 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 181 | JITCodeEntry* head = descriptor.head_.load(std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 182 | JITCodeEntry* entry = new JITCodeEntry; |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 183 | CHECK(entry != nullptr); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 184 | entry->symfile_addr_ = symfile.data(); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 185 | entry->symfile_size_ = symfile.size(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 186 | entry->prev_ = nullptr; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 187 | entry->next_.store(head, std::memory_order_relaxed); |
| 188 | entry->register_timestamp_ = timestamp; |
| 189 | |
| 190 | // We are going to modify the linked list, so take the seqlock. |
| 191 | ActionSeqlock(descriptor); |
| 192 | if (head != nullptr) { |
| 193 | head->prev_ = entry; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 194 | } |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 195 | descriptor.head_.store(entry, std::memory_order_relaxed); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 196 | descriptor.relevant_entry_ = entry; |
| 197 | descriptor.action_flag_ = JIT_REGISTER_FN; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 198 | descriptor.action_timestamp_ = timestamp; |
| 199 | ActionSequnlock(descriptor); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 200 | |
| 201 | (*register_code_ptr)(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 202 | return entry; |
| 203 | } |
| 204 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 205 | static void DeleteJITCodeEntryInternal( |
| 206 | JITDescriptor& descriptor, |
| 207 | void (*register_code_ptr)(), |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 208 | JITCodeEntry* entry, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 209 | bool free_symfile) { |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 210 | CHECK(entry != nullptr); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 211 | const uint8_t* symfile = entry->symfile_addr_; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 212 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 213 | // Ensure the timestamp is monotonically increasing even in presence of low |
| 214 | // granularity system timer. This ensures each entry has unique timestamp. |
| 215 | uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime()); |
| 216 | |
| 217 | // We are going to modify the linked list, so take the seqlock. |
| 218 | ActionSeqlock(descriptor); |
| 219 | JITCodeEntry* next = entry->next_.load(std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 220 | if (entry->prev_ != nullptr) { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 221 | entry->prev_->next_.store(next, std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 222 | } else { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 223 | descriptor.head_.store(next, std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 224 | } |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 225 | if (next != nullptr) { |
| 226 | next->prev_ = entry->prev_; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 227 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 228 | descriptor.relevant_entry_ = entry; |
| 229 | descriptor.action_flag_ = JIT_UNREGISTER_FN; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 230 | descriptor.action_timestamp_ = timestamp; |
| 231 | ActionSequnlock(descriptor); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 232 | |
| 233 | (*register_code_ptr)(); |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 234 | |
| 235 | // Ensure that clear below can not be reordered above the unlock above. |
| 236 | std::atomic_thread_fence(std::memory_order_release); |
| 237 | |
| 238 | // Aggressively clear the entry as an extra check of the synchronisation. |
| 239 | memset(entry, 0, sizeof(*entry)); |
| 240 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 241 | delete entry; |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 242 | if (free_symfile) { |
| 243 | delete[] symfile; |
| 244 | } |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 245 | } |
| 246 | |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 247 | static std::map<const DexFile*, JITCodeEntry*> g_dex_debug_entries GUARDED_BY(g_dex_debug_lock); |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 248 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 249 | void AddNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 250 | MutexLock mu(self, g_dex_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 251 | DCHECK(dexfile != nullptr); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 252 | // This is just defensive check. The class linker should not register the dex file twice. |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 253 | if (g_dex_debug_entries.count(dexfile) == 0) { |
| 254 | const ArrayRef<const uint8_t> symfile(dexfile->Begin(), dexfile->Size()); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 255 | JITCodeEntry* entry = CreateJITCodeEntryInternal(__dex_debug_descriptor, |
| 256 | __dex_debug_register_code_ptr, |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 257 | symfile, |
| 258 | /*copy_symfile=*/ false); |
| 259 | g_dex_debug_entries.emplace(dexfile, entry); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 260 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 261 | } |
| 262 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 263 | void RemoveNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 264 | MutexLock mu(self, g_dex_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 265 | auto it = g_dex_debug_entries.find(dexfile); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 266 | // We register dex files in the class linker and free them in DexFile_closeDexFile, but |
| 267 | // there might be cases where we load the dex file without using it in the class linker. |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 268 | if (it != g_dex_debug_entries.end()) { |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 269 | DeleteJITCodeEntryInternal(__dex_debug_descriptor, |
| 270 | __dex_debug_register_code_ptr, |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 271 | /*entry=*/ it->second, |
| 272 | /*free_symfile=*/ false); |
| 273 | g_dex_debug_entries.erase(it); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 274 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 275 | } |
| 276 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 277 | // Mapping from handle to entry. Used to manage life-time of the entries. |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 278 | using JITCodeEntries = std::multimap<const void*, JITCodeEntry*>; |
| 279 | static JITCodeEntries g_uncompressed_jit_debug_entries GUARDED_BY(g_jit_debug_lock); |
| 280 | static JITCodeEntries g_compressed_jit_debug_entries GUARDED_BY(g_jit_debug_lock); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 281 | |
| 282 | // Number of entries added since last packing. Used to pack entries in bulk. |
| 283 | static size_t g_jit_num_unpacked_entries GUARDED_BY(g_jit_debug_lock) = 0; |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 284 | static constexpr uint32_t kJitMaxUnpackedEntries = 64; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 285 | |
| 286 | // We postpone removal so that it is done in bulk. |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 287 | static std::set<const void*> g_jit_removed_entries GUARDED_BY(g_jit_debug_lock); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 288 | |
| 289 | // Split the JIT code cache into groups of fixed size and create singe JITCodeEntry for each group. |
| 290 | // The start address of method's code determines which group it belongs to. The end is irrelevant. |
| 291 | // As a consequnce, newly added mini debug infos will be merged and old ones (GCed) will be pruned. |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 292 | static void RepackEntries(PackElfFileForJITFunction pack, |
| 293 | InstructionSet isa, |
| 294 | const InstructionSetFeatures* features, |
| 295 | bool compress, |
| 296 | /*inout*/ JITCodeEntries* entries) |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 297 | REQUIRES(g_jit_debug_lock) { |
| 298 | // Size of memory range covered by each JITCodeEntry. |
| 299 | // The number of methods per entry is variable (depending on how many fit in that range). |
| 300 | constexpr uint32_t kGroupSize = 64 * KB; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 301 | |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 302 | JITCodeEntries packed_entries; |
| 303 | std::vector<ArrayRef<const uint8_t>> added; |
| 304 | std::vector<const void*> removed; |
| 305 | while (!entries->empty()) { |
| 306 | const void* group_ptr = AlignDown(entries->begin()->first, kGroupSize); |
| 307 | const void* group_end = reinterpret_cast<const uint8_t*>(group_ptr) + kGroupSize; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 308 | |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 309 | // Collect all entries that have been added or removed within our memory range. |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 310 | added.clear(); |
| 311 | auto add_it = entries->begin(); |
| 312 | for (; add_it != entries->end() && add_it->first < group_end; ++add_it) { |
| 313 | JITCodeEntry* entry = add_it->second; |
| 314 | added.emplace_back(entry->symfile_addr_, entry->symfile_size_); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 315 | } |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 316 | removed.clear(); |
| 317 | auto remove_it = g_jit_removed_entries.lower_bound(group_ptr); |
| 318 | for (; remove_it != g_jit_removed_entries.end() && *remove_it < group_end; ++remove_it) { |
| 319 | removed.push_back(*remove_it); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 320 | } |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 321 | CHECK_GT(added.size(), 0u); |
| 322 | if (added.size() == 1 && removed.size() == 0) { |
| 323 | packed_entries.insert(entries->extract(entries->begin())); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 324 | continue; // Nothing changed in this memory range. |
| 325 | } |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 326 | |
| 327 | // Create new single JITCodeEntry that covers this memory range. |
| 328 | uint64_t start_time = MicroTime(); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 329 | size_t symbols; |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 330 | std::vector<uint8_t> packed = pack(isa, features, added, removed, compress, &symbols); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 331 | VLOG(jit) |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 332 | << "JIT mini-debug-info repacked" |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 333 | << " for " << group_ptr |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 334 | << " in " << MicroTime() - start_time << "us" |
| 335 | << " files=" << added.size() |
| 336 | << " removed=" << removed.size() |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 337 | << " symbols=" << symbols |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 338 | << " size=" << PrettySize(packed.size()) |
| 339 | << " compress=" << compress; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 340 | |
| 341 | // Replace the old entries with the new one (with their lifetime temporally overlapping). |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 342 | packed_entries.emplace(group_ptr, CreateJITCodeEntryInternal( |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 343 | __jit_debug_descriptor, |
| 344 | __jit_debug_register_code_ptr, |
| 345 | ArrayRef<const uint8_t>(packed), |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 346 | /*copy_symfile=*/ true)); |
| 347 | for (auto it = entries->begin(); it != add_it; it = entries->erase(it)) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 348 | DeleteJITCodeEntryInternal(__jit_debug_descriptor, |
| 349 | __jit_debug_register_code_ptr, |
| 350 | /*entry=*/ it->second, |
| 351 | /*free_symfile=*/ true); |
| 352 | } |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 353 | } |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 354 | entries->swap(packed_entries); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 355 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 356 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 357 | void AddNativeDebugInfoForJit(Thread* self, |
| 358 | const void* code_ptr, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 359 | const std::vector<uint8_t>& symfile, |
| 360 | PackElfFileForJITFunction pack, |
| 361 | InstructionSet isa, |
| 362 | const InstructionSetFeatures* features) { |
| 363 | MutexLock mu(self, g_jit_debug_lock); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 364 | DCHECK_NE(symfile.size(), 0u); |
| 365 | |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 366 | // Pack and compress all entries. This will run on first compilation after a GC. |
| 367 | // Must be done before addition in case the added code_ptr is in the removed set. |
| 368 | if (!g_jit_removed_entries.empty()) { |
| 369 | g_compressed_jit_debug_entries.merge(g_uncompressed_jit_debug_entries); |
| 370 | RepackEntries(pack, isa, features, /*compress=*/ true, &g_compressed_jit_debug_entries); |
| 371 | g_jit_removed_entries.clear(); |
| 372 | g_jit_num_unpacked_entries = 0; |
| 373 | } |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 374 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 375 | JITCodeEntry* entry = CreateJITCodeEntryInternal( |
| 376 | __jit_debug_descriptor, |
| 377 | __jit_debug_register_code_ptr, |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 378 | ArrayRef<const uint8_t>(symfile), |
| 379 | /*copy_symfile=*/ true); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 380 | |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 381 | VLOG(jit) |
| 382 | << "JIT mini-debug-info added" |
| 383 | << " for " << code_ptr |
| 384 | << " size=" << PrettySize(symfile.size()); |
| 385 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 386 | // We don't provide code_ptr for type debug info, which means we cannot free it later. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 387 | // (this only happens when --generate-debug-info flag is enabled for the purpose |
| 388 | // of being debugged with gdb; it does not happen for debuggable apps by default). |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 389 | if (code_ptr == nullptr) { |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | g_uncompressed_jit_debug_entries.emplace(code_ptr, entry); |
| 394 | // Count how many entries we have added since the last mini-debug-info packing. |
| 395 | // We avoid g_uncompressed_jit_debug_entries.size() because it can shrink during packing. |
| 396 | ++g_jit_num_unpacked_entries; |
| 397 | |
| 398 | // Pack (but don't compress) recent entries - this is cheap and reduces memory use by ~4x. |
| 399 | // We delay compression until after GC since it is more expensive (and saves further ~4x). |
| 400 | if (g_jit_num_unpacked_entries >= kJitMaxUnpackedEntries) { |
| 401 | RepackEntries(pack, isa, features, /*compress=*/ false, &g_uncompressed_jit_debug_entries); |
| 402 | g_jit_num_unpacked_entries = 0; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 406 | void RemoveNativeDebugInfoForJit(Thread* self, const void* code_ptr) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 407 | MutexLock mu(self, g_jit_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 408 | // We generate JIT native debug info only if the right runtime flags are enabled, |
| 409 | // but we try to remove it unconditionally whenever code is freed from JIT cache. |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 410 | if (!g_uncompressed_jit_debug_entries.empty() || !g_compressed_jit_debug_entries.empty()) { |
| 411 | g_jit_removed_entries.insert(code_ptr); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | size_t GetJitMiniDebugInfoMemUsage() { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 416 | MutexLock mu(Thread::Current(), g_jit_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 417 | size_t size = 0; |
David Srbecky | 76b9c69 | 2019-04-01 19:36:33 +0100 | [diff] [blame] | 418 | for (const auto& entries : {g_uncompressed_jit_debug_entries, g_compressed_jit_debug_entries}) { |
| 419 | for (const auto& entry : entries) { |
| 420 | size += sizeof(JITCodeEntry) + entry.second->symfile_size_ + /*map entry*/ 4 * sizeof(void*); |
| 421 | } |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 422 | } |
| 423 | return size; |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 424 | } |
| 425 | |
David Srbecky | 1ed4515 | 2019-04-09 18:10:26 +0100 | [diff] [blame^] | 426 | Mutex* GetNativeDebugInfoLock() { |
| 427 | return &g_jit_debug_lock; |
| 428 | } |
| 429 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 430 | } // namespace art |