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 | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 22 | #include "base/mutex.h" |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 23 | #include "base/time_utils.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 24 | #include "thread-current-inl.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 25 | #include "thread.h" |
| 26 | |
| 27 | #include <unordered_map> |
| 28 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 29 | // |
| 30 | // Debug interface for native tools (gdb, lldb, libunwind, simpleperf). |
| 31 | // |
| 32 | // See http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 33 | // |
| 34 | // There are two ways for native tools to access the debug data safely: |
| 35 | // |
| 36 | // 1) Synchronously, by setting a breakpoint in the __*_debug_register_code |
| 37 | // method, which is called after every modification of the linked list. |
| 38 | // GDB does this, but it is complex to set up and it stops the process. |
| 39 | // |
| 40 | // 2) Asynchronously, by monitoring the action_counter_, which is incremented |
| 41 | // on every modification of the linked list and kept at -1 during updates. |
| 42 | // Therefore, if the tool process reads the counter both before and after |
| 43 | // iterating over the linked list, and the counters match and are not -1, |
| 44 | // the tool process can be sure the list was not modified during the read. |
| 45 | // Obviously, it can also cache the data and use the counter to determine |
| 46 | // if the cache is up to date, or to intelligently update it if needed. |
| 47 | // |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 48 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 49 | namespace art { |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 50 | extern "C" { |
| 51 | typedef enum { |
| 52 | JIT_NOACTION = 0, |
| 53 | JIT_REGISTER_FN, |
| 54 | JIT_UNREGISTER_FN |
| 55 | } JITAction; |
| 56 | |
| 57 | struct JITCodeEntry { |
| 58 | JITCodeEntry* next_; |
| 59 | JITCodeEntry* prev_; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 60 | const uint8_t* symfile_addr_; |
| 61 | uint64_t symfile_size_; // Beware of the offset (12 on x86; but 16 on ARM32). |
| 62 | |
| 63 | // Android-specific fields: |
| 64 | uint64_t register_timestamp_; // CLOCK_MONOTONIC time of entry registration. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct JITDescriptor { |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 68 | uint32_t version_ = 1; // NB: GDB supports only version 1. |
| 69 | uint32_t action_flag_ = JIT_NOACTION; // One of the JITAction enum values. |
| 70 | JITCodeEntry* relevant_entry_ = nullptr; // The entry affected by the action. |
| 71 | JITCodeEntry* first_entry_ = nullptr; // Head of link list of all entries. |
| 72 | |
| 73 | // Android-specific fields: |
| 74 | uint8_t magic_[8] = {'A', 'n', 'd', 'r', 'o', 'i', 'd', '1'}; |
| 75 | uint32_t flags_ = 0; // Reserved for future use. Must be 0. |
| 76 | uint32_t sizeof_descriptor = sizeof(JITDescriptor); |
| 77 | uint32_t sizeof_entry = sizeof(JITCodeEntry); |
| 78 | std::atomic_int32_t action_counter_; // Number of actions, or -1 if locked. |
| 79 | // It can overflow from INT32_MAX to 0. |
| 80 | uint64_t action_timestamp_ = 1; // CLOCK_MONOTONIC time of last action. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 83 | // Check that std::atomic_int32_t has the same layout as int32_t. |
| 84 | static_assert(alignof(std::atomic_int32_t) == alignof(int32_t), "Weird alignment"); |
| 85 | static_assert(sizeof(std::atomic_int32_t) == sizeof(int32_t), "Weird size"); |
| 86 | |
| 87 | // 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] | 88 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 89 | __asm__(""); |
| 90 | } |
| 91 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 92 | // Alternatively, native tools may overwrite this field to execute custom handler. |
David Srbecky | e8b4e85 | 2016-03-15 17:02:41 +0000 | [diff] [blame] | 93 | void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code; |
| 94 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 95 | // The root data structure describing of all JITed methods. |
| 96 | JITDescriptor __jit_debug_descriptor {}; |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 97 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 98 | // The following globals mirror the ones above, but are used to register dex files. |
| 99 | void __attribute__((noinline)) __dex_debug_register_code() { |
| 100 | __asm__(""); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 101 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 102 | void (*__dex_debug_register_code_ptr)() = __dex_debug_register_code; |
| 103 | JITDescriptor __dex_debug_descriptor {}; |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 104 | } |
| 105 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 106 | // Mark the descriptor as "locked", so native tools know the data is unstable. |
| 107 | // Returns the old value of the counter. |
| 108 | static int32_t LockActionCounter(JITDescriptor& descriptor) { |
| 109 | return descriptor.action_counter_.exchange(-1); |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 110 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 111 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 112 | // Mark the descriptor as "unlocked", so native tools know the data is safe to read. |
| 113 | // It will also increment the value so that the tools know the data has changed. |
| 114 | static void UnlockActionCounter(JITDescriptor& descriptor, int32_t old_value) { |
| 115 | int32_t new_value = (old_value + 1) & 0x7FFFFFFF; // Handle overflow to avoid -1. |
| 116 | descriptor.action_counter_.store(new_value); |
| 117 | } |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 118 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 119 | static JITCodeEntry* CreateJITCodeEntryInternal( |
| 120 | JITDescriptor& descriptor, |
| 121 | void (*register_code_ptr)(), |
| 122 | const ArrayRef<const uint8_t>& symfile) |
| 123 | REQUIRES(Locks::native_debug_interface_lock_) { |
| 124 | int32_t old_action_counter = LockActionCounter(descriptor); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 125 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 126 | JITCodeEntry* entry = new JITCodeEntry; |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 127 | CHECK(entry != nullptr); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 128 | entry->symfile_addr_ = symfile.data(); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 129 | entry->symfile_size_ = symfile.size(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 130 | entry->prev_ = nullptr; |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 131 | entry->next_ = descriptor.first_entry_; |
| 132 | entry->register_timestamp_ = NanoTime(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 133 | if (entry->next_ != nullptr) { |
| 134 | entry->next_->prev_ = entry; |
| 135 | } |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 136 | descriptor.first_entry_ = entry; |
| 137 | descriptor.relevant_entry_ = entry; |
| 138 | descriptor.action_flag_ = JIT_REGISTER_FN; |
| 139 | descriptor.action_timestamp_ = entry->register_timestamp_; |
| 140 | UnlockActionCounter(descriptor, old_action_counter); |
| 141 | |
| 142 | (*register_code_ptr)(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 143 | return entry; |
| 144 | } |
| 145 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 146 | static void DeleteJITCodeEntryInternal( |
| 147 | JITDescriptor& descriptor, |
| 148 | void (*register_code_ptr)(), |
| 149 | JITCodeEntry* entry) |
| 150 | REQUIRES(Locks::native_debug_interface_lock_) { |
| 151 | CHECK(entry != nullptr); |
| 152 | int32_t old_action_counter = LockActionCounter(descriptor); |
| 153 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 154 | if (entry->prev_ != nullptr) { |
| 155 | entry->prev_->next_ = entry->next_; |
| 156 | } else { |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 157 | descriptor.first_entry_ = entry->next_; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 158 | } |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 159 | if (entry->next_ != nullptr) { |
| 160 | entry->next_->prev_ = entry->prev_; |
| 161 | } |
| 162 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 163 | descriptor.relevant_entry_ = entry; |
| 164 | descriptor.action_flag_ = JIT_UNREGISTER_FN; |
| 165 | descriptor.action_timestamp_ = NanoTime(); |
| 166 | UnlockActionCounter(descriptor, old_action_counter); |
| 167 | |
| 168 | (*register_code_ptr)(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 169 | delete entry; |
| 170 | } |
| 171 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 172 | static std::unordered_map<const void*, JITCodeEntry*> __dex_debug_entries |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 173 | GUARDED_BY(Locks::native_debug_interface_lock_); |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 174 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 175 | void AddNativeDebugInfoForDex(Thread* current_thread, ArrayRef<const uint8_t> dexfile) { |
| 176 | MutexLock mu(current_thread, *Locks::native_debug_interface_lock_); |
| 177 | DCHECK(dexfile.data() != nullptr); |
| 178 | // This is just defensive check. The class linker should not register the dex file twice. |
| 179 | if (__dex_debug_entries.count(dexfile.data()) == 0) { |
| 180 | JITCodeEntry* entry = CreateJITCodeEntryInternal(__dex_debug_descriptor, |
| 181 | __dex_debug_register_code_ptr, |
| 182 | dexfile); |
| 183 | __dex_debug_entries.emplace(dexfile.data(), entry); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 184 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 185 | } |
| 186 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 187 | void RemoveNativeDebugInfoForDex(Thread* current_thread, ArrayRef<const uint8_t> dexfile) { |
| 188 | MutexLock mu(current_thread, *Locks::native_debug_interface_lock_); |
| 189 | auto it = __dex_debug_entries.find(dexfile.data()); |
| 190 | // We register dex files in the class linker and free them in DexFile_closeDexFile, but |
| 191 | // there might be cases where we load the dex file without using it in the class linker. |
| 192 | if (it != __dex_debug_entries.end()) { |
| 193 | DeleteJITCodeEntryInternal(__dex_debug_descriptor, |
| 194 | __dex_debug_register_code_ptr, |
| 195 | it->second); |
| 196 | __dex_debug_entries.erase(it); |
| 197 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 198 | } |
| 199 | |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 200 | static size_t __jit_debug_mem_usage |
| 201 | GUARDED_BY(Locks::native_debug_interface_lock_) = 0; |
| 202 | |
| 203 | // Mapping from handle to entry. Used to manage life-time of the entries. |
| 204 | static std::unordered_map<const void*, JITCodeEntry*> __jit_debug_entries |
| 205 | GUARDED_BY(Locks::native_debug_interface_lock_); |
| 206 | |
| 207 | void AddNativeDebugInfoForJit(const void* handle, const std::vector<uint8_t>& symfile) { |
| 208 | DCHECK_NE(symfile.size(), 0u); |
| 209 | |
| 210 | // Make a copy of the buffer to shrink it and to pass ownership to JITCodeEntry. |
| 211 | uint8_t* copy = new uint8_t[symfile.size()]; |
| 212 | CHECK(copy != nullptr); |
| 213 | memcpy(copy, symfile.data(), symfile.size()); |
| 214 | |
| 215 | JITCodeEntry* entry = CreateJITCodeEntryInternal( |
| 216 | __jit_debug_descriptor, |
| 217 | __jit_debug_register_code_ptr, |
| 218 | ArrayRef<const uint8_t>(copy, symfile.size())); |
| 219 | __jit_debug_mem_usage += sizeof(JITCodeEntry) + entry->symfile_size_; |
| 220 | |
| 221 | // We don't provide handle for type debug info, which means we cannot free it later. |
| 222 | // (this only happens when --generate-debug-info flag is enabled for the purpose |
| 223 | // of being debugged with gdb; it does not happen for debuggable apps by default). |
| 224 | bool ok = handle == nullptr || __jit_debug_entries.emplace(handle, entry).second; |
| 225 | DCHECK(ok) << "Native debug entry already exists for " << std::hex << handle; |
| 226 | } |
| 227 | |
| 228 | void RemoveNativeDebugInfoForJit(const void* handle) { |
| 229 | auto it = __jit_debug_entries.find(handle); |
| 230 | // We generate JIT native debug info only if the right runtime flags are enabled, |
| 231 | // but we try to remove it unconditionally whenever code is freed from JIT cache. |
| 232 | if (it != __jit_debug_entries.end()) { |
| 233 | JITCodeEntry* entry = it->second; |
| 234 | const uint8_t* symfile_addr = entry->symfile_addr_; |
| 235 | uint64_t symfile_size = entry->symfile_size_; |
| 236 | DeleteJITCodeEntryInternal(__jit_debug_descriptor, |
| 237 | __jit_debug_register_code_ptr, |
| 238 | entry); |
| 239 | __jit_debug_entries.erase(it); |
| 240 | __jit_debug_mem_usage -= sizeof(JITCodeEntry) + symfile_size; |
| 241 | delete[] symfile_addr; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | size_t GetJitNativeDebugInfoMemUsage() { |
| 246 | return __jit_debug_mem_usage + __jit_debug_entries.size() * 2 * sizeof(void*); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 247 | } |
| 248 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 249 | } // namespace art |