blob: bbc41be9f8c1c23fdceeea64941a56493021842b [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Clayton63094e02010-06-23 01:19:29 +000036Target::Target(Debugger &debugger) :
Chris Lattner24943d22010-06-08 16:52:24 +000037 Broadcaster("Target"),
Caroline Tice5bc8c972010-09-20 20:44:43 +000038 TargetInstanceSettings (*(Target::GetSettingsController().get())),
Greg Clayton63094e02010-06-23 01:19:29 +000039 m_debugger (debugger),
Chris Lattner24943d22010-06-08 16:52:24 +000040 m_images(),
Greg Claytoneea26402010-09-14 23:36:40 +000041 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000042 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 Clayton49480b12010-09-14 23:52:43 +000086 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000087 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 Clayton63094e02010-06-23 01:19:29 +0000118 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000119}
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 Clayton33ed1702010-08-24 00:45:41 +0000162Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000163{
Chris Lattner24943d22010-06-08 16:52:24 +0000164 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 Clayton33ed1702010-08-24 00:45:41 +0000168 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000169 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000170 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 Lattner24943d22010-06-08 16:52:24 +0000176 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 Clayton12bec712010-06-28 21:30:43 +0000189Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000190{
Greg Clayton12bec712010-06-28 21:30:43 +0000191 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 Lattner24943d22010-06-08 16:52:24 +0000199}
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 Claytonc7f5d5c2010-07-23 23:33:17 +0000241 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000242 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000243 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000244
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 Lattner24943d22010-06-08 16:52:24 +0000253 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 Claytonc7f5d5c2010-07-23 23:33:17 +0000265 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000266 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000267 m_internal_breakpoint_list.RemoveAll (false);
Chris Lattner24943d22010-06-08 16:52:24 +0000268}
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 Claytonc7f5d5c2010-07-23 23:33:17 +0000304 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000305 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000306 m_breakpoint_list.Remove(break_id, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000307 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 Ingham7508e732010-08-09 23:31:02 +0000383 // 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 Lattner24943d22010-06-08 16:52:24 +0000387 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 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000428
429 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000430}
431
432
433ModuleList&
434Target::GetImages ()
435{
436 return m_images;
437}
438
439ArchSpec
440Target::GetArchitecture () const
441{
Jim Ingham7508e732010-08-09 23:31:02 +0000442 return m_arch_spec;
Chris Lattner24943d22010-06-08 16:52:24 +0000443}
444
Jim Ingham7508e732010-08-09 23:31:02 +0000445bool
446Target::SetArchitecture (const ArchSpec &arch_spec)
447{
448 if (m_arch_spec == arch_spec)
449 {
450 // If we're setting the architecture to our current architecture, we
451 // don't need to do anything.
452 return true;
453 }
454 else if (!m_arch_spec.IsValid())
455 {
456 // If we haven't got a valid arch spec, then we just need to set it.
457 m_arch_spec = arch_spec;
458 return true;
459 }
460 else
461 {
462 // If we have an executable file, try to reset the executable to the desired architecture
463 m_arch_spec = arch_spec;
464 ModuleSP executable_sp = GetExecutableModule ();
465 m_images.Clear();
466 m_scratch_ast_context_ap.reset();
467 m_triple.Clear();
468 // Need to do something about unsetting breakpoints.
469
470 if (executable_sp)
471 {
472 FileSpec exec_file_spec = executable_sp->GetFileSpec();
473 Error error = ModuleList::GetSharedModule(exec_file_spec,
474 arch_spec,
475 NULL,
476 NULL,
477 0,
478 executable_sp,
479 NULL,
480 NULL);
481
482 if (!error.Fail() && executable_sp)
483 {
484 SetExecutableModule (executable_sp, true);
485 return true;
486 }
487 else
488 {
489 return false;
490 }
491 }
492 else
493 {
494 return false;
495 }
496 }
497}
Chris Lattner24943d22010-06-08 16:52:24 +0000498
499bool
500Target::GetTargetTriple(ConstString &triple)
501{
502 triple.Clear();
503
504 if (m_triple)
505 {
506 triple = m_triple;
507 }
508 else
509 {
510 Module *exe_module = GetExecutableModule().get();
511 if (exe_module)
512 {
513 ObjectFile *objfile = exe_module->GetObjectFile();
514 if (objfile)
515 {
516 objfile->GetTargetTriple(m_triple);
517 triple = m_triple;
518 }
519 }
520 }
521 return !triple.IsEmpty();
522}
523
524void
525Target::ModuleAdded (ModuleSP &module_sp)
526{
527 // A module is being added to this target for the first time
528 ModuleList module_list;
529 module_list.Append(module_sp);
530 ModulesDidLoad (module_list);
531}
532
533void
534Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
535{
536 // A module is being added to this target for the first time
537 ModuleList module_list;
538 module_list.Append (old_module_sp);
539 ModulesDidUnload (module_list);
540 module_list.Clear ();
541 module_list.Append (new_module_sp);
542 ModulesDidLoad (module_list);
543}
544
545void
546Target::ModulesDidLoad (ModuleList &module_list)
547{
548 m_breakpoint_list.UpdateBreakpoints (module_list, true);
549 // TODO: make event data that packages up the module_list
550 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
551}
552
553void
554Target::ModulesDidUnload (ModuleList &module_list)
555{
556 m_breakpoint_list.UpdateBreakpoints (module_list, false);
557 // TODO: make event data that packages up the module_list
558 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
559}
560
561size_t
Greg Clayton2cf6e9e2010-06-30 23:04:24 +0000562Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000563{
Chris Lattner24943d22010-06-08 16:52:24 +0000564 error.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000565
Greg Clayton70436352010-06-30 23:03:03 +0000566 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
567
568 Address resolved_addr(addr);
569 if (!resolved_addr.IsSectionOffset())
570 {
571 if (process_is_valid)
Chris Lattner24943d22010-06-08 16:52:24 +0000572 {
Greg Claytoneea26402010-09-14 23:36:40 +0000573 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000574 }
575 else
576 {
577 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
578 }
579 }
580
581
582 if (process_is_valid)
583 {
Greg Claytoneea26402010-09-14 23:36:40 +0000584 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000585 if (load_addr == LLDB_INVALID_ADDRESS)
586 {
587 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
588 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
589 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
590 resolved_addr.GetFileAddress());
591 else
592 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
593 }
594 else
595 {
596 size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000597 if (bytes_read != dst_len)
598 {
599 if (error.Success())
600 {
601 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000602 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000603 else
Greg Clayton70436352010-06-30 23:03:03 +0000604 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000605 }
606 }
Greg Clayton70436352010-06-30 23:03:03 +0000607 if (bytes_read)
608 return bytes_read;
609 // If the address is not section offset we have an address that
610 // doesn't resolve to any address in any currently loaded shared
611 // libaries and we failed to read memory so there isn't anything
612 // more we can do. If it is section offset, we might be able to
613 // read cached memory from the object file.
614 if (!resolved_addr.IsSectionOffset())
615 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000616 }
Chris Lattner24943d22010-06-08 16:52:24 +0000617 }
Greg Clayton70436352010-06-30 23:03:03 +0000618
619 const Section *section = resolved_addr.GetSection();
620 if (section && section->GetModule())
621 {
622 ObjectFile *objfile = section->GetModule()->GetObjectFile();
623 return section->ReadSectionDataFromObjectFile (objfile,
624 resolved_addr.GetOffset(),
625 dst,
626 dst_len);
627 }
628 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000629}
630
631
632ModuleSP
633Target::GetSharedModule
634(
635 const FileSpec& file_spec,
636 const ArchSpec& arch,
637 const UUID *uuid_ptr,
638 const ConstString *object_name,
639 off_t object_offset,
640 Error *error_ptr
641)
642{
643 // Don't pass in the UUID so we can tell if we have a stale value in our list
644 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
645 bool did_create_module = false;
646 ModuleSP module_sp;
647
648 // If there are image search path entries, try to use them first to acquire a suitable image.
649
650 Error error;
651
652 if (m_image_search_paths.GetSize())
653 {
654 FileSpec transformed_spec;
655 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
656 {
657 transformed_spec.GetFilename() = file_spec.GetFilename();
658 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
659 }
660 }
661
662 // If a module hasn't been found yet, use the unmodified path.
663
664 if (!module_sp)
665 {
666 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
667 }
668
669 if (module_sp)
670 {
671 m_images.Append (module_sp);
672 if (did_create_module)
673 {
674 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
675 ModuleUpdated(old_module_sp, module_sp);
676 else
677 ModuleAdded(module_sp);
678 }
679 }
680 if (error_ptr)
681 *error_ptr = error;
682 return module_sp;
683}
684
685
686Target *
687Target::CalculateTarget ()
688{
689 return this;
690}
691
692Process *
693Target::CalculateProcess ()
694{
695 return NULL;
696}
697
698Thread *
699Target::CalculateThread ()
700{
701 return NULL;
702}
703
704StackFrame *
705Target::CalculateStackFrame ()
706{
707 return NULL;
708}
709
710void
711Target::Calculate (ExecutionContext &exe_ctx)
712{
713 exe_ctx.target = this;
714 exe_ctx.process = NULL; // Do NOT fill in process...
715 exe_ctx.thread = NULL;
716 exe_ctx.frame = NULL;
717}
718
719PathMappingList &
720Target::GetImageSearchPathList ()
721{
722 return m_image_search_paths;
723}
724
725void
726Target::ImageSearchPathsChanged
727(
728 const PathMappingList &path_list,
729 void *baton
730)
731{
732 Target *target = (Target *)baton;
733 if (target->m_images.GetSize() > 1)
734 {
735 ModuleSP exe_module_sp (target->GetExecutableModule());
736 if (exe_module_sp)
737 {
738 target->m_images.Clear();
739 target->SetExecutableModule (exe_module_sp, true);
740 }
741 }
742}
743
744ClangASTContext *
745Target::GetScratchClangASTContext()
746{
747 return m_scratch_ast_context_ap.get();
748}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000749
750lldb::UserSettingsControllerSP
751Target::GetSettingsController (bool finish)
752{
753 static lldb::UserSettingsControllerSP g_settings_controller (new SettingsController);
754 static bool initialized = false;
755
756 if (!initialized)
757 {
758 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
759 Target::SettingsController::global_settings_table,
760 Target::SettingsController::instance_settings_table);
761 }
762
763 if (finish)
764 {
765 UserSettingsController::FinalizeSettingsController (g_settings_controller);
766 g_settings_controller.reset();
767 initialized = false;
768 }
769
770 return g_settings_controller;
771}
772
773ArchSpec
774Target::GetDefaultArchitecture ()
775{
776 lldb::UserSettingsControllerSP settings_controller = Target::GetSettingsController();
777 lldb::SettableVariableType var_type;
778 Error err;
779 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
780
781 const char *default_name = "";
782 if (result.GetSize() == 1 && err.Success())
783 default_name = result.GetStringAtIndex (0);
784
785 ArchSpec default_arch (default_name);
786 return default_arch;
787}
788
789void
790Target::SetDefaultArchitecture (ArchSpec new_arch)
791{
792 if (new_arch.IsValid())
793 Target::GetSettingsController ()->SetVariable ("target.default-arch", new_arch.AsCString(),
794 lldb::eVarSetOperationAssign, false, "[]");
795}
796
Caroline Tice1ebef442010-09-27 00:30:10 +0000797void
798Target::UpdateInstanceName ()
799{
800 StreamString sstr;
801
802 ModuleSP module_sp = GetExecutableModule();
803 if (module_sp)
804 {
805 sstr.Printf ("%s_%s", module_sp->GetFileSpec().GetFilename().AsCString(),
806 module_sp->GetArchitecture().AsCString());
807 Target::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
808 sstr.GetData());
809 }
810}
811
Caroline Tice5bc8c972010-09-20 20:44:43 +0000812//--------------------------------------------------------------
813// class Target::SettingsController
814//--------------------------------------------------------------
815
816Target::SettingsController::SettingsController () :
817 UserSettingsController ("target", Debugger::GetSettingsController()),
818 m_default_architecture ()
819{
820 m_default_settings.reset (new TargetInstanceSettings (*this, false,
821 InstanceSettings::GetDefaultName().AsCString()));
822}
823
824Target::SettingsController::~SettingsController ()
825{
826}
827
828lldb::InstanceSettingsSP
829Target::SettingsController::CreateInstanceSettings (const char *instance_name)
830{
831 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*(Target::GetSettingsController().get()),
832 false, instance_name);
833 lldb::InstanceSettingsSP new_settings_sp (new_settings);
834 return new_settings_sp;
835}
836
837const ConstString &
838Target::SettingsController::DefArchVarName ()
839{
840 static ConstString def_arch_var_name ("default-arch");
841
842 return def_arch_var_name;
843}
844
845bool
846Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
847 const char *index_value,
848 const char *value,
849 const SettingEntry &entry,
850 const lldb::VarSetOperationType op,
851 Error&err)
852{
853 if (var_name == DefArchVarName())
854 {
855 ArchSpec tmp_spec (value);
856 if (tmp_spec.IsValid())
857 m_default_architecture = tmp_spec;
858 else
859 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
860 }
861 return true;
862}
863
864
865bool
866Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
867 StringList &value,
868 Error &err)
869{
870 if (var_name == DefArchVarName())
871 {
872 value.AppendString (m_default_architecture.AsCString());
873 return true;
874 }
875 else
876 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
877
878 return false;
879}
880
881//--------------------------------------------------------------
882// class TargetInstanceSettings
883//--------------------------------------------------------------
884
885TargetInstanceSettings::TargetInstanceSettings (UserSettingsController &owner, bool live_instance,
886 const char *name) :
887 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance)
888{
889 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
890 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
891 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
892 // This is true for CreateInstanceName() too.
893
894 if (GetInstanceName () == InstanceSettings::InvalidName())
895 {
896 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
897 m_owner.RegisterInstanceSettings (this);
898 }
899
900 if (live_instance)
901 {
902 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
903 CopyInstanceSettings (pending_settings,false);
904 //m_owner.RemovePendingSettings (m_instance_name);
905 }
906}
907
908TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
909 InstanceSettings (*(Target::GetSettingsController().get()), CreateInstanceName().AsCString())
910{
911 if (m_instance_name != InstanceSettings::GetDefaultName())
912 {
913 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
914 CopyInstanceSettings (pending_settings,false);
915 //m_owner.RemovePendingSettings (m_instance_name);
916 }
917}
918
919TargetInstanceSettings::~TargetInstanceSettings ()
920{
921}
922
923TargetInstanceSettings&
924TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
925{
926 if (this != &rhs)
927 {
928 }
929
930 return *this;
931}
932
933
934void
935TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
936 const char *index_value,
937 const char *value,
938 const ConstString &instance_name,
939 const SettingEntry &entry,
940 lldb::VarSetOperationType op,
941 Error &err,
942 bool pending)
943{
944 // Currently 'target' does not have any instance settings.
945}
946
947void
948TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
949 bool pending)
950{
951 // Currently 'target' does not have any instance settings.
952}
953
Caroline Ticebcb5b452010-09-20 21:37:42 +0000954bool
Caroline Tice5bc8c972010-09-20 20:44:43 +0000955TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
956 const ConstString &var_name,
957 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +0000958 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000959{
Greg Claytond0b3e2b2010-09-22 00:23:59 +0000960 if (err)
961 err->SetErrorString ("'target' does not have any instance settings");
962 return false;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000963}
964
965const ConstString
966TargetInstanceSettings::CreateInstanceName ()
967{
Caroline Tice5bc8c972010-09-20 20:44:43 +0000968 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +0000969 static int instance_count = 1;
970
Caroline Tice5bc8c972010-09-20 20:44:43 +0000971 sstr.Printf ("target_%d", instance_count);
972 ++instance_count;
973
974 const ConstString ret_val (sstr.GetData());
975 return ret_val;
976}
977
978//--------------------------------------------------
979// Target::SettingsController Variable Tables
980//--------------------------------------------------
981
982SettingEntry
983Target::SettingsController::global_settings_table[] =
984{
985 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
986 { "default-arch", eSetVarTypeString, "x86_64", NULL, false, false, "Default architecture to choose, when there's a choice." },
987 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
988};
989
990SettingEntry
991Target::SettingsController::instance_settings_table[] =
992{
993 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
994 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
995};