blob: 505e626f679d8ffd34ea0075a705e38a1eeef077 [file] [log] [blame]
David Srbecky67feb172015-12-17 19:57:44 +00001/*
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 Gampe57943812017-12-06 21:39:13 -080019#include <android-base/logging.h>
20
David Srbecky440a9b32018-02-15 17:47:29 +000021#include "base/array_ref.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000022#include "base/mutex.h"
David Srbecky440a9b32018-02-15 17:47:29 +000023#include "base/time_utils.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070024#include "thread-current-inl.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000025#include "thread.h"
26
27#include <unordered_map>
28
David Srbecky440a9b32018-02-15 17:47:29 +000029//
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 Srbecky67feb172015-12-17 19:57:44 +000048
David Srbecky440a9b32018-02-15 17:47:29 +000049namespace art {
David Srbecky67feb172015-12-17 19:57:44 +000050extern "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 Srbecky440a9b32018-02-15 17:47:29 +000060 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 Srbecky67feb172015-12-17 19:57:44 +000065 };
66
67 struct JITDescriptor {
David Srbecky440a9b32018-02-15 17:47:29 +000068 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 Srbecky67feb172015-12-17 19:57:44 +000081 };
82
David Srbecky440a9b32018-02-15 17:47:29 +000083 // 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 Srbecky67feb172015-12-17 19:57:44 +000088 void __attribute__((noinline)) __jit_debug_register_code() {
89 __asm__("");
90 }
91
David Srbecky440a9b32018-02-15 17:47:29 +000092 // Alternatively, native tools may overwrite this field to execute custom handler.
David Srbeckye8b4e852016-03-15 17:02:41 +000093 void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code;
94
David Srbecky440a9b32018-02-15 17:47:29 +000095 // The root data structure describing of all JITed methods.
96 JITDescriptor __jit_debug_descriptor {};
David Srbeckyfb3de3d2018-01-29 16:11:49 +000097
David Srbecky440a9b32018-02-15 17:47:29 +000098 // 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 Srbeckyfb3de3d2018-01-29 16:11:49 +0000101 }
David Srbecky440a9b32018-02-15 17:47:29 +0000102 void (*__dex_debug_register_code_ptr)() = __dex_debug_register_code;
103 JITDescriptor __dex_debug_descriptor {};
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000104}
105
David Srbecky440a9b32018-02-15 17:47:29 +0000106// Mark the descriptor as "locked", so native tools know the data is unstable.
107// Returns the old value of the counter.
108static int32_t LockActionCounter(JITDescriptor& descriptor) {
109 return descriptor.action_counter_.exchange(-1);
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000110}
David Srbeckyc684f332018-01-19 17:38:06 +0000111
David Srbecky440a9b32018-02-15 17:47:29 +0000112// 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.
114static 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 Marko93205e32016-04-13 11:59:46 +0100118
David Srbecky440a9b32018-02-15 17:47:29 +0000119static 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 Srbecky5cc349f2015-12-18 15:04:48 +0000125
David Srbecky67feb172015-12-17 19:57:44 +0000126 JITCodeEntry* entry = new JITCodeEntry;
Vladimir Marko93205e32016-04-13 11:59:46 +0100127 CHECK(entry != nullptr);
David Srbecky440a9b32018-02-15 17:47:29 +0000128 entry->symfile_addr_ = symfile.data();
Vladimir Marko93205e32016-04-13 11:59:46 +0100129 entry->symfile_size_ = symfile.size();
David Srbecky67feb172015-12-17 19:57:44 +0000130 entry->prev_ = nullptr;
David Srbecky440a9b32018-02-15 17:47:29 +0000131 entry->next_ = descriptor.first_entry_;
132 entry->register_timestamp_ = NanoTime();
David Srbecky67feb172015-12-17 19:57:44 +0000133 if (entry->next_ != nullptr) {
134 entry->next_->prev_ = entry;
135 }
David Srbecky440a9b32018-02-15 17:47:29 +0000136 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 Srbecky67feb172015-12-17 19:57:44 +0000143 return entry;
144}
145
David Srbecky440a9b32018-02-15 17:47:29 +0000146static 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 Srbecky67feb172015-12-17 19:57:44 +0000154 if (entry->prev_ != nullptr) {
155 entry->prev_->next_ = entry->next_;
156 } else {
David Srbecky440a9b32018-02-15 17:47:29 +0000157 descriptor.first_entry_ = entry->next_;
David Srbecky67feb172015-12-17 19:57:44 +0000158 }
David Srbecky67feb172015-12-17 19:57:44 +0000159 if (entry->next_ != nullptr) {
160 entry->next_->prev_ = entry->prev_;
161 }
162
David Srbecky440a9b32018-02-15 17:47:29 +0000163 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 Srbecky67feb172015-12-17 19:57:44 +0000169 delete entry;
170}
171
David Srbecky440a9b32018-02-15 17:47:29 +0000172static std::unordered_map<const void*, JITCodeEntry*> __dex_debug_entries
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000173 GUARDED_BY(Locks::native_debug_interface_lock_);
David Srbeckyc684f332018-01-19 17:38:06 +0000174
David Srbecky440a9b32018-02-15 17:47:29 +0000175void 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 Srbecky5cc349f2015-12-18 15:04:48 +0000184 }
David Srbeckyc684f332018-01-19 17:38:06 +0000185}
186
David Srbecky440a9b32018-02-15 17:47:29 +0000187void 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 Srbeckyc684f332018-01-19 17:38:06 +0000198}
199
David Srbecky440a9b32018-02-15 17:47:29 +0000200static 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.
204static std::unordered_map<const void*, JITCodeEntry*> __jit_debug_entries
205 GUARDED_BY(Locks::native_debug_interface_lock_);
206
207void 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
228void 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
245size_t GetJitNativeDebugInfoMemUsage() {
246 return __jit_debug_mem_usage + __jit_debug_entries.size() * 2 * sizeof(void*);
David Srbecky5cc349f2015-12-18 15:04:48 +0000247}
248
David Srbecky67feb172015-12-17 19:57:44 +0000249} // namespace art