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 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 19 | #include "base/logging.h" |
| 20 | #include "base/mutex.h" |
| 21 | #include "thread-inl.h" |
| 22 | #include "thread.h" |
| 23 | |
| 24 | #include <unordered_map> |
| 25 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 26 | namespace art { |
| 27 | |
| 28 | // ------------------------------------------------------------------- |
| 29 | // Binary GDB JIT Interface as described in |
| 30 | // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 31 | // ------------------------------------------------------------------- |
| 32 | extern "C" { |
| 33 | typedef enum { |
| 34 | JIT_NOACTION = 0, |
| 35 | JIT_REGISTER_FN, |
| 36 | JIT_UNREGISTER_FN |
| 37 | } JITAction; |
| 38 | |
| 39 | struct JITCodeEntry { |
| 40 | JITCodeEntry* next_; |
| 41 | JITCodeEntry* prev_; |
| 42 | const uint8_t *symfile_addr_; |
| 43 | uint64_t symfile_size_; |
| 44 | }; |
| 45 | |
| 46 | struct JITDescriptor { |
| 47 | uint32_t version_; |
| 48 | uint32_t action_flag_; |
| 49 | JITCodeEntry* relevant_entry_; |
| 50 | JITCodeEntry* first_entry_; |
| 51 | }; |
| 52 | |
| 53 | // GDB will place breakpoint into this function. |
| 54 | // To prevent GCC from inlining or removing it we place noinline attribute |
| 55 | // and inline assembler statement inside. |
| 56 | void __attribute__((noinline)) __jit_debug_register_code(); |
| 57 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 58 | __asm__(""); |
| 59 | } |
| 60 | |
| 61 | // GDB will inspect contents of this descriptor. |
| 62 | // Static initialization is necessary to prevent GDB from seeing |
| 63 | // uninitialized descriptor. |
| 64 | JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr }; |
| 65 | } |
| 66 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 67 | static Mutex g_jit_debug_mutex("JIT debug interface lock", kJitDebugInterfaceLock); |
| 68 | |
| 69 | static JITCodeEntry* CreateJITCodeEntryInternal( |
| 70 | std::unique_ptr<const uint8_t[]> symfile_addr, |
| 71 | uintptr_t symfile_size) |
| 72 | REQUIRES(g_jit_debug_mutex) { |
| 73 | DCHECK(symfile_addr.get() != nullptr); |
| 74 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 75 | JITCodeEntry* entry = new JITCodeEntry; |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 76 | entry->symfile_addr_ = symfile_addr.release(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 77 | entry->symfile_size_ = symfile_size; |
| 78 | entry->prev_ = nullptr; |
| 79 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 80 | entry->next_ = __jit_debug_descriptor.first_entry_; |
| 81 | if (entry->next_ != nullptr) { |
| 82 | entry->next_->prev_ = entry; |
| 83 | } |
| 84 | __jit_debug_descriptor.first_entry_ = entry; |
| 85 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 86 | |
| 87 | __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN; |
| 88 | __jit_debug_register_code(); |
| 89 | return entry; |
| 90 | } |
| 91 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 92 | static void DeleteJITCodeEntryInternal(JITCodeEntry* entry) REQUIRES(g_jit_debug_mutex) { |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 93 | if (entry->prev_ != nullptr) { |
| 94 | entry->prev_->next_ = entry->next_; |
| 95 | } else { |
| 96 | __jit_debug_descriptor.first_entry_ = entry->next_; |
| 97 | } |
| 98 | |
| 99 | if (entry->next_ != nullptr) { |
| 100 | entry->next_->prev_ = entry->prev_; |
| 101 | } |
| 102 | |
| 103 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 104 | __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN; |
| 105 | __jit_debug_register_code(); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 106 | delete[] entry->symfile_addr_; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 107 | delete entry; |
| 108 | } |
| 109 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 110 | JITCodeEntry* CreateJITCodeEntry(std::unique_ptr<const uint8_t[]> symfile_addr, |
| 111 | uintptr_t symfile_size) { |
| 112 | Thread* self = Thread::Current(); |
| 113 | MutexLock mu(self, g_jit_debug_mutex); |
| 114 | return CreateJITCodeEntryInternal(std::move(symfile_addr), symfile_size); |
| 115 | } |
| 116 | |
| 117 | void DeleteJITCodeEntry(JITCodeEntry* entry) { |
| 118 | Thread* self = Thread::Current(); |
| 119 | MutexLock mu(self, g_jit_debug_mutex); |
| 120 | DeleteJITCodeEntryInternal(entry); |
| 121 | } |
| 122 | |
| 123 | // Mapping from address to entry. It takes ownership of the entries |
| 124 | // so that the user of the JIT interface does not have to store them. |
| 125 | static std::unordered_map<uintptr_t, JITCodeEntry*> g_jit_code_entries; |
| 126 | |
| 127 | void CreateJITCodeEntryForAddress(uintptr_t address, |
| 128 | std::unique_ptr<const uint8_t[]> symfile_addr, |
| 129 | uintptr_t symfile_size) { |
| 130 | Thread* self = Thread::Current(); |
| 131 | MutexLock mu(self, g_jit_debug_mutex); |
| 132 | DCHECK_NE(address, 0u); |
| 133 | DCHECK(g_jit_code_entries.find(address) == g_jit_code_entries.end()); |
| 134 | JITCodeEntry* entry = CreateJITCodeEntryInternal(std::move(symfile_addr), symfile_size); |
| 135 | g_jit_code_entries.emplace(address, entry); |
| 136 | } |
| 137 | |
| 138 | bool DeleteJITCodeEntryForAddress(uintptr_t address) { |
| 139 | Thread* self = Thread::Current(); |
| 140 | MutexLock mu(self, g_jit_debug_mutex); |
| 141 | const auto& it = g_jit_code_entries.find(address); |
| 142 | if (it == g_jit_code_entries.end()) { |
| 143 | return false; |
| 144 | } |
| 145 | DeleteJITCodeEntryInternal(it->second); |
| 146 | g_jit_code_entries.erase(it); |
| 147 | return true; |
| 148 | } |
| 149 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 150 | } // namespace art |