blob: 82110db57f9fac39d50573ca9353ff1e87a44bc4 [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"
Greg Clayton427f2902010-12-14 02:59:59 +000020#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000024#include "lldb/Core/Timer.h"
25#include "lldb/Core/ValueObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Host/Host.h"
27#include "lldb/lldb-private-log.h"
28#include "lldb/Symbol/ObjectFile.h"
29#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000030#include "lldb/Target/StackFrame.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031
32using namespace lldb;
33using namespace lldb_private;
34
35//----------------------------------------------------------------------
36// Target constructor
37//----------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +000038Target::Target(Debugger &debugger) :
Greg Clayton49ce6822010-10-31 03:01:06 +000039 Broadcaster("lldb.target"),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000040 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000041 m_debugger (debugger),
Greg Claytonbdcda462010-12-20 20:49:23 +000042 m_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000043 m_images(),
Greg Claytoneea26402010-09-14 23:36:40 +000044 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000045 m_breakpoint_list (false),
46 m_internal_breakpoint_list (true),
47 m_process_sp(),
Chris Lattner24943d22010-06-08 16:52:24 +000048 m_search_filter_sp(),
49 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000050 m_scratch_ast_context_ap (NULL),
51 m_persistent_variables ()
Chris Lattner24943d22010-06-08 16:52:24 +000052{
Greg Clayton49ce6822010-10-31 03:01:06 +000053 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
54 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
55 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
56
Greg Claytone005f2c2010-11-06 01:53:30 +000057 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000058 if (log)
59 log->Printf ("%p Target::Target()", this);
60}
61
62//----------------------------------------------------------------------
63// Destructor
64//----------------------------------------------------------------------
65Target::~Target()
66{
Greg Claytone005f2c2010-11-06 01:53:30 +000067 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000068 if (log)
69 log->Printf ("%p Target::~Target()", this);
70 DeleteCurrentProcess ();
71}
72
73void
Caroline Tice7826c882010-10-26 03:11:13 +000074Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000075{
Greg Clayton3fed8b92010-10-08 00:21:05 +000076// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000077 if (description_level != lldb::eDescriptionLevelBrief)
78 {
79 s->Indent();
80 s->PutCString("Target\n");
81 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000082 m_images.Dump(s);
83 m_breakpoint_list.Dump(s);
84 m_internal_breakpoint_list.Dump(s);
85 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000086 }
87 else
88 {
Greg Claytona66ba462010-10-30 04:51:46 +000089 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Tice7826c882010-10-26 03:11:13 +000090 }
Chris Lattner24943d22010-06-08 16:52:24 +000091}
92
93void
94Target::DeleteCurrentProcess ()
95{
96 if (m_process_sp.get())
97 {
Greg Clayton49480b12010-09-14 23:52:43 +000098 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000099 if (m_process_sp->IsAlive())
100 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000101
102 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000103
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 Clayton63094e02010-06-23 01:19:29 +0000130 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000131}
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 Clayton33ed1702010-08-24 00:45:41 +0000174Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000175{
Chris Lattner24943d22010-06-08 16:52:24 +0000176 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 Clayton33ed1702010-08-24 00:45:41 +0000180 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000181 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000182 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 Lattner24943d22010-06-08 16:52:24 +0000188 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 Clayton12bec712010-06-28 21:30:43 +0000201Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000202{
Greg Clayton12bec712010-06-28 21:30:43 +0000203 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 Lattner24943d22010-06-08 16:52:24 +0000211}
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 Claytonc7f5d5c2010-07-23 23:33:17 +0000253 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000254 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000255 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000256
Greg Claytone005f2c2010-11-06 01:53:30 +0000257 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000258 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 Lattner24943d22010-06-08 16:52:24 +0000265 bp_sp->ResolveBreakpoint();
266 }
Jim Inghamd1686902010-10-14 23:45:03 +0000267
268 if (!internal && bp_sp)
269 {
270 m_last_created_breakpoint = bp_sp;
271 }
272
Chris Lattner24943d22010-06-08 16:52:24 +0000273 return bp_sp;
274}
275
276void
277Target::RemoveAllBreakpoints (bool internal_also)
278{
Greg Claytone005f2c2010-11-06 01:53:30 +0000279 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000280 if (log)
281 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
282
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000283 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000284 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000285 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000286
287 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000288}
289
290void
291Target::DisableAllBreakpoints (bool internal_also)
292{
Greg Claytone005f2c2010-11-06 01:53:30 +0000293 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000294 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{
Greg Claytone005f2c2010-11-06 01:53:30 +0000305 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000306 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{
Greg Claytone005f2c2010-11-06 01:53:30 +0000317 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000318 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 Claytonc7f5d5c2010-07-23 23:33:17 +0000324 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000325 else
Jim Inghamd1686902010-10-14 23:45:03 +0000326 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000327 if (m_last_created_breakpoint)
328 {
329 if (m_last_created_breakpoint->GetID() == break_id)
330 m_last_created_breakpoint.reset();
331 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000332 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000333 }
Chris Lattner24943d22010-06-08 16:52:24 +0000334 return true;
335 }
336 return false;
337}
338
339bool
340Target::DisableBreakpointByID (break_id_t break_id)
341{
Greg Claytone005f2c2010-11-06 01:53:30 +0000342 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000343 if (log)
344 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
345
346 BreakpointSP bp_sp;
347
348 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
349 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
350 else
351 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
352 if (bp_sp)
353 {
354 bp_sp->SetEnabled (false);
355 return true;
356 }
357 return false;
358}
359
360bool
361Target::EnableBreakpointByID (break_id_t break_id)
362{
Greg Claytone005f2c2010-11-06 01:53:30 +0000363 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000364 if (log)
365 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
366 __FUNCTION__,
367 break_id,
368 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
369
370 BreakpointSP bp_sp;
371
372 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
373 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
374 else
375 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
376
377 if (bp_sp)
378 {
379 bp_sp->SetEnabled (true);
380 return true;
381 }
382 return false;
383}
384
385ModuleSP
386Target::GetExecutableModule ()
387{
388 ModuleSP executable_sp;
389 if (m_images.GetSize() > 0)
390 executable_sp = m_images.GetModuleAtIndex(0);
391 return executable_sp;
392}
393
394void
395Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
396{
397 m_images.Clear();
398 m_scratch_ast_context_ap.reset();
399
400 if (executable_sp.get())
401 {
402 Timer scoped_timer (__PRETTY_FUNCTION__,
403 "Target::SetExecutableModule (executable = '%s/%s')",
404 executable_sp->GetFileSpec().GetDirectory().AsCString(),
405 executable_sp->GetFileSpec().GetFilename().AsCString());
406
407 m_images.Append(executable_sp); // The first image is our exectuable file
408
409 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000410 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
411 if (!m_arch_spec.IsValid())
412 m_arch_spec = exe_arch;
413
Chris Lattner24943d22010-06-08 16:52:24 +0000414 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000415 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
416 assert (executable_objfile);
417 // TODO: remote assertion above after verifying that it doesn't fire off
418 // after the platform changes. The platform is what should be selecting
419 // the right slice of an executable file, and it also should be the one
420 // to resolve any executables in their bundles.
421// if (executable_objfile == NULL)
422// {
423//
424// FileSpec bundle_executable(executable_sp->GetFileSpec());
425// if (Host::ResolveExecutableInBundle (bundle_executable))
426// {
427// ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
428// exe_arch));
429// SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
430// if (bundle_exe_module_sp->GetObjectFile() != NULL)
431// executable_sp = bundle_exe_module_sp;
432// return;
433// }
434// }
Chris Lattner24943d22010-06-08 16:52:24 +0000435
436 if (executable_objfile)
437 {
438 executable_objfile->GetDependentModules(dependent_files);
439 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
440 {
441 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
442 exe_arch));
443 if (image_module_sp.get())
444 {
445 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
446 ObjectFile *objfile = image_module_sp->GetObjectFile();
447 if (objfile)
448 objfile->GetDependentModules(dependent_files);
449 }
450 }
451 }
452
453 // Now see if we know the target triple, and if so, create our scratch AST context:
Greg Clayton395fc332011-02-15 21:59:32 +0000454 if (m_arch_spec.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000455 {
Greg Clayton395fc332011-02-15 21:59:32 +0000456 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch_spec.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000457 }
458 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000459
460 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000461}
462
463
Jim Ingham7508e732010-08-09 23:31:02 +0000464bool
465Target::SetArchitecture (const ArchSpec &arch_spec)
466{
467 if (m_arch_spec == arch_spec)
468 {
469 // If we're setting the architecture to our current architecture, we
470 // don't need to do anything.
471 return true;
472 }
473 else if (!m_arch_spec.IsValid())
474 {
475 // If we haven't got a valid arch spec, then we just need to set it.
476 m_arch_spec = arch_spec;
477 return true;
478 }
479 else
480 {
481 // If we have an executable file, try to reset the executable to the desired architecture
482 m_arch_spec = arch_spec;
483 ModuleSP executable_sp = GetExecutableModule ();
484 m_images.Clear();
485 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000486 // Need to do something about unsetting breakpoints.
487
488 if (executable_sp)
489 {
490 FileSpec exec_file_spec = executable_sp->GetFileSpec();
491 Error error = ModuleList::GetSharedModule(exec_file_spec,
492 arch_spec,
493 NULL,
494 NULL,
495 0,
496 executable_sp,
497 NULL,
498 NULL);
499
500 if (!error.Fail() && executable_sp)
501 {
502 SetExecutableModule (executable_sp, true);
503 return true;
504 }
505 else
506 {
507 return false;
508 }
509 }
510 else
511 {
512 return false;
513 }
514 }
515}
Chris Lattner24943d22010-06-08 16:52:24 +0000516
Chris Lattner24943d22010-06-08 16:52:24 +0000517void
518Target::ModuleAdded (ModuleSP &module_sp)
519{
520 // A module is being added to this target for the first time
521 ModuleList module_list;
522 module_list.Append(module_sp);
523 ModulesDidLoad (module_list);
524}
525
526void
527Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
528{
529 // A module is being added to this target for the first time
530 ModuleList module_list;
531 module_list.Append (old_module_sp);
532 ModulesDidUnload (module_list);
533 module_list.Clear ();
534 module_list.Append (new_module_sp);
535 ModulesDidLoad (module_list);
536}
537
538void
539Target::ModulesDidLoad (ModuleList &module_list)
540{
541 m_breakpoint_list.UpdateBreakpoints (module_list, true);
542 // TODO: make event data that packages up the module_list
543 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
544}
545
546void
547Target::ModulesDidUnload (ModuleList &module_list)
548{
549 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000550
551 // Remove the images from the target image list
552 m_images.Remove(module_list);
553
Chris Lattner24943d22010-06-08 16:52:24 +0000554 // TODO: make event data that packages up the module_list
555 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
556}
557
558size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000559Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
560{
561 const Section *section = addr.GetSection();
562 if (section && section->GetModule())
563 {
564 ObjectFile *objfile = section->GetModule()->GetObjectFile();
565 if (objfile)
566 {
567 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
568 addr.GetOffset(),
569 dst,
570 dst_len);
571 if (bytes_read > 0)
572 return bytes_read;
573 else
574 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
575 }
576 else
577 {
578 error.SetErrorString("address isn't from a object file");
579 }
580 }
581 else
582 {
583 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
584 }
585 return 0;
586}
587
588size_t
589Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000590{
Chris Lattner24943d22010-06-08 16:52:24 +0000591 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000592
Greg Clayton70436352010-06-30 23:03:03 +0000593 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
594
Greg Clayton26100dc2011-01-07 01:57:07 +0000595 size_t bytes_read = 0;
Greg Clayton70436352010-06-30 23:03:03 +0000596 Address resolved_addr(addr);
597 if (!resolved_addr.IsSectionOffset())
598 {
599 if (process_is_valid)
Chris Lattner24943d22010-06-08 16:52:24 +0000600 {
Greg Claytoneea26402010-09-14 23:36:40 +0000601 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000602 }
603 else
604 {
605 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
606 }
607 }
608
Greg Clayton26100dc2011-01-07 01:57:07 +0000609 if (prefer_file_cache)
610 {
611 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
612 if (bytes_read > 0)
613 return bytes_read;
614 }
Greg Clayton70436352010-06-30 23:03:03 +0000615
616 if (process_is_valid)
617 {
Greg Claytoneea26402010-09-14 23:36:40 +0000618 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000619 if (load_addr == LLDB_INVALID_ADDRESS)
620 {
621 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
622 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
623 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
624 resolved_addr.GetFileAddress());
625 else
626 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
627 }
628 else
629 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000630 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000631 if (bytes_read != dst_len)
632 {
633 if (error.Success())
634 {
635 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000636 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000637 else
Greg Clayton70436352010-06-30 23:03:03 +0000638 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 +0000639 }
640 }
Greg Clayton70436352010-06-30 23:03:03 +0000641 if (bytes_read)
642 return bytes_read;
643 // If the address is not section offset we have an address that
644 // doesn't resolve to any address in any currently loaded shared
645 // libaries and we failed to read memory so there isn't anything
646 // more we can do. If it is section offset, we might be able to
647 // read cached memory from the object file.
648 if (!resolved_addr.IsSectionOffset())
649 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000650 }
Chris Lattner24943d22010-06-08 16:52:24 +0000651 }
Greg Clayton70436352010-06-30 23:03:03 +0000652
Greg Clayton26100dc2011-01-07 01:57:07 +0000653 if (!prefer_file_cache)
Greg Clayton70436352010-06-30 23:03:03 +0000654 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000655 // If we didn't already try and read from the object file cache, then
656 // try it after failing to read from the process.
657 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000658 }
659 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000660}
661
662
663ModuleSP
664Target::GetSharedModule
665(
666 const FileSpec& file_spec,
667 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000668 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000669 const ConstString *object_name,
670 off_t object_offset,
671 Error *error_ptr
672)
673{
674 // Don't pass in the UUID so we can tell if we have a stale value in our list
675 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
676 bool did_create_module = false;
677 ModuleSP module_sp;
678
679 // If there are image search path entries, try to use them first to acquire a suitable image.
680
681 Error error;
682
683 if (m_image_search_paths.GetSize())
684 {
685 FileSpec transformed_spec;
686 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
687 {
688 transformed_spec.GetFilename() = file_spec.GetFilename();
689 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
690 }
691 }
692
693 // If a module hasn't been found yet, use the unmodified path.
694
695 if (!module_sp)
696 {
697 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
698 }
699
700 if (module_sp)
701 {
702 m_images.Append (module_sp);
703 if (did_create_module)
704 {
705 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
706 ModuleUpdated(old_module_sp, module_sp);
707 else
708 ModuleAdded(module_sp);
709 }
710 }
711 if (error_ptr)
712 *error_ptr = error;
713 return module_sp;
714}
715
716
717Target *
718Target::CalculateTarget ()
719{
720 return this;
721}
722
723Process *
724Target::CalculateProcess ()
725{
726 return NULL;
727}
728
729Thread *
730Target::CalculateThread ()
731{
732 return NULL;
733}
734
735StackFrame *
736Target::CalculateStackFrame ()
737{
738 return NULL;
739}
740
741void
Greg Claytona830adb2010-10-04 01:05:56 +0000742Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000743{
744 exe_ctx.target = this;
745 exe_ctx.process = NULL; // Do NOT fill in process...
746 exe_ctx.thread = NULL;
747 exe_ctx.frame = NULL;
748}
749
750PathMappingList &
751Target::GetImageSearchPathList ()
752{
753 return m_image_search_paths;
754}
755
756void
757Target::ImageSearchPathsChanged
758(
759 const PathMappingList &path_list,
760 void *baton
761)
762{
763 Target *target = (Target *)baton;
764 if (target->m_images.GetSize() > 1)
765 {
766 ModuleSP exe_module_sp (target->GetExecutableModule());
767 if (exe_module_sp)
768 {
769 target->m_images.Clear();
770 target->SetExecutableModule (exe_module_sp, true);
771 }
772 }
773}
774
775ClangASTContext *
776Target::GetScratchClangASTContext()
777{
778 return m_scratch_ast_context_ap.get();
779}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000780
Greg Clayton990de7b2010-11-18 23:32:35 +0000781void
Caroline Tice2a456812011-03-10 22:14:10 +0000782Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000783{
Greg Clayton990de7b2010-11-18 23:32:35 +0000784 UserSettingsControllerSP &usc = GetSettingsController();
785 usc.reset (new SettingsController);
786 UserSettingsController::InitializeSettingsController (usc,
787 SettingsController::global_settings_table,
788 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000789
790 // Now call SettingsInitialize() on each 'child' setting of Target
791 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000792}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000793
Greg Clayton990de7b2010-11-18 23:32:35 +0000794void
Caroline Tice2a456812011-03-10 22:14:10 +0000795Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000796{
Caroline Tice2a456812011-03-10 22:14:10 +0000797
798 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
799
800 Process::SettingsTerminate ();
801
802 // Now terminate Target Settings.
803
Greg Clayton990de7b2010-11-18 23:32:35 +0000804 UserSettingsControllerSP &usc = GetSettingsController();
805 UserSettingsController::FinalizeSettingsController (usc);
806 usc.reset();
807}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000808
Greg Clayton990de7b2010-11-18 23:32:35 +0000809UserSettingsControllerSP &
810Target::GetSettingsController ()
811{
812 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000813 return g_settings_controller;
814}
815
816ArchSpec
817Target::GetDefaultArchitecture ()
818{
Greg Clayton940b1032011-02-23 00:35:02 +0000819 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
820
821 if (settings_controller_sp)
822 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
823 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000824}
825
826void
Greg Clayton940b1032011-02-23 00:35:02 +0000827Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000828{
Greg Clayton940b1032011-02-23 00:35:02 +0000829 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
830
831 if (settings_controller_sp)
832 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000833}
834
Greg Claytona830adb2010-10-04 01:05:56 +0000835Target *
836Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
837{
838 // The target can either exist in the "process" of ExecutionContext, or in
839 // the "target_sp" member of SymbolContext. This accessor helper function
840 // will get the target from one of these locations.
841
842 Target *target = NULL;
843 if (sc_ptr != NULL)
844 target = sc_ptr->target_sp.get();
845 if (target == NULL)
846 {
847 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
848 target = &exe_ctx_ptr->process->GetTarget();
849 }
850 return target;
851}
852
853
Caroline Tice1ebef442010-09-27 00:30:10 +0000854void
855Target::UpdateInstanceName ()
856{
857 StreamString sstr;
858
859 ModuleSP module_sp = GetExecutableModule();
860 if (module_sp)
861 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000862 sstr.Printf ("%s_%s",
863 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton940b1032011-02-23 00:35:02 +0000864 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000865 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
866 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000867 }
868}
869
Sean Callanan77e93942010-10-29 00:29:03 +0000870const char *
871Target::GetExpressionPrefixContentsAsCString ()
872{
873 return m_expr_prefix_contents.c_str();
874}
875
Greg Clayton427f2902010-12-14 02:59:59 +0000876ExecutionResults
877Target::EvaluateExpression
878(
879 const char *expr_cstr,
880 StackFrame *frame,
881 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +0000882 bool keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +0000883 lldb::ValueObjectSP &result_valobj_sp
884)
885{
886 ExecutionResults execution_results = eExecutionSetupError;
887
888 result_valobj_sp.reset();
889
890 ExecutionContext exe_ctx;
891 if (frame)
892 {
893 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000894 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +0000895 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
896 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
897 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton427f2902010-12-14 02:59:59 +0000898 }
899 else if (m_process_sp)
900 {
901 m_process_sp->CalculateExecutionContext(exe_ctx);
902 }
903 else
904 {
905 CalculateExecutionContext(exe_ctx);
906 }
907
908 if (result_valobj_sp)
909 {
910 execution_results = eExecutionCompleted;
911 // We got a result from the frame variable expression path above...
912 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
913
914 lldb::ValueObjectSP const_valobj_sp;
915
916 // Check in case our value is already a constant value
917 if (result_valobj_sp->GetIsConstant())
918 {
919 const_valobj_sp = result_valobj_sp;
920 const_valobj_sp->SetName (persistent_variable_name);
921 }
922 else
923 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
924 persistent_variable_name);
925
Sean Callanan6a925532011-01-13 08:53:35 +0000926 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
927
Greg Clayton427f2902010-12-14 02:59:59 +0000928 result_valobj_sp = const_valobj_sp;
929
Sean Callanan6a925532011-01-13 08:53:35 +0000930 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
931 assert (clang_expr_variable_sp.get());
932
933 // Set flags and live data as appropriate
934
935 const Value &result_value = live_valobj_sp->GetValue();
936
937 switch (result_value.GetValueType())
938 {
939 case Value::eValueTypeHostAddress:
940 case Value::eValueTypeFileAddress:
941 // we don't do anything with these for now
942 break;
943 case Value::eValueTypeScalar:
944 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
945 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
946 break;
947 case Value::eValueTypeLoadAddress:
948 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
949 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
950 break;
951 }
Greg Clayton427f2902010-12-14 02:59:59 +0000952 }
953 else
954 {
955 // Make sure we aren't just trying to see the value of a persistent
956 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +0000957 lldb::ClangExpressionVariableSP persistent_var_sp;
958 // Only check for persistent variables the expression starts with a '$'
959 if (expr_cstr[0] == '$')
960 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
961
Greg Clayton427f2902010-12-14 02:59:59 +0000962 if (persistent_var_sp)
963 {
964 result_valobj_sp = persistent_var_sp->GetValueObject ();
965 execution_results = eExecutionCompleted;
966 }
967 else
968 {
969 const char *prefix = GetExpressionPrefixContentsAsCString();
970
971 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000972 unwind_on_error,
973 keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +0000974 expr_cstr,
975 prefix,
976 result_valobj_sp);
977 }
978 }
979 return execution_results;
980}
981
Caroline Tice5bc8c972010-09-20 20:44:43 +0000982//--------------------------------------------------------------
983// class Target::SettingsController
984//--------------------------------------------------------------
985
986Target::SettingsController::SettingsController () :
987 UserSettingsController ("target", Debugger::GetSettingsController()),
988 m_default_architecture ()
989{
990 m_default_settings.reset (new TargetInstanceSettings (*this, false,
991 InstanceSettings::GetDefaultName().AsCString()));
992}
993
994Target::SettingsController::~SettingsController ()
995{
996}
997
998lldb::InstanceSettingsSP
999Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1000{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001001 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1002 false,
1003 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001004 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1005 return new_settings_sp;
1006}
1007
Caroline Tice5bc8c972010-09-20 20:44:43 +00001008
Greg Claytond284b662011-02-18 01:44:25 +00001009#define TSC_DEFAULT_ARCH "default-arch"
1010#define TSC_EXPR_PREFIX "expr-prefix"
1011#define TSC_EXEC_LEVEL "execution-level"
1012#define TSC_EXEC_MODE "execution-mode"
1013#define TSC_EXEC_OS_TYPE "execution-os-type"
1014
1015
1016static const ConstString &
1017GetSettingNameForDefaultArch ()
1018{
1019 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1020
1021 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001022}
1023
Greg Claytond284b662011-02-18 01:44:25 +00001024static const ConstString &
1025GetSettingNameForExpressionPrefix ()
1026{
1027 static ConstString g_const_string (TSC_EXPR_PREFIX);
1028 return g_const_string;
1029}
1030
1031static const ConstString &
1032GetSettingNameForExecutionLevel ()
1033{
1034 static ConstString g_const_string (TSC_EXEC_LEVEL);
1035 return g_const_string;
1036}
1037
1038static const ConstString &
1039GetSettingNameForExecutionMode ()
1040{
1041 static ConstString g_const_string (TSC_EXEC_MODE);
1042 return g_const_string;
1043}
1044
1045static const ConstString &
1046GetSettingNameForExecutionOSType ()
1047{
1048 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1049 return g_const_string;
1050}
1051
1052
Caroline Tice5bc8c972010-09-20 20:44:43 +00001053bool
1054Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1055 const char *index_value,
1056 const char *value,
1057 const SettingEntry &entry,
1058 const lldb::VarSetOperationType op,
1059 Error&err)
1060{
Greg Claytond284b662011-02-18 01:44:25 +00001061 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001062 {
Greg Clayton940b1032011-02-23 00:35:02 +00001063 m_default_architecture.SetTriple (value);
1064 if (!m_default_architecture.IsValid())
1065 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001066 }
1067 return true;
1068}
1069
1070
1071bool
1072Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1073 StringList &value,
1074 Error &err)
1075{
Greg Claytond284b662011-02-18 01:44:25 +00001076 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001077 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001078 // If the arch is invalid (the default), don't show a string for it
1079 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001080 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001081 return true;
1082 }
1083 else
1084 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1085
1086 return false;
1087}
1088
1089//--------------------------------------------------------------
1090// class TargetInstanceSettings
1091//--------------------------------------------------------------
1092
Greg Clayton638351a2010-12-04 00:10:17 +00001093TargetInstanceSettings::TargetInstanceSettings
1094(
1095 UserSettingsController &owner,
1096 bool live_instance,
1097 const char *name
1098) :
Greg Claytond284b662011-02-18 01:44:25 +00001099 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1100 m_expr_prefix_path (),
1101 m_expr_prefix_contents (),
1102 m_execution_level (eExecutionLevelAuto),
1103 m_execution_mode (eExecutionModeAuto),
1104 m_execution_os_type (eExecutionOSTypeAuto)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001105{
1106 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1107 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1108 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1109 // This is true for CreateInstanceName() too.
1110
1111 if (GetInstanceName () == InstanceSettings::InvalidName())
1112 {
1113 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1114 m_owner.RegisterInstanceSettings (this);
1115 }
1116
1117 if (live_instance)
1118 {
1119 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1120 CopyInstanceSettings (pending_settings,false);
1121 //m_owner.RemovePendingSettings (m_instance_name);
1122 }
1123}
1124
1125TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001126 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001127{
1128 if (m_instance_name != InstanceSettings::GetDefaultName())
1129 {
1130 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1131 CopyInstanceSettings (pending_settings,false);
1132 //m_owner.RemovePendingSettings (m_instance_name);
1133 }
1134}
1135
1136TargetInstanceSettings::~TargetInstanceSettings ()
1137{
1138}
1139
1140TargetInstanceSettings&
1141TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1142{
1143 if (this != &rhs)
1144 {
1145 }
1146
1147 return *this;
1148}
1149
Caroline Tice5bc8c972010-09-20 20:44:43 +00001150void
1151TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1152 const char *index_value,
1153 const char *value,
1154 const ConstString &instance_name,
1155 const SettingEntry &entry,
1156 lldb::VarSetOperationType op,
1157 Error &err,
1158 bool pending)
1159{
Greg Claytond284b662011-02-18 01:44:25 +00001160 int new_enum = -1;
1161
1162 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001163 {
1164 switch (op)
1165 {
1166 default:
1167 err.SetErrorToGenericError ();
1168 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1169 return;
1170 case lldb::eVarSetOperationAssign:
1171 {
1172 FileSpec file_spec(value, true);
1173
1174 if (!file_spec.Exists())
1175 {
1176 err.SetErrorToGenericError ();
1177 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1178 return;
1179 }
1180
Greg Clayton9f527042011-02-03 17:47:47 +00001181 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan77e93942010-10-29 00:29:03 +00001182
Greg Clayton9f527042011-02-03 17:47:47 +00001183 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan77e93942010-10-29 00:29:03 +00001184 {
1185 err.SetErrorToGenericError ();
Greg Clayton9f527042011-02-03 17:47:47 +00001186 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan77e93942010-10-29 00:29:03 +00001187 return;
1188 }
1189
1190 m_expr_prefix_path = value;
Greg Clayton9f527042011-02-03 17:47:47 +00001191 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan77e93942010-10-29 00:29:03 +00001192 }
1193 return;
1194 case lldb::eVarSetOperationAppend:
1195 err.SetErrorToGenericError ();
1196 err.SetErrorString ("Cannot append to a path.\n");
1197 return;
1198 case lldb::eVarSetOperationClear:
1199 m_expr_prefix_path.clear ();
1200 m_expr_prefix_contents.clear ();
1201 return;
1202 }
1203 }
Greg Claytond284b662011-02-18 01:44:25 +00001204 else if (var_name == GetSettingNameForExecutionLevel ())
1205 {
1206 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1207 if (err.Success())
1208 m_execution_level = (ExecutionLevel)new_enum;
1209 }
1210 else if (var_name == GetSettingNameForExecutionMode ())
1211 {
1212 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1213 if (err.Success())
1214 m_execution_mode = (ExecutionMode)new_enum;
1215 }
1216 else if (var_name == GetSettingNameForExecutionOSType ())
1217 {
1218 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1219 if (err.Success())
1220 m_execution_os_type = (ExecutionOSType)new_enum;
1221 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001222}
1223
1224void
Greg Claytond284b662011-02-18 01:44:25 +00001225TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001226{
Sean Callanan77e93942010-10-29 00:29:03 +00001227 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1228
1229 if (!new_settings_ptr)
1230 return;
1231
Greg Claytond284b662011-02-18 01:44:25 +00001232 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1233 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
1234 m_execution_level = new_settings_ptr->m_execution_level;
1235 m_execution_mode = new_settings_ptr->m_execution_mode;
1236 m_execution_os_type = new_settings_ptr->m_execution_os_type;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001237}
1238
Caroline Ticebcb5b452010-09-20 21:37:42 +00001239bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001240TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1241 const ConstString &var_name,
1242 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001243 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001244{
Greg Claytond284b662011-02-18 01:44:25 +00001245 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001246 {
1247 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1248 }
Greg Claytond284b662011-02-18 01:44:25 +00001249 else if (var_name == GetSettingNameForExecutionLevel ())
1250 {
1251 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_level));
1252 }
1253 else if (var_name == GetSettingNameForExecutionMode ())
1254 {
1255 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_mode));
1256 }
1257 else if (var_name == GetSettingNameForExecutionOSType ())
1258 {
1259 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_os_type));
1260 }
Sean Callanan77e93942010-10-29 00:29:03 +00001261 else
1262 {
1263 if (err)
1264 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1265 return false;
1266 }
1267
1268 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001269}
1270
1271const ConstString
1272TargetInstanceSettings::CreateInstanceName ()
1273{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001274 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001275 static int instance_count = 1;
1276
Caroline Tice5bc8c972010-09-20 20:44:43 +00001277 sstr.Printf ("target_%d", instance_count);
1278 ++instance_count;
1279
1280 const ConstString ret_val (sstr.GetData());
1281 return ret_val;
1282}
1283
1284//--------------------------------------------------
1285// Target::SettingsController Variable Tables
1286//--------------------------------------------------
1287
1288SettingEntry
1289Target::SettingsController::global_settings_table[] =
1290{
Greg Claytond284b662011-02-18 01:44:25 +00001291 // var-name var-type default enum init'd hidden help-text
1292 // ================= ================== =========== ==== ====== ====== =========================================================================
1293 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1294 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1295};
1296
1297static lldb::OptionEnumValueElement
1298g_execution_level_enums[] =
1299{
1300 { eExecutionLevelAuto , "auto" , "Automatically detect the execution level (user/kernel)." },
1301 { eExecutionLevelKernel , "kernel" , "Treat executables as kernel executables." },
1302 { eExecutionLevelUser , "user" , "Treat executables as user space executables." },
1303 { 0 , NULL , NULL }
1304};
1305
1306static lldb::OptionEnumValueElement
1307g_execution_mode_enums[] =
1308{
1309 { eExecutionModeAuto , "auto" , "Automatically detect the execution mode (static/dynamic)." },
1310 { eExecutionModeStatic , "static" , "All executables are static and addresses at the virtual addresses in the object files." },
1311 { eExecutionModeDynamic , "dynamic" , "Executables and shared libraries are dynamically loaded.." },
1312 { 0 , NULL , NULL }
1313};
1314
1315static lldb::OptionEnumValueElement
1316g_execution_os_enums[] =
1317{
1318 { eExecutionOSTypeAuto , "auto" , "Automatically detect the execution OS (none/halted/live)." },
1319 { eExecutionOSTypeNone , "none" , "There is no operating system available (no processes or threads)." },
1320 { eExecutionOSTypeHalted, "halted" , "There is an operating system, but it is halted when the debugger is halted. "
1321 "Processes and threads must be discovered by accessing symbols and reading "
1322 "memory." },
1323 { eExecutionOSTypeLive , "live" , "There is a live operating system with debug services that can be used to "
1324 "get processes, threads and theirs states." },
1325 { 0, NULL, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001326};
1327
1328SettingEntry
1329Target::SettingsController::instance_settings_table[] =
1330{
Greg Claytond284b662011-02-18 01:44:25 +00001331 // var-name var-type default enum-table init'd hidden help-text
1332 // ================= ================== =========== ========================= ====== ====== =========================================================================
1333 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL , false, false, "Path to a file containing expressions to be prepended to all expressions." },
1334 { TSC_EXEC_LEVEL , eSetVarTypeEnum , "auto" , g_execution_level_enums , false, false, "Sets the execution level for a target." },
1335 { TSC_EXEC_MODE , eSetVarTypeEnum , "auto" , g_execution_mode_enums , false, false, "Sets the execution mode for a target." },
1336 { TSC_EXEC_OS_TYPE , eSetVarTypeEnum , "auto" , g_execution_os_enums , false, false, "Sets the execution OS for a target." },
1337 { NULL , eSetVarTypeNone , NULL , NULL , false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001338};