blob: f522be81bdc0d58422f6ca618a25c4ce3382a0af [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 Srbecky0b21e412018-12-05 13:24:06 +000022#include "base/logging.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000023#include "base/mutex.h"
David Srbecky440a9b32018-02-15 17:47:29 +000024#include "base/time_utils.h"
David Srbecky0b21e412018-12-05 13:24:06 +000025#include "base/utils.h"
David Srbeckyafc60cd2018-12-05 11:59:31 +000026#include "dex/dex_file.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070027#include "thread-current-inl.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000028#include "thread.h"
29
David Srbeckyd767f2d2018-02-26 16:18:40 +000030#include <atomic>
David Srbeckyd767f2d2018-02-26 16:18:40 +000031#include <cstddef>
David Srbecky0b21e412018-12-05 13:24:06 +000032#include <deque>
David Srbeckyafc60cd2018-12-05 11:59:31 +000033#include <map>
David Srbecky76b9c692019-04-01 19:36:33 +010034#include <set>
David Srbecky5cc349f2015-12-18 15:04:48 +000035
David Srbecky440a9b32018-02-15 17:47:29 +000036//
37// Debug interface for native tools (gdb, lldb, libunwind, simpleperf).
38//
39// See http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
40//
41// There are two ways for native tools to access the debug data safely:
42//
43// 1) Synchronously, by setting a breakpoint in the __*_debug_register_code
44// method, which is called after every modification of the linked list.
45// GDB does this, but it is complex to set up and it stops the process.
46//
David Srbeckyd767f2d2018-02-26 16:18:40 +000047// 2) Asynchronously, by monitoring the action_seqlock_.
48// * The seqlock is a monotonically increasing counter which is incremented
49// before and after every modification of the linked list. Odd value of
50// the counter means the linked list is being modified (it is locked).
51// * The tool should read the value of the seqlock both before and after
52// copying the linked list. If the seqlock values match and are even,
53// the copy is consistent. Otherwise, the reader should try again.
54// * Note that using the data directly while is it being modified
55// might crash the tool. Therefore, the only safe way is to make
56// a copy and use the copy only after the seqlock has been checked.
57// * Note that the process might even free and munmap the data while
58// it is being copied, therefore the reader should either handle
59// SEGV or use OS calls to read the memory (e.g. process_vm_readv).
60// * The seqlock can be used to determine the number of modifications of
61// the linked list, which can be used to intelligently cache the data.
62// Note the possible overflow of the seqlock. It is intentionally
63// 32-bit, since 64-bit atomics can be tricky on some architectures.
64// * The timestamps on the entry record the time when the entry was
65// created which is relevant if the unwinding is not live and is
66// postponed until much later. All timestamps must be unique.
67// * Memory barriers are used to make it possible to reason about
68// the data even when it is being modified (e.g. the process crashed
69// while that data was locked, and thus it will be never unlocked).
70// * In particular, it should be possible to:
71// 1) read the seqlock and then the linked list head pointer.
72// 2) copy the entry and check that seqlock has not changed.
73// 3) copy the symfile and check that seqlock has not changed.
74// 4) go back to step 2 using the next pointer (if non-null).
75// This safely creates copy of all symfiles, although other data
76// might be inconsistent/unusable (e.g. prev_, action_timestamp_).
77// * For full conformance with the C++ memory model, all seqlock
78// protected accesses should be atomic. We currently do this in the
79// more critical cases. The rest will have to be fixed before
80// attempting to run TSAN on this code.
David Srbecky440a9b32018-02-15 17:47:29 +000081//
David Srbecky67feb172015-12-17 19:57:44 +000082
David Srbecky440a9b32018-02-15 17:47:29 +000083namespace art {
David Srbecky0b21e412018-12-05 13:24:06 +000084
85static Mutex g_jit_debug_lock("JIT native debug entries", kNativeDebugInterfaceLock);
86static Mutex g_dex_debug_lock("DEX native debug entries", kNativeDebugInterfaceLock);
87
David Srbecky67feb172015-12-17 19:57:44 +000088extern "C" {
Andreas Gampec55bb392018-09-21 00:02:02 +000089 enum JITAction {
David Srbecky67feb172015-12-17 19:57:44 +000090 JIT_NOACTION = 0,
91 JIT_REGISTER_FN,
92 JIT_UNREGISTER_FN
Andreas Gampec55bb392018-09-21 00:02:02 +000093 };
David Srbecky67feb172015-12-17 19:57:44 +000094
95 struct JITCodeEntry {
David Srbeckyd767f2d2018-02-26 16:18:40 +000096 // Atomic to ensure the reader can always iterate over the linked list
97 // (e.g. the process could crash in the middle of writing this field).
98 std::atomic<JITCodeEntry*> next_;
99 // Non-atomic. The reader should not use it. It is only used for deletion.
David Srbecky67feb172015-12-17 19:57:44 +0000100 JITCodeEntry* prev_;
David Srbecky440a9b32018-02-15 17:47:29 +0000101 const uint8_t* symfile_addr_;
102 uint64_t symfile_size_; // Beware of the offset (12 on x86; but 16 on ARM32).
103
104 // Android-specific fields:
105 uint64_t register_timestamp_; // CLOCK_MONOTONIC time of entry registration.
David Srbecky67feb172015-12-17 19:57:44 +0000106 };
107
108 struct JITDescriptor {
David Srbeckyd767f2d2018-02-26 16:18:40 +0000109 uint32_t version_ = 1; // NB: GDB supports only version 1.
110 uint32_t action_flag_ = JIT_NOACTION; // One of the JITAction enum values.
111 JITCodeEntry* relevant_entry_ = nullptr; // The entry affected by the action.
112 std::atomic<JITCodeEntry*> head_{nullptr}; // Head of link list of all entries.
David Srbecky440a9b32018-02-15 17:47:29 +0000113
114 // Android-specific fields:
115 uint8_t magic_[8] = {'A', 'n', 'd', 'r', 'o', 'i', 'd', '1'};
David Srbeckyd767f2d2018-02-26 16:18:40 +0000116 uint32_t flags_ = 0; // Reserved for future use. Must be 0.
David Srbecky440a9b32018-02-15 17:47:29 +0000117 uint32_t sizeof_descriptor = sizeof(JITDescriptor);
118 uint32_t sizeof_entry = sizeof(JITCodeEntry);
David Srbeckyd767f2d2018-02-26 16:18:40 +0000119 std::atomic_uint32_t action_seqlock_{0}; // Incremented before and after any modification.
120 uint64_t action_timestamp_ = 1; // CLOCK_MONOTONIC time of last action.
David Srbecky67feb172015-12-17 19:57:44 +0000121 };
122
David Srbeckyd767f2d2018-02-26 16:18:40 +0000123 // Check that std::atomic has the expected layout.
124 static_assert(alignof(std::atomic_uint32_t) == alignof(uint32_t), "Weird alignment");
125 static_assert(sizeof(std::atomic_uint32_t) == sizeof(uint32_t), "Weird size");
126 static_assert(alignof(std::atomic<void*>) == alignof(void*), "Weird alignment");
127 static_assert(sizeof(std::atomic<void*>) == sizeof(void*), "Weird size");
David Srbecky440a9b32018-02-15 17:47:29 +0000128
129 // GDB may set breakpoint here. We must ensure it is not removed or deduplicated.
David Srbecky67feb172015-12-17 19:57:44 +0000130 void __attribute__((noinline)) __jit_debug_register_code() {
131 __asm__("");
132 }
133
David Srbecky440a9b32018-02-15 17:47:29 +0000134 // Alternatively, native tools may overwrite this field to execute custom handler.
David Srbeckye8b4e852016-03-15 17:02:41 +0000135 void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code;
136
David Srbecky440a9b32018-02-15 17:47:29 +0000137 // The root data structure describing of all JITed methods.
David Srbecky0b21e412018-12-05 13:24:06 +0000138 JITDescriptor __jit_debug_descriptor GUARDED_BY(g_jit_debug_lock) {};
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000139
David Srbecky440a9b32018-02-15 17:47:29 +0000140 // The following globals mirror the ones above, but are used to register dex files.
141 void __attribute__((noinline)) __dex_debug_register_code() {
142 __asm__("");
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000143 }
David Srbecky440a9b32018-02-15 17:47:29 +0000144 void (*__dex_debug_register_code_ptr)() = __dex_debug_register_code;
David Srbecky0b21e412018-12-05 13:24:06 +0000145 JITDescriptor __dex_debug_descriptor GUARDED_BY(g_dex_debug_lock) {};
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000146}
147
David Srbeckyd767f2d2018-02-26 16:18:40 +0000148// Mark the descriptor as "locked", so native tools know the data is being modified.
149static void ActionSeqlock(JITDescriptor& descriptor) {
150 DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 0u) << "Already locked";
151 descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed);
152 // Ensure that any writes within the locked section cannot be reordered before the increment.
153 std::atomic_thread_fence(std::memory_order_release);
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000154}
David Srbeckyc684f332018-01-19 17:38:06 +0000155
David Srbecky440a9b32018-02-15 17:47:29 +0000156// Mark the descriptor as "unlocked", so native tools know the data is safe to read.
David Srbeckyd767f2d2018-02-26 16:18:40 +0000157static void ActionSequnlock(JITDescriptor& descriptor) {
158 DCHECK_EQ(descriptor.action_seqlock_.load() & 1, 1u) << "Already unlocked";
159 // Ensure that any writes within the locked section cannot be reordered after the increment.
160 std::atomic_thread_fence(std::memory_order_release);
161 descriptor.action_seqlock_.fetch_add(1, std::memory_order_relaxed);
David Srbecky440a9b32018-02-15 17:47:29 +0000162}
Vladimir Marko93205e32016-04-13 11:59:46 +0100163
David Srbecky440a9b32018-02-15 17:47:29 +0000164static JITCodeEntry* CreateJITCodeEntryInternal(
165 JITDescriptor& descriptor,
166 void (*register_code_ptr)(),
David Srbeckyafc60cd2018-12-05 11:59:31 +0000167 ArrayRef<const uint8_t> symfile,
David Srbecky0b21e412018-12-05 13:24:06 +0000168 bool copy_symfile) {
David Srbeckyafc60cd2018-12-05 11:59:31 +0000169 // Make a copy of the buffer to shrink it and to pass ownership to JITCodeEntry.
170 if (copy_symfile) {
171 uint8_t* copy = new uint8_t[symfile.size()];
172 CHECK(copy != nullptr);
173 memcpy(copy, symfile.data(), symfile.size());
174 symfile = ArrayRef<const uint8_t>(copy, symfile.size());
175 }
176
David Srbeckyd767f2d2018-02-26 16:18:40 +0000177 // Ensure the timestamp is monotonically increasing even in presence of low
178 // granularity system timer. This ensures each entry has unique timestamp.
179 uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime());
David Srbecky5cc349f2015-12-18 15:04:48 +0000180
David Srbeckyd767f2d2018-02-26 16:18:40 +0000181 JITCodeEntry* head = descriptor.head_.load(std::memory_order_relaxed);
David Srbecky67feb172015-12-17 19:57:44 +0000182 JITCodeEntry* entry = new JITCodeEntry;
Vladimir Marko93205e32016-04-13 11:59:46 +0100183 CHECK(entry != nullptr);
David Srbecky440a9b32018-02-15 17:47:29 +0000184 entry->symfile_addr_ = symfile.data();
Vladimir Marko93205e32016-04-13 11:59:46 +0100185 entry->symfile_size_ = symfile.size();
David Srbecky67feb172015-12-17 19:57:44 +0000186 entry->prev_ = nullptr;
David Srbeckyd767f2d2018-02-26 16:18:40 +0000187 entry->next_.store(head, std::memory_order_relaxed);
188 entry->register_timestamp_ = timestamp;
189
190 // We are going to modify the linked list, so take the seqlock.
191 ActionSeqlock(descriptor);
192 if (head != nullptr) {
193 head->prev_ = entry;
David Srbecky67feb172015-12-17 19:57:44 +0000194 }
David Srbeckyd767f2d2018-02-26 16:18:40 +0000195 descriptor.head_.store(entry, std::memory_order_relaxed);
David Srbecky440a9b32018-02-15 17:47:29 +0000196 descriptor.relevant_entry_ = entry;
197 descriptor.action_flag_ = JIT_REGISTER_FN;
David Srbeckyd767f2d2018-02-26 16:18:40 +0000198 descriptor.action_timestamp_ = timestamp;
199 ActionSequnlock(descriptor);
David Srbecky440a9b32018-02-15 17:47:29 +0000200
201 (*register_code_ptr)();
David Srbecky67feb172015-12-17 19:57:44 +0000202 return entry;
203}
204
David Srbecky440a9b32018-02-15 17:47:29 +0000205static void DeleteJITCodeEntryInternal(
206 JITDescriptor& descriptor,
207 void (*register_code_ptr)(),
David Srbeckyafc60cd2018-12-05 11:59:31 +0000208 JITCodeEntry* entry,
David Srbecky0b21e412018-12-05 13:24:06 +0000209 bool free_symfile) {
David Srbecky440a9b32018-02-15 17:47:29 +0000210 CHECK(entry != nullptr);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000211 const uint8_t* symfile = entry->symfile_addr_;
David Srbecky440a9b32018-02-15 17:47:29 +0000212
David Srbeckyd767f2d2018-02-26 16:18:40 +0000213 // Ensure the timestamp is monotonically increasing even in presence of low
214 // granularity system timer. This ensures each entry has unique timestamp.
215 uint64_t timestamp = std::max(descriptor.action_timestamp_ + 1, NanoTime());
216
217 // We are going to modify the linked list, so take the seqlock.
218 ActionSeqlock(descriptor);
219 JITCodeEntry* next = entry->next_.load(std::memory_order_relaxed);
David Srbecky67feb172015-12-17 19:57:44 +0000220 if (entry->prev_ != nullptr) {
David Srbeckyd767f2d2018-02-26 16:18:40 +0000221 entry->prev_->next_.store(next, std::memory_order_relaxed);
David Srbecky67feb172015-12-17 19:57:44 +0000222 } else {
David Srbeckyd767f2d2018-02-26 16:18:40 +0000223 descriptor.head_.store(next, std::memory_order_relaxed);
David Srbecky67feb172015-12-17 19:57:44 +0000224 }
David Srbeckyd767f2d2018-02-26 16:18:40 +0000225 if (next != nullptr) {
226 next->prev_ = entry->prev_;
David Srbecky67feb172015-12-17 19:57:44 +0000227 }
David Srbecky440a9b32018-02-15 17:47:29 +0000228 descriptor.relevant_entry_ = entry;
229 descriptor.action_flag_ = JIT_UNREGISTER_FN;
David Srbeckyd767f2d2018-02-26 16:18:40 +0000230 descriptor.action_timestamp_ = timestamp;
231 ActionSequnlock(descriptor);
David Srbecky440a9b32018-02-15 17:47:29 +0000232
233 (*register_code_ptr)();
David Srbeckyd767f2d2018-02-26 16:18:40 +0000234
235 // Ensure that clear below can not be reordered above the unlock above.
236 std::atomic_thread_fence(std::memory_order_release);
237
238 // Aggressively clear the entry as an extra check of the synchronisation.
239 memset(entry, 0, sizeof(*entry));
240
David Srbecky67feb172015-12-17 19:57:44 +0000241 delete entry;
David Srbeckyafc60cd2018-12-05 11:59:31 +0000242 if (free_symfile) {
243 delete[] symfile;
244 }
David Srbecky67feb172015-12-17 19:57:44 +0000245}
246
David Srbecky0b21e412018-12-05 13:24:06 +0000247static std::map<const DexFile*, JITCodeEntry*> g_dex_debug_entries GUARDED_BY(g_dex_debug_lock);
David Srbeckyc684f332018-01-19 17:38:06 +0000248
David Srbeckyafc60cd2018-12-05 11:59:31 +0000249void AddNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) {
David Srbecky0b21e412018-12-05 13:24:06 +0000250 MutexLock mu(self, g_dex_debug_lock);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000251 DCHECK(dexfile != nullptr);
David Srbecky440a9b32018-02-15 17:47:29 +0000252 // This is just defensive check. The class linker should not register the dex file twice.
David Srbeckyafc60cd2018-12-05 11:59:31 +0000253 if (g_dex_debug_entries.count(dexfile) == 0) {
254 const ArrayRef<const uint8_t> symfile(dexfile->Begin(), dexfile->Size());
David Srbecky440a9b32018-02-15 17:47:29 +0000255 JITCodeEntry* entry = CreateJITCodeEntryInternal(__dex_debug_descriptor,
256 __dex_debug_register_code_ptr,
David Srbeckyafc60cd2018-12-05 11:59:31 +0000257 symfile,
258 /*copy_symfile=*/ false);
259 g_dex_debug_entries.emplace(dexfile, entry);
David Srbecky5cc349f2015-12-18 15:04:48 +0000260 }
David Srbeckyc684f332018-01-19 17:38:06 +0000261}
262
David Srbeckyafc60cd2018-12-05 11:59:31 +0000263void RemoveNativeDebugInfoForDex(Thread* self, const DexFile* dexfile) {
David Srbecky0b21e412018-12-05 13:24:06 +0000264 MutexLock mu(self, g_dex_debug_lock);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000265 auto it = g_dex_debug_entries.find(dexfile);
David Srbecky440a9b32018-02-15 17:47:29 +0000266 // We register dex files in the class linker and free them in DexFile_closeDexFile, but
267 // there might be cases where we load the dex file without using it in the class linker.
David Srbeckyafc60cd2018-12-05 11:59:31 +0000268 if (it != g_dex_debug_entries.end()) {
David Srbecky440a9b32018-02-15 17:47:29 +0000269 DeleteJITCodeEntryInternal(__dex_debug_descriptor,
270 __dex_debug_register_code_ptr,
David Srbeckyafc60cd2018-12-05 11:59:31 +0000271 /*entry=*/ it->second,
272 /*free_symfile=*/ false);
273 g_dex_debug_entries.erase(it);
David Srbecky440a9b32018-02-15 17:47:29 +0000274 }
David Srbeckyc684f332018-01-19 17:38:06 +0000275}
276
David Srbecky440a9b32018-02-15 17:47:29 +0000277// Mapping from handle to entry. Used to manage life-time of the entries.
David Srbecky76b9c692019-04-01 19:36:33 +0100278using JITCodeEntries = std::multimap<const void*, JITCodeEntry*>;
279static JITCodeEntries g_uncompressed_jit_debug_entries GUARDED_BY(g_jit_debug_lock);
280static JITCodeEntries g_compressed_jit_debug_entries GUARDED_BY(g_jit_debug_lock);
David Srbecky0b21e412018-12-05 13:24:06 +0000281
282// Number of entries added since last packing. Used to pack entries in bulk.
283static size_t g_jit_num_unpacked_entries GUARDED_BY(g_jit_debug_lock) = 0;
David Srbecky76b9c692019-04-01 19:36:33 +0100284static constexpr uint32_t kJitMaxUnpackedEntries = 64;
David Srbecky0b21e412018-12-05 13:24:06 +0000285
286// We postpone removal so that it is done in bulk.
David Srbecky76b9c692019-04-01 19:36:33 +0100287static std::set<const void*> g_jit_removed_entries GUARDED_BY(g_jit_debug_lock);
David Srbecky0b21e412018-12-05 13:24:06 +0000288
289// Split the JIT code cache into groups of fixed size and create singe JITCodeEntry for each group.
290// The start address of method's code determines which group it belongs to. The end is irrelevant.
291// As a consequnce, newly added mini debug infos will be merged and old ones (GCed) will be pruned.
David Srbecky76b9c692019-04-01 19:36:33 +0100292static void RepackEntries(PackElfFileForJITFunction pack,
293 InstructionSet isa,
294 const InstructionSetFeatures* features,
295 bool compress,
296 /*inout*/ JITCodeEntries* entries)
David Srbecky0b21e412018-12-05 13:24:06 +0000297 REQUIRES(g_jit_debug_lock) {
298 // Size of memory range covered by each JITCodeEntry.
299 // The number of methods per entry is variable (depending on how many fit in that range).
300 constexpr uint32_t kGroupSize = 64 * KB;
David Srbecky0b21e412018-12-05 13:24:06 +0000301
David Srbecky76b9c692019-04-01 19:36:33 +0100302 JITCodeEntries packed_entries;
303 std::vector<ArrayRef<const uint8_t>> added;
304 std::vector<const void*> removed;
305 while (!entries->empty()) {
306 const void* group_ptr = AlignDown(entries->begin()->first, kGroupSize);
307 const void* group_end = reinterpret_cast<const uint8_t*>(group_ptr) + kGroupSize;
David Srbecky0b21e412018-12-05 13:24:06 +0000308
David Srbecky0b21e412018-12-05 13:24:06 +0000309 // Collect all entries that have been added or removed within our memory range.
David Srbecky76b9c692019-04-01 19:36:33 +0100310 added.clear();
311 auto add_it = entries->begin();
312 for (; add_it != entries->end() && add_it->first < group_end; ++add_it) {
313 JITCodeEntry* entry = add_it->second;
314 added.emplace_back(entry->symfile_addr_, entry->symfile_size_);
David Srbecky0b21e412018-12-05 13:24:06 +0000315 }
David Srbecky76b9c692019-04-01 19:36:33 +0100316 removed.clear();
317 auto remove_it = g_jit_removed_entries.lower_bound(group_ptr);
318 for (; remove_it != g_jit_removed_entries.end() && *remove_it < group_end; ++remove_it) {
319 removed.push_back(*remove_it);
David Srbecky0b21e412018-12-05 13:24:06 +0000320 }
David Srbecky76b9c692019-04-01 19:36:33 +0100321 CHECK_GT(added.size(), 0u);
322 if (added.size() == 1 && removed.size() == 0) {
323 packed_entries.insert(entries->extract(entries->begin()));
David Srbecky0b21e412018-12-05 13:24:06 +0000324 continue; // Nothing changed in this memory range.
325 }
David Srbecky76b9c692019-04-01 19:36:33 +0100326
327 // Create new single JITCodeEntry that covers this memory range.
328 uint64_t start_time = MicroTime();
David Srbecky0b21e412018-12-05 13:24:06 +0000329 size_t symbols;
David Srbecky76b9c692019-04-01 19:36:33 +0100330 std::vector<uint8_t> packed = pack(isa, features, added, removed, compress, &symbols);
David Srbecky0b21e412018-12-05 13:24:06 +0000331 VLOG(jit)
David Srbecky76b9c692019-04-01 19:36:33 +0100332 << "JIT mini-debug-info repacked"
David Srbecky0b21e412018-12-05 13:24:06 +0000333 << " for " << group_ptr
David Srbecky76b9c692019-04-01 19:36:33 +0100334 << " in " << MicroTime() - start_time << "us"
335 << " files=" << added.size()
336 << " removed=" << removed.size()
David Srbecky0b21e412018-12-05 13:24:06 +0000337 << " symbols=" << symbols
David Srbecky76b9c692019-04-01 19:36:33 +0100338 << " size=" << PrettySize(packed.size())
339 << " compress=" << compress;
David Srbecky0b21e412018-12-05 13:24:06 +0000340
341 // Replace the old entries with the new one (with their lifetime temporally overlapping).
David Srbecky76b9c692019-04-01 19:36:33 +0100342 packed_entries.emplace(group_ptr, CreateJITCodeEntryInternal(
David Srbecky0b21e412018-12-05 13:24:06 +0000343 __jit_debug_descriptor,
344 __jit_debug_register_code_ptr,
345 ArrayRef<const uint8_t>(packed),
David Srbecky76b9c692019-04-01 19:36:33 +0100346 /*copy_symfile=*/ true));
347 for (auto it = entries->begin(); it != add_it; it = entries->erase(it)) {
David Srbecky0b21e412018-12-05 13:24:06 +0000348 DeleteJITCodeEntryInternal(__jit_debug_descriptor,
349 __jit_debug_register_code_ptr,
350 /*entry=*/ it->second,
351 /*free_symfile=*/ true);
352 }
David Srbecky0b21e412018-12-05 13:24:06 +0000353 }
David Srbecky76b9c692019-04-01 19:36:33 +0100354 entries->swap(packed_entries);
David Srbecky0b21e412018-12-05 13:24:06 +0000355}
David Srbecky440a9b32018-02-15 17:47:29 +0000356
David Srbeckyafc60cd2018-12-05 11:59:31 +0000357void AddNativeDebugInfoForJit(Thread* self,
358 const void* code_ptr,
David Srbecky0b21e412018-12-05 13:24:06 +0000359 const std::vector<uint8_t>& symfile,
360 PackElfFileForJITFunction pack,
361 InstructionSet isa,
362 const InstructionSetFeatures* features) {
363 MutexLock mu(self, g_jit_debug_lock);
David Srbecky440a9b32018-02-15 17:47:29 +0000364 DCHECK_NE(symfile.size(), 0u);
365
David Srbecky76b9c692019-04-01 19:36:33 +0100366 // Pack and compress all entries. This will run on first compilation after a GC.
367 // Must be done before addition in case the added code_ptr is in the removed set.
368 if (!g_jit_removed_entries.empty()) {
369 g_compressed_jit_debug_entries.merge(g_uncompressed_jit_debug_entries);
370 RepackEntries(pack, isa, features, /*compress=*/ true, &g_compressed_jit_debug_entries);
371 g_jit_removed_entries.clear();
372 g_jit_num_unpacked_entries = 0;
373 }
David Srbecky0b21e412018-12-05 13:24:06 +0000374
David Srbecky440a9b32018-02-15 17:47:29 +0000375 JITCodeEntry* entry = CreateJITCodeEntryInternal(
376 __jit_debug_descriptor,
377 __jit_debug_register_code_ptr,
David Srbeckyafc60cd2018-12-05 11:59:31 +0000378 ArrayRef<const uint8_t>(symfile),
379 /*copy_symfile=*/ true);
David Srbecky440a9b32018-02-15 17:47:29 +0000380
David Srbecky0b21e412018-12-05 13:24:06 +0000381 VLOG(jit)
382 << "JIT mini-debug-info added"
383 << " for " << code_ptr
384 << " size=" << PrettySize(symfile.size());
385
David Srbeckyafc60cd2018-12-05 11:59:31 +0000386 // We don't provide code_ptr for type debug info, which means we cannot free it later.
David Srbecky440a9b32018-02-15 17:47:29 +0000387 // (this only happens when --generate-debug-info flag is enabled for the purpose
388 // of being debugged with gdb; it does not happen for debuggable apps by default).
David Srbecky76b9c692019-04-01 19:36:33 +0100389 if (code_ptr == nullptr) {
390 return;
391 }
392
393 g_uncompressed_jit_debug_entries.emplace(code_ptr, entry);
394 // Count how many entries we have added since the last mini-debug-info packing.
395 // We avoid g_uncompressed_jit_debug_entries.size() because it can shrink during packing.
396 ++g_jit_num_unpacked_entries;
397
398 // Pack (but don't compress) recent entries - this is cheap and reduces memory use by ~4x.
399 // We delay compression until after GC since it is more expensive (and saves further ~4x).
400 if (g_jit_num_unpacked_entries >= kJitMaxUnpackedEntries) {
401 RepackEntries(pack, isa, features, /*compress=*/ false, &g_uncompressed_jit_debug_entries);
402 g_jit_num_unpacked_entries = 0;
David Srbecky440a9b32018-02-15 17:47:29 +0000403 }
404}
405
David Srbeckyafc60cd2018-12-05 11:59:31 +0000406void RemoveNativeDebugInfoForJit(Thread* self, const void* code_ptr) {
David Srbecky0b21e412018-12-05 13:24:06 +0000407 MutexLock mu(self, g_jit_debug_lock);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000408 // We generate JIT native debug info only if the right runtime flags are enabled,
409 // but we try to remove it unconditionally whenever code is freed from JIT cache.
David Srbecky76b9c692019-04-01 19:36:33 +0100410 if (!g_uncompressed_jit_debug_entries.empty() || !g_compressed_jit_debug_entries.empty()) {
411 g_jit_removed_entries.insert(code_ptr);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000412 }
413}
414
415size_t GetJitMiniDebugInfoMemUsage() {
David Srbecky0b21e412018-12-05 13:24:06 +0000416 MutexLock mu(Thread::Current(), g_jit_debug_lock);
David Srbeckyafc60cd2018-12-05 11:59:31 +0000417 size_t size = 0;
David Srbecky76b9c692019-04-01 19:36:33 +0100418 for (const auto& entries : {g_uncompressed_jit_debug_entries, g_compressed_jit_debug_entries}) {
419 for (const auto& entry : entries) {
420 size += sizeof(JITCodeEntry) + entry.second->symfile_size_ + /*map entry*/ 4 * sizeof(void*);
421 }
David Srbeckyafc60cd2018-12-05 11:59:31 +0000422 }
423 return size;
David Srbecky5cc349f2015-12-18 15:04:48 +0000424}
425
David Srbecky1ed45152019-04-09 18:10:26 +0100426Mutex* GetNativeDebugInfoLock() {
427 return &g_jit_debug_lock;
428}
429
David Srbecky67feb172015-12-17 19:57:44 +0000430} // namespace art