blob: f8e54aec57ed7b033b6ad05ca22bab463d1f2645 [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 }
Caroline Tice1559a462010-09-27 00:30:10428
429 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24430}
431
432
433ModuleList&
434Target::GetImages ()
435{
436 return m_images;
437}
438
439ArchSpec
440Target::GetArchitecture () const
441{
Jim Ingham5aee1622010-08-09 23:31:02442 return m_arch_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24443}
444
Jim Ingham5aee1622010-08-09 23:31:02445bool
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 Lattner30fdc8d2010-06-08 16:52:24498
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
Chris Lattner30fdc8d2010-06-08 16:52:24561size_t
Greg Clayton35f3dd22010-06-30 23:04:24562Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24563{
Chris Lattner30fdc8d2010-06-08 16:52:24564 error.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24565
Greg Claytondda4f7b2010-06-30 23:03:03566 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 Lattner30fdc8d2010-06-08 16:52:24572 {
Greg Claytonf5e56de2010-09-14 23:36:40573 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03574 }
575 else
576 {
577 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
578 }
579 }
580
581
582 if (process_is_valid)
583 {
Greg Claytonf5e56de2010-09-14 23:36:40584 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03585 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 Lattner30fdc8d2010-06-08 16:52:24597 if (bytes_read != dst_len)
598 {
599 if (error.Success())
600 {
601 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03602 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24603 else
Greg Claytondda4f7b2010-06-30 23:03:03604 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:24605 }
606 }
Greg Claytondda4f7b2010-06-30 23:03:03607 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 Lattner30fdc8d2010-06-08 16:52:24616 }
Chris Lattner30fdc8d2010-06-08 16:52:24617 }
Greg Claytondda4f7b2010-06-30 23:03:03618
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 Lattner30fdc8d2010-06-08 16:52:24629}
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
Greg Clayton0603aa92010-10-04 01:05:56711Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24712{
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 Ticedaccaa92010-09-20 20:44:43749
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
Greg Clayton0603aa92010-10-04 01:05:56797Target *
798Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
799{
800 // The target can either exist in the "process" of ExecutionContext, or in
801 // the "target_sp" member of SymbolContext. This accessor helper function
802 // will get the target from one of these locations.
803
804 Target *target = NULL;
805 if (sc_ptr != NULL)
806 target = sc_ptr->target_sp.get();
807 if (target == NULL)
808 {
809 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
810 target = &exe_ctx_ptr->process->GetTarget();
811 }
812 return target;
813}
814
815
Caroline Tice1559a462010-09-27 00:30:10816void
817Target::UpdateInstanceName ()
818{
819 StreamString sstr;
820
821 ModuleSP module_sp = GetExecutableModule();
822 if (module_sp)
823 {
824 sstr.Printf ("%s_%s", module_sp->GetFileSpec().GetFilename().AsCString(),
825 module_sp->GetArchitecture().AsCString());
826 Target::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
827 sstr.GetData());
828 }
829}
830
Caroline Ticedaccaa92010-09-20 20:44:43831//--------------------------------------------------------------
832// class Target::SettingsController
833//--------------------------------------------------------------
834
835Target::SettingsController::SettingsController () :
836 UserSettingsController ("target", Debugger::GetSettingsController()),
837 m_default_architecture ()
838{
839 m_default_settings.reset (new TargetInstanceSettings (*this, false,
840 InstanceSettings::GetDefaultName().AsCString()));
841}
842
843Target::SettingsController::~SettingsController ()
844{
845}
846
847lldb::InstanceSettingsSP
848Target::SettingsController::CreateInstanceSettings (const char *instance_name)
849{
850 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*(Target::GetSettingsController().get()),
851 false, instance_name);
852 lldb::InstanceSettingsSP new_settings_sp (new_settings);
853 return new_settings_sp;
854}
855
856const ConstString &
857Target::SettingsController::DefArchVarName ()
858{
859 static ConstString def_arch_var_name ("default-arch");
860
861 return def_arch_var_name;
862}
863
864bool
865Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
866 const char *index_value,
867 const char *value,
868 const SettingEntry &entry,
869 const lldb::VarSetOperationType op,
870 Error&err)
871{
872 if (var_name == DefArchVarName())
873 {
874 ArchSpec tmp_spec (value);
875 if (tmp_spec.IsValid())
876 m_default_architecture = tmp_spec;
877 else
878 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
879 }
880 return true;
881}
882
883
884bool
885Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
886 StringList &value,
887 Error &err)
888{
889 if (var_name == DefArchVarName())
890 {
891 value.AppendString (m_default_architecture.AsCString());
892 return true;
893 }
894 else
895 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
896
897 return false;
898}
899
900//--------------------------------------------------------------
901// class TargetInstanceSettings
902//--------------------------------------------------------------
903
904TargetInstanceSettings::TargetInstanceSettings (UserSettingsController &owner, bool live_instance,
905 const char *name) :
906 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance)
907{
908 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
909 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
910 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
911 // This is true for CreateInstanceName() too.
912
913 if (GetInstanceName () == InstanceSettings::InvalidName())
914 {
915 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
916 m_owner.RegisterInstanceSettings (this);
917 }
918
919 if (live_instance)
920 {
921 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
922 CopyInstanceSettings (pending_settings,false);
923 //m_owner.RemovePendingSettings (m_instance_name);
924 }
925}
926
927TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
928 InstanceSettings (*(Target::GetSettingsController().get()), CreateInstanceName().AsCString())
929{
930 if (m_instance_name != InstanceSettings::GetDefaultName())
931 {
932 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
933 CopyInstanceSettings (pending_settings,false);
934 //m_owner.RemovePendingSettings (m_instance_name);
935 }
936}
937
938TargetInstanceSettings::~TargetInstanceSettings ()
939{
940}
941
942TargetInstanceSettings&
943TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
944{
945 if (this != &rhs)
946 {
947 }
948
949 return *this;
950}
951
952
953void
954TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
955 const char *index_value,
956 const char *value,
957 const ConstString &instance_name,
958 const SettingEntry &entry,
959 lldb::VarSetOperationType op,
960 Error &err,
961 bool pending)
962{
963 // Currently 'target' does not have any instance settings.
964}
965
966void
967TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
968 bool pending)
969{
970 // Currently 'target' does not have any instance settings.
971}
972
Caroline Tice12cecd72010-09-20 21:37:42973bool
Caroline Ticedaccaa92010-09-20 20:44:43974TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
975 const ConstString &var_name,
976 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42977 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:43978{
Greg Claytond7aa1142010-09-22 00:23:59979 if (err)
980 err->SetErrorString ("'target' does not have any instance settings");
981 return false;
Caroline Ticedaccaa92010-09-20 20:44:43982}
983
984const ConstString
985TargetInstanceSettings::CreateInstanceName ()
986{
Caroline Ticedaccaa92010-09-20 20:44:43987 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:10988 static int instance_count = 1;
989
Caroline Ticedaccaa92010-09-20 20:44:43990 sstr.Printf ("target_%d", instance_count);
991 ++instance_count;
992
993 const ConstString ret_val (sstr.GetData());
994 return ret_val;
995}
996
997//--------------------------------------------------
998// Target::SettingsController Variable Tables
999//--------------------------------------------------
1000
1001SettingEntry
1002Target::SettingsController::global_settings_table[] =
1003{
1004 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1005 { "default-arch", eSetVarTypeString, "x86_64", NULL, false, false, "Default architecture to choose, when there's a choice." },
1006 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1007};
1008
1009SettingEntry
1010Target::SettingsController::instance_settings_table[] =
1011{
1012 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1013 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1014};