blob: e8ed69d2834da04bc409260fc64ca10684f0aa7d [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();
76 m_images.Dump(s);
77 m_breakpoint_list.Dump(s);
78 m_internal_breakpoint_list.Dump(s);
79 }
80 else
81 {
82 char path[PATH_MAX];
83 int path_len = PATH_MAX;
84 if (GetExecutableModule()->GetFileSpec().GetPath (path, path_len))
85 s->Printf ("Target: %s\n", path);
86 else
87 s->Printf ("Target: <unknown>\n");
88 }
Chris Lattner24943d22010-06-08 16:52:24 +000089// if (m_process_sp.get())
90// m_process_sp->Dump(s);
91 s->IndentLess();
92}
93
94void
95Target::DeleteCurrentProcess ()
96{
97 if (m_process_sp.get())
98 {
Greg Clayton49480b12010-09-14 23:52:43 +000099 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000100 if (m_process_sp->IsAlive())
101 m_process_sp->Destroy();
102 else
103 m_process_sp->Finalize();
104
105 // Do any cleanup of the target we need to do between process instances.
106 // NB It is better to do this before destroying the process in case the
107 // clean up needs some help from the process.
108 m_breakpoint_list.ClearAllBreakpointSites();
109 m_internal_breakpoint_list.ClearAllBreakpointSites();
110 m_process_sp.reset();
111 }
112}
113
114const lldb::ProcessSP &
115Target::CreateProcess (Listener &listener, const char *plugin_name)
116{
117 DeleteCurrentProcess ();
118 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
119 return m_process_sp;
120}
121
122const lldb::ProcessSP &
123Target::GetProcessSP () const
124{
125 return m_process_sp;
126}
127
128lldb::TargetSP
129Target::GetSP()
130{
Greg Clayton63094e02010-06-23 01:19:29 +0000131 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000132}
133
134BreakpointList &
135Target::GetBreakpointList(bool internal)
136{
137 if (internal)
138 return m_internal_breakpoint_list;
139 else
140 return m_breakpoint_list;
141}
142
143const BreakpointList &
144Target::GetBreakpointList(bool internal) const
145{
146 if (internal)
147 return m_internal_breakpoint_list;
148 else
149 return m_breakpoint_list;
150}
151
152BreakpointSP
153Target::GetBreakpointByID (break_id_t break_id)
154{
155 BreakpointSP bp_sp;
156
157 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
158 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
159 else
160 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
161
162 return bp_sp;
163}
164
165BreakpointSP
166Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
167{
168 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
169 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
170 return CreateBreakpoint (filter_sp, resolver_sp, internal);
171}
172
173
174BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000175Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000176{
Chris Lattner24943d22010-06-08 16:52:24 +0000177 Address so_addr;
178 // Attempt to resolve our load address if possible, though it is ok if
179 // it doesn't resolve to section/offset.
180
Greg Clayton33ed1702010-08-24 00:45:41 +0000181 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000182 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000183 if (!so_addr.IsValid())
184 {
185 // The address didn't resolve, so just set this as an absolute address
186 so_addr.SetOffset (addr);
187 }
188 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000189 return bp_sp;
190}
191
192BreakpointSP
193Target::CreateBreakpoint (Address &addr, bool internal)
194{
195 TargetSP target_sp = this->GetSP();
196 SearchFilterSP filter_sp(new SearchFilter (target_sp));
197 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
198 return CreateBreakpoint (filter_sp, resolver_sp, internal);
199}
200
201BreakpointSP
Greg Clayton12bec712010-06-28 21:30:43 +0000202Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000203{
Greg Clayton12bec712010-06-28 21:30:43 +0000204 BreakpointSP bp_sp;
205 if (func_name)
206 {
207 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
208 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
209 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
210 }
211 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000212}
213
214
215SearchFilterSP
216Target::GetSearchFilterForModule (const FileSpec *containingModule)
217{
218 SearchFilterSP filter_sp;
219 lldb::TargetSP target_sp = this->GetSP();
220 if (containingModule != NULL)
221 {
222 // TODO: We should look into sharing module based search filters
223 // across many breakpoints like we do for the simple target based one
224 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
225 }
226 else
227 {
228 if (m_search_filter_sp.get() == NULL)
229 m_search_filter_sp.reset (new SearchFilter (target_sp));
230 filter_sp = m_search_filter_sp;
231 }
232 return filter_sp;
233}
234
235BreakpointSP
236Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
237{
238 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
239 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
240
241 return CreateBreakpoint (filter_sp, resolver_sp, internal);
242}
243
244BreakpointSP
245Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
246{
247 BreakpointSP bp_sp;
248 if (filter_sp && resolver_sp)
249 {
250 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
251 resolver_sp->SetBreakpoint (bp_sp.get());
252
253 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000254 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000255 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000256 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000257
258 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
259 if (log)
260 {
261 StreamString s;
262 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
263 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
264 }
265
Chris Lattner24943d22010-06-08 16:52:24 +0000266 bp_sp->ResolveBreakpoint();
267 }
Jim Inghamd1686902010-10-14 23:45:03 +0000268
269 if (!internal && bp_sp)
270 {
271 m_last_created_breakpoint = bp_sp;
272 }
273
Chris Lattner24943d22010-06-08 16:52:24 +0000274 return bp_sp;
275}
276
277void
278Target::RemoveAllBreakpoints (bool internal_also)
279{
280 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
281 if (log)
282 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
283
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000284 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000285 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000286 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000287
288 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000289}
290
291void
292Target::DisableAllBreakpoints (bool internal_also)
293{
294 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
295 if (log)
296 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
297
298 m_breakpoint_list.SetEnabledAll (false);
299 if (internal_also)
300 m_internal_breakpoint_list.SetEnabledAll (false);
301}
302
303void
304Target::EnableAllBreakpoints (bool internal_also)
305{
306 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
307 if (log)
308 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
309
310 m_breakpoint_list.SetEnabledAll (true);
311 if (internal_also)
312 m_internal_breakpoint_list.SetEnabledAll (true);
313}
314
315bool
316Target::RemoveBreakpointByID (break_id_t break_id)
317{
318 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
319 if (log)
320 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
321
322 if (DisableBreakpointByID (break_id))
323 {
324 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000325 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000326 else
Jim Inghamd1686902010-10-14 23:45:03 +0000327 {
328 if (m_last_created_breakpoint->GetID() == break_id)
329 m_last_created_breakpoint.reset();
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000330 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000331 }
Chris Lattner24943d22010-06-08 16:52:24 +0000332 return true;
333 }
334 return false;
335}
336
337bool
338Target::DisableBreakpointByID (break_id_t break_id)
339{
340 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
341 if (log)
342 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
343
344 BreakpointSP bp_sp;
345
346 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
347 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
348 else
349 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
350 if (bp_sp)
351 {
352 bp_sp->SetEnabled (false);
353 return true;
354 }
355 return false;
356}
357
358bool
359Target::EnableBreakpointByID (break_id_t break_id)
360{
361 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
362 if (log)
363 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
364 __FUNCTION__,
365 break_id,
366 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
367
368 BreakpointSP bp_sp;
369
370 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
371 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
372 else
373 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
374
375 if (bp_sp)
376 {
377 bp_sp->SetEnabled (true);
378 return true;
379 }
380 return false;
381}
382
383ModuleSP
384Target::GetExecutableModule ()
385{
386 ModuleSP executable_sp;
387 if (m_images.GetSize() > 0)
388 executable_sp = m_images.GetModuleAtIndex(0);
389 return executable_sp;
390}
391
392void
393Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
394{
395 m_images.Clear();
396 m_scratch_ast_context_ap.reset();
397
398 if (executable_sp.get())
399 {
400 Timer scoped_timer (__PRETTY_FUNCTION__,
401 "Target::SetExecutableModule (executable = '%s/%s')",
402 executable_sp->GetFileSpec().GetDirectory().AsCString(),
403 executable_sp->GetFileSpec().GetFilename().AsCString());
404
405 m_images.Append(executable_sp); // The first image is our exectuable file
406
407 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000408 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
409 if (!m_arch_spec.IsValid())
410 m_arch_spec = exe_arch;
411
Chris Lattner24943d22010-06-08 16:52:24 +0000412 FileSpecList dependent_files;
413 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
414 if (executable_objfile == NULL)
415 {
416
417 FileSpec bundle_executable(executable_sp->GetFileSpec());
Greg Clayton24b48ff2010-10-17 22:03:32 +0000418 if (Host::ResolveExecutableInBundle (bundle_executable))
Chris Lattner24943d22010-06-08 16:52:24 +0000419 {
420 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
421 exe_arch));
422 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
423 if (bundle_exe_module_sp->GetObjectFile() != NULL)
424 executable_sp = bundle_exe_module_sp;
425 return;
426 }
427 }
428
429 if (executable_objfile)
430 {
431 executable_objfile->GetDependentModules(dependent_files);
432 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
433 {
434 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
435 exe_arch));
436 if (image_module_sp.get())
437 {
438 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
439 ObjectFile *objfile = image_module_sp->GetObjectFile();
440 if (objfile)
441 objfile->GetDependentModules(dependent_files);
442 }
443 }
444 }
445
446 // Now see if we know the target triple, and if so, create our scratch AST context:
447 ConstString target_triple;
448 if (GetTargetTriple(target_triple))
449 {
450 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString()));
451 }
452 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000453
454 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000455}
456
457
458ModuleList&
459Target::GetImages ()
460{
461 return m_images;
462}
463
464ArchSpec
465Target::GetArchitecture () const
466{
Jim Ingham7508e732010-08-09 23:31:02 +0000467 return m_arch_spec;
Chris Lattner24943d22010-06-08 16:52:24 +0000468}
469
Jim Ingham7508e732010-08-09 23:31:02 +0000470bool
471Target::SetArchitecture (const ArchSpec &arch_spec)
472{
473 if (m_arch_spec == arch_spec)
474 {
475 // If we're setting the architecture to our current architecture, we
476 // don't need to do anything.
477 return true;
478 }
479 else if (!m_arch_spec.IsValid())
480 {
481 // If we haven't got a valid arch spec, then we just need to set it.
482 m_arch_spec = arch_spec;
483 return true;
484 }
485 else
486 {
487 // If we have an executable file, try to reset the executable to the desired architecture
488 m_arch_spec = arch_spec;
489 ModuleSP executable_sp = GetExecutableModule ();
490 m_images.Clear();
491 m_scratch_ast_context_ap.reset();
492 m_triple.Clear();
493 // Need to do something about unsetting breakpoints.
494
495 if (executable_sp)
496 {
497 FileSpec exec_file_spec = executable_sp->GetFileSpec();
498 Error error = ModuleList::GetSharedModule(exec_file_spec,
499 arch_spec,
500 NULL,
501 NULL,
502 0,
503 executable_sp,
504 NULL,
505 NULL);
506
507 if (!error.Fail() && executable_sp)
508 {
509 SetExecutableModule (executable_sp, true);
510 return true;
511 }
512 else
513 {
514 return false;
515 }
516 }
517 else
518 {
519 return false;
520 }
521 }
522}
Chris Lattner24943d22010-06-08 16:52:24 +0000523
524bool
525Target::GetTargetTriple(ConstString &triple)
526{
527 triple.Clear();
528
529 if (m_triple)
530 {
531 triple = m_triple;
532 }
533 else
534 {
535 Module *exe_module = GetExecutableModule().get();
536 if (exe_module)
537 {
538 ObjectFile *objfile = exe_module->GetObjectFile();
539 if (objfile)
540 {
541 objfile->GetTargetTriple(m_triple);
542 triple = m_triple;
543 }
544 }
545 }
546 return !triple.IsEmpty();
547}
548
549void
550Target::ModuleAdded (ModuleSP &module_sp)
551{
552 // A module is being added to this target for the first time
553 ModuleList module_list;
554 module_list.Append(module_sp);
555 ModulesDidLoad (module_list);
556}
557
558void
559Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
560{
561 // A module is being added to this target for the first time
562 ModuleList module_list;
563 module_list.Append (old_module_sp);
564 ModulesDidUnload (module_list);
565 module_list.Clear ();
566 module_list.Append (new_module_sp);
567 ModulesDidLoad (module_list);
568}
569
570void
571Target::ModulesDidLoad (ModuleList &module_list)
572{
573 m_breakpoint_list.UpdateBreakpoints (module_list, true);
574 // TODO: make event data that packages up the module_list
575 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
576}
577
578void
579Target::ModulesDidUnload (ModuleList &module_list)
580{
581 m_breakpoint_list.UpdateBreakpoints (module_list, false);
582 // TODO: make event data that packages up the module_list
583 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
584}
585
586size_t
Greg Clayton2cf6e9e2010-06-30 23:04:24 +0000587Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000588{
Chris Lattner24943d22010-06-08 16:52:24 +0000589 error.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000590
Greg Clayton70436352010-06-30 23:03:03 +0000591 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
592
593 Address resolved_addr(addr);
594 if (!resolved_addr.IsSectionOffset())
595 {
596 if (process_is_valid)
Chris Lattner24943d22010-06-08 16:52:24 +0000597 {
Greg Claytoneea26402010-09-14 23:36:40 +0000598 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000599 }
600 else
601 {
602 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
603 }
604 }
605
606
607 if (process_is_valid)
608 {
Greg Claytoneea26402010-09-14 23:36:40 +0000609 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000610 if (load_addr == LLDB_INVALID_ADDRESS)
611 {
612 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
613 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
614 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
615 resolved_addr.GetFileAddress());
616 else
617 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
618 }
619 else
620 {
621 size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000622 if (bytes_read != dst_len)
623 {
624 if (error.Success())
625 {
626 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000627 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000628 else
Greg Clayton70436352010-06-30 23:03:03 +0000629 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 +0000630 }
631 }
Greg Clayton70436352010-06-30 23:03:03 +0000632 if (bytes_read)
633 return bytes_read;
634 // If the address is not section offset we have an address that
635 // doesn't resolve to any address in any currently loaded shared
636 // libaries and we failed to read memory so there isn't anything
637 // more we can do. If it is section offset, we might be able to
638 // read cached memory from the object file.
639 if (!resolved_addr.IsSectionOffset())
640 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000641 }
Chris Lattner24943d22010-06-08 16:52:24 +0000642 }
Greg Clayton70436352010-06-30 23:03:03 +0000643
644 const Section *section = resolved_addr.GetSection();
645 if (section && section->GetModule())
646 {
647 ObjectFile *objfile = section->GetModule()->GetObjectFile();
648 return section->ReadSectionDataFromObjectFile (objfile,
649 resolved_addr.GetOffset(),
650 dst,
651 dst_len);
652 }
653 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000654}
655
656
657ModuleSP
658Target::GetSharedModule
659(
660 const FileSpec& file_spec,
661 const ArchSpec& arch,
662 const UUID *uuid_ptr,
663 const ConstString *object_name,
664 off_t object_offset,
665 Error *error_ptr
666)
667{
668 // Don't pass in the UUID so we can tell if we have a stale value in our list
669 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
670 bool did_create_module = false;
671 ModuleSP module_sp;
672
673 // If there are image search path entries, try to use them first to acquire a suitable image.
674
675 Error error;
676
677 if (m_image_search_paths.GetSize())
678 {
679 FileSpec transformed_spec;
680 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
681 {
682 transformed_spec.GetFilename() = file_spec.GetFilename();
683 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
684 }
685 }
686
687 // If a module hasn't been found yet, use the unmodified path.
688
689 if (!module_sp)
690 {
691 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
692 }
693
694 if (module_sp)
695 {
696 m_images.Append (module_sp);
697 if (did_create_module)
698 {
699 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
700 ModuleUpdated(old_module_sp, module_sp);
701 else
702 ModuleAdded(module_sp);
703 }
704 }
705 if (error_ptr)
706 *error_ptr = error;
707 return module_sp;
708}
709
710
711Target *
712Target::CalculateTarget ()
713{
714 return this;
715}
716
717Process *
718Target::CalculateProcess ()
719{
720 return NULL;
721}
722
723Thread *
724Target::CalculateThread ()
725{
726 return NULL;
727}
728
729StackFrame *
730Target::CalculateStackFrame ()
731{
732 return NULL;
733}
734
735void
Greg Claytona830adb2010-10-04 01:05:56 +0000736Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000737{
738 exe_ctx.target = this;
739 exe_ctx.process = NULL; // Do NOT fill in process...
740 exe_ctx.thread = NULL;
741 exe_ctx.frame = NULL;
742}
743
744PathMappingList &
745Target::GetImageSearchPathList ()
746{
747 return m_image_search_paths;
748}
749
750void
751Target::ImageSearchPathsChanged
752(
753 const PathMappingList &path_list,
754 void *baton
755)
756{
757 Target *target = (Target *)baton;
758 if (target->m_images.GetSize() > 1)
759 {
760 ModuleSP exe_module_sp (target->GetExecutableModule());
761 if (exe_module_sp)
762 {
763 target->m_images.Clear();
764 target->SetExecutableModule (exe_module_sp, true);
765 }
766 }
767}
768
769ClangASTContext *
770Target::GetScratchClangASTContext()
771{
772 return m_scratch_ast_context_ap.get();
773}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000774
775lldb::UserSettingsControllerSP
776Target::GetSettingsController (bool finish)
777{
778 static lldb::UserSettingsControllerSP g_settings_controller (new SettingsController);
779 static bool initialized = false;
780
781 if (!initialized)
782 {
783 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
784 Target::SettingsController::global_settings_table,
785 Target::SettingsController::instance_settings_table);
786 }
787
788 if (finish)
789 {
790 UserSettingsController::FinalizeSettingsController (g_settings_controller);
791 g_settings_controller.reset();
792 initialized = false;
793 }
794
795 return g_settings_controller;
796}
797
798ArchSpec
799Target::GetDefaultArchitecture ()
800{
801 lldb::UserSettingsControllerSP settings_controller = Target::GetSettingsController();
802 lldb::SettableVariableType var_type;
803 Error err;
804 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
805
806 const char *default_name = "";
807 if (result.GetSize() == 1 && err.Success())
808 default_name = result.GetStringAtIndex (0);
809
810 ArchSpec default_arch (default_name);
811 return default_arch;
812}
813
814void
815Target::SetDefaultArchitecture (ArchSpec new_arch)
816{
817 if (new_arch.IsValid())
818 Target::GetSettingsController ()->SetVariable ("target.default-arch", new_arch.AsCString(),
819 lldb::eVarSetOperationAssign, false, "[]");
820}
821
Greg Claytona830adb2010-10-04 01:05:56 +0000822Target *
823Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
824{
825 // The target can either exist in the "process" of ExecutionContext, or in
826 // the "target_sp" member of SymbolContext. This accessor helper function
827 // will get the target from one of these locations.
828
829 Target *target = NULL;
830 if (sc_ptr != NULL)
831 target = sc_ptr->target_sp.get();
832 if (target == NULL)
833 {
834 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
835 target = &exe_ctx_ptr->process->GetTarget();
836 }
837 return target;
838}
839
840
Caroline Tice1ebef442010-09-27 00:30:10 +0000841void
842Target::UpdateInstanceName ()
843{
844 StreamString sstr;
845
846 ModuleSP module_sp = GetExecutableModule();
847 if (module_sp)
848 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000849 sstr.Printf ("%s_%s",
850 module_sp->GetFileSpec().GetFilename().AsCString(),
Caroline Tice1ebef442010-09-27 00:30:10 +0000851 module_sp->GetArchitecture().AsCString());
Greg Claytonbf6e2102010-10-27 02:06:37 +0000852 Target::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
853 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000854 }
855}
856
Sean Callanan77e93942010-10-29 00:29:03 +0000857const char *
858Target::GetExpressionPrefixContentsAsCString ()
859{
860 return m_expr_prefix_contents.c_str();
861}
862
Caroline Tice5bc8c972010-09-20 20:44:43 +0000863//--------------------------------------------------------------
864// class Target::SettingsController
865//--------------------------------------------------------------
866
867Target::SettingsController::SettingsController () :
868 UserSettingsController ("target", Debugger::GetSettingsController()),
869 m_default_architecture ()
870{
871 m_default_settings.reset (new TargetInstanceSettings (*this, false,
872 InstanceSettings::GetDefaultName().AsCString()));
873}
874
875Target::SettingsController::~SettingsController ()
876{
877}
878
879lldb::InstanceSettingsSP
880Target::SettingsController::CreateInstanceSettings (const char *instance_name)
881{
882 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*(Target::GetSettingsController().get()),
883 false, instance_name);
884 lldb::InstanceSettingsSP new_settings_sp (new_settings);
885 return new_settings_sp;
886}
887
888const ConstString &
889Target::SettingsController::DefArchVarName ()
890{
891 static ConstString def_arch_var_name ("default-arch");
892
893 return def_arch_var_name;
894}
895
896bool
897Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
898 const char *index_value,
899 const char *value,
900 const SettingEntry &entry,
901 const lldb::VarSetOperationType op,
902 Error&err)
903{
904 if (var_name == DefArchVarName())
905 {
906 ArchSpec tmp_spec (value);
907 if (tmp_spec.IsValid())
908 m_default_architecture = tmp_spec;
909 else
910 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
911 }
912 return true;
913}
914
915
916bool
917Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
918 StringList &value,
919 Error &err)
920{
921 if (var_name == DefArchVarName())
922 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000923 // If the arch is invalid (the default), don't show a string for it
924 if (m_default_architecture.IsValid())
925 value.AppendString (m_default_architecture.AsCString());
Caroline Tice5bc8c972010-09-20 20:44:43 +0000926 return true;
927 }
928 else
929 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
930
931 return false;
932}
933
934//--------------------------------------------------------------
935// class TargetInstanceSettings
936//--------------------------------------------------------------
937
938TargetInstanceSettings::TargetInstanceSettings (UserSettingsController &owner, bool live_instance,
939 const char *name) :
940 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance)
941{
942 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
943 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
944 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
945 // This is true for CreateInstanceName() too.
946
947 if (GetInstanceName () == InstanceSettings::InvalidName())
948 {
949 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
950 m_owner.RegisterInstanceSettings (this);
951 }
952
953 if (live_instance)
954 {
955 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
956 CopyInstanceSettings (pending_settings,false);
957 //m_owner.RemovePendingSettings (m_instance_name);
958 }
959}
960
961TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
962 InstanceSettings (*(Target::GetSettingsController().get()), CreateInstanceName().AsCString())
963{
964 if (m_instance_name != InstanceSettings::GetDefaultName())
965 {
966 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
967 CopyInstanceSettings (pending_settings,false);
968 //m_owner.RemovePendingSettings (m_instance_name);
969 }
970}
971
972TargetInstanceSettings::~TargetInstanceSettings ()
973{
974}
975
976TargetInstanceSettings&
977TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
978{
979 if (this != &rhs)
980 {
981 }
982
983 return *this;
984}
985
Sean Callanan77e93942010-10-29 00:29:03 +0000986#define EXPR_PREFIX_STRING "expr-prefix"
Caroline Tice5bc8c972010-09-20 20:44:43 +0000987
988void
989TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
990 const char *index_value,
991 const char *value,
992 const ConstString &instance_name,
993 const SettingEntry &entry,
994 lldb::VarSetOperationType op,
995 Error &err,
996 bool pending)
997{
Sean Callanan77e93942010-10-29 00:29:03 +0000998 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
999
1000 if (var_name == expr_prefix_str)
1001 {
1002 switch (op)
1003 {
1004 default:
1005 err.SetErrorToGenericError ();
1006 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1007 return;
1008 case lldb::eVarSetOperationAssign:
1009 {
1010 FileSpec file_spec(value, true);
1011
1012 if (!file_spec.Exists())
1013 {
1014 err.SetErrorToGenericError ();
1015 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1016 return;
1017 }
1018
1019 DataBufferMemoryMap buf;
1020
1021 if (!buf.MemoryMapFromFileSpec(&file_spec) &&
1022 buf.GetError().Fail())
1023 {
1024 err.SetErrorToGenericError ();
1025 err.SetErrorStringWithFormat ("Couldn't read from %s: %s\n", value, buf.GetError().AsCString());
1026 return;
1027 }
1028
1029 m_expr_prefix_path = value;
1030 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(buf.GetBytes()), buf.GetByteSize());
1031 }
1032 return;
1033 case lldb::eVarSetOperationAppend:
1034 err.SetErrorToGenericError ();
1035 err.SetErrorString ("Cannot append to a path.\n");
1036 return;
1037 case lldb::eVarSetOperationClear:
1038 m_expr_prefix_path.clear ();
1039 m_expr_prefix_contents.clear ();
1040 return;
1041 }
1042 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001043}
1044
1045void
1046TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
Sean Callanan77e93942010-10-29 00:29:03 +00001047 bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001048{
Sean Callanan77e93942010-10-29 00:29:03 +00001049 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1050
1051 if (!new_settings_ptr)
1052 return;
1053
1054 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1055 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001056}
1057
Caroline Ticebcb5b452010-09-20 21:37:42 +00001058bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001059TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1060 const ConstString &var_name,
1061 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001062 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001063{
Sean Callanan77e93942010-10-29 00:29:03 +00001064 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
1065
1066 if (var_name == expr_prefix_str)
1067 {
1068 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1069 }
1070 else
1071 {
1072 if (err)
1073 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1074 return false;
1075 }
1076
1077 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001078}
1079
1080const ConstString
1081TargetInstanceSettings::CreateInstanceName ()
1082{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001083 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001084 static int instance_count = 1;
1085
Caroline Tice5bc8c972010-09-20 20:44:43 +00001086 sstr.Printf ("target_%d", instance_count);
1087 ++instance_count;
1088
1089 const ConstString ret_val (sstr.GetData());
1090 return ret_val;
1091}
1092
1093//--------------------------------------------------
1094// Target::SettingsController Variable Tables
1095//--------------------------------------------------
1096
1097SettingEntry
1098Target::SettingsController::global_settings_table[] =
1099{
Sean Callanan77e93942010-10-29 00:29:03 +00001100 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1101 { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." },
Caroline Tice5bc8c972010-09-20 20:44:43 +00001102 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1103};
1104
1105SettingEntry
1106Target::SettingsController::instance_settings_table[] =
1107{
Sean Callanan77e93942010-10-29 00:29:03 +00001108 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1109 { 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 +00001110 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1111};