blob: 1d315f11611887c5b82d566ed2465df0fe90da0d [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"
Sean Callanan77e93942010-10-29 00:29:03 +000020#include "lldb/Core/DataBufferMemoryMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
23#include "lldb/Core/Timer.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Host/Host.h"
26#include "lldb/lldb-private-log.h"
27#include "lldb/Symbol/ObjectFile.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Core/Debugger.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34//----------------------------------------------------------------------
35// Target constructor
36//----------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +000037Target::Target(Debugger &debugger) :
Chris Lattner24943d22010-06-08 16:52:24 +000038 Broadcaster("Target"),
Caroline Tice5bc8c972010-09-20 20:44:43 +000039 TargetInstanceSettings (*(Target::GetSettingsController().get())),
Greg Clayton63094e02010-06-23 01:19:29 +000040 m_debugger (debugger),
Chris Lattner24943d22010-06-08 16:52:24 +000041 m_images(),
Greg Claytoneea26402010-09-14 23:36:40 +000042 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000043 m_breakpoint_list (false),
44 m_internal_breakpoint_list (true),
45 m_process_sp(),
46 m_triple(),
47 m_search_filter_sp(),
48 m_image_search_paths (ImageSearchPathsChanged, this),
49 m_scratch_ast_context_ap(NULL)
50{
51 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
52 if (log)
53 log->Printf ("%p Target::Target()", this);
54}
55
56//----------------------------------------------------------------------
57// Destructor
58//----------------------------------------------------------------------
59Target::~Target()
60{
61 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
62 if (log)
63 log->Printf ("%p Target::~Target()", this);
64 DeleteCurrentProcess ();
65}
66
67void
Caroline Tice7826c882010-10-26 03:11:13 +000068Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000069{
Greg Clayton3fed8b92010-10-08 00:21:05 +000070// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000071 if (description_level != lldb::eDescriptionLevelBrief)
72 {
73 s->Indent();
74 s->PutCString("Target\n");
75 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000076 m_images.Dump(s);
77 m_breakpoint_list.Dump(s);
78 m_internal_breakpoint_list.Dump(s);
79 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000080 }
81 else
82 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000083 s->Printf ("%s", GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Tice7826c882010-10-26 03:11:13 +000084 }
Chris Lattner24943d22010-06-08 16:52:24 +000085}
86
87void
88Target::DeleteCurrentProcess ()
89{
90 if (m_process_sp.get())
91 {
Greg Clayton49480b12010-09-14 23:52:43 +000092 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000093 if (m_process_sp->IsAlive())
94 m_process_sp->Destroy();
95 else
96 m_process_sp->Finalize();
97
98 // Do any cleanup of the target we need to do between process instances.
99 // NB It is better to do this before destroying the process in case the
100 // clean up needs some help from the process.
101 m_breakpoint_list.ClearAllBreakpointSites();
102 m_internal_breakpoint_list.ClearAllBreakpointSites();
103 m_process_sp.reset();
104 }
105}
106
107const lldb::ProcessSP &
108Target::CreateProcess (Listener &listener, const char *plugin_name)
109{
110 DeleteCurrentProcess ();
111 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
112 return m_process_sp;
113}
114
115const lldb::ProcessSP &
116Target::GetProcessSP () const
117{
118 return m_process_sp;
119}
120
121lldb::TargetSP
122Target::GetSP()
123{
Greg Clayton63094e02010-06-23 01:19:29 +0000124 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000125}
126
127BreakpointList &
128Target::GetBreakpointList(bool internal)
129{
130 if (internal)
131 return m_internal_breakpoint_list;
132 else
133 return m_breakpoint_list;
134}
135
136const BreakpointList &
137Target::GetBreakpointList(bool internal) const
138{
139 if (internal)
140 return m_internal_breakpoint_list;
141 else
142 return m_breakpoint_list;
143}
144
145BreakpointSP
146Target::GetBreakpointByID (break_id_t break_id)
147{
148 BreakpointSP bp_sp;
149
150 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
151 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
152 else
153 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
154
155 return bp_sp;
156}
157
158BreakpointSP
159Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
160{
161 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
162 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
163 return CreateBreakpoint (filter_sp, resolver_sp, internal);
164}
165
166
167BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000168Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000169{
Chris Lattner24943d22010-06-08 16:52:24 +0000170 Address so_addr;
171 // Attempt to resolve our load address if possible, though it is ok if
172 // it doesn't resolve to section/offset.
173
Greg Clayton33ed1702010-08-24 00:45:41 +0000174 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000175 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000176 if (!so_addr.IsValid())
177 {
178 // The address didn't resolve, so just set this as an absolute address
179 so_addr.SetOffset (addr);
180 }
181 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000182 return bp_sp;
183}
184
185BreakpointSP
186Target::CreateBreakpoint (Address &addr, bool internal)
187{
188 TargetSP target_sp = this->GetSP();
189 SearchFilterSP filter_sp(new SearchFilter (target_sp));
190 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
191 return CreateBreakpoint (filter_sp, resolver_sp, internal);
192}
193
194BreakpointSP
Greg Clayton12bec712010-06-28 21:30:43 +0000195Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000196{
Greg Clayton12bec712010-06-28 21:30:43 +0000197 BreakpointSP bp_sp;
198 if (func_name)
199 {
200 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
201 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
202 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
203 }
204 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000205}
206
207
208SearchFilterSP
209Target::GetSearchFilterForModule (const FileSpec *containingModule)
210{
211 SearchFilterSP filter_sp;
212 lldb::TargetSP target_sp = this->GetSP();
213 if (containingModule != NULL)
214 {
215 // TODO: We should look into sharing module based search filters
216 // across many breakpoints like we do for the simple target based one
217 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
218 }
219 else
220 {
221 if (m_search_filter_sp.get() == NULL)
222 m_search_filter_sp.reset (new SearchFilter (target_sp));
223 filter_sp = m_search_filter_sp;
224 }
225 return filter_sp;
226}
227
228BreakpointSP
229Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
230{
231 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
232 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
233
234 return CreateBreakpoint (filter_sp, resolver_sp, internal);
235}
236
237BreakpointSP
238Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
239{
240 BreakpointSP bp_sp;
241 if (filter_sp && resolver_sp)
242 {
243 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
244 resolver_sp->SetBreakpoint (bp_sp.get());
245
246 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000247 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000248 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000249 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000250
251 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
252 if (log)
253 {
254 StreamString s;
255 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
256 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
257 }
258
Chris Lattner24943d22010-06-08 16:52:24 +0000259 bp_sp->ResolveBreakpoint();
260 }
Jim Inghamd1686902010-10-14 23:45:03 +0000261
262 if (!internal && bp_sp)
263 {
264 m_last_created_breakpoint = bp_sp;
265 }
266
Chris Lattner24943d22010-06-08 16:52:24 +0000267 return bp_sp;
268}
269
270void
271Target::RemoveAllBreakpoints (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
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000277 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000278 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000279 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000280
281 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000282}
283
284void
285Target::DisableAllBreakpoints (bool internal_also)
286{
287 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
288 if (log)
289 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
290
291 m_breakpoint_list.SetEnabledAll (false);
292 if (internal_also)
293 m_internal_breakpoint_list.SetEnabledAll (false);
294}
295
296void
297Target::EnableAllBreakpoints (bool internal_also)
298{
299 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
300 if (log)
301 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
302
303 m_breakpoint_list.SetEnabledAll (true);
304 if (internal_also)
305 m_internal_breakpoint_list.SetEnabledAll (true);
306}
307
308bool
309Target::RemoveBreakpointByID (break_id_t break_id)
310{
311 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
312 if (log)
313 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
314
315 if (DisableBreakpointByID (break_id))
316 {
317 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000318 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000319 else
Jim Inghamd1686902010-10-14 23:45:03 +0000320 {
321 if (m_last_created_breakpoint->GetID() == break_id)
322 m_last_created_breakpoint.reset();
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000323 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000324 }
Chris Lattner24943d22010-06-08 16:52:24 +0000325 return true;
326 }
327 return false;
328}
329
330bool
331Target::DisableBreakpointByID (break_id_t break_id)
332{
333 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
334 if (log)
335 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
336
337 BreakpointSP bp_sp;
338
339 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
340 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
341 else
342 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
343 if (bp_sp)
344 {
345 bp_sp->SetEnabled (false);
346 return true;
347 }
348 return false;
349}
350
351bool
352Target::EnableBreakpointByID (break_id_t break_id)
353{
354 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
355 if (log)
356 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
357 __FUNCTION__,
358 break_id,
359 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
360
361 BreakpointSP bp_sp;
362
363 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
364 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
365 else
366 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
367
368 if (bp_sp)
369 {
370 bp_sp->SetEnabled (true);
371 return true;
372 }
373 return false;
374}
375
376ModuleSP
377Target::GetExecutableModule ()
378{
379 ModuleSP executable_sp;
380 if (m_images.GetSize() > 0)
381 executable_sp = m_images.GetModuleAtIndex(0);
382 return executable_sp;
383}
384
385void
386Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
387{
388 m_images.Clear();
389 m_scratch_ast_context_ap.reset();
390
391 if (executable_sp.get())
392 {
393 Timer scoped_timer (__PRETTY_FUNCTION__,
394 "Target::SetExecutableModule (executable = '%s/%s')",
395 executable_sp->GetFileSpec().GetDirectory().AsCString(),
396 executable_sp->GetFileSpec().GetFilename().AsCString());
397
398 m_images.Append(executable_sp); // The first image is our exectuable file
399
400 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000401 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
402 if (!m_arch_spec.IsValid())
403 m_arch_spec = exe_arch;
404
Chris Lattner24943d22010-06-08 16:52:24 +0000405 FileSpecList dependent_files;
406 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
407 if (executable_objfile == NULL)
408 {
409
410 FileSpec bundle_executable(executable_sp->GetFileSpec());
Greg Clayton24b48ff2010-10-17 22:03:32 +0000411 if (Host::ResolveExecutableInBundle (bundle_executable))
Chris Lattner24943d22010-06-08 16:52:24 +0000412 {
413 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
414 exe_arch));
415 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
416 if (bundle_exe_module_sp->GetObjectFile() != NULL)
417 executable_sp = bundle_exe_module_sp;
418 return;
419 }
420 }
421
422 if (executable_objfile)
423 {
424 executable_objfile->GetDependentModules(dependent_files);
425 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
426 {
427 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
428 exe_arch));
429 if (image_module_sp.get())
430 {
431 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
432 ObjectFile *objfile = image_module_sp->GetObjectFile();
433 if (objfile)
434 objfile->GetDependentModules(dependent_files);
435 }
436 }
437 }
438
439 // Now see if we know the target triple, and if so, create our scratch AST context:
440 ConstString target_triple;
441 if (GetTargetTriple(target_triple))
442 {
443 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString()));
444 }
445 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000446
447 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000448}
449
450
451ModuleList&
452Target::GetImages ()
453{
454 return m_images;
455}
456
457ArchSpec
458Target::GetArchitecture () const
459{
Jim Ingham7508e732010-08-09 23:31:02 +0000460 return m_arch_spec;
Chris Lattner24943d22010-06-08 16:52:24 +0000461}
462
Jim Ingham7508e732010-08-09 23:31:02 +0000463bool
464Target::SetArchitecture (const ArchSpec &arch_spec)
465{
466 if (m_arch_spec == arch_spec)
467 {
468 // If we're setting the architecture to our current architecture, we
469 // don't need to do anything.
470 return true;
471 }
472 else if (!m_arch_spec.IsValid())
473 {
474 // If we haven't got a valid arch spec, then we just need to set it.
475 m_arch_spec = arch_spec;
476 return true;
477 }
478 else
479 {
480 // If we have an executable file, try to reset the executable to the desired architecture
481 m_arch_spec = arch_spec;
482 ModuleSP executable_sp = GetExecutableModule ();
483 m_images.Clear();
484 m_scratch_ast_context_ap.reset();
485 m_triple.Clear();
486 // Need to do something about unsetting breakpoints.
487
488 if (executable_sp)
489 {
490 FileSpec exec_file_spec = executable_sp->GetFileSpec();
491 Error error = ModuleList::GetSharedModule(exec_file_spec,
492 arch_spec,
493 NULL,
494 NULL,
495 0,
496 executable_sp,
497 NULL,
498 NULL);
499
500 if (!error.Fail() && executable_sp)
501 {
502 SetExecutableModule (executable_sp, true);
503 return true;
504 }
505 else
506 {
507 return false;
508 }
509 }
510 else
511 {
512 return false;
513 }
514 }
515}
Chris Lattner24943d22010-06-08 16:52:24 +0000516
517bool
518Target::GetTargetTriple(ConstString &triple)
519{
520 triple.Clear();
521
522 if (m_triple)
523 {
524 triple = m_triple;
525 }
526 else
527 {
528 Module *exe_module = GetExecutableModule().get();
529 if (exe_module)
530 {
531 ObjectFile *objfile = exe_module->GetObjectFile();
532 if (objfile)
533 {
534 objfile->GetTargetTriple(m_triple);
535 triple = m_triple;
536 }
537 }
538 }
539 return !triple.IsEmpty();
540}
541
542void
543Target::ModuleAdded (ModuleSP &module_sp)
544{
545 // A module is being added to this target for the first time
546 ModuleList module_list;
547 module_list.Append(module_sp);
548 ModulesDidLoad (module_list);
549}
550
551void
552Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
553{
554 // A module is being added to this target for the first time
555 ModuleList module_list;
556 module_list.Append (old_module_sp);
557 ModulesDidUnload (module_list);
558 module_list.Clear ();
559 module_list.Append (new_module_sp);
560 ModulesDidLoad (module_list);
561}
562
563void
564Target::ModulesDidLoad (ModuleList &module_list)
565{
566 m_breakpoint_list.UpdateBreakpoints (module_list, true);
567 // TODO: make event data that packages up the module_list
568 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
569}
570
571void
572Target::ModulesDidUnload (ModuleList &module_list)
573{
574 m_breakpoint_list.UpdateBreakpoints (module_list, false);
575 // TODO: make event data that packages up the module_list
576 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
577}
578
579size_t
Greg Clayton2cf6e9e2010-06-30 23:04:24 +0000580Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000581{
Chris Lattner24943d22010-06-08 16:52:24 +0000582 error.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000583
Greg Clayton70436352010-06-30 23:03:03 +0000584 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
585
586 Address resolved_addr(addr);
587 if (!resolved_addr.IsSectionOffset())
588 {
589 if (process_is_valid)
Chris Lattner24943d22010-06-08 16:52:24 +0000590 {
Greg Claytoneea26402010-09-14 23:36:40 +0000591 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000592 }
593 else
594 {
595 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
596 }
597 }
598
599
600 if (process_is_valid)
601 {
Greg Claytoneea26402010-09-14 23:36:40 +0000602 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000603 if (load_addr == LLDB_INVALID_ADDRESS)
604 {
605 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
606 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
607 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
608 resolved_addr.GetFileAddress());
609 else
610 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
611 }
612 else
613 {
614 size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000615 if (bytes_read != dst_len)
616 {
617 if (error.Success())
618 {
619 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000620 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000621 else
Greg Clayton70436352010-06-30 23:03:03 +0000622 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 +0000623 }
624 }
Greg Clayton70436352010-06-30 23:03:03 +0000625 if (bytes_read)
626 return bytes_read;
627 // If the address is not section offset we have an address that
628 // doesn't resolve to any address in any currently loaded shared
629 // libaries and we failed to read memory so there isn't anything
630 // more we can do. If it is section offset, we might be able to
631 // read cached memory from the object file.
632 if (!resolved_addr.IsSectionOffset())
633 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000634 }
Chris Lattner24943d22010-06-08 16:52:24 +0000635 }
Greg Clayton70436352010-06-30 23:03:03 +0000636
637 const Section *section = resolved_addr.GetSection();
638 if (section && section->GetModule())
639 {
640 ObjectFile *objfile = section->GetModule()->GetObjectFile();
641 return section->ReadSectionDataFromObjectFile (objfile,
642 resolved_addr.GetOffset(),
643 dst,
644 dst_len);
645 }
646 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000647}
648
649
650ModuleSP
651Target::GetSharedModule
652(
653 const FileSpec& file_spec,
654 const ArchSpec& arch,
655 const UUID *uuid_ptr,
656 const ConstString *object_name,
657 off_t object_offset,
658 Error *error_ptr
659)
660{
661 // Don't pass in the UUID so we can tell if we have a stale value in our list
662 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
663 bool did_create_module = false;
664 ModuleSP module_sp;
665
666 // If there are image search path entries, try to use them first to acquire a suitable image.
667
668 Error error;
669
670 if (m_image_search_paths.GetSize())
671 {
672 FileSpec transformed_spec;
673 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
674 {
675 transformed_spec.GetFilename() = file_spec.GetFilename();
676 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
677 }
678 }
679
680 // If a module hasn't been found yet, use the unmodified path.
681
682 if (!module_sp)
683 {
684 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
685 }
686
687 if (module_sp)
688 {
689 m_images.Append (module_sp);
690 if (did_create_module)
691 {
692 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
693 ModuleUpdated(old_module_sp, module_sp);
694 else
695 ModuleAdded(module_sp);
696 }
697 }
698 if (error_ptr)
699 *error_ptr = error;
700 return module_sp;
701}
702
703
704Target *
705Target::CalculateTarget ()
706{
707 return this;
708}
709
710Process *
711Target::CalculateProcess ()
712{
713 return NULL;
714}
715
716Thread *
717Target::CalculateThread ()
718{
719 return NULL;
720}
721
722StackFrame *
723Target::CalculateStackFrame ()
724{
725 return NULL;
726}
727
728void
Greg Claytona830adb2010-10-04 01:05:56 +0000729Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000730{
731 exe_ctx.target = this;
732 exe_ctx.process = NULL; // Do NOT fill in process...
733 exe_ctx.thread = NULL;
734 exe_ctx.frame = NULL;
735}
736
737PathMappingList &
738Target::GetImageSearchPathList ()
739{
740 return m_image_search_paths;
741}
742
743void
744Target::ImageSearchPathsChanged
745(
746 const PathMappingList &path_list,
747 void *baton
748)
749{
750 Target *target = (Target *)baton;
751 if (target->m_images.GetSize() > 1)
752 {
753 ModuleSP exe_module_sp (target->GetExecutableModule());
754 if (exe_module_sp)
755 {
756 target->m_images.Clear();
757 target->SetExecutableModule (exe_module_sp, true);
758 }
759 }
760}
761
762ClangASTContext *
763Target::GetScratchClangASTContext()
764{
765 return m_scratch_ast_context_ap.get();
766}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000767
768lldb::UserSettingsControllerSP
769Target::GetSettingsController (bool finish)
770{
771 static lldb::UserSettingsControllerSP g_settings_controller (new SettingsController);
772 static bool initialized = false;
773
774 if (!initialized)
775 {
776 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
777 Target::SettingsController::global_settings_table,
778 Target::SettingsController::instance_settings_table);
779 }
780
781 if (finish)
782 {
783 UserSettingsController::FinalizeSettingsController (g_settings_controller);
784 g_settings_controller.reset();
785 initialized = false;
786 }
787
788 return g_settings_controller;
789}
790
791ArchSpec
792Target::GetDefaultArchitecture ()
793{
794 lldb::UserSettingsControllerSP settings_controller = Target::GetSettingsController();
795 lldb::SettableVariableType var_type;
796 Error err;
797 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
798
799 const char *default_name = "";
800 if (result.GetSize() == 1 && err.Success())
801 default_name = result.GetStringAtIndex (0);
802
803 ArchSpec default_arch (default_name);
804 return default_arch;
805}
806
807void
808Target::SetDefaultArchitecture (ArchSpec new_arch)
809{
810 if (new_arch.IsValid())
811 Target::GetSettingsController ()->SetVariable ("target.default-arch", new_arch.AsCString(),
812 lldb::eVarSetOperationAssign, false, "[]");
813}
814
Greg Claytona830adb2010-10-04 01:05:56 +0000815Target *
816Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
817{
818 // The target can either exist in the "process" of ExecutionContext, or in
819 // the "target_sp" member of SymbolContext. This accessor helper function
820 // will get the target from one of these locations.
821
822 Target *target = NULL;
823 if (sc_ptr != NULL)
824 target = sc_ptr->target_sp.get();
825 if (target == NULL)
826 {
827 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
828 target = &exe_ctx_ptr->process->GetTarget();
829 }
830 return target;
831}
832
833
Caroline Tice1ebef442010-09-27 00:30:10 +0000834void
835Target::UpdateInstanceName ()
836{
837 StreamString sstr;
838
839 ModuleSP module_sp = GetExecutableModule();
840 if (module_sp)
841 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000842 sstr.Printf ("%s_%s",
843 module_sp->GetFileSpec().GetFilename().AsCString(),
Caroline Tice1ebef442010-09-27 00:30:10 +0000844 module_sp->GetArchitecture().AsCString());
Greg Claytonbf6e2102010-10-27 02:06:37 +0000845 Target::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
846 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000847 }
848}
849
Sean Callanan77e93942010-10-29 00:29:03 +0000850const char *
851Target::GetExpressionPrefixContentsAsCString ()
852{
853 return m_expr_prefix_contents.c_str();
854}
855
Caroline Tice5bc8c972010-09-20 20:44:43 +0000856//--------------------------------------------------------------
857// class Target::SettingsController
858//--------------------------------------------------------------
859
860Target::SettingsController::SettingsController () :
861 UserSettingsController ("target", Debugger::GetSettingsController()),
862 m_default_architecture ()
863{
864 m_default_settings.reset (new TargetInstanceSettings (*this, false,
865 InstanceSettings::GetDefaultName().AsCString()));
866}
867
868Target::SettingsController::~SettingsController ()
869{
870}
871
872lldb::InstanceSettingsSP
873Target::SettingsController::CreateInstanceSettings (const char *instance_name)
874{
875 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*(Target::GetSettingsController().get()),
876 false, instance_name);
877 lldb::InstanceSettingsSP new_settings_sp (new_settings);
878 return new_settings_sp;
879}
880
881const ConstString &
882Target::SettingsController::DefArchVarName ()
883{
884 static ConstString def_arch_var_name ("default-arch");
885
886 return def_arch_var_name;
887}
888
889bool
890Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
891 const char *index_value,
892 const char *value,
893 const SettingEntry &entry,
894 const lldb::VarSetOperationType op,
895 Error&err)
896{
897 if (var_name == DefArchVarName())
898 {
899 ArchSpec tmp_spec (value);
900 if (tmp_spec.IsValid())
901 m_default_architecture = tmp_spec;
902 else
903 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
904 }
905 return true;
906}
907
908
909bool
910Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
911 StringList &value,
912 Error &err)
913{
914 if (var_name == DefArchVarName())
915 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000916 // If the arch is invalid (the default), don't show a string for it
917 if (m_default_architecture.IsValid())
918 value.AppendString (m_default_architecture.AsCString());
Caroline Tice5bc8c972010-09-20 20:44:43 +0000919 return true;
920 }
921 else
922 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
923
924 return false;
925}
926
927//--------------------------------------------------------------
928// class TargetInstanceSettings
929//--------------------------------------------------------------
930
931TargetInstanceSettings::TargetInstanceSettings (UserSettingsController &owner, bool live_instance,
932 const char *name) :
933 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance)
934{
935 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
936 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
937 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
938 // This is true for CreateInstanceName() too.
939
940 if (GetInstanceName () == InstanceSettings::InvalidName())
941 {
942 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
943 m_owner.RegisterInstanceSettings (this);
944 }
945
946 if (live_instance)
947 {
948 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
949 CopyInstanceSettings (pending_settings,false);
950 //m_owner.RemovePendingSettings (m_instance_name);
951 }
952}
953
954TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
955 InstanceSettings (*(Target::GetSettingsController().get()), CreateInstanceName().AsCString())
956{
957 if (m_instance_name != InstanceSettings::GetDefaultName())
958 {
959 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
960 CopyInstanceSettings (pending_settings,false);
961 //m_owner.RemovePendingSettings (m_instance_name);
962 }
963}
964
965TargetInstanceSettings::~TargetInstanceSettings ()
966{
967}
968
969TargetInstanceSettings&
970TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
971{
972 if (this != &rhs)
973 {
974 }
975
976 return *this;
977}
978
Sean Callanan77e93942010-10-29 00:29:03 +0000979#define EXPR_PREFIX_STRING "expr-prefix"
Caroline Tice5bc8c972010-09-20 20:44:43 +0000980
981void
982TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
983 const char *index_value,
984 const char *value,
985 const ConstString &instance_name,
986 const SettingEntry &entry,
987 lldb::VarSetOperationType op,
988 Error &err,
989 bool pending)
990{
Sean Callanan77e93942010-10-29 00:29:03 +0000991 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
992
993 if (var_name == expr_prefix_str)
994 {
995 switch (op)
996 {
997 default:
998 err.SetErrorToGenericError ();
999 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1000 return;
1001 case lldb::eVarSetOperationAssign:
1002 {
1003 FileSpec file_spec(value, true);
1004
1005 if (!file_spec.Exists())
1006 {
1007 err.SetErrorToGenericError ();
1008 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1009 return;
1010 }
1011
1012 DataBufferMemoryMap buf;
1013
1014 if (!buf.MemoryMapFromFileSpec(&file_spec) &&
1015 buf.GetError().Fail())
1016 {
1017 err.SetErrorToGenericError ();
1018 err.SetErrorStringWithFormat ("Couldn't read from %s: %s\n", value, buf.GetError().AsCString());
1019 return;
1020 }
1021
1022 m_expr_prefix_path = value;
1023 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(buf.GetBytes()), buf.GetByteSize());
1024 }
1025 return;
1026 case lldb::eVarSetOperationAppend:
1027 err.SetErrorToGenericError ();
1028 err.SetErrorString ("Cannot append to a path.\n");
1029 return;
1030 case lldb::eVarSetOperationClear:
1031 m_expr_prefix_path.clear ();
1032 m_expr_prefix_contents.clear ();
1033 return;
1034 }
1035 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001036}
1037
1038void
1039TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
Sean Callanan77e93942010-10-29 00:29:03 +00001040 bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001041{
Sean Callanan77e93942010-10-29 00:29:03 +00001042 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1043
1044 if (!new_settings_ptr)
1045 return;
1046
1047 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1048 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001049}
1050
Caroline Ticebcb5b452010-09-20 21:37:42 +00001051bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001052TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1053 const ConstString &var_name,
1054 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001055 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001056{
Sean Callanan77e93942010-10-29 00:29:03 +00001057 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
1058
1059 if (var_name == expr_prefix_str)
1060 {
1061 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1062 }
1063 else
1064 {
1065 if (err)
1066 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1067 return false;
1068 }
1069
1070 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001071}
1072
1073const ConstString
1074TargetInstanceSettings::CreateInstanceName ()
1075{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001076 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001077 static int instance_count = 1;
1078
Caroline Tice5bc8c972010-09-20 20:44:43 +00001079 sstr.Printf ("target_%d", instance_count);
1080 ++instance_count;
1081
1082 const ConstString ret_val (sstr.GetData());
1083 return ret_val;
1084}
1085
1086//--------------------------------------------------
1087// Target::SettingsController Variable Tables
1088//--------------------------------------------------
1089
1090SettingEntry
1091Target::SettingsController::global_settings_table[] =
1092{
Sean Callanan77e93942010-10-29 00:29:03 +00001093 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1094 { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." },
Caroline Tice5bc8c972010-09-20 20:44:43 +00001095 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1096};
1097
1098SettingEntry
1099Target::SettingsController::instance_settings_table[] =
1100{
Sean Callanan77e93942010-10-29 00:29:03 +00001101 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1102 { EXPR_PREFIX_STRING, eSetVarTypeString, NULL, NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
Caroline Tice5bc8c972010-09-20 20:44:43 +00001103 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1104};