blob: 0e295e244279bb31760f2f4732e6b3e9d08c2cfd [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 Srbecky5cc349f2015-12-18 15:04:48 +000021#include "base/mutex.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070022#include "thread-current-inl.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000023#include "thread.h"
24
25#include <unordered_map>
26
David Srbecky67feb172015-12-17 19:57:44 +000027namespace art {
28
29// -------------------------------------------------------------------
30// Binary GDB JIT Interface as described in
31// http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
32// -------------------------------------------------------------------
33extern "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 Srbeckyc684f332018-01-19 17:38:06 +000045 uint32_t ref_count; // ART internal field.
David Srbecky67feb172015-12-17 19:57:44 +000046 };
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 Srbeckye8b4e852016-03-15 17:02:41 +000063 // 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 Srbecky67feb172015-12-17 19:57:44 +000067 // 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 Srbeckyc684f332018-01-19 17:38:06 +000073Mutex g_jit_debug_mutex("JIT debug interface lock", kJitDebugInterfaceLock);
David Srbecky5cc349f2015-12-18 15:04:48 +000074
David Srbeckyc684f332018-01-19 17:38:06 +000075static size_t g_jit_debug_mem_usage = 0;
76
77JITCodeEntry* CreateJITCodeEntry(const std::vector<uint8_t>& symfile) {
Vladimir Marko93205e32016-04-13 11:59:46 +010078 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 Srbecky5cc349f2015-12-18 15:04:48 +000084
David Srbecky67feb172015-12-17 19:57:44 +000085 JITCodeEntry* entry = new JITCodeEntry;
Vladimir Marko93205e32016-04-13 11:59:46 +010086 CHECK(entry != nullptr);
87 entry->symfile_addr_ = symfile_copy;
88 entry->symfile_size_ = symfile.size();
David Srbecky67feb172015-12-17 19:57:44 +000089 entry->prev_ = nullptr;
David Srbeckyc684f332018-01-19 17:38:06 +000090 entry->ref_count = 0;
David Srbecky67feb172015-12-17 19:57:44 +000091 entry->next_ = __jit_debug_descriptor.first_entry_;
92 if (entry->next_ != nullptr) {
93 entry->next_->prev_ = entry;
94 }
David Srbeckyc684f332018-01-19 17:38:06 +000095 g_jit_debug_mem_usage += sizeof(JITCodeEntry) + entry->symfile_size_;
David Srbecky67feb172015-12-17 19:57:44 +000096 __jit_debug_descriptor.first_entry_ = entry;
97 __jit_debug_descriptor.relevant_entry_ = entry;
David Srbecky67feb172015-12-17 19:57:44 +000098 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
David Srbeckye8b4e852016-03-15 17:02:41 +000099 (*__jit_debug_register_code_ptr)();
David Srbecky67feb172015-12-17 19:57:44 +0000100 return entry;
101}
102
David Srbeckyc684f332018-01-19 17:38:06 +0000103void DeleteJITCodeEntry(JITCodeEntry* entry) {
David Srbecky67feb172015-12-17 19:57:44 +0000104 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 Srbeckyc684f332018-01-19 17:38:06 +0000114 g_jit_debug_mem_usage -= sizeof(JITCodeEntry) + entry->symfile_size_;
David Srbecky67feb172015-12-17 19:57:44 +0000115 __jit_debug_descriptor.relevant_entry_ = entry;
116 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
David Srbeckye8b4e852016-03-15 17:02:41 +0000117 (*__jit_debug_register_code_ptr)();
David Srbecky5cc349f2015-12-18 15:04:48 +0000118 delete[] entry->symfile_addr_;
David Srbecky67feb172015-12-17 19:57:44 +0000119 delete entry;
120}
121
David Srbeckyc684f332018-01-19 17:38:06 +0000122// Mapping from code address to entry. Used to manage life-time of the entries.
123static std::unordered_map<uintptr_t, JITCodeEntry*> g_jit_code_entries
124 GUARDED_BY(g_jit_debug_mutex);
125
126void 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 Srbecky5cc349f2015-12-18 15:04:48 +0000131}
132
David Srbeckyc684f332018-01-19 17:38:06 +0000133void 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 Srbecky5cc349f2015-12-18 15:04:48 +0000138 }
David Srbeckyc684f332018-01-19 17:38:06 +0000139 g_jit_code_entries.erase(code_address);
140}
141
142JITCodeEntry* 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
147size_t GetJITCodeEntryMemUsage() {
148 return g_jit_debug_mem_usage + g_jit_code_entries.size() * 2 * sizeof(void*);
David Srbecky5cc349f2015-12-18 15:04:48 +0000149}
150
David Srbecky67feb172015-12-17 19:57:44 +0000151} // namespace art