blob: 83024be1e9325c7dabfc2d460e9686af2ec4bd62 [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"
Greg Clayton427f2902010-12-14 02:59:59 +000021#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Core/Event.h"
23#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Host/Host.h"
28#include "lldb/lldb-private-log.h"
29#include "lldb/Symbol/ObjectFile.h"
30#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000031#include "lldb/Target/StackFrame.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032
33using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
37// Target constructor
38//----------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +000039Target::Target(Debugger &debugger) :
Greg Clayton49ce6822010-10-31 03:01:06 +000040 Broadcaster("lldb.target"),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000041 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000042 m_debugger (debugger),
Greg Claytonbdcda462010-12-20 20:49:23 +000043 m_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000044 m_images(),
Greg Claytoneea26402010-09-14 23:36:40 +000045 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000046 m_breakpoint_list (false),
47 m_internal_breakpoint_list (true),
48 m_process_sp(),
49 m_triple(),
50 m_search_filter_sp(),
51 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000052 m_scratch_ast_context_ap (NULL),
53 m_persistent_variables ()
Chris Lattner24943d22010-06-08 16:52:24 +000054{
Greg Clayton49ce6822010-10-31 03:01:06 +000055 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
56 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
57 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
58
Greg Claytone005f2c2010-11-06 01:53:30 +000059 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000060 if (log)
61 log->Printf ("%p Target::Target()", this);
62}
63
64//----------------------------------------------------------------------
65// Destructor
66//----------------------------------------------------------------------
67Target::~Target()
68{
Greg Claytone005f2c2010-11-06 01:53:30 +000069 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000070 if (log)
71 log->Printf ("%p Target::~Target()", this);
72 DeleteCurrentProcess ();
73}
74
75void
Caroline Tice7826c882010-10-26 03:11:13 +000076Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000077{
Greg Clayton3fed8b92010-10-08 00:21:05 +000078// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000079 if (description_level != lldb::eDescriptionLevelBrief)
80 {
81 s->Indent();
82 s->PutCString("Target\n");
83 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000084 m_images.Dump(s);
85 m_breakpoint_list.Dump(s);
86 m_internal_breakpoint_list.Dump(s);
87 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000088 }
89 else
90 {
Greg Claytona66ba462010-10-30 04:51:46 +000091 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Tice7826c882010-10-26 03:11:13 +000092 }
Chris Lattner24943d22010-06-08 16:52:24 +000093}
94
95void
96Target::DeleteCurrentProcess ()
97{
98 if (m_process_sp.get())
99 {
Greg Clayton49480b12010-09-14 23:52:43 +0000100 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000101 if (m_process_sp->IsAlive())
102 m_process_sp->Destroy();
103 else
104 m_process_sp->Finalize();
105
106 // Do any cleanup of the target we need to do between process instances.
107 // NB It is better to do this before destroying the process in case the
108 // clean up needs some help from the process.
109 m_breakpoint_list.ClearAllBreakpointSites();
110 m_internal_breakpoint_list.ClearAllBreakpointSites();
111 m_process_sp.reset();
112 }
113}
114
115const lldb::ProcessSP &
116Target::CreateProcess (Listener &listener, const char *plugin_name)
117{
118 DeleteCurrentProcess ();
119 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
120 return m_process_sp;
121}
122
123const lldb::ProcessSP &
124Target::GetProcessSP () const
125{
126 return m_process_sp;
127}
128
129lldb::TargetSP
130Target::GetSP()
131{
Greg Clayton63094e02010-06-23 01:19:29 +0000132 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000133}
134
135BreakpointList &
136Target::GetBreakpointList(bool internal)
137{
138 if (internal)
139 return m_internal_breakpoint_list;
140 else
141 return m_breakpoint_list;
142}
143
144const BreakpointList &
145Target::GetBreakpointList(bool internal) const
146{
147 if (internal)
148 return m_internal_breakpoint_list;
149 else
150 return m_breakpoint_list;
151}
152
153BreakpointSP
154Target::GetBreakpointByID (break_id_t break_id)
155{
156 BreakpointSP bp_sp;
157
158 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
159 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
160 else
161 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
162
163 return bp_sp;
164}
165
166BreakpointSP
167Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
168{
169 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
170 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
171 return CreateBreakpoint (filter_sp, resolver_sp, internal);
172}
173
174
175BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000176Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000177{
Chris Lattner24943d22010-06-08 16:52:24 +0000178 Address so_addr;
179 // Attempt to resolve our load address if possible, though it is ok if
180 // it doesn't resolve to section/offset.
181
Greg Clayton33ed1702010-08-24 00:45:41 +0000182 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000183 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000184 if (!so_addr.IsValid())
185 {
186 // The address didn't resolve, so just set this as an absolute address
187 so_addr.SetOffset (addr);
188 }
189 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000190 return bp_sp;
191}
192
193BreakpointSP
194Target::CreateBreakpoint (Address &addr, bool internal)
195{
196 TargetSP target_sp = this->GetSP();
197 SearchFilterSP filter_sp(new SearchFilter (target_sp));
198 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
199 return CreateBreakpoint (filter_sp, resolver_sp, internal);
200}
201
202BreakpointSP
Greg Clayton12bec712010-06-28 21:30:43 +0000203Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000204{
Greg Clayton12bec712010-06-28 21:30:43 +0000205 BreakpointSP bp_sp;
206 if (func_name)
207 {
208 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
209 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
210 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
211 }
212 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000213}
214
215
216SearchFilterSP
217Target::GetSearchFilterForModule (const FileSpec *containingModule)
218{
219 SearchFilterSP filter_sp;
220 lldb::TargetSP target_sp = this->GetSP();
221 if (containingModule != NULL)
222 {
223 // TODO: We should look into sharing module based search filters
224 // across many breakpoints like we do for the simple target based one
225 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
226 }
227 else
228 {
229 if (m_search_filter_sp.get() == NULL)
230 m_search_filter_sp.reset (new SearchFilter (target_sp));
231 filter_sp = m_search_filter_sp;
232 }
233 return filter_sp;
234}
235
236BreakpointSP
237Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
238{
239 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
240 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
241
242 return CreateBreakpoint (filter_sp, resolver_sp, internal);
243}
244
245BreakpointSP
246Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
247{
248 BreakpointSP bp_sp;
249 if (filter_sp && resolver_sp)
250 {
251 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
252 resolver_sp->SetBreakpoint (bp_sp.get());
253
254 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000255 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000256 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000257 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000258
Greg Claytone005f2c2010-11-06 01:53:30 +0000259 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000260 if (log)
261 {
262 StreamString s;
263 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
264 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
265 }
266
Chris Lattner24943d22010-06-08 16:52:24 +0000267 bp_sp->ResolveBreakpoint();
268 }
Jim Inghamd1686902010-10-14 23:45:03 +0000269
270 if (!internal && bp_sp)
271 {
272 m_last_created_breakpoint = bp_sp;
273 }
274
Chris Lattner24943d22010-06-08 16:52:24 +0000275 return bp_sp;
276}
277
278void
279Target::RemoveAllBreakpoints (bool internal_also)
280{
Greg Claytone005f2c2010-11-06 01:53:30 +0000281 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000282 if (log)
283 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
284
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000285 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000286 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000287 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000288
289 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000290}
291
292void
293Target::DisableAllBreakpoints (bool internal_also)
294{
Greg Claytone005f2c2010-11-06 01:53:30 +0000295 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000296 if (log)
297 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
298
299 m_breakpoint_list.SetEnabledAll (false);
300 if (internal_also)
301 m_internal_breakpoint_list.SetEnabledAll (false);
302}
303
304void
305Target::EnableAllBreakpoints (bool internal_also)
306{
Greg Claytone005f2c2010-11-06 01:53:30 +0000307 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000308 if (log)
309 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
310
311 m_breakpoint_list.SetEnabledAll (true);
312 if (internal_also)
313 m_internal_breakpoint_list.SetEnabledAll (true);
314}
315
316bool
317Target::RemoveBreakpointByID (break_id_t break_id)
318{
Greg Claytone005f2c2010-11-06 01:53:30 +0000319 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000320 if (log)
321 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
322
323 if (DisableBreakpointByID (break_id))
324 {
325 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000326 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000327 else
Jim Inghamd1686902010-10-14 23:45:03 +0000328 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000329 if (m_last_created_breakpoint)
330 {
331 if (m_last_created_breakpoint->GetID() == break_id)
332 m_last_created_breakpoint.reset();
333 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000334 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000335 }
Chris Lattner24943d22010-06-08 16:52:24 +0000336 return true;
337 }
338 return false;
339}
340
341bool
342Target::DisableBreakpointByID (break_id_t break_id)
343{
Greg Claytone005f2c2010-11-06 01:53:30 +0000344 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000345 if (log)
346 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
347
348 BreakpointSP bp_sp;
349
350 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
351 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
352 else
353 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
354 if (bp_sp)
355 {
356 bp_sp->SetEnabled (false);
357 return true;
358 }
359 return false;
360}
361
362bool
363Target::EnableBreakpointByID (break_id_t break_id)
364{
Greg Claytone005f2c2010-11-06 01:53:30 +0000365 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000366 if (log)
367 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
368 __FUNCTION__,
369 break_id,
370 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
371
372 BreakpointSP bp_sp;
373
374 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
375 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
376 else
377 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
378
379 if (bp_sp)
380 {
381 bp_sp->SetEnabled (true);
382 return true;
383 }
384 return false;
385}
386
387ModuleSP
388Target::GetExecutableModule ()
389{
390 ModuleSP executable_sp;
391 if (m_images.GetSize() > 0)
392 executable_sp = m_images.GetModuleAtIndex(0);
393 return executable_sp;
394}
395
396void
397Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
398{
399 m_images.Clear();
400 m_scratch_ast_context_ap.reset();
401
402 if (executable_sp.get())
403 {
404 Timer scoped_timer (__PRETTY_FUNCTION__,
405 "Target::SetExecutableModule (executable = '%s/%s')",
406 executable_sp->GetFileSpec().GetDirectory().AsCString(),
407 executable_sp->GetFileSpec().GetFilename().AsCString());
408
409 m_images.Append(executable_sp); // The first image is our exectuable file
410
411 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000412 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
413 if (!m_arch_spec.IsValid())
414 m_arch_spec = exe_arch;
415
Chris Lattner24943d22010-06-08 16:52:24 +0000416 FileSpecList dependent_files;
417 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
418 if (executable_objfile == NULL)
419 {
420
421 FileSpec bundle_executable(executable_sp->GetFileSpec());
Greg Clayton24b48ff2010-10-17 22:03:32 +0000422 if (Host::ResolveExecutableInBundle (bundle_executable))
Chris Lattner24943d22010-06-08 16:52:24 +0000423 {
424 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
425 exe_arch));
426 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
427 if (bundle_exe_module_sp->GetObjectFile() != NULL)
428 executable_sp = bundle_exe_module_sp;
429 return;
430 }
431 }
432
433 if (executable_objfile)
434 {
435 executable_objfile->GetDependentModules(dependent_files);
436 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
437 {
438 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
439 exe_arch));
440 if (image_module_sp.get())
441 {
442 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
443 ObjectFile *objfile = image_module_sp->GetObjectFile();
444 if (objfile)
445 objfile->GetDependentModules(dependent_files);
446 }
447 }
448 }
449
450 // Now see if we know the target triple, and if so, create our scratch AST context:
451 ConstString target_triple;
452 if (GetTargetTriple(target_triple))
453 {
454 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString()));
455 }
456 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000457
458 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000459}
460
461
462ModuleList&
463Target::GetImages ()
464{
465 return m_images;
466}
467
468ArchSpec
469Target::GetArchitecture () const
470{
Jim Ingham7508e732010-08-09 23:31:02 +0000471 return m_arch_spec;
Chris Lattner24943d22010-06-08 16:52:24 +0000472}
473
Jim Ingham7508e732010-08-09 23:31:02 +0000474bool
475Target::SetArchitecture (const ArchSpec &arch_spec)
476{
477 if (m_arch_spec == arch_spec)
478 {
479 // If we're setting the architecture to our current architecture, we
480 // don't need to do anything.
481 return true;
482 }
483 else if (!m_arch_spec.IsValid())
484 {
485 // If we haven't got a valid arch spec, then we just need to set it.
486 m_arch_spec = arch_spec;
487 return true;
488 }
489 else
490 {
491 // If we have an executable file, try to reset the executable to the desired architecture
492 m_arch_spec = arch_spec;
493 ModuleSP executable_sp = GetExecutableModule ();
494 m_images.Clear();
495 m_scratch_ast_context_ap.reset();
496 m_triple.Clear();
497 // Need to do something about unsetting breakpoints.
498
499 if (executable_sp)
500 {
501 FileSpec exec_file_spec = executable_sp->GetFileSpec();
502 Error error = ModuleList::GetSharedModule(exec_file_spec,
503 arch_spec,
504 NULL,
505 NULL,
506 0,
507 executable_sp,
508 NULL,
509 NULL);
510
511 if (!error.Fail() && executable_sp)
512 {
513 SetExecutableModule (executable_sp, true);
514 return true;
515 }
516 else
517 {
518 return false;
519 }
520 }
521 else
522 {
523 return false;
524 }
525 }
526}
Chris Lattner24943d22010-06-08 16:52:24 +0000527
528bool
529Target::GetTargetTriple(ConstString &triple)
530{
531 triple.Clear();
532
533 if (m_triple)
534 {
535 triple = m_triple;
536 }
537 else
538 {
539 Module *exe_module = GetExecutableModule().get();
540 if (exe_module)
541 {
542 ObjectFile *objfile = exe_module->GetObjectFile();
543 if (objfile)
544 {
545 objfile->GetTargetTriple(m_triple);
546 triple = m_triple;
547 }
548 }
549 }
550 return !triple.IsEmpty();
551}
552
553void
554Target::ModuleAdded (ModuleSP &module_sp)
555{
556 // A module is being added to this target for the first time
557 ModuleList module_list;
558 module_list.Append(module_sp);
559 ModulesDidLoad (module_list);
560}
561
562void
563Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
564{
565 // A module is being added to this target for the first time
566 ModuleList module_list;
567 module_list.Append (old_module_sp);
568 ModulesDidUnload (module_list);
569 module_list.Clear ();
570 module_list.Append (new_module_sp);
571 ModulesDidLoad (module_list);
572}
573
574void
575Target::ModulesDidLoad (ModuleList &module_list)
576{
577 m_breakpoint_list.UpdateBreakpoints (module_list, true);
578 // TODO: make event data that packages up the module_list
579 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
580}
581
582void
583Target::ModulesDidUnload (ModuleList &module_list)
584{
585 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000586
587 // Remove the images from the target image list
588 m_images.Remove(module_list);
589
Chris Lattner24943d22010-06-08 16:52:24 +0000590 // TODO: make event data that packages up the module_list
591 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
592}
593
594size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000595Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
596{
597 const Section *section = addr.GetSection();
598 if (section && section->GetModule())
599 {
600 ObjectFile *objfile = section->GetModule()->GetObjectFile();
601 if (objfile)
602 {
603 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
604 addr.GetOffset(),
605 dst,
606 dst_len);
607 if (bytes_read > 0)
608 return bytes_read;
609 else
610 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
611 }
612 else
613 {
614 error.SetErrorString("address isn't from a object file");
615 }
616 }
617 else
618 {
619 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
620 }
621 return 0;
622}
623
624size_t
625Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000626{
Chris Lattner24943d22010-06-08 16:52:24 +0000627 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000628
Greg Clayton70436352010-06-30 23:03:03 +0000629 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
630
Greg Clayton26100dc2011-01-07 01:57:07 +0000631 size_t bytes_read = 0;
Greg Clayton70436352010-06-30 23:03:03 +0000632 Address resolved_addr(addr);
633 if (!resolved_addr.IsSectionOffset())
634 {
635 if (process_is_valid)
Chris Lattner24943d22010-06-08 16:52:24 +0000636 {
Greg Claytoneea26402010-09-14 23:36:40 +0000637 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000638 }
639 else
640 {
641 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
642 }
643 }
644
Greg Clayton26100dc2011-01-07 01:57:07 +0000645 if (prefer_file_cache)
646 {
647 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
648 if (bytes_read > 0)
649 return bytes_read;
650 }
Greg Clayton70436352010-06-30 23:03:03 +0000651
652 if (process_is_valid)
653 {
Greg Claytoneea26402010-09-14 23:36:40 +0000654 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000655 if (load_addr == LLDB_INVALID_ADDRESS)
656 {
657 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
658 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
659 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
660 resolved_addr.GetFileAddress());
661 else
662 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
663 }
664 else
665 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000666 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000667 if (bytes_read != dst_len)
668 {
669 if (error.Success())
670 {
671 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000672 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000673 else
Greg Clayton70436352010-06-30 23:03:03 +0000674 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 +0000675 }
676 }
Greg Clayton70436352010-06-30 23:03:03 +0000677 if (bytes_read)
678 return bytes_read;
679 // If the address is not section offset we have an address that
680 // doesn't resolve to any address in any currently loaded shared
681 // libaries and we failed to read memory so there isn't anything
682 // more we can do. If it is section offset, we might be able to
683 // read cached memory from the object file.
684 if (!resolved_addr.IsSectionOffset())
685 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000686 }
Chris Lattner24943d22010-06-08 16:52:24 +0000687 }
Greg Clayton70436352010-06-30 23:03:03 +0000688
Greg Clayton26100dc2011-01-07 01:57:07 +0000689 if (!prefer_file_cache)
Greg Clayton70436352010-06-30 23:03:03 +0000690 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000691 // If we didn't already try and read from the object file cache, then
692 // try it after failing to read from the process.
693 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000694 }
695 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000696}
697
698
699ModuleSP
700Target::GetSharedModule
701(
702 const FileSpec& file_spec,
703 const ArchSpec& arch,
704 const UUID *uuid_ptr,
705 const ConstString *object_name,
706 off_t object_offset,
707 Error *error_ptr
708)
709{
710 // Don't pass in the UUID so we can tell if we have a stale value in our list
711 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
712 bool did_create_module = false;
713 ModuleSP module_sp;
714
715 // If there are image search path entries, try to use them first to acquire a suitable image.
716
717 Error error;
718
719 if (m_image_search_paths.GetSize())
720 {
721 FileSpec transformed_spec;
722 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
723 {
724 transformed_spec.GetFilename() = file_spec.GetFilename();
725 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
726 }
727 }
728
729 // If a module hasn't been found yet, use the unmodified path.
730
731 if (!module_sp)
732 {
733 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
734 }
735
736 if (module_sp)
737 {
738 m_images.Append (module_sp);
739 if (did_create_module)
740 {
741 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
742 ModuleUpdated(old_module_sp, module_sp);
743 else
744 ModuleAdded(module_sp);
745 }
746 }
747 if (error_ptr)
748 *error_ptr = error;
749 return module_sp;
750}
751
752
753Target *
754Target::CalculateTarget ()
755{
756 return this;
757}
758
759Process *
760Target::CalculateProcess ()
761{
762 return NULL;
763}
764
765Thread *
766Target::CalculateThread ()
767{
768 return NULL;
769}
770
771StackFrame *
772Target::CalculateStackFrame ()
773{
774 return NULL;
775}
776
777void
Greg Claytona830adb2010-10-04 01:05:56 +0000778Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000779{
780 exe_ctx.target = this;
781 exe_ctx.process = NULL; // Do NOT fill in process...
782 exe_ctx.thread = NULL;
783 exe_ctx.frame = NULL;
784}
785
786PathMappingList &
787Target::GetImageSearchPathList ()
788{
789 return m_image_search_paths;
790}
791
792void
793Target::ImageSearchPathsChanged
794(
795 const PathMappingList &path_list,
796 void *baton
797)
798{
799 Target *target = (Target *)baton;
800 if (target->m_images.GetSize() > 1)
801 {
802 ModuleSP exe_module_sp (target->GetExecutableModule());
803 if (exe_module_sp)
804 {
805 target->m_images.Clear();
806 target->SetExecutableModule (exe_module_sp, true);
807 }
808 }
809}
810
811ClangASTContext *
812Target::GetScratchClangASTContext()
813{
814 return m_scratch_ast_context_ap.get();
815}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000816
Greg Clayton990de7b2010-11-18 23:32:35 +0000817void
818Target::Initialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000819{
Greg Clayton990de7b2010-11-18 23:32:35 +0000820 UserSettingsControllerSP &usc = GetSettingsController();
821 usc.reset (new SettingsController);
822 UserSettingsController::InitializeSettingsController (usc,
823 SettingsController::global_settings_table,
824 SettingsController::instance_settings_table);
825}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000826
Greg Clayton990de7b2010-11-18 23:32:35 +0000827void
828Target::Terminate ()
829{
830 UserSettingsControllerSP &usc = GetSettingsController();
831 UserSettingsController::FinalizeSettingsController (usc);
832 usc.reset();
833}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000834
Greg Clayton990de7b2010-11-18 23:32:35 +0000835UserSettingsControllerSP &
836Target::GetSettingsController ()
837{
838 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000839 return g_settings_controller;
840}
841
842ArchSpec
843Target::GetDefaultArchitecture ()
844{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000845 lldb::UserSettingsControllerSP &settings_controller = GetSettingsController();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000846 lldb::SettableVariableType var_type;
847 Error err;
848 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
849
850 const char *default_name = "";
851 if (result.GetSize() == 1 && err.Success())
852 default_name = result.GetStringAtIndex (0);
853
854 ArchSpec default_arch (default_name);
855 return default_arch;
856}
857
858void
859Target::SetDefaultArchitecture (ArchSpec new_arch)
860{
861 if (new_arch.IsValid())
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000862 GetSettingsController ()->SetVariable ("target.default-arch",
863 new_arch.AsCString(),
864 lldb::eVarSetOperationAssign,
865 false,
866 "[]");
Caroline Tice5bc8c972010-09-20 20:44:43 +0000867}
868
Greg Claytona830adb2010-10-04 01:05:56 +0000869Target *
870Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
871{
872 // The target can either exist in the "process" of ExecutionContext, or in
873 // the "target_sp" member of SymbolContext. This accessor helper function
874 // will get the target from one of these locations.
875
876 Target *target = NULL;
877 if (sc_ptr != NULL)
878 target = sc_ptr->target_sp.get();
879 if (target == NULL)
880 {
881 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
882 target = &exe_ctx_ptr->process->GetTarget();
883 }
884 return target;
885}
886
887
Caroline Tice1ebef442010-09-27 00:30:10 +0000888void
889Target::UpdateInstanceName ()
890{
891 StreamString sstr;
892
893 ModuleSP module_sp = GetExecutableModule();
894 if (module_sp)
895 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000896 sstr.Printf ("%s_%s",
897 module_sp->GetFileSpec().GetFilename().AsCString(),
Caroline Tice1ebef442010-09-27 00:30:10 +0000898 module_sp->GetArchitecture().AsCString());
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000899 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
900 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000901 }
902}
903
Sean Callanan77e93942010-10-29 00:29:03 +0000904const char *
905Target::GetExpressionPrefixContentsAsCString ()
906{
907 return m_expr_prefix_contents.c_str();
908}
909
Greg Clayton427f2902010-12-14 02:59:59 +0000910ExecutionResults
911Target::EvaluateExpression
912(
913 const char *expr_cstr,
914 StackFrame *frame,
915 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +0000916 bool keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +0000917 lldb::ValueObjectSP &result_valobj_sp
918)
919{
920 ExecutionResults execution_results = eExecutionSetupError;
921
922 result_valobj_sp.reset();
923
924 ExecutionContext exe_ctx;
925 if (frame)
926 {
927 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000928 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +0000929 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
930 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
931 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton427f2902010-12-14 02:59:59 +0000932 }
933 else if (m_process_sp)
934 {
935 m_process_sp->CalculateExecutionContext(exe_ctx);
936 }
937 else
938 {
939 CalculateExecutionContext(exe_ctx);
940 }
941
942 if (result_valobj_sp)
943 {
944 execution_results = eExecutionCompleted;
945 // We got a result from the frame variable expression path above...
946 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
947
948 lldb::ValueObjectSP const_valobj_sp;
949
950 // Check in case our value is already a constant value
951 if (result_valobj_sp->GetIsConstant())
952 {
953 const_valobj_sp = result_valobj_sp;
954 const_valobj_sp->SetName (persistent_variable_name);
955 }
956 else
957 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
958 persistent_variable_name);
959
Sean Callanan6a925532011-01-13 08:53:35 +0000960 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
961
Greg Clayton427f2902010-12-14 02:59:59 +0000962 result_valobj_sp = const_valobj_sp;
963
Sean Callanan6a925532011-01-13 08:53:35 +0000964 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
965 assert (clang_expr_variable_sp.get());
966
967 // Set flags and live data as appropriate
968
969 const Value &result_value = live_valobj_sp->GetValue();
970
971 switch (result_value.GetValueType())
972 {
973 case Value::eValueTypeHostAddress:
974 case Value::eValueTypeFileAddress:
975 // we don't do anything with these for now
976 break;
977 case Value::eValueTypeScalar:
978 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
979 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
980 break;
981 case Value::eValueTypeLoadAddress:
982 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
983 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
984 break;
985 }
Greg Clayton427f2902010-12-14 02:59:59 +0000986 }
987 else
988 {
989 // Make sure we aren't just trying to see the value of a persistent
990 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +0000991 lldb::ClangExpressionVariableSP persistent_var_sp;
992 // Only check for persistent variables the expression starts with a '$'
993 if (expr_cstr[0] == '$')
994 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
995
Greg Clayton427f2902010-12-14 02:59:59 +0000996 if (persistent_var_sp)
997 {
998 result_valobj_sp = persistent_var_sp->GetValueObject ();
999 execution_results = eExecutionCompleted;
1000 }
1001 else
1002 {
1003 const char *prefix = GetExpressionPrefixContentsAsCString();
1004
1005 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +00001006 unwind_on_error,
1007 keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +00001008 expr_cstr,
1009 prefix,
1010 result_valobj_sp);
1011 }
1012 }
1013 return execution_results;
1014}
1015
Caroline Tice5bc8c972010-09-20 20:44:43 +00001016//--------------------------------------------------------------
1017// class Target::SettingsController
1018//--------------------------------------------------------------
1019
1020Target::SettingsController::SettingsController () :
1021 UserSettingsController ("target", Debugger::GetSettingsController()),
1022 m_default_architecture ()
1023{
1024 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1025 InstanceSettings::GetDefaultName().AsCString()));
1026}
1027
1028Target::SettingsController::~SettingsController ()
1029{
1030}
1031
1032lldb::InstanceSettingsSP
1033Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1034{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001035 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1036 false,
1037 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001038 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1039 return new_settings_sp;
1040}
1041
1042const ConstString &
1043Target::SettingsController::DefArchVarName ()
1044{
1045 static ConstString def_arch_var_name ("default-arch");
1046
1047 return def_arch_var_name;
1048}
1049
1050bool
1051Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1052 const char *index_value,
1053 const char *value,
1054 const SettingEntry &entry,
1055 const lldb::VarSetOperationType op,
1056 Error&err)
1057{
1058 if (var_name == DefArchVarName())
1059 {
1060 ArchSpec tmp_spec (value);
1061 if (tmp_spec.IsValid())
1062 m_default_architecture = tmp_spec;
1063 else
1064 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
1065 }
1066 return true;
1067}
1068
1069
1070bool
1071Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1072 StringList &value,
1073 Error &err)
1074{
1075 if (var_name == DefArchVarName())
1076 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001077 // If the arch is invalid (the default), don't show a string for it
1078 if (m_default_architecture.IsValid())
1079 value.AppendString (m_default_architecture.AsCString());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001080 return true;
1081 }
1082 else
1083 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1084
1085 return false;
1086}
1087
1088//--------------------------------------------------------------
1089// class TargetInstanceSettings
1090//--------------------------------------------------------------
1091
Greg Clayton638351a2010-12-04 00:10:17 +00001092TargetInstanceSettings::TargetInstanceSettings
1093(
1094 UserSettingsController &owner,
1095 bool live_instance,
1096 const char *name
1097) :
1098 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001099{
1100 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1101 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1102 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1103 // This is true for CreateInstanceName() too.
1104
1105 if (GetInstanceName () == InstanceSettings::InvalidName())
1106 {
1107 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1108 m_owner.RegisterInstanceSettings (this);
1109 }
1110
1111 if (live_instance)
1112 {
1113 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1114 CopyInstanceSettings (pending_settings,false);
1115 //m_owner.RemovePendingSettings (m_instance_name);
1116 }
1117}
1118
1119TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001120 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001121{
1122 if (m_instance_name != InstanceSettings::GetDefaultName())
1123 {
1124 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1125 CopyInstanceSettings (pending_settings,false);
1126 //m_owner.RemovePendingSettings (m_instance_name);
1127 }
1128}
1129
1130TargetInstanceSettings::~TargetInstanceSettings ()
1131{
1132}
1133
1134TargetInstanceSettings&
1135TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1136{
1137 if (this != &rhs)
1138 {
1139 }
1140
1141 return *this;
1142}
1143
Sean Callanan77e93942010-10-29 00:29:03 +00001144#define EXPR_PREFIX_STRING "expr-prefix"
Caroline Tice5bc8c972010-09-20 20:44:43 +00001145
1146void
1147TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1148 const char *index_value,
1149 const char *value,
1150 const ConstString &instance_name,
1151 const SettingEntry &entry,
1152 lldb::VarSetOperationType op,
1153 Error &err,
1154 bool pending)
1155{
Sean Callanan77e93942010-10-29 00:29:03 +00001156 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
1157
1158 if (var_name == expr_prefix_str)
1159 {
1160 switch (op)
1161 {
1162 default:
1163 err.SetErrorToGenericError ();
1164 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1165 return;
1166 case lldb::eVarSetOperationAssign:
1167 {
1168 FileSpec file_spec(value, true);
1169
1170 if (!file_spec.Exists())
1171 {
1172 err.SetErrorToGenericError ();
1173 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1174 return;
1175 }
1176
Greg Clayton9f527042011-02-03 17:47:47 +00001177 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan77e93942010-10-29 00:29:03 +00001178
Greg Clayton9f527042011-02-03 17:47:47 +00001179 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan77e93942010-10-29 00:29:03 +00001180 {
1181 err.SetErrorToGenericError ();
Greg Clayton9f527042011-02-03 17:47:47 +00001182 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan77e93942010-10-29 00:29:03 +00001183 return;
1184 }
1185
1186 m_expr_prefix_path = value;
Greg Clayton9f527042011-02-03 17:47:47 +00001187 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan77e93942010-10-29 00:29:03 +00001188 }
1189 return;
1190 case lldb::eVarSetOperationAppend:
1191 err.SetErrorToGenericError ();
1192 err.SetErrorString ("Cannot append to a path.\n");
1193 return;
1194 case lldb::eVarSetOperationClear:
1195 m_expr_prefix_path.clear ();
1196 m_expr_prefix_contents.clear ();
1197 return;
1198 }
1199 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001200}
1201
1202void
1203TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
Sean Callanan77e93942010-10-29 00:29:03 +00001204 bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001205{
Sean Callanan77e93942010-10-29 00:29:03 +00001206 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1207
1208 if (!new_settings_ptr)
1209 return;
1210
1211 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1212 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001213}
1214
Caroline Ticebcb5b452010-09-20 21:37:42 +00001215bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001216TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1217 const ConstString &var_name,
1218 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001219 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001220{
Sean Callanan77e93942010-10-29 00:29:03 +00001221 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
1222
1223 if (var_name == expr_prefix_str)
1224 {
1225 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1226 }
1227 else
1228 {
1229 if (err)
1230 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1231 return false;
1232 }
1233
1234 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001235}
1236
1237const ConstString
1238TargetInstanceSettings::CreateInstanceName ()
1239{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001240 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001241 static int instance_count = 1;
1242
Caroline Tice5bc8c972010-09-20 20:44:43 +00001243 sstr.Printf ("target_%d", instance_count);
1244 ++instance_count;
1245
1246 const ConstString ret_val (sstr.GetData());
1247 return ret_val;
1248}
1249
1250//--------------------------------------------------
1251// Target::SettingsController Variable Tables
1252//--------------------------------------------------
1253
1254SettingEntry
1255Target::SettingsController::global_settings_table[] =
1256{
Sean Callanan77e93942010-10-29 00:29:03 +00001257 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1258 { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." },
Caroline Tice5bc8c972010-09-20 20:44:43 +00001259 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1260};
1261
1262SettingEntry
1263Target::SettingsController::instance_settings_table[] =
1264{
Sean Callanan77e93942010-10-29 00:29:03 +00001265 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1266 { 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 +00001267 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1268};