blob: f67c8cef692102e6fb239eb931728d17c7f462e2 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- Target.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#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
20#include "lldb/Core/Event.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Core/Timer.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Host/Host.h"
25#include "lldb/lldb-private-log.h"
26#include "lldb/Symbol/ObjectFile.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Core/Debugger.h"
29
30using namespace lldb;
31using namespace lldb_private;
32
33//----------------------------------------------------------------------
34// Target constructor
35//----------------------------------------------------------------------
Greg Clayton66111032010-06-23 01:19:2936Target::Target(Debugger &debugger) :
Chris Lattner30fdc8d2010-06-08 16:52:2437 Broadcaster("Target"),
Caroline Ticedaccaa92010-09-20 20:44:4338 TargetInstanceSettings (*(Target::GetSettingsController().get())),
Greg Clayton66111032010-06-23 01:19:2939 m_debugger (debugger),
Chris Lattner30fdc8d2010-06-08 16:52:2440 m_images(),
Greg Claytonf5e56de2010-09-14 23:36:4041 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2442 m_breakpoint_list (false),
43 m_internal_breakpoint_list (true),
44 m_process_sp(),
45 m_triple(),
46 m_search_filter_sp(),
47 m_image_search_paths (ImageSearchPathsChanged, this),
48 m_scratch_ast_context_ap(NULL)
49{
50 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
51 if (log)
52 log->Printf ("%p Target::Target()", this);
53}
54
55//----------------------------------------------------------------------
56// Destructor
57//----------------------------------------------------------------------
58Target::~Target()
59{
60 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
61 if (log)
62 log->Printf ("%p Target::~Target()", this);
63 DeleteCurrentProcess ();
64}
65
66void
67Target::Dump (Stream *s)
68{
69 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
70 s->Indent();
71 s->PutCString("Target\n");
72 s->IndentMore();
73 m_images.Dump(s);
74 m_breakpoint_list.Dump(s);
75 m_internal_breakpoint_list.Dump(s);
76// if (m_process_sp.get())
77// m_process_sp->Dump(s);
78 s->IndentLess();
79}
80
81void
82Target::DeleteCurrentProcess ()
83{
84 if (m_process_sp.get())
85 {
Greg Clayton17f69202010-09-14 23:52:4386 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:2487 if (m_process_sp->IsAlive())
88 m_process_sp->Destroy();
89 else
90 m_process_sp->Finalize();
91
92 // Do any cleanup of the target we need to do between process instances.
93 // NB It is better to do this before destroying the process in case the
94 // clean up needs some help from the process.
95 m_breakpoint_list.ClearAllBreakpointSites();
96 m_internal_breakpoint_list.ClearAllBreakpointSites();
97 m_process_sp.reset();
98 }
99}
100
101const lldb::ProcessSP &
102Target::CreateProcess (Listener &listener, const char *plugin_name)
103{
104 DeleteCurrentProcess ();
105 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
106 return m_process_sp;
107}
108
109const lldb::ProcessSP &
110Target::GetProcessSP () const
111{
112 return m_process_sp;
113}
114
115lldb::TargetSP
116Target::GetSP()
117{
Greg Clayton66111032010-06-23 01:19:29118 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24119}
120
121BreakpointList &
122Target::GetBreakpointList(bool internal)
123{
124 if (internal)
125 return m_internal_breakpoint_list;
126 else
127 return m_breakpoint_list;
128}
129
130const BreakpointList &
131Target::GetBreakpointList(bool internal) const
132{
133 if (internal)
134 return m_internal_breakpoint_list;
135 else
136 return m_breakpoint_list;
137}
138
139BreakpointSP
140Target::GetBreakpointByID (break_id_t break_id)
141{
142 BreakpointSP bp_sp;
143
144 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
145 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
146 else
147 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
148
149 return bp_sp;
150}
151
152BreakpointSP
153Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
154{
155 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
156 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
157 return CreateBreakpoint (filter_sp, resolver_sp, internal);
158}
159
160
161BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41162Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24163{
Chris Lattner30fdc8d2010-06-08 16:52:24164 Address so_addr;
165 // Attempt to resolve our load address if possible, though it is ok if
166 // it doesn't resolve to section/offset.
167
Greg Clayton1b72fcb2010-08-24 00:45:41168 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40169 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41170 if (!so_addr.IsValid())
171 {
172 // The address didn't resolve, so just set this as an absolute address
173 so_addr.SetOffset (addr);
174 }
175 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24176 return bp_sp;
177}
178
179BreakpointSP
180Target::CreateBreakpoint (Address &addr, bool internal)
181{
182 TargetSP target_sp = this->GetSP();
183 SearchFilterSP filter_sp(new SearchFilter (target_sp));
184 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
185 return CreateBreakpoint (filter_sp, resolver_sp, internal);
186}
187
188BreakpointSP
Greg Clayton0c5cd902010-06-28 21:30:43189Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24190{
Greg Clayton0c5cd902010-06-28 21:30:43191 BreakpointSP bp_sp;
192 if (func_name)
193 {
194 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
195 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
196 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
197 }
198 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24199}
200
201
202SearchFilterSP
203Target::GetSearchFilterForModule (const FileSpec *containingModule)
204{
205 SearchFilterSP filter_sp;
206 lldb::TargetSP target_sp = this->GetSP();
207 if (containingModule != NULL)
208 {
209 // TODO: We should look into sharing module based search filters
210 // across many breakpoints like we do for the simple target based one
211 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
212 }
213 else
214 {
215 if (m_search_filter_sp.get() == NULL)
216 m_search_filter_sp.reset (new SearchFilter (target_sp));
217 filter_sp = m_search_filter_sp;
218 }
219 return filter_sp;
220}
221
222BreakpointSP
223Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
224{
225 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
226 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
227
228 return CreateBreakpoint (filter_sp, resolver_sp, internal);
229}
230
231BreakpointSP
232Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
233{
234 BreakpointSP bp_sp;
235 if (filter_sp && resolver_sp)
236 {
237 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
238 resolver_sp->SetBreakpoint (bp_sp.get());
239
240 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17241 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24242 else
Greg Clayton9fed0d82010-07-23 23:33:17243 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24244
245 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
246 if (log)
247 {
248 StreamString s;
249 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
250 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
251 }
252
Chris Lattner30fdc8d2010-06-08 16:52:24253 bp_sp->ResolveBreakpoint();
254 }
255 return bp_sp;
256}
257
258void
259Target::RemoveAllBreakpoints (bool internal_also)
260{
261 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
262 if (log)
263 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
264
Greg Clayton9fed0d82010-07-23 23:33:17265 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24266 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17267 m_internal_breakpoint_list.RemoveAll (false);
Chris Lattner30fdc8d2010-06-08 16:52:24268}
269
270void
271Target::DisableAllBreakpoints (bool internal_also)
272{
273 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
274 if (log)
275 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
276
277 m_breakpoint_list.SetEnabledAll (false);
278 if (internal_also)
279 m_internal_breakpoint_list.SetEnabledAll (false);
280}
281
282void
283Target::EnableAllBreakpoints (bool internal_also)
284{
285 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
286 if (log)
287 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
288
289 m_breakpoint_list.SetEnabledAll (true);
290 if (internal_also)
291 m_internal_breakpoint_list.SetEnabledAll (true);
292}
293
294bool
295Target::RemoveBreakpointByID (break_id_t break_id)
296{
297 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
298 if (log)
299 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
300
301 if (DisableBreakpointByID (break_id))
302 {
303 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17304 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24305 else
Greg Clayton9fed0d82010-07-23 23:33:17306 m_breakpoint_list.Remove(break_id, true);
Chris Lattner30fdc8d2010-06-08 16:52:24307 return true;
308 }
309 return false;
310}
311
312bool
313Target::DisableBreakpointByID (break_id_t break_id)
314{
315 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
316 if (log)
317 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
318
319 BreakpointSP bp_sp;
320
321 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
322 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
323 else
324 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
325 if (bp_sp)
326 {
327 bp_sp->SetEnabled (false);
328 return true;
329 }
330 return false;
331}
332
333bool
334Target::EnableBreakpointByID (break_id_t break_id)
335{
336 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
337 if (log)
338 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
339 __FUNCTION__,
340 break_id,
341 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
342
343 BreakpointSP bp_sp;
344
345 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
346 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
347 else
348 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
349
350 if (bp_sp)
351 {
352 bp_sp->SetEnabled (true);
353 return true;
354 }
355 return false;
356}
357
358ModuleSP
359Target::GetExecutableModule ()
360{
361 ModuleSP executable_sp;
362 if (m_images.GetSize() > 0)
363 executable_sp = m_images.GetModuleAtIndex(0);
364 return executable_sp;
365}
366
367void
368Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
369{
370 m_images.Clear();
371 m_scratch_ast_context_ap.reset();
372
373 if (executable_sp.get())
374 {
375 Timer scoped_timer (__PRETTY_FUNCTION__,
376 "Target::SetExecutableModule (executable = '%s/%s')",
377 executable_sp->GetFileSpec().GetDirectory().AsCString(),
378 executable_sp->GetFileSpec().GetFilename().AsCString());
379
380 m_images.Append(executable_sp); // The first image is our exectuable file
381
382 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02383 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
384 if (!m_arch_spec.IsValid())
385 m_arch_spec = exe_arch;
386
Chris Lattner30fdc8d2010-06-08 16:52:24387 FileSpecList dependent_files;
388 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
389 if (executable_objfile == NULL)
390 {
391
392 FileSpec bundle_executable(executable_sp->GetFileSpec());
393 if (Host::ResolveExecutableInBundle (&bundle_executable))
394 {
395 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
396 exe_arch));
397 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
398 if (bundle_exe_module_sp->GetObjectFile() != NULL)
399 executable_sp = bundle_exe_module_sp;
400 return;
401 }
402 }
403
404 if (executable_objfile)
405 {
406 executable_objfile->GetDependentModules(dependent_files);
407 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
408 {
409 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
410 exe_arch));
411 if (image_module_sp.get())
412 {
413 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
414 ObjectFile *objfile = image_module_sp->GetObjectFile();
415 if (objfile)
416 objfile->GetDependentModules(dependent_files);
417 }
418 }
419 }
420
421 // Now see if we know the target triple, and if so, create our scratch AST context:
422 ConstString target_triple;
423 if (GetTargetTriple(target_triple))
424 {
425 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString()));
426 }
427 }
428}
429
430
431ModuleList&
432Target::GetImages ()
433{
434 return m_images;
435}
436
437ArchSpec
438Target::GetArchitecture () const
439{
Jim Ingham5aee1622010-08-09 23:31:02440 return m_arch_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24441}
442
Jim Ingham5aee1622010-08-09 23:31:02443bool
444Target::SetArchitecture (const ArchSpec &arch_spec)
445{
446 if (m_arch_spec == arch_spec)
447 {
448 // If we're setting the architecture to our current architecture, we
449 // don't need to do anything.
450 return true;
451 }
452 else if (!m_arch_spec.IsValid())
453 {
454 // If we haven't got a valid arch spec, then we just need to set it.
455 m_arch_spec = arch_spec;
456 return true;
457 }
458 else
459 {
460 // If we have an executable file, try to reset the executable to the desired architecture
461 m_arch_spec = arch_spec;
462 ModuleSP executable_sp = GetExecutableModule ();
463 m_images.Clear();
464 m_scratch_ast_context_ap.reset();
465 m_triple.Clear();
466 // Need to do something about unsetting breakpoints.
467
468 if (executable_sp)
469 {
470 FileSpec exec_file_spec = executable_sp->GetFileSpec();
471 Error error = ModuleList::GetSharedModule(exec_file_spec,
472 arch_spec,
473 NULL,
474 NULL,
475 0,
476 executable_sp,
477 NULL,
478 NULL);
479
480 if (!error.Fail() && executable_sp)
481 {
482 SetExecutableModule (executable_sp, true);
483 return true;
484 }
485 else
486 {
487 return false;
488 }
489 }
490 else
491 {
492 return false;
493 }
494 }
495}
Chris Lattner30fdc8d2010-06-08 16:52:24496
497bool
498Target::GetTargetTriple(ConstString &triple)
499{
500 triple.Clear();
501
502 if (m_triple)
503 {
504 triple = m_triple;
505 }
506 else
507 {
508 Module *exe_module = GetExecutableModule().get();
509 if (exe_module)
510 {
511 ObjectFile *objfile = exe_module->GetObjectFile();
512 if (objfile)
513 {
514 objfile->GetTargetTriple(m_triple);
515 triple = m_triple;
516 }
517 }
518 }
519 return !triple.IsEmpty();
520}
521
522void
523Target::ModuleAdded (ModuleSP &module_sp)
524{
525 // A module is being added to this target for the first time
526 ModuleList module_list;
527 module_list.Append(module_sp);
528 ModulesDidLoad (module_list);
529}
530
531void
532Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
533{
534 // A module is being added to this target for the first time
535 ModuleList module_list;
536 module_list.Append (old_module_sp);
537 ModulesDidUnload (module_list);
538 module_list.Clear ();
539 module_list.Append (new_module_sp);
540 ModulesDidLoad (module_list);
541}
542
543void
544Target::ModulesDidLoad (ModuleList &module_list)
545{
546 m_breakpoint_list.UpdateBreakpoints (module_list, true);
547 // TODO: make event data that packages up the module_list
548 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
549}
550
551void
552Target::ModulesDidUnload (ModuleList &module_list)
553{
554 m_breakpoint_list.UpdateBreakpoints (module_list, false);
555 // TODO: make event data that packages up the module_list
556 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
557}
558
Chris Lattner30fdc8d2010-06-08 16:52:24559size_t
Greg Clayton35f3dd22010-06-30 23:04:24560Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24561{
Chris Lattner30fdc8d2010-06-08 16:52:24562 error.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24563
Greg Claytondda4f7b2010-06-30 23:03:03564 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
565
566 Address resolved_addr(addr);
567 if (!resolved_addr.IsSectionOffset())
568 {
569 if (process_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24570 {
Greg Claytonf5e56de2010-09-14 23:36:40571 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03572 }
573 else
574 {
575 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
576 }
577 }
578
579
580 if (process_is_valid)
581 {
Greg Claytonf5e56de2010-09-14 23:36:40582 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03583 if (load_addr == LLDB_INVALID_ADDRESS)
584 {
585 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
586 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
587 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
588 resolved_addr.GetFileAddress());
589 else
590 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
591 }
592 else
593 {
594 size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24595 if (bytes_read != dst_len)
596 {
597 if (error.Success())
598 {
599 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03600 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24601 else
Greg Claytondda4f7b2010-06-30 23:03:03602 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24603 }
604 }
Greg Claytondda4f7b2010-06-30 23:03:03605 if (bytes_read)
606 return bytes_read;
607 // If the address is not section offset we have an address that
608 // doesn't resolve to any address in any currently loaded shared
609 // libaries and we failed to read memory so there isn't anything
610 // more we can do. If it is section offset, we might be able to
611 // read cached memory from the object file.
612 if (!resolved_addr.IsSectionOffset())
613 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24614 }
Chris Lattner30fdc8d2010-06-08 16:52:24615 }
Greg Claytondda4f7b2010-06-30 23:03:03616
617 const Section *section = resolved_addr.GetSection();
618 if (section && section->GetModule())
619 {
620 ObjectFile *objfile = section->GetModule()->GetObjectFile();
621 return section->ReadSectionDataFromObjectFile (objfile,
622 resolved_addr.GetOffset(),
623 dst,
624 dst_len);
625 }
626 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24627}
628
629
630ModuleSP
631Target::GetSharedModule
632(
633 const FileSpec& file_spec,
634 const ArchSpec& arch,
635 const UUID *uuid_ptr,
636 const ConstString *object_name,
637 off_t object_offset,
638 Error *error_ptr
639)
640{
641 // Don't pass in the UUID so we can tell if we have a stale value in our list
642 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
643 bool did_create_module = false;
644 ModuleSP module_sp;
645
646 // If there are image search path entries, try to use them first to acquire a suitable image.
647
648 Error error;
649
650 if (m_image_search_paths.GetSize())
651 {
652 FileSpec transformed_spec;
653 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
654 {
655 transformed_spec.GetFilename() = file_spec.GetFilename();
656 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
657 }
658 }
659
660 // If a module hasn't been found yet, use the unmodified path.
661
662 if (!module_sp)
663 {
664 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
665 }
666
667 if (module_sp)
668 {
669 m_images.Append (module_sp);
670 if (did_create_module)
671 {
672 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
673 ModuleUpdated(old_module_sp, module_sp);
674 else
675 ModuleAdded(module_sp);
676 }
677 }
678 if (error_ptr)
679 *error_ptr = error;
680 return module_sp;
681}
682
683
684Target *
685Target::CalculateTarget ()
686{
687 return this;
688}
689
690Process *
691Target::CalculateProcess ()
692{
693 return NULL;
694}
695
696Thread *
697Target::CalculateThread ()
698{
699 return NULL;
700}
701
702StackFrame *
703Target::CalculateStackFrame ()
704{
705 return NULL;
706}
707
708void
709Target::Calculate (ExecutionContext &exe_ctx)
710{
711 exe_ctx.target = this;
712 exe_ctx.process = NULL; // Do NOT fill in process...
713 exe_ctx.thread = NULL;
714 exe_ctx.frame = NULL;
715}
716
717PathMappingList &
718Target::GetImageSearchPathList ()
719{
720 return m_image_search_paths;
721}
722
723void
724Target::ImageSearchPathsChanged
725(
726 const PathMappingList &path_list,
727 void *baton
728)
729{
730 Target *target = (Target *)baton;
731 if (target->m_images.GetSize() > 1)
732 {
733 ModuleSP exe_module_sp (target->GetExecutableModule());
734 if (exe_module_sp)
735 {
736 target->m_images.Clear();
737 target->SetExecutableModule (exe_module_sp, true);
738 }
739 }
740}
741
742ClangASTContext *
743Target::GetScratchClangASTContext()
744{
745 return m_scratch_ast_context_ap.get();
746}
Caroline Ticedaccaa92010-09-20 20:44:43747
748lldb::UserSettingsControllerSP
749Target::GetSettingsController (bool finish)
750{
751 static lldb::UserSettingsControllerSP g_settings_controller (new SettingsController);
752 static bool initialized = false;
753
754 if (!initialized)
755 {
756 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
757 Target::SettingsController::global_settings_table,
758 Target::SettingsController::instance_settings_table);
759 }
760
761 if (finish)
762 {
763 UserSettingsController::FinalizeSettingsController (g_settings_controller);
764 g_settings_controller.reset();
765 initialized = false;
766 }
767
768 return g_settings_controller;
769}
770
771ArchSpec
772Target::GetDefaultArchitecture ()
773{
774 lldb::UserSettingsControllerSP settings_controller = Target::GetSettingsController();
775 lldb::SettableVariableType var_type;
776 Error err;
777 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
778
779 const char *default_name = "";
780 if (result.GetSize() == 1 && err.Success())
781 default_name = result.GetStringAtIndex (0);
782
783 ArchSpec default_arch (default_name);
784 return default_arch;
785}
786
787void
788Target::SetDefaultArchitecture (ArchSpec new_arch)
789{
790 if (new_arch.IsValid())
791 Target::GetSettingsController ()->SetVariable ("target.default-arch", new_arch.AsCString(),
792 lldb::eVarSetOperationAssign, false, "[]");
793}
794
795//--------------------------------------------------------------
796// class Target::SettingsController
797//--------------------------------------------------------------
798
799Target::SettingsController::SettingsController () :
800 UserSettingsController ("target", Debugger::GetSettingsController()),
801 m_default_architecture ()
802{
803 m_default_settings.reset (new TargetInstanceSettings (*this, false,
804 InstanceSettings::GetDefaultName().AsCString()));
805}
806
807Target::SettingsController::~SettingsController ()
808{
809}
810
811lldb::InstanceSettingsSP
812Target::SettingsController::CreateInstanceSettings (const char *instance_name)
813{
814 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*(Target::GetSettingsController().get()),
815 false, instance_name);
816 lldb::InstanceSettingsSP new_settings_sp (new_settings);
817 return new_settings_sp;
818}
819
820const ConstString &
821Target::SettingsController::DefArchVarName ()
822{
823 static ConstString def_arch_var_name ("default-arch");
824
825 return def_arch_var_name;
826}
827
828bool
829Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
830 const char *index_value,
831 const char *value,
832 const SettingEntry &entry,
833 const lldb::VarSetOperationType op,
834 Error&err)
835{
836 if (var_name == DefArchVarName())
837 {
838 ArchSpec tmp_spec (value);
839 if (tmp_spec.IsValid())
840 m_default_architecture = tmp_spec;
841 else
842 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
843 }
844 return true;
845}
846
847
848bool
849Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
850 StringList &value,
851 Error &err)
852{
853 if (var_name == DefArchVarName())
854 {
855 value.AppendString (m_default_architecture.AsCString());
856 return true;
857 }
858 else
859 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
860
861 return false;
862}
863
864//--------------------------------------------------------------
865// class TargetInstanceSettings
866//--------------------------------------------------------------
867
868TargetInstanceSettings::TargetInstanceSettings (UserSettingsController &owner, bool live_instance,
869 const char *name) :
870 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance)
871{
872 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
873 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
874 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
875 // This is true for CreateInstanceName() too.
876
877 if (GetInstanceName () == InstanceSettings::InvalidName())
878 {
879 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
880 m_owner.RegisterInstanceSettings (this);
881 }
882
883 if (live_instance)
884 {
885 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
886 CopyInstanceSettings (pending_settings,false);
887 //m_owner.RemovePendingSettings (m_instance_name);
888 }
889}
890
891TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
892 InstanceSettings (*(Target::GetSettingsController().get()), CreateInstanceName().AsCString())
893{
894 if (m_instance_name != InstanceSettings::GetDefaultName())
895 {
896 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
897 CopyInstanceSettings (pending_settings,false);
898 //m_owner.RemovePendingSettings (m_instance_name);
899 }
900}
901
902TargetInstanceSettings::~TargetInstanceSettings ()
903{
904}
905
906TargetInstanceSettings&
907TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
908{
909 if (this != &rhs)
910 {
911 }
912
913 return *this;
914}
915
916
917void
918TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
919 const char *index_value,
920 const char *value,
921 const ConstString &instance_name,
922 const SettingEntry &entry,
923 lldb::VarSetOperationType op,
924 Error &err,
925 bool pending)
926{
927 // Currently 'target' does not have any instance settings.
928}
929
930void
931TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
932 bool pending)
933{
934 // Currently 'target' does not have any instance settings.
935}
936
937void
938TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
939 const ConstString &var_name,
940 StringList &value,
941 Error &err)
942{
943 // Currently 'target' does not have any instance settings.
944}
945
946const ConstString
947TargetInstanceSettings::CreateInstanceName ()
948{
949 static int instance_count = 1;
950 StreamString sstr;
951
952 sstr.Printf ("target_%d", instance_count);
953 ++instance_count;
954
955 const ConstString ret_val (sstr.GetData());
956 return ret_val;
957}
958
959//--------------------------------------------------
960// Target::SettingsController Variable Tables
961//--------------------------------------------------
962
963SettingEntry
964Target::SettingsController::global_settings_table[] =
965{
966 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
967 { "default-arch", eSetVarTypeString, "x86_64", NULL, false, false, "Default architecture to choose, when there's a choice." },
968 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
969};
970
971SettingEntry
972Target::SettingsController::instance_settings_table[] =
973{
974 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
975 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
976};