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 | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 21 | #include "base/mutex.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 22 | #include "thread-current-inl.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 23 | #include "thread.h" |
| 24 | |
| 25 | #include <unordered_map> |
| 26 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 27 | namespace art { |
| 28 | |
| 29 | // ------------------------------------------------------------------- |
| 30 | // Binary GDB JIT Interface as described in |
| 31 | // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 32 | // ------------------------------------------------------------------- |
| 33 | extern "C" { |
| 34 | typedef enum { |
| 35 | JIT_NOACTION = 0, |
| 36 | JIT_REGISTER_FN, |
| 37 | JIT_UNREGISTER_FN |
| 38 | } JITAction; |
| 39 | |
| 40 | struct JITCodeEntry { |
| 41 | JITCodeEntry* next_; |
| 42 | JITCodeEntry* prev_; |
| 43 | const uint8_t *symfile_addr_; |
| 44 | uint64_t symfile_size_; |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 45 | uint32_t ref_count; // ART internal field. |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | struct JITDescriptor { |
| 49 | uint32_t version_; |
| 50 | uint32_t action_flag_; |
| 51 | JITCodeEntry* relevant_entry_; |
| 52 | JITCodeEntry* first_entry_; |
| 53 | }; |
| 54 | |
| 55 | // GDB will place breakpoint into this function. |
| 56 | // To prevent GCC from inlining or removing it we place noinline attribute |
| 57 | // and inline assembler statement inside. |
| 58 | void __attribute__((noinline)) __jit_debug_register_code(); |
| 59 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 60 | __asm__(""); |
| 61 | } |
| 62 | |
David Srbecky | e8b4e85 | 2016-03-15 17:02:41 +0000 | [diff] [blame] | 63 | // Call __jit_debug_register_code indirectly via global variable. |
| 64 | // This gives the debugger an easy way to inject custom code to handle the events. |
| 65 | void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code; |
| 66 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 67 | // GDB will inspect contents of this descriptor. |
| 68 | // Static initialization is necessary to prevent GDB from seeing |
| 69 | // uninitialized descriptor. |
| 70 | JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr }; |
| 71 | } |
| 72 | |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 73 | Mutex g_jit_debug_mutex("JIT debug interface lock", kJitDebugInterfaceLock); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 74 | |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 75 | static size_t g_jit_debug_mem_usage = 0; |
| 76 | |
| 77 | JITCodeEntry* CreateJITCodeEntry(const std::vector<uint8_t>& symfile) { |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 78 | DCHECK_NE(symfile.size(), 0u); |
| 79 | |
| 80 | // Make a copy of the buffer. We want to shrink it anyway. |
| 81 | uint8_t* symfile_copy = new uint8_t[symfile.size()]; |
| 82 | CHECK(symfile_copy != nullptr); |
| 83 | memcpy(symfile_copy, symfile.data(), symfile.size()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 84 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 85 | JITCodeEntry* entry = new JITCodeEntry; |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 86 | CHECK(entry != nullptr); |
| 87 | entry->symfile_addr_ = symfile_copy; |
| 88 | entry->symfile_size_ = symfile.size(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 89 | entry->prev_ = nullptr; |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 90 | entry->ref_count = 0; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 91 | entry->next_ = __jit_debug_descriptor.first_entry_; |
| 92 | if (entry->next_ != nullptr) { |
| 93 | entry->next_->prev_ = entry; |
| 94 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 95 | g_jit_debug_mem_usage += sizeof(JITCodeEntry) + entry->symfile_size_; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 96 | __jit_debug_descriptor.first_entry_ = entry; |
| 97 | __jit_debug_descriptor.relevant_entry_ = entry; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 98 | __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN; |
David Srbecky | e8b4e85 | 2016-03-15 17:02:41 +0000 | [diff] [blame] | 99 | (*__jit_debug_register_code_ptr)(); |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 100 | return entry; |
| 101 | } |
| 102 | |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 103 | void DeleteJITCodeEntry(JITCodeEntry* entry) { |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 104 | if (entry->prev_ != nullptr) { |
| 105 | entry->prev_->next_ = entry->next_; |
| 106 | } else { |
| 107 | __jit_debug_descriptor.first_entry_ = entry->next_; |
| 108 | } |
| 109 | |
| 110 | if (entry->next_ != nullptr) { |
| 111 | entry->next_->prev_ = entry->prev_; |
| 112 | } |
| 113 | |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 114 | g_jit_debug_mem_usage -= sizeof(JITCodeEntry) + entry->symfile_size_; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 115 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 116 | __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN; |
David Srbecky | e8b4e85 | 2016-03-15 17:02:41 +0000 | [diff] [blame] | 117 | (*__jit_debug_register_code_ptr)(); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 118 | delete[] entry->symfile_addr_; |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 119 | delete entry; |
| 120 | } |
| 121 | |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 122 | // Mapping from code address to entry. Used to manage life-time of the entries. |
| 123 | static std::unordered_map<uintptr_t, JITCodeEntry*> g_jit_code_entries |
| 124 | GUARDED_BY(g_jit_debug_mutex); |
| 125 | |
| 126 | void IncrementJITCodeEntryRefcount(JITCodeEntry* entry, uintptr_t code_address) { |
| 127 | DCHECK(entry != nullptr); |
| 128 | DCHECK_EQ(g_jit_code_entries.count(code_address), 0u); |
| 129 | entry->ref_count++; |
| 130 | g_jit_code_entries.emplace(code_address, entry); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 131 | } |
| 132 | |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 133 | void DecrementJITCodeEntryRefcount(JITCodeEntry* entry, uintptr_t code_address) { |
| 134 | DCHECK(entry != nullptr); |
| 135 | DCHECK(g_jit_code_entries[code_address] == entry); |
| 136 | if (--entry->ref_count == 0) { |
| 137 | DeleteJITCodeEntry(entry); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 138 | } |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 139 | g_jit_code_entries.erase(code_address); |
| 140 | } |
| 141 | |
| 142 | JITCodeEntry* GetJITCodeEntry(uintptr_t code_address) { |
| 143 | auto it = g_jit_code_entries.find(code_address); |
| 144 | return it == g_jit_code_entries.end() ? nullptr : it->second; |
| 145 | } |
| 146 | |
| 147 | size_t GetJITCodeEntryMemUsage() { |
| 148 | return g_jit_debug_mem_usage + g_jit_code_entries.size() * 2 * sizeof(void*); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 149 | } |
| 150 | |
David Srbecky | 67feb17 | 2015-12-17 19:57:44 +0000 | [diff] [blame] | 151 | } // namespace art |