blob: 17e2c875bae8071a60847bbe4c63523f5764d2eb [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
Caroline Ticeceb6b132010-10-26 03:11:1367Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2468{
Greg Clayton89411422010-10-08 00:21:0569// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1370 if (description_level != lldb::eDescriptionLevelBrief)
71 {
72 s->Indent();
73 s->PutCString("Target\n");
74 s->IndentMore();
75 m_images.Dump(s);
76 m_breakpoint_list.Dump(s);
77 m_internal_breakpoint_list.Dump(s);
78 }
79 else
80 {
81 char path[PATH_MAX];
82 int path_len = PATH_MAX;
83 if (GetExecutableModule()->GetFileSpec().GetPath (path, path_len))
84 s->Printf ("Target: %s\n", path);
85 else
86 s->Printf ("Target: <unknown>\n");
87 }
Chris Lattner30fdc8d2010-06-08 16:52:2488// if (m_process_sp.get())
89// m_process_sp->Dump(s);
90 s->IndentLess();
91}
92
93void
94Target::DeleteCurrentProcess ()
95{
96 if (m_process_sp.get())
97 {
Greg Clayton17f69202010-09-14 23:52:4398 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:2499 if (m_process_sp->IsAlive())
100 m_process_sp->Destroy();
101 else
102 m_process_sp->Finalize();
103
104 // Do any cleanup of the target we need to do between process instances.
105 // NB It is better to do this before destroying the process in case the
106 // clean up needs some help from the process.
107 m_breakpoint_list.ClearAllBreakpointSites();
108 m_internal_breakpoint_list.ClearAllBreakpointSites();
109 m_process_sp.reset();
110 }
111}
112
113const lldb::ProcessSP &
114Target::CreateProcess (Listener &listener, const char *plugin_name)
115{
116 DeleteCurrentProcess ();
117 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
118 return m_process_sp;
119}
120
121const lldb::ProcessSP &
122Target::GetProcessSP () const
123{
124 return m_process_sp;
125}
126
127lldb::TargetSP
128Target::GetSP()
129{
Greg Clayton66111032010-06-23 01:19:29130 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24131}
132
133BreakpointList &
134Target::GetBreakpointList(bool internal)
135{
136 if (internal)
137 return m_internal_breakpoint_list;
138 else
139 return m_breakpoint_list;
140}
141
142const BreakpointList &
143Target::GetBreakpointList(bool internal) const
144{
145 if (internal)
146 return m_internal_breakpoint_list;
147 else
148 return m_breakpoint_list;
149}
150
151BreakpointSP
152Target::GetBreakpointByID (break_id_t break_id)
153{
154 BreakpointSP bp_sp;
155
156 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
157 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
158 else
159 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
160
161 return bp_sp;
162}
163
164BreakpointSP
165Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
166{
167 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
168 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
169 return CreateBreakpoint (filter_sp, resolver_sp, internal);
170}
171
172
173BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41174Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24175{
Chris Lattner30fdc8d2010-06-08 16:52:24176 Address so_addr;
177 // Attempt to resolve our load address if possible, though it is ok if
178 // it doesn't resolve to section/offset.
179
Greg Clayton1b72fcb2010-08-24 00:45:41180 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40181 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41182 if (!so_addr.IsValid())
183 {
184 // The address didn't resolve, so just set this as an absolute address
185 so_addr.SetOffset (addr);
186 }
187 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24188 return bp_sp;
189}
190
191BreakpointSP
192Target::CreateBreakpoint (Address &addr, bool internal)
193{
194 TargetSP target_sp = this->GetSP();
195 SearchFilterSP filter_sp(new SearchFilter (target_sp));
196 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
197 return CreateBreakpoint (filter_sp, resolver_sp, internal);
198}
199
200BreakpointSP
Greg Clayton0c5cd902010-06-28 21:30:43201Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24202{
Greg Clayton0c5cd902010-06-28 21:30:43203 BreakpointSP bp_sp;
204 if (func_name)
205 {
206 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
207 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
208 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
209 }
210 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24211}
212
213
214SearchFilterSP
215Target::GetSearchFilterForModule (const FileSpec *containingModule)
216{
217 SearchFilterSP filter_sp;
218 lldb::TargetSP target_sp = this->GetSP();
219 if (containingModule != NULL)
220 {
221 // TODO: We should look into sharing module based search filters
222 // across many breakpoints like we do for the simple target based one
223 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
224 }
225 else
226 {
227 if (m_search_filter_sp.get() == NULL)
228 m_search_filter_sp.reset (new SearchFilter (target_sp));
229 filter_sp = m_search_filter_sp;
230 }
231 return filter_sp;
232}
233
234BreakpointSP
235Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
236{
237 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
238 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
239
240 return CreateBreakpoint (filter_sp, resolver_sp, internal);
241}
242
243BreakpointSP
244Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
245{
246 BreakpointSP bp_sp;
247 if (filter_sp && resolver_sp)
248 {
249 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
250 resolver_sp->SetBreakpoint (bp_sp.get());
251
252 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17253 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24254 else
Greg Clayton9fed0d82010-07-23 23:33:17255 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24256
257 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
258 if (log)
259 {
260 StreamString s;
261 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
262 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
263 }
264
Chris Lattner30fdc8d2010-06-08 16:52:24265 bp_sp->ResolveBreakpoint();
266 }
Jim Ingham36f3b362010-10-14 23:45:03267
268 if (!internal && bp_sp)
269 {
270 m_last_created_breakpoint = bp_sp;
271 }
272
Chris Lattner30fdc8d2010-06-08 16:52:24273 return bp_sp;
274}
275
276void
277Target::RemoveAllBreakpoints (bool internal_also)
278{
279 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
280 if (log)
281 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
282
Greg Clayton9fed0d82010-07-23 23:33:17283 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24284 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17285 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03286
287 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24288}
289
290void
291Target::DisableAllBreakpoints (bool internal_also)
292{
293 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
294 if (log)
295 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
296
297 m_breakpoint_list.SetEnabledAll (false);
298 if (internal_also)
299 m_internal_breakpoint_list.SetEnabledAll (false);
300}
301
302void
303Target::EnableAllBreakpoints (bool internal_also)
304{
305 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
306 if (log)
307 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
308
309 m_breakpoint_list.SetEnabledAll (true);
310 if (internal_also)
311 m_internal_breakpoint_list.SetEnabledAll (true);
312}
313
314bool
315Target::RemoveBreakpointByID (break_id_t break_id)
316{
317 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
318 if (log)
319 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
320
321 if (DisableBreakpointByID (break_id))
322 {
323 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17324 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24325 else
Jim Ingham36f3b362010-10-14 23:45:03326 {
327 if (m_last_created_breakpoint->GetID() == break_id)
328 m_last_created_breakpoint.reset();
Greg Clayton9fed0d82010-07-23 23:33:17329 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03330 }
Chris Lattner30fdc8d2010-06-08 16:52:24331 return true;
332 }
333 return false;
334}
335
336bool
337Target::DisableBreakpointByID (break_id_t break_id)
338{
339 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
340 if (log)
341 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, 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 if (bp_sp)
350 {
351 bp_sp->SetEnabled (false);
352 return true;
353 }
354 return false;
355}
356
357bool
358Target::EnableBreakpointByID (break_id_t break_id)
359{
360 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
361 if (log)
362 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
363 __FUNCTION__,
364 break_id,
365 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
366
367 BreakpointSP bp_sp;
368
369 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
370 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
371 else
372 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
373
374 if (bp_sp)
375 {
376 bp_sp->SetEnabled (true);
377 return true;
378 }
379 return false;
380}
381
382ModuleSP
383Target::GetExecutableModule ()
384{
385 ModuleSP executable_sp;
386 if (m_images.GetSize() > 0)
387 executable_sp = m_images.GetModuleAtIndex(0);
388 return executable_sp;
389}
390
391void
392Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
393{
394 m_images.Clear();
395 m_scratch_ast_context_ap.reset();
396
397 if (executable_sp.get())
398 {
399 Timer scoped_timer (__PRETTY_FUNCTION__,
400 "Target::SetExecutableModule (executable = '%s/%s')",
401 executable_sp->GetFileSpec().GetDirectory().AsCString(),
402 executable_sp->GetFileSpec().GetFilename().AsCString());
403
404 m_images.Append(executable_sp); // The first image is our exectuable file
405
406 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02407 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
408 if (!m_arch_spec.IsValid())
409 m_arch_spec = exe_arch;
410
Chris Lattner30fdc8d2010-06-08 16:52:24411 FileSpecList dependent_files;
412 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
413 if (executable_objfile == NULL)
414 {
415
416 FileSpec bundle_executable(executable_sp->GetFileSpec());
Greg Claytondd36def2010-10-17 22:03:32417 if (Host::ResolveExecutableInBundle (bundle_executable))
Chris Lattner30fdc8d2010-06-08 16:52:24418 {
419 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
420 exe_arch));
421 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
422 if (bundle_exe_module_sp->GetObjectFile() != NULL)
423 executable_sp = bundle_exe_module_sp;
424 return;
425 }
426 }
427
428 if (executable_objfile)
429 {
430 executable_objfile->GetDependentModules(dependent_files);
431 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
432 {
433 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
434 exe_arch));
435 if (image_module_sp.get())
436 {
437 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
438 ObjectFile *objfile = image_module_sp->GetObjectFile();
439 if (objfile)
440 objfile->GetDependentModules(dependent_files);
441 }
442 }
443 }
444
445 // Now see if we know the target triple, and if so, create our scratch AST context:
446 ConstString target_triple;
447 if (GetTargetTriple(target_triple))
448 {
449 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString()));
450 }
451 }
Caroline Tice1559a462010-09-27 00:30:10452
453 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24454}
455
456
457ModuleList&
458Target::GetImages ()
459{
460 return m_images;
461}
462
463ArchSpec
464Target::GetArchitecture () const
465{
Jim Ingham5aee1622010-08-09 23:31:02466 return m_arch_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24467}
468
Jim Ingham5aee1622010-08-09 23:31:02469bool
470Target::SetArchitecture (const ArchSpec &arch_spec)
471{
472 if (m_arch_spec == arch_spec)
473 {
474 // If we're setting the architecture to our current architecture, we
475 // don't need to do anything.
476 return true;
477 }
478 else if (!m_arch_spec.IsValid())
479 {
480 // If we haven't got a valid arch spec, then we just need to set it.
481 m_arch_spec = arch_spec;
482 return true;
483 }
484 else
485 {
486 // If we have an executable file, try to reset the executable to the desired architecture
487 m_arch_spec = arch_spec;
488 ModuleSP executable_sp = GetExecutableModule ();
489 m_images.Clear();
490 m_scratch_ast_context_ap.reset();
491 m_triple.Clear();
492 // Need to do something about unsetting breakpoints.
493
494 if (executable_sp)
495 {
496 FileSpec exec_file_spec = executable_sp->GetFileSpec();
497 Error error = ModuleList::GetSharedModule(exec_file_spec,
498 arch_spec,
499 NULL,
500 NULL,
501 0,
502 executable_sp,
503 NULL,
504 NULL);
505
506 if (!error.Fail() && executable_sp)
507 {
508 SetExecutableModule (executable_sp, true);
509 return true;
510 }
511 else
512 {
513 return false;
514 }
515 }
516 else
517 {
518 return false;
519 }
520 }
521}
Chris Lattner30fdc8d2010-06-08 16:52:24522
523bool
524Target::GetTargetTriple(ConstString &triple)
525{
526 triple.Clear();
527
528 if (m_triple)
529 {
530 triple = m_triple;
531 }
532 else
533 {
534 Module *exe_module = GetExecutableModule().get();
535 if (exe_module)
536 {
537 ObjectFile *objfile = exe_module->GetObjectFile();
538 if (objfile)
539 {
540 objfile->GetTargetTriple(m_triple);
541 triple = m_triple;
542 }
543 }
544 }
545 return !triple.IsEmpty();
546}
547
548void
549Target::ModuleAdded (ModuleSP &module_sp)
550{
551 // A module is being added to this target for the first time
552 ModuleList module_list;
553 module_list.Append(module_sp);
554 ModulesDidLoad (module_list);
555}
556
557void
558Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
559{
560 // A module is being added to this target for the first time
561 ModuleList module_list;
562 module_list.Append (old_module_sp);
563 ModulesDidUnload (module_list);
564 module_list.Clear ();
565 module_list.Append (new_module_sp);
566 ModulesDidLoad (module_list);
567}
568
569void
570Target::ModulesDidLoad (ModuleList &module_list)
571{
572 m_breakpoint_list.UpdateBreakpoints (module_list, true);
573 // TODO: make event data that packages up the module_list
574 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
575}
576
577void
578Target::ModulesDidUnload (ModuleList &module_list)
579{
580 m_breakpoint_list.UpdateBreakpoints (module_list, false);
581 // TODO: make event data that packages up the module_list
582 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
583}
584
Chris Lattner30fdc8d2010-06-08 16:52:24585size_t
Greg Clayton35f3dd22010-06-30 23:04:24586Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24587{
Chris Lattner30fdc8d2010-06-08 16:52:24588 error.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24589
Greg Claytondda4f7b2010-06-30 23:03:03590 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
591
592 Address resolved_addr(addr);
593 if (!resolved_addr.IsSectionOffset())
594 {
595 if (process_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24596 {
Greg Claytonf5e56de2010-09-14 23:36:40597 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03598 }
599 else
600 {
601 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
602 }
603 }
604
605
606 if (process_is_valid)
607 {
Greg Claytonf5e56de2010-09-14 23:36:40608 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03609 if (load_addr == LLDB_INVALID_ADDRESS)
610 {
611 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
612 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
613 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
614 resolved_addr.GetFileAddress());
615 else
616 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
617 }
618 else
619 {
620 size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24621 if (bytes_read != dst_len)
622 {
623 if (error.Success())
624 {
625 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03626 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24627 else
Greg Claytondda4f7b2010-06-30 23:03:03628 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:24629 }
630 }
Greg Claytondda4f7b2010-06-30 23:03:03631 if (bytes_read)
632 return bytes_read;
633 // If the address is not section offset we have an address that
634 // doesn't resolve to any address in any currently loaded shared
635 // libaries and we failed to read memory so there isn't anything
636 // more we can do. If it is section offset, we might be able to
637 // read cached memory from the object file.
638 if (!resolved_addr.IsSectionOffset())
639 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24640 }
Chris Lattner30fdc8d2010-06-08 16:52:24641 }
Greg Claytondda4f7b2010-06-30 23:03:03642
643 const Section *section = resolved_addr.GetSection();
644 if (section && section->GetModule())
645 {
646 ObjectFile *objfile = section->GetModule()->GetObjectFile();
647 return section->ReadSectionDataFromObjectFile (objfile,
648 resolved_addr.GetOffset(),
649 dst,
650 dst_len);
651 }
652 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24653}
654
655
656ModuleSP
657Target::GetSharedModule
658(
659 const FileSpec& file_spec,
660 const ArchSpec& arch,
661 const UUID *uuid_ptr,
662 const ConstString *object_name,
663 off_t object_offset,
664 Error *error_ptr
665)
666{
667 // Don't pass in the UUID so we can tell if we have a stale value in our list
668 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
669 bool did_create_module = false;
670 ModuleSP module_sp;
671
672 // If there are image search path entries, try to use them first to acquire a suitable image.
673
674 Error error;
675
676 if (m_image_search_paths.GetSize())
677 {
678 FileSpec transformed_spec;
679 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
680 {
681 transformed_spec.GetFilename() = file_spec.GetFilename();
682 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
683 }
684 }
685
686 // If a module hasn't been found yet, use the unmodified path.
687
688 if (!module_sp)
689 {
690 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
691 }
692
693 if (module_sp)
694 {
695 m_images.Append (module_sp);
696 if (did_create_module)
697 {
698 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
699 ModuleUpdated(old_module_sp, module_sp);
700 else
701 ModuleAdded(module_sp);
702 }
703 }
704 if (error_ptr)
705 *error_ptr = error;
706 return module_sp;
707}
708
709
710Target *
711Target::CalculateTarget ()
712{
713 return this;
714}
715
716Process *
717Target::CalculateProcess ()
718{
719 return NULL;
720}
721
722Thread *
723Target::CalculateThread ()
724{
725 return NULL;
726}
727
728StackFrame *
729Target::CalculateStackFrame ()
730{
731 return NULL;
732}
733
734void
Greg Clayton0603aa92010-10-04 01:05:56735Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24736{
737 exe_ctx.target = this;
738 exe_ctx.process = NULL; // Do NOT fill in process...
739 exe_ctx.thread = NULL;
740 exe_ctx.frame = NULL;
741}
742
743PathMappingList &
744Target::GetImageSearchPathList ()
745{
746 return m_image_search_paths;
747}
748
749void
750Target::ImageSearchPathsChanged
751(
752 const PathMappingList &path_list,
753 void *baton
754)
755{
756 Target *target = (Target *)baton;
757 if (target->m_images.GetSize() > 1)
758 {
759 ModuleSP exe_module_sp (target->GetExecutableModule());
760 if (exe_module_sp)
761 {
762 target->m_images.Clear();
763 target->SetExecutableModule (exe_module_sp, true);
764 }
765 }
766}
767
768ClangASTContext *
769Target::GetScratchClangASTContext()
770{
771 return m_scratch_ast_context_ap.get();
772}
Caroline Ticedaccaa92010-09-20 20:44:43773
774lldb::UserSettingsControllerSP
775Target::GetSettingsController (bool finish)
776{
777 static lldb::UserSettingsControllerSP g_settings_controller (new SettingsController);
778 static bool initialized = false;
779
780 if (!initialized)
781 {
782 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
783 Target::SettingsController::global_settings_table,
784 Target::SettingsController::instance_settings_table);
785 }
786
787 if (finish)
788 {
789 UserSettingsController::FinalizeSettingsController (g_settings_controller);
790 g_settings_controller.reset();
791 initialized = false;
792 }
793
794 return g_settings_controller;
795}
796
797ArchSpec
798Target::GetDefaultArchitecture ()
799{
800 lldb::UserSettingsControllerSP settings_controller = Target::GetSettingsController();
801 lldb::SettableVariableType var_type;
802 Error err;
803 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
804
805 const char *default_name = "";
806 if (result.GetSize() == 1 && err.Success())
807 default_name = result.GetStringAtIndex (0);
808
809 ArchSpec default_arch (default_name);
810 return default_arch;
811}
812
813void
814Target::SetDefaultArchitecture (ArchSpec new_arch)
815{
816 if (new_arch.IsValid())
817 Target::GetSettingsController ()->SetVariable ("target.default-arch", new_arch.AsCString(),
818 lldb::eVarSetOperationAssign, false, "[]");
819}
820
Greg Clayton0603aa92010-10-04 01:05:56821Target *
822Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
823{
824 // The target can either exist in the "process" of ExecutionContext, or in
825 // the "target_sp" member of SymbolContext. This accessor helper function
826 // will get the target from one of these locations.
827
828 Target *target = NULL;
829 if (sc_ptr != NULL)
830 target = sc_ptr->target_sp.get();
831 if (target == NULL)
832 {
833 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
834 target = &exe_ctx_ptr->process->GetTarget();
835 }
836 return target;
837}
838
839
Caroline Tice1559a462010-09-27 00:30:10840void
841Target::UpdateInstanceName ()
842{
843 StreamString sstr;
844
845 ModuleSP module_sp = GetExecutableModule();
846 if (module_sp)
847 {
848 sstr.Printf ("%s_%s", module_sp->GetFileSpec().GetFilename().AsCString(),
849 module_sp->GetArchitecture().AsCString());
850 Target::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
851 sstr.GetData());
852 }
853}
854
Caroline Ticedaccaa92010-09-20 20:44:43855//--------------------------------------------------------------
856// class Target::SettingsController
857//--------------------------------------------------------------
858
859Target::SettingsController::SettingsController () :
860 UserSettingsController ("target", Debugger::GetSettingsController()),
861 m_default_architecture ()
862{
863 m_default_settings.reset (new TargetInstanceSettings (*this, false,
864 InstanceSettings::GetDefaultName().AsCString()));
865}
866
867Target::SettingsController::~SettingsController ()
868{
869}
870
871lldb::InstanceSettingsSP
872Target::SettingsController::CreateInstanceSettings (const char *instance_name)
873{
874 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*(Target::GetSettingsController().get()),
875 false, instance_name);
876 lldb::InstanceSettingsSP new_settings_sp (new_settings);
877 return new_settings_sp;
878}
879
880const ConstString &
881Target::SettingsController::DefArchVarName ()
882{
883 static ConstString def_arch_var_name ("default-arch");
884
885 return def_arch_var_name;
886}
887
888bool
889Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
890 const char *index_value,
891 const char *value,
892 const SettingEntry &entry,
893 const lldb::VarSetOperationType op,
894 Error&err)
895{
896 if (var_name == DefArchVarName())
897 {
898 ArchSpec tmp_spec (value);
899 if (tmp_spec.IsValid())
900 m_default_architecture = tmp_spec;
901 else
902 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
903 }
904 return true;
905}
906
907
908bool
909Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
910 StringList &value,
911 Error &err)
912{
913 if (var_name == DefArchVarName())
914 {
915 value.AppendString (m_default_architecture.AsCString());
916 return true;
917 }
918 else
919 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
920
921 return false;
922}
923
924//--------------------------------------------------------------
925// class TargetInstanceSettings
926//--------------------------------------------------------------
927
928TargetInstanceSettings::TargetInstanceSettings (UserSettingsController &owner, bool live_instance,
929 const char *name) :
930 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance)
931{
932 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
933 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
934 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
935 // This is true for CreateInstanceName() too.
936
937 if (GetInstanceName () == InstanceSettings::InvalidName())
938 {
939 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
940 m_owner.RegisterInstanceSettings (this);
941 }
942
943 if (live_instance)
944 {
945 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
946 CopyInstanceSettings (pending_settings,false);
947 //m_owner.RemovePendingSettings (m_instance_name);
948 }
949}
950
951TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
952 InstanceSettings (*(Target::GetSettingsController().get()), CreateInstanceName().AsCString())
953{
954 if (m_instance_name != InstanceSettings::GetDefaultName())
955 {
956 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
957 CopyInstanceSettings (pending_settings,false);
958 //m_owner.RemovePendingSettings (m_instance_name);
959 }
960}
961
962TargetInstanceSettings::~TargetInstanceSettings ()
963{
964}
965
966TargetInstanceSettings&
967TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
968{
969 if (this != &rhs)
970 {
971 }
972
973 return *this;
974}
975
976
977void
978TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
979 const char *index_value,
980 const char *value,
981 const ConstString &instance_name,
982 const SettingEntry &entry,
983 lldb::VarSetOperationType op,
984 Error &err,
985 bool pending)
986{
987 // Currently 'target' does not have any instance settings.
988}
989
990void
991TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
992 bool pending)
993{
994 // Currently 'target' does not have any instance settings.
995}
996
Caroline Tice12cecd72010-09-20 21:37:42997bool
Caroline Ticedaccaa92010-09-20 20:44:43998TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
999 const ConstString &var_name,
1000 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421001 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431002{
Greg Claytond7aa1142010-09-22 00:23:591003 if (err)
1004 err->SetErrorString ("'target' does not have any instance settings");
1005 return false;
Caroline Ticedaccaa92010-09-20 20:44:431006}
1007
1008const ConstString
1009TargetInstanceSettings::CreateInstanceName ()
1010{
Caroline Ticedaccaa92010-09-20 20:44:431011 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:101012 static int instance_count = 1;
1013
Caroline Ticedaccaa92010-09-20 20:44:431014 sstr.Printf ("target_%d", instance_count);
1015 ++instance_count;
1016
1017 const ConstString ret_val (sstr.GetData());
1018 return ret_val;
1019}
1020
1021//--------------------------------------------------
1022// Target::SettingsController Variable Tables
1023//--------------------------------------------------
1024
1025SettingEntry
1026Target::SettingsController::global_settings_table[] =
1027{
1028 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
Greg Clayton0668d1e2010-10-25 20:08:151029 { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." },
Caroline Ticedaccaa92010-09-20 20:44:431030 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1031};
1032
1033SettingEntry
1034Target::SettingsController::instance_settings_table[] =
1035{
1036 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1037 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1038};