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 | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 34 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 35 | // |
| 36 | // Debug interface for native tools (gdb, lldb, libunwind, simpleperf). |
| 37 | // |
| 38 | // See http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 39 | // |
| 40 | // There are two ways for native tools to access the debug data safely: |
| 41 | // |
| 42 | // 1) Synchronously, by setting a breakpoint in the __*_debug_register_code |
| 43 | // method, which is called after every modification of the linked list. |
| 44 | // GDB does this, but it is complex to set up and it stops the process. |
| 45 | // |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 46 | // 2) Asynchronously, by monitoring the action_seqlock_. |
| 47 | // * The seqlock is a monotonically increasing counter which is incremented |
| 48 | // before and after every modification of the linked list. Odd value of |
| 49 | // the counter means the linked list is being modified (it is locked). |
| 50 | // * The tool should read the value of the seqlock both before and after |
| 51 | // copying the linked list. If the seqlock values match and are even, |
| 52 | // the copy is consistent. Otherwise, the reader should try again. |
| 53 | // * Note that using the data directly while is it being modified |
| 54 | // might crash the tool. Therefore, the only safe way is to make |
| 55 | // a copy and use the copy only after the seqlock has been checked. |
| 56 | // * Note that the process might even free and munmap the data while |
| 57 | // it is being copied, therefore the reader should either handle |
| 58 | // SEGV or use OS calls to read the memory (e.g. process_vm_readv). |
| 59 | // * The seqlock can be used to determine the number of modifications of |
| 60 | // the linked list, which can be used to intelligently cache the data. |
| 61 | // Note the possible overflow of the seqlock. It is intentionally |
| 62 | // 32-bit, since 64-bit atomics can be tricky on some architectures. |
| 63 | // * The timestamps on the entry record the time when the entry was |
| 64 | // created which is relevant if the unwinding is not live and is |
| 65 | // postponed until much later. All timestamps must be unique. |
| 66 | // * Memory barriers are used to make it possible to reason about |
| 67 | // the data even when it is being modified (e.g. the process crashed |
| 68 | // while that data was locked, and thus it will be never unlocked). |
| 69 | // * In particular, it should be possible to: |
| 70 | // 1) read the seqlock and then the linked list head pointer. |
| 71 | // 2) copy the entry and check that seqlock has not changed. |
| 72 | // 3) copy the symfile and check that seqlock has not changed. |
| 73 | // 4) go back to step 2 using the next pointer (if non-null). |
| 74 | // This safely creates copy of all symfiles, although other data |
| 75 | // might be inconsistent/unusable (e.g. prev_, action_timestamp_). |
| 76 | // * For full conformance with the C++ memory model, all seqlock |
| 77 | // protected accesses should be atomic. We currently do this in the |
| 78 | // more critical cases. The rest will have to be fixed before |
| 79 | // attempting to run TSAN on this code. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 80 | // |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 81 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 82 | namespace art { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 83 | |
| 84 | static Mutex g_jit_debug_lock("JIT native debug entries", kNativeDebugInterfaceLock); |
| 85 | static Mutex g_dex_debug_lock("DEX native debug entries", kNativeDebugInterfaceLock); |
| 86 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 87 | extern "C" { |
Andreas Gampe | c55bb39 | 2018-09-21 00:02:02 +0000 | [diff] [blame] | 88 | enum JITAction { |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 89 | JIT_NOACTION = 0, |
| 90 | JIT_REGISTER_FN, |
| 91 | JIT_UNREGISTER_FN |
Andreas Gampe | c55bb39 | 2018-09-21 00:02:02 +0000 | [diff] [blame] | 92 | }; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 93 | |
| 94 | struct JITCodeEntry { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 95 | // Atomic to ensure the reader can always iterate over the linked list |
| 96 | // (e.g. the process could crash in the middle of writing this field). |
| 97 | std::atomic<JITCodeEntry*> next_; |
| 98 | // 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] | 99 | JITCodeEntry* prev_; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 100 | const uint8_t* symfile_addr_; |
| 101 | uint64_t symfile_size_; // Beware of the offset (12 on x86; but 16 on ARM32). |
| 102 | |
| 103 | // Android-specific fields: |
| 104 | uint64_t register_timestamp_; // CLOCK_MONOTONIC time of entry registration. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct JITDescriptor { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 108 | uint32_t version_ = 1; // NB: GDB supports only version 1. |
| 109 | uint32_t action_flag_ = JIT_NOACTION; // One of the JITAction enum values. |
| 110 | JITCodeEntry* relevant_entry_ = nullptr; // The entry affected by the action. |
| 111 | std::atomic<JITCodeEntry*> head_{nullptr}; // Head of link list of all entries. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 112 | |
| 113 | // Android-specific fields: |
| 114 | uint8_t magic_[8] = {'A', 'n', 'd', 'r', 'o', 'i', 'd', '1'}; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 115 | uint32_t flags_ = 0; // Reserved for future use. Must be 0. |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 116 | uint32_t sizeof_descriptor = sizeof(JITDescriptor); |
| 117 | uint32_t sizeof_entry = sizeof(JITCodeEntry); |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 118 | std::atomic_uint32_t action_seqlock_{0}; // Incremented before and after any modification. |
| 119 | uint64_t action_timestamp_ = 1; // CLOCK_MONOTONIC time of last action. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 122 | // Check that std::atomic has the expected layout. |
| 123 | static_assert(alignof(std::atomic_uint32_t) == alignof(uint32_t), "Weird alignment"); |
| 124 | static_assert(sizeof(std::atomic_uint32_t) == sizeof(uint32_t), "Weird size"); |
| 125 | static_assert(alignof(std::atomic<void*>) == alignof(void*), "Weird alignment"); |
| 126 | static_assert(sizeof(std::atomic<void*>) == sizeof(void*), "Weird size"); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 127 | |
| 128 | // 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] | 129 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 130 | __asm__(""); |
| 131 | } |
| 132 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 133 | // Alternatively, native tools may overwrite this field to execute custom handler. |
David Srbecky | e8b4e85 | 2016-03-15 17:02:41 +0000 | [diff] [blame] | 134 | void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code; |
| 135 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 136 | // The root data structure describing of all JITed methods. |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 137 | JITDescriptor __jit_debug_descriptor GUARDED_BY(g_jit_debug_lock) {}; |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 138 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 139 | // The following globals mirror the ones above, but are used to register dex files. |
| 140 | void __attribute__((noinline)) __dex_debug_register_code() { |
| 141 | __asm__(""); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 142 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 143 | void (*__dex_debug_register_code_ptr)() = __dex_debug_register_code; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 144 | JITDescriptor __dex_debug_descriptor GUARDED_BY(g_dex_debug_lock) {}; |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 145 | } |
| 146 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 147 | // Mark the descriptor as "locked", so native tools know the data is being modified. |
| 148 | static void ActionSeqlock(JITDescriptor& descriptor) { |
| 149 | DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 0u) << "Already locked"; |
| 150 | descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed); |
| 151 | // Ensure that any writes within the locked section cannot be reordered before the increment. |
| 152 | std::atomic_thread_fence(std::memory_order_release); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 153 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 154 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 155 | // 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] | 156 | static void ActionSequnlock(JITDescriptor& descriptor) { |
| 157 | DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 1u) << "Already unlocked"; |
| 158 | // Ensure that any writes within the locked section cannot be reordered after the increment. |
| 159 | std::atomic_thread_fence(std::memory_order_release); |
| 160 | descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 161 | } |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 162 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 163 | static JITCodeEntry* CreateJITCodeEntryInternal( |
| 164 | JITDescriptor& descriptor, |
| 165 | void (*register_code_ptr)(), |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 166 | ArrayRef<const uint8_t> symfile, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 167 | bool copy_symfile) { |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 168 | // Make a copy of the buffer to shrink it and to pass ownership to JITCodeEntry. |
| 169 | if (copy_symfile) { |
| 170 | uint8_t* copy = new uint8_t[symfile.size()]; |
| 171 | CHECK(copy != nullptr); |
| 172 | memcpy(copy, symfile.data(), symfile.size()); |
| 173 | symfile = ArrayRef<const uint8_t>(copy, symfile.size()); |
| 174 | } |
| 175 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 176 | // Ensure the timestamp is monotonically increasing even in presence of low |
| 177 | // granularity system timer. This ensures each entry has unique timestamp. |
| 178 | uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 179 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 180 | JITCodeEntry* head = descriptor.head_.load(std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 181 | JITCodeEntry* entry = new JITCodeEntry; |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 182 | CHECK(entry != nullptr); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 183 | entry->symfile_addr_ = symfile.data(); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 184 | entry->symfile_size_ = symfile.size(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 185 | entry->prev_ = nullptr; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 186 | entry->next_.store(head, std::memory_order_relaxed); |
| 187 | entry->register_timestamp_ = timestamp; |
| 188 | |
| 189 | // We are going to modify the linked list, so take the seqlock. |
| 190 | ActionSeqlock(descriptor); |
| 191 | if (head != nullptr) { |
| 192 | head->prev_ = entry; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 193 | } |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 194 | descriptor.head_.store(entry, std::memory_order_relaxed); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 195 | descriptor.relevant_entry_ = entry; |
| 196 | descriptor.action_flag_ = JIT_REGISTER_FN; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 197 | descriptor.action_timestamp_ = timestamp; |
| 198 | ActionSequnlock(descriptor); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 199 | |
| 200 | (*register_code_ptr)(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 201 | return entry; |
| 202 | } |
| 203 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 204 | static void DeleteJITCodeEntryInternal( |
| 205 | JITDescriptor& descriptor, |
| 206 | void (*register_code_ptr)(), |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 207 | JITCodeEntry* entry, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 208 | bool free_symfile) { |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 209 | CHECK(entry != nullptr); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 210 | const uint8_t* symfile = entry->symfile_addr_; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 211 | |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 212 | // Ensure the timestamp is monotonically increasing even in presence of low |
| 213 | // granularity system timer. This ensures each entry has unique timestamp. |
| 214 | uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime()); |
| 215 | |
| 216 | // We are going to modify the linked list, so take the seqlock. |
| 217 | ActionSeqlock(descriptor); |
| 218 | JITCodeEntry* next = entry->next_.load(std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 219 | if (entry->prev_ != nullptr) { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 220 | entry->prev_->next_.store(next, std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 221 | } else { |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 222 | descriptor.head_.store(next, std::memory_order_relaxed); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 223 | } |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 224 | if (next != nullptr) { |
| 225 | next->prev_ = entry->prev_; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 226 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 227 | descriptor.relevant_entry_ = entry; |
| 228 | descriptor.action_flag_ = JIT_UNREGISTER_FN; |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 229 | descriptor.action_timestamp_ = timestamp; |
| 230 | ActionSequnlock(descriptor); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 231 | |
| 232 | (*register_code_ptr)(); |
David Srbecky | d767f2d | 2018-02-26 16:18:40 +0000 | [diff] [blame] | 233 | |
| 234 | // Ensure that clear below can not be reordered above the unlock above. |
| 235 | std::atomic_thread_fence(std::memory_order_release); |
| 236 | |
| 237 | // Aggressively clear the entry as an extra check of the synchronisation. |
| 238 | memset(entry, 0, sizeof(*entry)); |
| 239 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 240 | delete entry; |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 241 | if (free_symfile) { |
| 242 | delete[] symfile; |
| 243 | } |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 244 | } |
| 245 | |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 246 | 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] | 247 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 248 | void AddNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 249 | MutexLock mu(self, g_dex_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 250 | DCHECK(dexfile != nullptr); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 251 | // 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] | 252 | if (g_dex_debug_entries.count(dexfile) == 0) { |
| 253 | const ArrayRef<const uint8_t> symfile(dexfile->Begin(), dexfile->Size()); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 254 | JITCodeEntry* entry = CreateJITCodeEntryInternal(__dex_debug_descriptor, |
| 255 | __dex_debug_register_code_ptr, |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 256 | symfile, |
| 257 | /*copy_symfile=*/ false); |
| 258 | g_dex_debug_entries.emplace(dexfile, entry); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 259 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 260 | } |
| 261 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 262 | void RemoveNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 263 | MutexLock mu(self, g_dex_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 264 | auto it = g_dex_debug_entries.find(dexfile); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 265 | // We register dex files in the class linker and free them in DexFile_closeDexFile, but |
| 266 | // 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] | 267 | if (it != g_dex_debug_entries.end()) { |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 268 | DeleteJITCodeEntryInternal(__dex_debug_descriptor, |
| 269 | __dex_debug_register_code_ptr, |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 270 | /*entry=*/ it->second, |
| 271 | /*free_symfile=*/ false); |
| 272 | g_dex_debug_entries.erase(it); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 273 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 274 | } |
| 275 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 276 | // Mapping from handle to entry. Used to manage life-time of the entries. |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 277 | static std::map<const void*, JITCodeEntry*> g_jit_debug_entries GUARDED_BY(g_jit_debug_lock); |
| 278 | |
| 279 | // Number of entries added since last packing. Used to pack entries in bulk. |
| 280 | static size_t g_jit_num_unpacked_entries GUARDED_BY(g_jit_debug_lock) = 0; |
| 281 | |
| 282 | // We postpone removal so that it is done in bulk. |
| 283 | static std::deque<const void*> g_jit_removed_entries GUARDED_BY(g_jit_debug_lock); |
| 284 | |
| 285 | // Split the JIT code cache into groups of fixed size and create singe JITCodeEntry for each group. |
| 286 | // The start address of method's code determines which group it belongs to. The end is irrelevant. |
| 287 | // As a consequnce, newly added mini debug infos will be merged and old ones (GCed) will be pruned. |
| 288 | static void MaybePackJitMiniDebugInfo(PackElfFileForJITFunction pack, |
| 289 | InstructionSet isa, |
| 290 | const InstructionSetFeatures* features) |
| 291 | REQUIRES(g_jit_debug_lock) { |
| 292 | // Size of memory range covered by each JITCodeEntry. |
| 293 | // The number of methods per entry is variable (depending on how many fit in that range). |
| 294 | constexpr uint32_t kGroupSize = 64 * KB; |
| 295 | // Even if there are no removed entries, we want to pack new entries on regular basis. |
| 296 | constexpr uint32_t kPackFrequency = 64; |
| 297 | |
| 298 | std::deque<const void*>& removed_entries = g_jit_removed_entries; |
| 299 | std::sort(removed_entries.begin(), removed_entries.end()); |
| 300 | if (removed_entries.empty() && g_jit_num_unpacked_entries < kPackFrequency) { |
| 301 | return; // Nothing to do. |
| 302 | } |
| 303 | |
| 304 | std::vector<const uint8_t*> added_elf_files; |
| 305 | std::vector<const void*> removed_symbols; |
| 306 | auto added_it = g_jit_debug_entries.begin(); |
| 307 | auto removed_it = removed_entries.begin(); |
| 308 | while (added_it != g_jit_debug_entries.end()) { |
| 309 | // Collect all entries that have been added or removed within our memory range. |
| 310 | const void* group_ptr = AlignDown(added_it->first, kGroupSize); |
| 311 | added_elf_files.clear(); |
| 312 | auto added_begin = added_it; |
| 313 | while (added_it != g_jit_debug_entries.end() && |
| 314 | AlignDown(added_it->first, kGroupSize) == group_ptr) { |
| 315 | added_elf_files.push_back((added_it++)->second->symfile_addr_); |
| 316 | } |
| 317 | removed_symbols.clear(); |
| 318 | while (removed_it != removed_entries.end() && |
| 319 | AlignDown(*removed_it, kGroupSize) == group_ptr) { |
| 320 | removed_symbols.push_back(*(removed_it++)); |
| 321 | } |
| 322 | |
| 323 | // Create new singe JITCodeEntry that covers this memory range. |
| 324 | if (added_elf_files.size() == 1 && removed_symbols.size() == 0) { |
| 325 | continue; // Nothing changed in this memory range. |
| 326 | } |
| 327 | uint64_t start_time = MilliTime(); |
| 328 | size_t symbols; |
| 329 | std::vector<uint8_t> packed = pack(isa, features, added_elf_files, removed_symbols, &symbols); |
| 330 | VLOG(jit) |
| 331 | << "JIT mini-debug-info packed" |
| 332 | << " for " << group_ptr |
| 333 | << " in " << MilliTime() - start_time << "ms" |
| 334 | << " files=" << added_elf_files.size() |
| 335 | << " removed=" << removed_symbols.size() |
| 336 | << " symbols=" << symbols |
| 337 | << " size=" << PrettySize(packed.size()); |
| 338 | |
| 339 | // Replace the old entries with the new one (with their lifetime temporally overlapping). |
| 340 | JITCodeEntry* packed_entry = CreateJITCodeEntryInternal( |
| 341 | __jit_debug_descriptor, |
| 342 | __jit_debug_register_code_ptr, |
| 343 | ArrayRef<const uint8_t>(packed), |
| 344 | /*copy_symfile=*/ true); |
| 345 | for (auto it = added_begin; it != added_it; ++it) { |
| 346 | DeleteJITCodeEntryInternal(__jit_debug_descriptor, |
| 347 | __jit_debug_register_code_ptr, |
| 348 | /*entry=*/ it->second, |
| 349 | /*free_symfile=*/ true); |
| 350 | } |
| 351 | g_jit_debug_entries.erase(added_begin, added_it); |
| 352 | g_jit_debug_entries.emplace(group_ptr, packed_entry); |
| 353 | } |
| 354 | CHECK(added_it == g_jit_debug_entries.end()); |
| 355 | CHECK(removed_it == removed_entries.end()); |
| 356 | removed_entries.clear(); |
| 357 | g_jit_num_unpacked_entries = 0; |
| 358 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 359 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 360 | void AddNativeDebugInfoForJit(Thread* self, |
| 361 | const void* code_ptr, |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 362 | const std::vector<uint8_t>& symfile, |
| 363 | PackElfFileForJITFunction pack, |
| 364 | InstructionSet isa, |
| 365 | const InstructionSetFeatures* features) { |
| 366 | MutexLock mu(self, g_jit_debug_lock); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 367 | DCHECK_NE(symfile.size(), 0u); |
| 368 | |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 369 | MaybePackJitMiniDebugInfo(pack, isa, features); |
| 370 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 371 | JITCodeEntry* entry = CreateJITCodeEntryInternal( |
| 372 | __jit_debug_descriptor, |
| 373 | __jit_debug_register_code_ptr, |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 374 | ArrayRef<const uint8_t>(symfile), |
| 375 | /*copy_symfile=*/ true); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 376 | |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 377 | VLOG(jit) |
| 378 | << "JIT mini-debug-info added" |
| 379 | << " for " << code_ptr |
| 380 | << " size=" << PrettySize(symfile.size()); |
| 381 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 382 | // 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] | 383 | // (this only happens when --generate-debug-info flag is enabled for the purpose |
| 384 | // of being debugged with gdb; it does not happen for debuggable apps by default). |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 385 | if (code_ptr != nullptr) { |
| 386 | bool ok = g_jit_debug_entries.emplace(code_ptr, entry).second; |
| 387 | DCHECK(ok) << "Native debug entry already exists for " << std::hex << code_ptr; |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 388 | // Count how many entries we have added since the last mini-debug-info packing. |
| 389 | // We avoid g_jit_debug_entries.size() here because it can shrink during packing. |
| 390 | g_jit_num_unpacked_entries++; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 394 | void RemoveNativeDebugInfoForJit(Thread* self, const void* code_ptr) { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 395 | MutexLock mu(self, g_jit_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 396 | // We generate JIT native debug info only if the right runtime flags are enabled, |
| 397 | // but we try to remove it unconditionally whenever code is freed from JIT cache. |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 398 | if (!g_jit_debug_entries.empty()) { |
| 399 | g_jit_removed_entries.push_back(code_ptr); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | size_t GetJitMiniDebugInfoMemUsage() { |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame^] | 404 | MutexLock mu(Thread::Current(), g_jit_debug_lock); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 405 | size_t size = 0; |
| 406 | for (auto entry : g_jit_debug_entries) { |
| 407 | size += sizeof(JITCodeEntry) + entry.second->symfile_size_ + /*map entry*/ 4 * sizeof(void*); |
| 408 | } |
| 409 | return size; |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 410 | } |
| 411 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 412 | } // namespace art |