blob: cd303cb0768f4a63ba52987c19e774cbe03217ba [file] [log] [blame]
Andrew MacPherson17220c12014-03-05 10:12:43 +00001//===-- JITLoaderGDB.cpp ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11
12#include "lldb/Breakpoint/Breakpoint.h"
13#include "lldb/Core/PluginManager.h"
14#include "lldb/Core/Log.h"
15#include "lldb/Core/Module.h"
16#include "lldb/Core/ModuleSpec.h"
17#include "lldb/Core/Section.h"
18#include "lldb/Core/StreamString.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Target/SectionLoadList.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Symbol/SymbolVendor.h"
23
24#include "JITLoaderGDB.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29//------------------------------------------------------------------
30// Debug Interface Structures
31//------------------------------------------------------------------
32typedef enum
33{
34 JIT_NOACTION = 0,
35 JIT_REGISTER_FN,
36 JIT_UNREGISTER_FN
37} jit_actions_t;
38
Todd Fiala49bc2d9d2014-09-15 19:55:27 +000039#pragma pack(push, 4)
40template <typename ptr_t>
Andrew MacPherson17220c12014-03-05 10:12:43 +000041struct jit_code_entry
42{
Todd Fiala49bc2d9d2014-09-15 19:55:27 +000043 ptr_t next_entry; // pointer
44 ptr_t prev_entry; // pointer
45 ptr_t symfile_addr; // pointer
Andrew MacPherson17220c12014-03-05 10:12:43 +000046 uint64_t symfile_size;
47};
Todd Fiala49bc2d9d2014-09-15 19:55:27 +000048template <typename ptr_t>
Andrew MacPherson17220c12014-03-05 10:12:43 +000049struct jit_descriptor
50{
51 uint32_t version;
52 uint32_t action_flag; // Values are jit_action_t
Todd Fiala49bc2d9d2014-09-15 19:55:27 +000053 ptr_t relevant_entry; // pointer
54 ptr_t first_entry; // pointer
Andrew MacPherson17220c12014-03-05 10:12:43 +000055};
Todd Fiala49bc2d9d2014-09-15 19:55:27 +000056#pragma pack(pop)
Andrew MacPherson17220c12014-03-05 10:12:43 +000057
58JITLoaderGDB::JITLoaderGDB (lldb_private::Process *process) :
59 JITLoader(process),
60 m_jit_objects(),
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +000061 m_jit_break_id(LLDB_INVALID_BREAK_ID),
62 m_jit_descriptor_addr(LLDB_INVALID_ADDRESS)
Andrew MacPherson17220c12014-03-05 10:12:43 +000063{
Andrew MacPherson17220c12014-03-05 10:12:43 +000064}
65
66JITLoaderGDB::~JITLoaderGDB ()
67{
68 if (LLDB_BREAK_ID_IS_VALID(m_jit_break_id))
69 m_process->GetTarget().RemoveBreakpointByID (m_jit_break_id);
Andrew MacPherson17220c12014-03-05 10:12:43 +000070}
71
72void JITLoaderGDB::DidAttach()
73{
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +000074 Target &target = m_process->GetTarget();
75 ModuleList &module_list = target.GetImages();
76 SetJITBreakpoint(module_list);
Andrew MacPherson17220c12014-03-05 10:12:43 +000077}
78
79void JITLoaderGDB::DidLaunch()
80{
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +000081 Target &target = m_process->GetTarget();
82 ModuleList &module_list = target.GetImages();
83 SetJITBreakpoint(module_list);
84}
85
86void
87JITLoaderGDB::ModulesDidLoad(ModuleList &module_list)
88{
89 if (!DidSetJITBreakpoint() && m_process->IsAlive())
90 SetJITBreakpoint(module_list);
Andrew MacPherson17220c12014-03-05 10:12:43 +000091}
92
93//------------------------------------------------------------------
94// Setup the JIT Breakpoint
95//------------------------------------------------------------------
96void
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +000097JITLoaderGDB::SetJITBreakpoint(lldb_private::ModuleList &module_list)
Andrew MacPherson17220c12014-03-05 10:12:43 +000098{
99 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
100
101 if ( DidSetJITBreakpoint() )
102 return;
103
104 if (log)
105 log->Printf("JITLoaderGDB::%s looking for JIT register hook",
106 __FUNCTION__);
107
Greg Clayton48672af2014-06-24 22:22:43 +0000108 addr_t jit_addr = GetSymbolAddress(module_list,
109 ConstString("__jit_debug_register_code"),
110 eSymbolTypeAny);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000111 if (jit_addr == LLDB_INVALID_ADDRESS)
112 return;
113
Greg Clayton48672af2014-06-24 22:22:43 +0000114 m_jit_descriptor_addr = GetSymbolAddress(module_list,
115 ConstString("__jit_debug_descriptor"),
116 eSymbolTypeData);
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +0000117 if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS)
118 {
119 if (log)
120 log->Printf(
121 "JITLoaderGDB::%s failed to find JIT descriptor address",
122 __FUNCTION__);
123 return;
124 }
125
Andrew MacPherson17220c12014-03-05 10:12:43 +0000126 if (log)
127 log->Printf("JITLoaderGDB::%s setting JIT breakpoint",
128 __FUNCTION__);
129
130 Breakpoint *bp =
131 m_process->GetTarget().CreateBreakpoint(jit_addr, true, false).get();
132 bp->SetCallback(JITDebugBreakpointHit, this, true);
133 bp->SetBreakpointKind("jit-debug-register");
134 m_jit_break_id = bp->GetID();
135
136 ReadJITDescriptor(true);
137}
138
139bool
140JITLoaderGDB::JITDebugBreakpointHit(void *baton,
141 StoppointCallbackContext *context,
142 user_id_t break_id, user_id_t break_loc_id)
143{
144 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
145 if (log)
146 log->Printf("JITLoaderGDB::%s hit JIT breakpoint",
147 __FUNCTION__);
148 JITLoaderGDB *instance = static_cast<JITLoaderGDB *>(baton);
149 return instance->ReadJITDescriptor(false);
150}
151
Greg Clayton48672af2014-06-24 22:22:43 +0000152static void updateSectionLoadAddress(const SectionList &section_list,
153 Target &target,
154 uint64_t symbolfile_addr,
155 uint64_t symbolfile_size,
156 uint64_t &vmaddrheuristic,
157 uint64_t &min_addr,
158 uint64_t &max_addr)
159{
160 const uint32_t num_sections = section_list.GetSize();
161 for (uint32_t i = 0; i<num_sections; ++i)
162 {
163 SectionSP section_sp(section_list.GetSectionAtIndex(i));
164 if (section_sp)
165 {
166 if(section_sp->IsFake()) {
167 uint64_t lower = (uint64_t)-1;
168 uint64_t upper = 0;
169 updateSectionLoadAddress(section_sp->GetChildren(), target, symbolfile_addr, symbolfile_size, vmaddrheuristic,
170 lower, upper);
171 if (lower < min_addr)
172 min_addr = lower;
173 if (upper > max_addr)
174 max_addr = upper;
175 const lldb::addr_t slide_amount = lower - section_sp->GetFileAddress();
176 section_sp->Slide(slide_amount, false);
177 section_sp->GetChildren().Slide(-slide_amount, false);
178 section_sp->SetByteSize (upper - lower);
179 } else {
180 vmaddrheuristic += 2<<section_sp->GetLog2Align();
181 uint64_t lower;
182 if (section_sp->GetFileAddress() > vmaddrheuristic)
183 lower = section_sp->GetFileAddress();
184 else {
185 lower = symbolfile_addr+section_sp->GetFileOffset();
186 section_sp->SetFileAddress(symbolfile_addr+section_sp->GetFileOffset());
187 }
188 target.SetSectionLoadAddress(section_sp, lower, true);
189 uint64_t upper = lower + section_sp->GetByteSize();
190 if (lower < min_addr)
191 min_addr = lower;
192 if (upper > max_addr)
193 max_addr = upper;
194 // This is an upper bound, but a good enough heuristic
195 vmaddrheuristic += section_sp->GetByteSize();
196 }
197 }
198 }
199}
200
Andrew MacPherson17220c12014-03-05 10:12:43 +0000201bool
202JITLoaderGDB::ReadJITDescriptor(bool all_entries)
203{
Todd Fiala49bc2d9d2014-09-15 19:55:27 +0000204 Target &target = m_process->GetTarget();
205 if (target.GetArchitecture().GetAddressByteSize() == 8)
206 return ReadJITDescriptorImpl<uint64_t>(all_entries);
207 else
208 return ReadJITDescriptorImpl<uint32_t>(all_entries);
209}
210
211template <typename ptr_t>
212bool
213JITLoaderGDB::ReadJITDescriptorImpl(bool all_entries)
214{
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +0000215 if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS)
216 return false;
217
Andrew MacPherson17220c12014-03-05 10:12:43 +0000218 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
219 Target &target = m_process->GetTarget();
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +0000220 ModuleList &module_list = target.GetImages();
Andrew MacPherson17220c12014-03-05 10:12:43 +0000221
Todd Fiala49bc2d9d2014-09-15 19:55:27 +0000222 jit_descriptor<ptr_t> jit_desc;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000223 const size_t jit_desc_size = sizeof(jit_desc);
224 Error error;
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +0000225 size_t bytes_read = m_process->DoReadMemory(
226 m_jit_descriptor_addr, &jit_desc, jit_desc_size, error);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000227 if (bytes_read != jit_desc_size || !error.Success())
228 {
229 if (log)
230 log->Printf("JITLoaderGDB::%s failed to read JIT descriptor",
231 __FUNCTION__);
232 return false;
233 }
234
235 jit_actions_t jit_action = (jit_actions_t)jit_desc.action_flag;
236 addr_t jit_relevant_entry = (addr_t)jit_desc.relevant_entry;
237 if (all_entries)
238 {
239 jit_action = JIT_REGISTER_FN;
240 jit_relevant_entry = (addr_t)jit_desc.first_entry;
241 }
242
243 while (jit_relevant_entry != 0)
244 {
Todd Fiala49bc2d9d2014-09-15 19:55:27 +0000245 jit_code_entry<ptr_t> jit_entry;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000246 const size_t jit_entry_size = sizeof(jit_entry);
247 bytes_read = m_process->DoReadMemory(jit_relevant_entry, &jit_entry, jit_entry_size, error);
248 if (bytes_read != jit_entry_size || !error.Success())
249 {
250 if (log)
251 log->Printf(
252 "JITLoaderGDB::%s failed to read JIT entry at 0x%" PRIx64,
253 __FUNCTION__, jit_relevant_entry);
254 return false;
255 }
256
257 const addr_t &symbolfile_addr = (addr_t)jit_entry.symfile_addr;
258 const size_t &symbolfile_size = (size_t)jit_entry.symfile_size;
259 ModuleSP module_sp;
260
261 if (jit_action == JIT_REGISTER_FN)
262 {
263 if (log)
264 log->Printf(
265 "JITLoaderGDB::%s registering JIT entry at 0x%" PRIx64
266 " (%" PRIu64 " bytes)",
Jason Molendab509a412014-03-05 23:48:15 +0000267 __FUNCTION__, symbolfile_addr, (uint64_t) symbolfile_size);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000268
269 char jit_name[64];
270 snprintf(jit_name, 64, "JIT(0x%" PRIx64 ")", symbolfile_addr);
271 module_sp = m_process->ReadModuleFromMemory(
272 FileSpec(jit_name, false), symbolfile_addr, symbolfile_size);
273
274 if (module_sp && module_sp->GetObjectFile())
275 {
276 bool changed;
Greg Clayton48672af2014-06-24 22:22:43 +0000277 m_jit_objects.insert(std::make_pair(symbolfile_addr, module_sp));
278 if (module_sp->GetObjectFile()->GetPluginName() == ConstString("mach-o"))
279 {
280 ObjectFile *image_object_file = module_sp->GetObjectFile();
281 if (image_object_file)
282 {
283 const SectionList *section_list = image_object_file->GetSectionList ();
284 if (section_list)
285 {
286 uint64_t vmaddrheuristic = 0;
287 uint64_t lower = (uint64_t)-1;
288 uint64_t upper = 0;
289 updateSectionLoadAddress(*section_list, target, symbolfile_addr, symbolfile_size,
290 vmaddrheuristic, lower, upper);
291 }
292 }
293 }
294 else
295 {
296 module_sp->SetLoadAddress(target, 0, true, changed);
297 }
Andrew MacPherson17220c12014-03-05 10:12:43 +0000298
299 // load the symbol table right away
300 module_sp->GetObjectFile()->GetSymtab();
301
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +0000302 module_list.AppendIfNeeded(module_sp);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000303
304 ModuleList module_list;
305 module_list.Append(module_sp);
306 target.ModulesDidLoad(module_list);
307 }
308 else
309 {
310 if (log)
311 log->Printf("JITLoaderGDB::%s failed to load module for "
312 "JIT entry at 0x%" PRIx64,
313 __FUNCTION__, symbolfile_addr);
314 }
315 }
316 else if (jit_action == JIT_UNREGISTER_FN)
317 {
318 if (log)
319 log->Printf(
320 "JITLoaderGDB::%s unregistering JIT entry at 0x%" PRIx64,
321 __FUNCTION__, symbolfile_addr);
322
323 JITObjectMap::iterator it = m_jit_objects.find(symbolfile_addr);
324 if (it != m_jit_objects.end())
325 {
326 module_sp = it->second;
327 ObjectFile *image_object_file = module_sp->GetObjectFile();
328 if (image_object_file)
329 {
330 const SectionList *section_list = image_object_file->GetSectionList ();
331 if (section_list)
332 {
333 const uint32_t num_sections = section_list->GetSize();
334 for (uint32_t i = 0; i<num_sections; ++i)
335 {
336 SectionSP section_sp(section_list->GetSectionAtIndex(i));
337 if (section_sp)
338 {
339 target.GetSectionLoadList().SetSectionUnloaded (section_sp);
340 }
341 }
342 }
343 }
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +0000344 module_list.Remove(module_sp);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000345 m_jit_objects.erase(it);
346 }
347 }
348 else if (jit_action == JIT_NOACTION)
349 {
350 // Nothing to do
351 }
352 else
353 {
354 assert(false && "Unknown jit action");
355 }
356
357 if (all_entries)
358 jit_relevant_entry = (addr_t)jit_entry.next_entry;
359 else
360 jit_relevant_entry = 0;
361 }
362
363 return false; // Continue Running.
364}
365
366//------------------------------------------------------------------
367// PluginInterface protocol
368//------------------------------------------------------------------
369lldb_private::ConstString
370JITLoaderGDB::GetPluginNameStatic()
371{
372 static ConstString g_name("gdb");
373 return g_name;
374}
375
376JITLoaderSP
377JITLoaderGDB::CreateInstance(Process *process, bool force)
378{
Greg Clayton521c2272014-04-07 20:13:57 +0000379 JITLoaderSP jit_loader_sp;
380 ArchSpec arch (process->GetTarget().GetArchitecture());
381 if (arch.GetTriple().getVendor() != llvm::Triple::Apple)
382 jit_loader_sp.reset(new JITLoaderGDB(process));
Andrew MacPherson17220c12014-03-05 10:12:43 +0000383 return jit_loader_sp;
384}
385
386const char *
387JITLoaderGDB::GetPluginDescriptionStatic()
388{
389 return "JIT loader plug-in that watches for JIT events using the GDB interface.";
390}
391
392lldb_private::ConstString
393JITLoaderGDB::GetPluginName()
394{
395 return GetPluginNameStatic();
396}
397
398uint32_t
399JITLoaderGDB::GetPluginVersion()
400{
401 return 1;
402}
403
404void
405JITLoaderGDB::Initialize()
406{
407 PluginManager::RegisterPlugin (GetPluginNameStatic(),
408 GetPluginDescriptionStatic(),
409 CreateInstance);
410}
411
412void
413JITLoaderGDB::Terminate()
414{
415 PluginManager::UnregisterPlugin (CreateInstance);
416}
417
418bool
419JITLoaderGDB::DidSetJITBreakpoint() const
420{
421 return LLDB_BREAK_ID_IS_VALID(m_jit_break_id);
422}
423
Andrew MacPherson17220c12014-03-05 10:12:43 +0000424addr_t
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +0000425JITLoaderGDB::GetSymbolAddress(ModuleList &module_list, const ConstString &name,
426 SymbolType symbol_type) const
Andrew MacPherson17220c12014-03-05 10:12:43 +0000427{
428 SymbolContextList target_symbols;
429 Target &target = m_process->GetTarget();
Andrew MacPherson17220c12014-03-05 10:12:43 +0000430
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +0000431 if (!module_list.FindSymbolsWithNameAndType(name, symbol_type,
432 target_symbols))
Andrew MacPherson17220c12014-03-05 10:12:43 +0000433 return LLDB_INVALID_ADDRESS;
434
435 SymbolContext sym_ctx;
436 target_symbols.GetContextAtIndex(0, sym_ctx);
437
438 const Address *jit_descriptor_addr = &sym_ctx.symbol->GetAddress();
439 if (!jit_descriptor_addr || !jit_descriptor_addr->IsValid())
440 return LLDB_INVALID_ADDRESS;
441
442 const addr_t jit_addr = jit_descriptor_addr->GetLoadAddress(&target);
443 return jit_addr;
444}