blob: 24ca0fc1e1a25fff514e07d14e7c0684b64d091c [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 Srbecky8fc2f952019-07-31 18:40:09 +010022#include "base/bit_utils.h"
David Srbecky0b21e412018-12-05 13:24:06 +000023#include "base/logging.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000024#include "base/mutex.h"
David Srbecky440a9b32018-02-15 17:47:29 +000025#include "base/time_utils.h"
David Srbecky0b21e412018-12-05 13:24:06 +000026#include "base/utils.h"
David Srbeckyafc60cd2018-12-05 11:59:31 +000027#include "dex/dex_file.h"
David Srbecky8fc2f952019-07-31 18:40:09 +010028#include "jit/jit.h"
David Srbecky9ac8e432019-08-13 13:16:13 +010029#include "jit/jit_code_cache.h"
30#include "jit/jit_memory_region.h"
David Srbecky8fc2f952019-07-31 18:40:09 +010031#include "runtime.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070032#include "thread-current-inl.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000033#include "thread.h"
34
David Srbeckyd767f2d2018-02-26 16:18:40 +000035#include <atomic>
David Srbeckyd767f2d2018-02-26 16:18:40 +000036#include <cstddef>
David Srbecky5cc349f2015-12-18 15:04:48 +000037
David Srbecky440a9b32018-02-15 17:47:29 +000038//
39// Debug interface for native tools (gdb, lldb, libunwind, simpleperf).
40//
41// See http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
42//
43// There are two ways for native tools to access the debug data safely:
44//
45// 1) Synchronously, by setting a breakpoint in the __*_debug_register_code
46// method, which is called after every modification of the linked list.
47// GDB does this, but it is complex to set up and it stops the process.
48//
David Srbeckyd767f2d2018-02-26 16:18:40 +000049// 2) Asynchronously, by monitoring the action_seqlock_.
50// * The seqlock is a monotonically increasing counter which is incremented
51// before and after every modification of the linked list. Odd value of
52// the counter means the linked list is being modified (it is locked).
53// * The tool should read the value of the seqlock both before and after
54// copying the linked list. If the seqlock values match and are even,
55// the copy is consistent. Otherwise, the reader should try again.
56// * Note that using the data directly while is it being modified
57// might crash the tool. Therefore, the only safe way is to make
58// a copy and use the copy only after the seqlock has been checked.
59// * Note that the process might even free and munmap the data while
60// it is being copied, therefore the reader should either handle
61// SEGV or use OS calls to read the memory (e.g. process_vm_readv).
62// * The seqlock can be used to determine the number of modifications of
63// the linked list, which can be used to intelligently cache the data.
64// Note the possible overflow of the seqlock. It is intentionally
65// 32-bit, since 64-bit atomics can be tricky on some architectures.
66// * The timestamps on the entry record the time when the entry was
67// created which is relevant if the unwinding is not live and is
68// postponed until much later. All timestamps must be unique.
69// * Memory barriers are used to make it possible to reason about
70// the data even when it is being modified (e.g. the process crashed
71// while that data was locked, and thus it will be never unlocked).
72// * In particular, it should be possible to:
73// 1) read the seqlock and then the linked list head pointer.
74// 2) copy the entry and check that seqlock has not changed.
75// 3) copy the symfile and check that seqlock has not changed.
76// 4) go back to step 2 using the next pointer (if non-null).
77// This safely creates copy of all symfiles, although other data
78// might be inconsistent/unusable (e.g. prev_, action_timestamp_).
79// * For full conformance with the C++ memory model, all seqlock
80// protected accesses should be atomic. We currently do this in the
81// more critical cases. The rest will have to be fixed before
82// attempting to run TSAN on this code.
David Srbecky440a9b32018-02-15 17:47:29 +000083//
David Srbecky67feb172015-12-17 19:57:44 +000084
David Srbecky440a9b32018-02-15 17:47:29 +000085namespace art {
David Srbecky0b21e412018-12-05 13:24:06 +000086
87static Mutex g_jit_debug_lock("JIT native debug entries", kNativeDebugInterfaceLock);
88static Mutex g_dex_debug_lock("DEX native debug entries", kNativeDebugInterfaceLock);
89
David Srbecky67feb172015-12-17 19:57:44 +000090extern "C" {
Andreas Gampec55bb392018-09-21 00:02:02 +000091 enum JITAction {
David Srbecky67feb172015-12-17 19:57:44 +000092 JIT_NOACTION = 0,
93 JIT_REGISTER_FN,
94 JIT_UNREGISTER_FN
Andreas Gampec55bb392018-09-21 00:02:02 +000095 };
David Srbecky67feb172015-12-17 19:57:44 +000096
David Srbecky8fc2f952019-07-31 18:40:09 +010097 // Public/stable binary interface.
98 struct JITCodeEntryPublic {
David Srbeckyd767f2d2018-02-26 16:18:40 +000099 // Atomic to ensure the reader can always iterate over the linked list
100 // (e.g. the process could crash in the middle of writing this field).
David Srbeckye09b87e2019-08-19 21:31:31 +0100101 std::atomic<const JITCodeEntry*> next_;
102 const JITCodeEntry* prev_; // For linked list deletion. Unused in readers.
David Srbecky8fc2f952019-07-31 18:40:09 +0100103 const uint8_t* symfile_addr_; // Address of the in-memory ELF file.
104 uint64_t symfile_size_; // Beware of the offset (12 on x86; but 16 on ARM32).
David Srbecky440a9b32018-02-15 17:47:29 +0000105
106 // Android-specific fields:
107 uint64_t register_timestamp_; // CLOCK_MONOTONIC time of entry registration.
David Srbecky67feb172015-12-17 19:57:44 +0000108 };
109
David Srbecky8fc2f952019-07-31 18:40:09 +0100110 // Implementation-specific fields (which can be used only in this file).
111 struct JITCodeEntry : public JITCodeEntryPublic {
112 // Unpacked entries: Code address of the symbol in the ELF file.
113 // Packed entries: The start address of the covered memory range.
114 const void* addr_ = nullptr;
115 // Allow merging of ELF files to save space.
116 // Packing drops advanced DWARF data, so it is not always desirable.
117 bool allow_packing_ = false;
118 // Whether this entry has been LZMA compressed.
119 // Compression is expensive, so we don't always do it.
120 bool is_compressed_ = false;
121 };
122
David Srbecky67feb172015-12-17 19:57:44 +0000123 struct JITDescriptor {
David Srbeckye09b87e2019-08-19 21:31:31 +0100124 uint32_t version_ = 1; // NB: GDB supports only version 1.
125 uint32_t action_flag_ = JIT_NOACTION; // One of the JITAction enum values.
126 const JITCodeEntry* relevant_entry_ = nullptr; // The entry affected by the action.
127 std::atomic<const JITCodeEntry*> head_{nullptr}; // Head of link list of all entries.
David Srbecky440a9b32018-02-15 17:47:29 +0000128
129 // Android-specific fields:
130 uint8_t magic_[8] = {'A', 'n', 'd', 'r', 'o', 'i', 'd', '1'};
David Srbeckyd767f2d2018-02-26 16:18:40 +0000131 uint32_t flags_ = 0; // Reserved for future use. Must be 0.
David Srbecky440a9b32018-02-15 17:47:29 +0000132 uint32_t sizeof_descriptor = sizeof(JITDescriptor);
David Srbecky8fc2f952019-07-31 18:40:09 +0100133 uint32_t sizeof_entry = sizeof(JITCodeEntryPublic);
David Srbeckyd767f2d2018-02-26 16:18:40 +0000134 std::atomic_uint32_t action_seqlock_{0}; // Incremented before and after any modification.
135 uint64_t action_timestamp_ = 1; // CLOCK_MONOTONIC time of last action.
David Srbecky67feb172015-12-17 19:57:44 +0000136 };
137
David Srbeckyd767f2d2018-02-26 16:18:40 +0000138 // Check that std::atomic has the expected layout.
139 static_assert(alignof(std::atomic_uint32_t) == alignof(uint32_t), "Weird alignment");
140 static_assert(sizeof(std::atomic_uint32_t) == sizeof(uint32_t), "Weird size");
141 static_assert(alignof(std::atomic<void*>) == alignof(void*), "Weird alignment");
142 static_assert(sizeof(std::atomic<void*>) == sizeof(void*), "Weird size");
David Srbecky440a9b32018-02-15 17:47:29 +0000143
144 // GDB may set breakpoint here. We must ensure it is not removed or deduplicated.
David Srbecky67feb172015-12-17 19:57:44 +0000145 void __attribute__((noinline)) __jit_debug_register_code() {
146 __asm__("");
147 }
148
David Srbecky440a9b32018-02-15 17:47:29 +0000149 // Alternatively, native tools may overwrite this field to execute custom handler.
David Srbeckye8b4e852016-03-15 17:02:41 +0000150 void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code;
151
David Srbecky440a9b32018-02-15 17:47:29 +0000152 // The root data structure describing of all JITed methods.
David Srbecky0b21e412018-12-05 13:24:06 +0000153 JITDescriptor __jit_debug_descriptor GUARDED_BY(g_jit_debug_lock) {};
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000154
David Srbecky440a9b32018-02-15 17:47:29 +0000155 // The following globals mirror the ones above, but are used to register dex files.
156 void __attribute__((noinline)) __dex_debug_register_code() {
157 __asm__("");
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000158 }
David Srbecky440a9b32018-02-15 17:47:29 +0000159 void (*__dex_debug_register_code_ptr)() = __dex_debug_register_code;
David Srbecky0b21e412018-12-05 13:24:06 +0000160 JITDescriptor __dex_debug_descriptor GUARDED_BY(g_dex_debug_lock) {};
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000161}
162
David Srbecky9ac8e432019-08-13 13:16:13 +0100163struct DexNativeInfo {
164 static constexpr bool kCopySymfileData = false; // Just reference DEX files.
165 static JITDescriptor& Descriptor() { return __dex_debug_descriptor; }
166 static void NotifyNativeDebugger() { __dex_debug_register_code_ptr(); }
David Srbeckye09b87e2019-08-19 21:31:31 +0100167 static const void* Alloc(size_t size) { return malloc(size); }
168 static void Free(const void* ptr) { free(const_cast<void*>(ptr)); }
169 template<class T> static T* Writable(const T* v) { return const_cast<T*>(v); }
David Srbecky9ac8e432019-08-13 13:16:13 +0100170};
171
172struct JitNativeInfo {
173 static constexpr bool kCopySymfileData = true; // Copy debug info to JIT memory.
174 static JITDescriptor& Descriptor() { return __jit_debug_descriptor; }
175 static void NotifyNativeDebugger() { __jit_debug_register_code_ptr(); }
David Srbeckye09b87e2019-08-19 21:31:31 +0100176 static const void* Alloc(size_t size) { return Memory()->AllocateData(size); }
David Srbecky87fb0322019-08-20 10:34:02 +0100177 static void Free(const void* ptr) { Memory()->FreeData(reinterpret_cast<const uint8_t*>(ptr)); }
David Srbeckye09b87e2019-08-19 21:31:31 +0100178 static void Free(void* ptr) = delete;
179 template<class T> static T* Writable(const T* v) {
180 return const_cast<T*>(Memory()->GetWritableDataAddress(v));
181 }
David Srbecky9ac8e432019-08-13 13:16:13 +0100182
David Srbeckye09b87e2019-08-19 21:31:31 +0100183 static jit::JitMemoryRegion* Memory() ASSERT_CAPABILITY(Locks::jit_lock_) {
David Srbecky9ac8e432019-08-13 13:16:13 +0100184 Locks::jit_lock_->AssertHeld(Thread::Current());
185 jit::JitCodeCache* jit_code_cache = Runtime::Current()->GetJitCodeCache();
186 CHECK(jit_code_cache != nullptr);
187 jit::JitMemoryRegion* memory = jit_code_cache->GetCurrentRegion();
188 CHECK(memory->IsValid());
189 return memory;
190 }
191};
192
David Srbeckye09b87e2019-08-19 21:31:31 +0100193ArrayRef<const uint8_t> GetJITCodeEntrySymFile(const JITCodeEntry* entry) {
David Srbecky8fc2f952019-07-31 18:40:09 +0100194 return ArrayRef<const uint8_t>(entry->symfile_addr_, entry->symfile_size_);
195}
196
David Srbeckyd767f2d2018-02-26 16:18:40 +0000197// Mark the descriptor as "locked", so native tools know the data is being modified.
198static void ActionSeqlock(JITDescriptor& descriptor) {
199 DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 0u) << "Already locked";
200 descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed);
201 // Ensure that any writes within the locked section cannot be reordered before the increment.
202 std::atomic_thread_fence(std::memory_order_release);
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000203}
David Srbeckyc684f332018-01-19 17:38:06 +0000204
David Srbecky440a9b32018-02-15 17:47:29 +0000205// Mark the descriptor as "unlocked", so native tools know the data is safe to read.
David Srbeckyd767f2d2018-02-26 16:18:40 +0000206static void ActionSequnlock(JITDescriptor& descriptor) {
207 DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 1u) << "Already unlocked";
208 // Ensure that any writes within the locked section cannot be reordered after the increment.
209 std::atomic_thread_fence(std::memory_order_release);
210 descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed);
David Srbecky440a9b32018-02-15 17:47:29 +0000211}
Vladimir Marko93205e32016-04-13 11:59:46 +0100212
David Srbecky9ac8e432019-08-13 13:16:13 +0100213template<class NativeInfo>
David Srbeckye09b87e2019-08-19 21:31:31 +0100214static const JITCodeEntry* CreateJITCodeEntryInternal(
David Srbeckyafc60cd2018-12-05 11:59:31 +0000215 ArrayRef<const uint8_t> symfile,
David Srbecky8fc2f952019-07-31 18:40:09 +0100216 const void* addr = nullptr,
217 bool allow_packing = false,
218 bool is_compressed = false) {
David Srbecky9ac8e432019-08-13 13:16:13 +0100219 JITDescriptor& descriptor = NativeInfo::Descriptor();
220
David Srbeckyafc60cd2018-12-05 11:59:31 +0000221 // Make a copy of the buffer to shrink it and to pass ownership to JITCodeEntry.
David Srbeckye09b87e2019-08-19 21:31:31 +0100222 const uint8_t* copy = nullptr;
David Srbecky9ac8e432019-08-13 13:16:13 +0100223 if (NativeInfo::kCopySymfileData) {
David Srbeckye09b87e2019-08-19 21:31:31 +0100224 copy = reinterpret_cast<const uint8_t*>(NativeInfo::Alloc(symfile.size()));
David Srbecky9ac8e432019-08-13 13:16:13 +0100225 if (copy == nullptr) {
226 LOG(ERROR) << "Failed to allocate memory for native debug info";
227 return nullptr;
228 }
David Srbeckye09b87e2019-08-19 21:31:31 +0100229 memcpy(NativeInfo::Writable(copy), symfile.data(), symfile.size());
David Srbeckyafc60cd2018-12-05 11:59:31 +0000230 symfile = ArrayRef<const uint8_t>(copy, symfile.size());
231 }
232
David Srbeckyd767f2d2018-02-26 16:18:40 +0000233 // Ensure the timestamp is monotonically increasing even in presence of low
234 // granularity system timer. This ensures each entry has unique timestamp.
235 uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime());
David Srbecky5cc349f2015-12-18 15:04:48 +0000236
David Srbeckye09b87e2019-08-19 21:31:31 +0100237 const JITCodeEntry* head = descriptor.head_.load(std::memory_order_relaxed);
238 const void* memory = NativeInfo::Alloc(sizeof(JITCodeEntry));
David Srbecky9ac8e432019-08-13 13:16:13 +0100239 if (memory == nullptr) {
240 LOG(ERROR) << "Failed to allocate memory for native debug info";
241 if (copy != nullptr) {
242 NativeInfo::Free(copy);
243 }
244 return nullptr;
245 }
David Srbeckye09b87e2019-08-19 21:31:31 +0100246 const JITCodeEntry* entry = reinterpret_cast<const JITCodeEntry*>(memory);
247 JITCodeEntry* writable_entry = NativeInfo::Writable(entry);
248 writable_entry->symfile_addr_ = symfile.data();
249 writable_entry->symfile_size_ = symfile.size();
250 writable_entry->prev_ = nullptr;
251 writable_entry->next_.store(head, std::memory_order_relaxed);
252 writable_entry->register_timestamp_ = timestamp;
253 writable_entry->addr_ = addr;
254 writable_entry->allow_packing_ = allow_packing;
255 writable_entry->is_compressed_ = is_compressed;
David Srbeckyd767f2d2018-02-26 16:18:40 +0000256
257 // We are going to modify the linked list, so take the seqlock.
258 ActionSeqlock(descriptor);
259 if (head != nullptr) {
David Srbeckye09b87e2019-08-19 21:31:31 +0100260 NativeInfo::Writable(head)->prev_ = entry;
David Srbecky67feb172015-12-17 19:57:44 +0000261 }
David Srbeckyd767f2d2018-02-26 16:18:40 +0000262 descriptor.head_.store(entry, std::memory_order_relaxed);
David Srbecky440a9b32018-02-15 17:47:29 +0000263 descriptor.relevant_entry_ = entry;
264 descriptor.action_flag_ = JIT_REGISTER_FN;
David Srbeckyd767f2d2018-02-26 16:18:40 +0000265 descriptor.action_timestamp_ = timestamp;
266 ActionSequnlock(descriptor);
David Srbecky440a9b32018-02-15 17:47:29 +0000267
David Srbecky9ac8e432019-08-13 13:16:13 +0100268 NativeInfo::NotifyNativeDebugger();
269
David Srbecky67feb172015-12-17 19:57:44 +0000270 return entry;
271}
272
David Srbecky9ac8e432019-08-13 13:16:13 +0100273template<class NativeInfo>
David Srbeckye09b87e2019-08-19 21:31:31 +0100274static void DeleteJITCodeEntryInternal(const JITCodeEntry* entry) {
David Srbecky440a9b32018-02-15 17:47:29 +0000275 CHECK(entry != nullptr);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000276 const uint8_t* symfile = entry->symfile_addr_;
David Srbecky9ac8e432019-08-13 13:16:13 +0100277 JITDescriptor& descriptor = NativeInfo::Descriptor();
David Srbecky440a9b32018-02-15 17:47:29 +0000278
David Srbeckyd767f2d2018-02-26 16:18:40 +0000279 // Ensure the timestamp is monotonically increasing even in presence of low
280 // granularity system timer. This ensures each entry has unique timestamp.
281 uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime());
282
283 // We are going to modify the linked list, so take the seqlock.
284 ActionSeqlock(descriptor);
David Srbeckye09b87e2019-08-19 21:31:31 +0100285 const JITCodeEntry* next = entry->next_.load(std::memory_order_relaxed);
David Srbecky67feb172015-12-17 19:57:44 +0000286 if (entry->prev_ != nullptr) {
David Srbeckye09b87e2019-08-19 21:31:31 +0100287 NativeInfo::Writable(entry->prev_)->next_.store(next, std::memory_order_relaxed);
David Srbecky67feb172015-12-17 19:57:44 +0000288 } else {
David Srbeckyd767f2d2018-02-26 16:18:40 +0000289 descriptor.head_.store(next, std::memory_order_relaxed);
David Srbecky67feb172015-12-17 19:57:44 +0000290 }
David Srbeckyd767f2d2018-02-26 16:18:40 +0000291 if (next != nullptr) {
David Srbeckye09b87e2019-08-19 21:31:31 +0100292 NativeInfo::Writable(next)->prev_ = entry->prev_;
David Srbecky67feb172015-12-17 19:57:44 +0000293 }
David Srbecky440a9b32018-02-15 17:47:29 +0000294 descriptor.relevant_entry_ = entry;
295 descriptor.action_flag_ = JIT_UNREGISTER_FN;
David Srbeckyd767f2d2018-02-26 16:18:40 +0000296 descriptor.action_timestamp_ = timestamp;
297 ActionSequnlock(descriptor);
David Srbecky440a9b32018-02-15 17:47:29 +0000298
David Srbecky9ac8e432019-08-13 13:16:13 +0100299 NativeInfo::NotifyNativeDebugger();
David Srbeckyd767f2d2018-02-26 16:18:40 +0000300
301 // Ensure that clear below can not be reordered above the unlock above.
302 std::atomic_thread_fence(std::memory_order_release);
303
304 // Aggressively clear the entry as an extra check of the synchronisation.
David Srbeckye09b87e2019-08-19 21:31:31 +0100305 memset(NativeInfo::Writable(entry), 0, sizeof(*entry));
David Srbeckyd767f2d2018-02-26 16:18:40 +0000306
David Srbecky9ac8e432019-08-13 13:16:13 +0100307 NativeInfo::Free(entry);
308 if (NativeInfo::kCopySymfileData) {
David Srbeckye09b87e2019-08-19 21:31:31 +0100309 NativeInfo::Free(symfile);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000310 }
David Srbecky67feb172015-12-17 19:57:44 +0000311}
312
David Srbeckyafc60cd2018-12-05 11:59:31 +0000313void AddNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) {
David Srbecky0b21e412018-12-05 13:24:06 +0000314 MutexLock mu(self, g_dex_debug_lock);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000315 DCHECK(dexfile != nullptr);
David Srbecky8fc2f952019-07-31 18:40:09 +0100316 const ArrayRef<const uint8_t> symfile(dexfile->Begin(), dexfile->Size());
David Srbecky9ac8e432019-08-13 13:16:13 +0100317 CreateJITCodeEntryInternal<DexNativeInfo>(symfile);
David Srbeckyc684f332018-01-19 17:38:06 +0000318}
319
David Srbeckyafc60cd2018-12-05 11:59:31 +0000320void RemoveNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) {
David Srbecky0b21e412018-12-05 13:24:06 +0000321 MutexLock mu(self, g_dex_debug_lock);
David Srbecky8fc2f952019-07-31 18:40:09 +0100322 DCHECK(dexfile != nullptr);
David Srbecky440a9b32018-02-15 17:47:29 +0000323 // We register dex files in the class linker and free them in DexFile_closeDexFile, but
324 // there might be cases where we load the dex file without using it in the class linker.
David Srbecky8fc2f952019-07-31 18:40:09 +0100325 // On the other hand, single dex file might also be used with different class-loaders.
David Srbeckye09b87e2019-08-19 21:31:31 +0100326 for (const JITCodeEntry* entry = __dex_debug_descriptor.head_; entry != nullptr; ) {
327 const JITCodeEntry* next = entry->next_; // Save next pointer before we free the memory.
David Srbecky8fc2f952019-07-31 18:40:09 +0100328 if (entry->symfile_addr_ == dexfile->Begin()) {
David Srbecky9ac8e432019-08-13 13:16:13 +0100329 DeleteJITCodeEntryInternal<DexNativeInfo>(entry);
David Srbecky8fc2f952019-07-31 18:40:09 +0100330 }
331 entry = next;
David Srbecky440a9b32018-02-15 17:47:29 +0000332 }
David Srbeckyc684f332018-01-19 17:38:06 +0000333}
334
David Srbecky8fc2f952019-07-31 18:40:09 +0100335// Size of JIT code range covered by each packed JITCodeEntry.
336static constexpr uint32_t kJitRepackGroupSize = 64 * KB;
David Srbecky0b21e412018-12-05 13:24:06 +0000337
David Srbecky8fc2f952019-07-31 18:40:09 +0100338// Automatically call the repack method every 'n' new entries.
339static constexpr uint32_t kJitRepackFrequency = 64;
340static uint32_t g_jit_num_unpacked_entries = 0;
David Srbecky0b21e412018-12-05 13:24:06 +0000341
David Srbecky8fc2f952019-07-31 18:40:09 +0100342// Split the JIT code cache into groups of fixed size and create single JITCodeEntry for each group.
David Srbecky0b21e412018-12-05 13:24:06 +0000343// The start address of method's code determines which group it belongs to. The end is irrelevant.
David Srbecky8fc2f952019-07-31 18:40:09 +0100344// New mini debug infos will be merged if possible, and entries for GCed functions will be removed.
345static void RepackEntries(bool compress, ArrayRef<const void*> removed)
David Srbecky0b21e412018-12-05 13:24:06 +0000346 REQUIRES(g_jit_debug_lock) {
David Srbecky8fc2f952019-07-31 18:40:09 +0100347 DCHECK(std::is_sorted(removed.begin(), removed.end()));
348 jit::Jit* jit = Runtime::Current()->GetJit();
349 if (jit == nullptr) {
350 return;
351 }
David Srbecky0b21e412018-12-05 13:24:06 +0000352
David Srbecky8fc2f952019-07-31 18:40:09 +0100353 // Collect entries that we want to pack.
David Srbeckye09b87e2019-08-19 21:31:31 +0100354 std::vector<const JITCodeEntry*> entries;
David Srbecky8fc2f952019-07-31 18:40:09 +0100355 entries.reserve(2 * kJitRepackFrequency);
David Srbeckye09b87e2019-08-19 21:31:31 +0100356 for (const JITCodeEntry* it = __jit_debug_descriptor.head_; it != nullptr; it = it->next_) {
David Srbecky8fc2f952019-07-31 18:40:09 +0100357 if (it->allow_packing_) {
358 if (!compress && it->is_compressed_ && removed.empty()) {
359 continue; // If we are not compressing, also avoid decompressing.
360 }
361 entries.push_back(it);
362 }
363 }
David Srbeckye09b87e2019-08-19 21:31:31 +0100364 auto cmp = [](const JITCodeEntry* l, const JITCodeEntry* r) { return l->addr_ < r->addr_; };
David Srbecky8fc2f952019-07-31 18:40:09 +0100365 std::sort(entries.begin(), entries.end(), cmp); // Sort by address.
David Srbecky0b21e412018-12-05 13:24:06 +0000366
David Srbecky8fc2f952019-07-31 18:40:09 +0100367 // Process the entries in groups (each spanning memory range of size kJitRepackGroupSize).
368 for (auto group_it = entries.begin(); group_it != entries.end();) {
369 const void* group_ptr = AlignDown((*group_it)->addr_, kJitRepackGroupSize);
370 const void* group_end = reinterpret_cast<const uint8_t*>(group_ptr) + kJitRepackGroupSize;
371
372 // Find all entries in this group (each entry is an in-memory ELF file).
373 auto begin = group_it;
374 auto end = std::find_if(begin, entries.end(), [=](auto* e) { return e->addr_ >= group_end; });
375 CHECK(end > begin);
David Srbeckye09b87e2019-08-19 21:31:31 +0100376 ArrayRef<const JITCodeEntry*> elfs(&*begin, end - begin);
David Srbecky8fc2f952019-07-31 18:40:09 +0100377
378 // Find all symbols that have been removed in this memory range.
379 auto removed_begin = std::lower_bound(removed.begin(), removed.end(), group_ptr);
380 auto removed_end = std::lower_bound(removed.begin(), removed.end(), group_end);
381 CHECK(removed_end >= removed_begin);
382 ArrayRef<const void*> removed_subset(&*removed_begin, removed_end - removed_begin);
383
384 // Bail out early if there is nothing to do for this group.
385 if (elfs.size() == 1 && removed_subset.empty() && (*begin)->is_compressed_ == compress) {
386 group_it = end; // Go to next group.
387 continue;
David Srbecky0b21e412018-12-05 13:24:06 +0000388 }
David Srbecky76b9c692019-04-01 19:36:33 +0100389
390 // Create new single JITCodeEntry that covers this memory range.
391 uint64_t start_time = MicroTime();
David Srbecky8fc2f952019-07-31 18:40:09 +0100392 size_t live_symbols;
393 std::vector<uint8_t> packed = jit->GetJitCompiler()->PackElfFileForJIT(
394 elfs, removed_subset, compress, &live_symbols);
David Srbecky0b21e412018-12-05 13:24:06 +0000395 VLOG(jit)
David Srbecky76b9c692019-04-01 19:36:33 +0100396 << "JIT mini-debug-info repacked"
David Srbecky0b21e412018-12-05 13:24:06 +0000397 << " for " << group_ptr
David Srbecky76b9c692019-04-01 19:36:33 +0100398 << " in " << MicroTime() - start_time << "us"
David Srbecky8fc2f952019-07-31 18:40:09 +0100399 << " elfs=" << elfs.size()
400 << " dead=" << removed_subset.size()
401 << " live=" << live_symbols
402 << " size=" << packed.size() << (compress ? "(lzma)" : "");
David Srbecky0b21e412018-12-05 13:24:06 +0000403
404 // Replace the old entries with the new one (with their lifetime temporally overlapping).
David Srbecky9ac8e432019-08-13 13:16:13 +0100405 CreateJITCodeEntryInternal<JitNativeInfo>(ArrayRef<const uint8_t>(packed),
406 /*addr_=*/ group_ptr,
407 /*allow_packing_=*/ true,
408 /*is_compressed_=*/ compress);
David Srbecky8fc2f952019-07-31 18:40:09 +0100409 for (auto it : elfs) {
David Srbecky9ac8e432019-08-13 13:16:13 +0100410 DeleteJITCodeEntryInternal<JitNativeInfo>(/*entry=*/ it);
David Srbecky0b21e412018-12-05 13:24:06 +0000411 }
David Srbecky8fc2f952019-07-31 18:40:09 +0100412 group_it = end; // Go to next group.
David Srbecky0b21e412018-12-05 13:24:06 +0000413 }
David Srbecky8fc2f952019-07-31 18:40:09 +0100414 g_jit_num_unpacked_entries = 0;
David Srbecky0b21e412018-12-05 13:24:06 +0000415}
David Srbecky440a9b32018-02-15 17:47:29 +0000416
David Srbecky8fc2f952019-07-31 18:40:09 +0100417void AddNativeDebugInfoForJit(const void* code_ptr,
David Srbecky0b21e412018-12-05 13:24:06 +0000418 const std::vector<uint8_t>& symfile,
David Srbecky8fc2f952019-07-31 18:40:09 +0100419 bool allow_packing) {
420 MutexLock mu(Thread::Current(), g_jit_debug_lock);
David Srbecky440a9b32018-02-15 17:47:29 +0000421 DCHECK_NE(symfile.size(), 0u);
422
David Srbecky9ac8e432019-08-13 13:16:13 +0100423 if (Runtime::Current()->IsZygote()) {
424 return; // TODO: Implement memory sharing with the zygote process.
425 }
426
427 CreateJITCodeEntryInternal<JitNativeInfo>(ArrayRef<const uint8_t>(symfile),
428 /*addr=*/ code_ptr,
429 /*allow_packing=*/ allow_packing,
430 /*is_compressed=*/ false);
David Srbecky440a9b32018-02-15 17:47:29 +0000431
David Srbecky0b21e412018-12-05 13:24:06 +0000432 VLOG(jit)
433 << "JIT mini-debug-info added"
434 << " for " << code_ptr
435 << " size=" << PrettySize(symfile.size());
436
David Srbecky8fc2f952019-07-31 18:40:09 +0100437 // Automatically repack entries on regular basis to save space.
David Srbecky76b9c692019-04-01 19:36:33 +0100438 // Pack (but don't compress) recent entries - this is cheap and reduces memory use by ~4x.
439 // We delay compression until after GC since it is more expensive (and saves further ~4x).
David Srbecky8fc2f952019-07-31 18:40:09 +0100440 if (++g_jit_num_unpacked_entries >= kJitRepackFrequency) {
441 RepackEntries(/*compress=*/ false, /*removed=*/ ArrayRef<const void*>());
David Srbecky440a9b32018-02-15 17:47:29 +0000442 }
443}
444
David Srbecky8fc2f952019-07-31 18:40:09 +0100445void RemoveNativeDebugInfoForJit(ArrayRef<const void*> removed) {
446 MutexLock mu(Thread::Current(), g_jit_debug_lock);
447 RepackEntries(/*compress=*/ true, removed);
448
449 // Remove entries which are not allowed to be packed (containing single method each).
David Srbeckye09b87e2019-08-19 21:31:31 +0100450 for (const JITCodeEntry* it = __jit_debug_descriptor.head_; it != nullptr; it = it->next_) {
David Srbecky8fc2f952019-07-31 18:40:09 +0100451 if (!it->allow_packing_ && std::binary_search(removed.begin(), removed.end(), it->addr_)) {
David Srbecky9ac8e432019-08-13 13:16:13 +0100452 DeleteJITCodeEntryInternal<JitNativeInfo>(/*entry=*/ it);
David Srbecky8fc2f952019-07-31 18:40:09 +0100453 }
David Srbeckyafc60cd2018-12-05 11:59:31 +0000454 }
455}
456
457size_t GetJitMiniDebugInfoMemUsage() {
David Srbecky0b21e412018-12-05 13:24:06 +0000458 MutexLock mu(Thread::Current(), g_jit_debug_lock);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000459 size_t size = 0;
David Srbeckye09b87e2019-08-19 21:31:31 +0100460 for (const JITCodeEntry* it = __jit_debug_descriptor.head_; it != nullptr; it = it->next_) {
David Srbecky8fc2f952019-07-31 18:40:09 +0100461 size += sizeof(JITCodeEntry) + it->symfile_size_;
David Srbeckyafc60cd2018-12-05 11:59:31 +0000462 }
463 return size;
David Srbecky5cc349f2015-12-18 15:04:48 +0000464}
465
David Srbecky1ed45152019-04-09 18:10:26 +0100466Mutex* GetNativeDebugInfoLock() {
467 return &g_jit_debug_lock;
468}
469
David Srbecky67feb172015-12-17 19:57:44 +0000470} // namespace art