blob: e1e73c24cab26a119eb3b35045bb39f35be796f6 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5920#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2421#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:2423#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5924#include "lldb/Core/Timer.h"
25#include "lldb/Core/ValueObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2426#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 Clayton8b2fe6d2010-12-14 02:59:5930#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:2431
32using namespace lldb;
33using namespace lldb_private;
34
35//----------------------------------------------------------------------
36// Target constructor
37//----------------------------------------------------------------------
Greg Clayton66111032010-06-23 01:19:2938Target::Target(Debugger &debugger) :
Greg Claytoncfd1ace2010-10-31 03:01:0639 Broadcaster("lldb.target"),
Greg Claytondbe54502010-11-19 03:46:0140 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2941 m_debugger (debugger),
Greg Claytonaf67cec2010-12-20 20:49:2342 m_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:2443 m_images(),
Greg Claytonf5e56de2010-09-14 23:36:4044 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2445 m_breakpoint_list (false),
46 m_internal_breakpoint_list (true),
47 m_process_sp(),
Chris Lattner30fdc8d2010-06-08 16:52:2448 m_search_filter_sp(),
49 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5950 m_scratch_ast_context_ap (NULL),
51 m_persistent_variables ()
Chris Lattner30fdc8d2010-06-08 16:52:2452{
Greg Claytoncfd1ace2010-10-31 03:01:0653 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
54 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
55 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
56
Greg Clayton2d4edfb2010-11-06 01:53:3057 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2458 if (log)
59 log->Printf ("%p Target::Target()", this);
60}
61
62//----------------------------------------------------------------------
63// Destructor
64//----------------------------------------------------------------------
65Target::~Target()
66{
Greg Clayton2d4edfb2010-11-06 01:53:3067 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2468 if (log)
69 log->Printf ("%p Target::~Target()", this);
70 DeleteCurrentProcess ();
71}
72
73void
Caroline Ticeceb6b132010-10-26 03:11:1374Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2475{
Greg Clayton89411422010-10-08 00:21:0576// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1377 if (description_level != lldb::eDescriptionLevelBrief)
78 {
79 s->Indent();
80 s->PutCString("Target\n");
81 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3582 m_images.Dump(s);
83 m_breakpoint_list.Dump(s);
84 m_internal_breakpoint_list.Dump(s);
85 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:1386 }
87 else
88 {
Greg Clayton48381312010-10-30 04:51:4689 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Ticeceb6b132010-10-26 03:11:1390 }
Chris Lattner30fdc8d2010-06-08 16:52:2491}
92
93void
94Target::DeleteCurrentProcess ()
95{
96 if (m_process_sp.get())
97 {
Greg Clayton17f69202010-09-14 23:52:4398 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:2499 if (m_process_sp->IsAlive())
100 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55101
102 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24103
104 // Do any cleanup of the target we need to do between process instances.
105 // NB It is better to do this before destroying the process in case the
106 // clean up needs some help from the process.
107 m_breakpoint_list.ClearAllBreakpointSites();
108 m_internal_breakpoint_list.ClearAllBreakpointSites();
109 m_process_sp.reset();
110 }
111}
112
113const lldb::ProcessSP &
114Target::CreateProcess (Listener &listener, const char *plugin_name)
115{
116 DeleteCurrentProcess ();
117 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
118 return m_process_sp;
119}
120
121const lldb::ProcessSP &
122Target::GetProcessSP () const
123{
124 return m_process_sp;
125}
126
127lldb::TargetSP
128Target::GetSP()
129{
Greg Clayton66111032010-06-23 01:19:29130 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24131}
132
133BreakpointList &
134Target::GetBreakpointList(bool internal)
135{
136 if (internal)
137 return m_internal_breakpoint_list;
138 else
139 return m_breakpoint_list;
140}
141
142const BreakpointList &
143Target::GetBreakpointList(bool internal) const
144{
145 if (internal)
146 return m_internal_breakpoint_list;
147 else
148 return m_breakpoint_list;
149}
150
151BreakpointSP
152Target::GetBreakpointByID (break_id_t break_id)
153{
154 BreakpointSP bp_sp;
155
156 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
157 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
158 else
159 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
160
161 return bp_sp;
162}
163
164BreakpointSP
165Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
166{
167 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
168 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
169 return CreateBreakpoint (filter_sp, resolver_sp, internal);
170}
171
172
173BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41174Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24175{
Chris Lattner30fdc8d2010-06-08 16:52:24176 Address so_addr;
177 // Attempt to resolve our load address if possible, though it is ok if
178 // it doesn't resolve to section/offset.
179
Greg Clayton1b72fcb2010-08-24 00:45:41180 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40181 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41182 if (!so_addr.IsValid())
183 {
184 // The address didn't resolve, so just set this as an absolute address
185 so_addr.SetOffset (addr);
186 }
187 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24188 return bp_sp;
189}
190
191BreakpointSP
192Target::CreateBreakpoint (Address &addr, bool internal)
193{
194 TargetSP target_sp = this->GetSP();
195 SearchFilterSP filter_sp(new SearchFilter (target_sp));
196 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
197 return CreateBreakpoint (filter_sp, resolver_sp, internal);
198}
199
200BreakpointSP
Greg Clayton0c5cd902010-06-28 21:30:43201Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24202{
Greg Clayton0c5cd902010-06-28 21:30:43203 BreakpointSP bp_sp;
204 if (func_name)
205 {
206 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
207 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
208 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
209 }
210 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24211}
212
213
214SearchFilterSP
215Target::GetSearchFilterForModule (const FileSpec *containingModule)
216{
217 SearchFilterSP filter_sp;
218 lldb::TargetSP target_sp = this->GetSP();
219 if (containingModule != NULL)
220 {
221 // TODO: We should look into sharing module based search filters
222 // across many breakpoints like we do for the simple target based one
223 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
224 }
225 else
226 {
227 if (m_search_filter_sp.get() == NULL)
228 m_search_filter_sp.reset (new SearchFilter (target_sp));
229 filter_sp = m_search_filter_sp;
230 }
231 return filter_sp;
232}
233
234BreakpointSP
235Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
236{
237 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
238 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
239
240 return CreateBreakpoint (filter_sp, resolver_sp, internal);
241}
242
243BreakpointSP
244Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
245{
246 BreakpointSP bp_sp;
247 if (filter_sp && resolver_sp)
248 {
249 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
250 resolver_sp->SetBreakpoint (bp_sp.get());
251
252 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17253 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24254 else
Greg Clayton9fed0d82010-07-23 23:33:17255 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24256
Greg Clayton2d4edfb2010-11-06 01:53:30257 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24258 if (log)
259 {
260 StreamString s;
261 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
262 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
263 }
264
Chris Lattner30fdc8d2010-06-08 16:52:24265 bp_sp->ResolveBreakpoint();
266 }
Jim Ingham36f3b362010-10-14 23:45:03267
268 if (!internal && bp_sp)
269 {
270 m_last_created_breakpoint = bp_sp;
271 }
272
Chris Lattner30fdc8d2010-06-08 16:52:24273 return bp_sp;
274}
275
276void
277Target::RemoveAllBreakpoints (bool internal_also)
278{
Greg Clayton2d4edfb2010-11-06 01:53:30279 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24280 if (log)
281 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
282
Greg Clayton9fed0d82010-07-23 23:33:17283 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24284 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17285 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03286
287 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24288}
289
290void
291Target::DisableAllBreakpoints (bool internal_also)
292{
Greg Clayton2d4edfb2010-11-06 01:53:30293 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24294 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 Clayton2d4edfb2010-11-06 01:53:30305 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24306 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 Clayton2d4edfb2010-11-06 01:53:30317 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24318 if (log)
319 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
320
321 if (DisableBreakpointByID (break_id))
322 {
323 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17324 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24325 else
Jim Ingham36f3b362010-10-14 23:45:03326 {
Greg Claytonaa1c5872011-01-24 23:35:47327 if (m_last_created_breakpoint)
328 {
329 if (m_last_created_breakpoint->GetID() == break_id)
330 m_last_created_breakpoint.reset();
331 }
Greg Clayton9fed0d82010-07-23 23:33:17332 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03333 }
Chris Lattner30fdc8d2010-06-08 16:52:24334 return true;
335 }
336 return false;
337}
338
339bool
340Target::DisableBreakpointByID (break_id_t break_id)
341{
Greg Clayton2d4edfb2010-11-06 01:53:30342 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24343 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 Clayton2d4edfb2010-11-06 01:53:30363 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24364 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 Ingham5aee1622010-08-09 23:31:02410 // 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 Lattner30fdc8d2010-06-08 16:52:24414 FileSpecList dependent_files;
415 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
416 if (executable_objfile == NULL)
417 {
418
419 FileSpec bundle_executable(executable_sp->GetFileSpec());
Greg Claytondd36def2010-10-17 22:03:32420 if (Host::ResolveExecutableInBundle (bundle_executable))
Chris Lattner30fdc8d2010-06-08 16:52:24421 {
422 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
423 exe_arch));
424 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
425 if (bundle_exe_module_sp->GetObjectFile() != NULL)
426 executable_sp = bundle_exe_module_sp;
427 return;
428 }
429 }
430
431 if (executable_objfile)
432 {
433 executable_objfile->GetDependentModules(dependent_files);
434 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
435 {
436 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
437 exe_arch));
438 if (image_module_sp.get())
439 {
440 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
441 ObjectFile *objfile = image_module_sp->GetObjectFile();
442 if (objfile)
443 objfile->GetDependentModules(dependent_files);
444 }
445 }
446 }
447
448 // Now see if we know the target triple, and if so, create our scratch AST context:
Greg Clayton514487e2011-02-15 21:59:32449 if (m_arch_spec.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24450 {
Greg Clayton514487e2011-02-15 21:59:32451 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch_spec.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:24452 }
453 }
Caroline Tice1559a462010-09-27 00:30:10454
455 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24456}
457
458
Jim Ingham5aee1622010-08-09 23:31:02459bool
460Target::SetArchitecture (const ArchSpec &arch_spec)
461{
462 if (m_arch_spec == arch_spec)
463 {
464 // If we're setting the architecture to our current architecture, we
465 // don't need to do anything.
466 return true;
467 }
468 else if (!m_arch_spec.IsValid())
469 {
470 // If we haven't got a valid arch spec, then we just need to set it.
471 m_arch_spec = arch_spec;
472 return true;
473 }
474 else
475 {
476 // If we have an executable file, try to reset the executable to the desired architecture
477 m_arch_spec = arch_spec;
478 ModuleSP executable_sp = GetExecutableModule ();
479 m_images.Clear();
480 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02481 // Need to do something about unsetting breakpoints.
482
483 if (executable_sp)
484 {
485 FileSpec exec_file_spec = executable_sp->GetFileSpec();
486 Error error = ModuleList::GetSharedModule(exec_file_spec,
487 arch_spec,
488 NULL,
489 NULL,
490 0,
491 executable_sp,
492 NULL,
493 NULL);
494
495 if (!error.Fail() && executable_sp)
496 {
497 SetExecutableModule (executable_sp, true);
498 return true;
499 }
500 else
501 {
502 return false;
503 }
504 }
505 else
506 {
507 return false;
508 }
509 }
510}
Chris Lattner30fdc8d2010-06-08 16:52:24511
Chris Lattner30fdc8d2010-06-08 16:52:24512void
513Target::ModuleAdded (ModuleSP &module_sp)
514{
515 // A module is being added to this target for the first time
516 ModuleList module_list;
517 module_list.Append(module_sp);
518 ModulesDidLoad (module_list);
519}
520
521void
522Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
523{
524 // A module is being added to this target for the first time
525 ModuleList module_list;
526 module_list.Append (old_module_sp);
527 ModulesDidUnload (module_list);
528 module_list.Clear ();
529 module_list.Append (new_module_sp);
530 ModulesDidLoad (module_list);
531}
532
533void
534Target::ModulesDidLoad (ModuleList &module_list)
535{
536 m_breakpoint_list.UpdateBreakpoints (module_list, true);
537 // TODO: make event data that packages up the module_list
538 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
539}
540
541void
542Target::ModulesDidUnload (ModuleList &module_list)
543{
544 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26545
546 // Remove the images from the target image list
547 m_images.Remove(module_list);
548
Chris Lattner30fdc8d2010-06-08 16:52:24549 // TODO: make event data that packages up the module_list
550 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
551}
552
553size_t
Greg Claytondb598232011-01-07 01:57:07554Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
555{
556 const Section *section = addr.GetSection();
557 if (section && section->GetModule())
558 {
559 ObjectFile *objfile = section->GetModule()->GetObjectFile();
560 if (objfile)
561 {
562 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
563 addr.GetOffset(),
564 dst,
565 dst_len);
566 if (bytes_read > 0)
567 return bytes_read;
568 else
569 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
570 }
571 else
572 {
573 error.SetErrorString("address isn't from a object file");
574 }
575 }
576 else
577 {
578 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
579 }
580 return 0;
581}
582
583size_t
584Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24585{
Chris Lattner30fdc8d2010-06-08 16:52:24586 error.Clear();
Greg Claytondb598232011-01-07 01:57:07587
Greg Claytondda4f7b2010-06-30 23:03:03588 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
589
Greg Claytondb598232011-01-07 01:57:07590 size_t bytes_read = 0;
Greg Claytondda4f7b2010-06-30 23:03:03591 Address resolved_addr(addr);
592 if (!resolved_addr.IsSectionOffset())
593 {
594 if (process_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24595 {
Greg Claytonf5e56de2010-09-14 23:36:40596 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03597 }
598 else
599 {
600 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
601 }
602 }
603
Greg Claytondb598232011-01-07 01:57:07604 if (prefer_file_cache)
605 {
606 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
607 if (bytes_read > 0)
608 return bytes_read;
609 }
Greg Claytondda4f7b2010-06-30 23:03:03610
611 if (process_is_valid)
612 {
Greg Claytonf5e56de2010-09-14 23:36:40613 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03614 if (load_addr == LLDB_INVALID_ADDRESS)
615 {
616 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
617 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
618 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
619 resolved_addr.GetFileAddress());
620 else
621 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
622 }
623 else
624 {
Greg Claytondb598232011-01-07 01:57:07625 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24626 if (bytes_read != dst_len)
627 {
628 if (error.Success())
629 {
630 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03631 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24632 else
Greg Claytondda4f7b2010-06-30 23:03:03633 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24634 }
635 }
Greg Claytondda4f7b2010-06-30 23:03:03636 if (bytes_read)
637 return bytes_read;
638 // If the address is not section offset we have an address that
639 // doesn't resolve to any address in any currently loaded shared
640 // libaries and we failed to read memory so there isn't anything
641 // more we can do. If it is section offset, we might be able to
642 // read cached memory from the object file.
643 if (!resolved_addr.IsSectionOffset())
644 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24645 }
Chris Lattner30fdc8d2010-06-08 16:52:24646 }
Greg Claytondda4f7b2010-06-30 23:03:03647
Greg Claytondb598232011-01-07 01:57:07648 if (!prefer_file_cache)
Greg Claytondda4f7b2010-06-30 23:03:03649 {
Greg Claytondb598232011-01-07 01:57:07650 // If we didn't already try and read from the object file cache, then
651 // try it after failing to read from the process.
652 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03653 }
654 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24655}
656
657
658ModuleSP
659Target::GetSharedModule
660(
661 const FileSpec& file_spec,
662 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10663 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24664 const ConstString *object_name,
665 off_t object_offset,
666 Error *error_ptr
667)
668{
669 // Don't pass in the UUID so we can tell if we have a stale value in our list
670 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
671 bool did_create_module = false;
672 ModuleSP module_sp;
673
674 // If there are image search path entries, try to use them first to acquire a suitable image.
675
676 Error error;
677
678 if (m_image_search_paths.GetSize())
679 {
680 FileSpec transformed_spec;
681 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
682 {
683 transformed_spec.GetFilename() = file_spec.GetFilename();
684 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
685 }
686 }
687
688 // If a module hasn't been found yet, use the unmodified path.
689
690 if (!module_sp)
691 {
692 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
693 }
694
695 if (module_sp)
696 {
697 m_images.Append (module_sp);
698 if (did_create_module)
699 {
700 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
701 ModuleUpdated(old_module_sp, module_sp);
702 else
703 ModuleAdded(module_sp);
704 }
705 }
706 if (error_ptr)
707 *error_ptr = error;
708 return module_sp;
709}
710
711
712Target *
713Target::CalculateTarget ()
714{
715 return this;
716}
717
718Process *
719Target::CalculateProcess ()
720{
721 return NULL;
722}
723
724Thread *
725Target::CalculateThread ()
726{
727 return NULL;
728}
729
730StackFrame *
731Target::CalculateStackFrame ()
732{
733 return NULL;
734}
735
736void
Greg Clayton0603aa92010-10-04 01:05:56737Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24738{
739 exe_ctx.target = this;
740 exe_ctx.process = NULL; // Do NOT fill in process...
741 exe_ctx.thread = NULL;
742 exe_ctx.frame = NULL;
743}
744
745PathMappingList &
746Target::GetImageSearchPathList ()
747{
748 return m_image_search_paths;
749}
750
751void
752Target::ImageSearchPathsChanged
753(
754 const PathMappingList &path_list,
755 void *baton
756)
757{
758 Target *target = (Target *)baton;
759 if (target->m_images.GetSize() > 1)
760 {
761 ModuleSP exe_module_sp (target->GetExecutableModule());
762 if (exe_module_sp)
763 {
764 target->m_images.Clear();
765 target->SetExecutableModule (exe_module_sp, true);
766 }
767 }
768}
769
770ClangASTContext *
771Target::GetScratchClangASTContext()
772{
773 return m_scratch_ast_context_ap.get();
774}
Caroline Ticedaccaa92010-09-20 20:44:43775
Greg Clayton99d0faf2010-11-18 23:32:35776void
777Target::Initialize ()
Caroline Ticedaccaa92010-09-20 20:44:43778{
Greg Clayton99d0faf2010-11-18 23:32:35779 UserSettingsControllerSP &usc = GetSettingsController();
780 usc.reset (new SettingsController);
781 UserSettingsController::InitializeSettingsController (usc,
782 SettingsController::global_settings_table,
783 SettingsController::instance_settings_table);
784}
Caroline Ticedaccaa92010-09-20 20:44:43785
Greg Clayton99d0faf2010-11-18 23:32:35786void
787Target::Terminate ()
788{
789 UserSettingsControllerSP &usc = GetSettingsController();
790 UserSettingsController::FinalizeSettingsController (usc);
791 usc.reset();
792}
Caroline Ticedaccaa92010-09-20 20:44:43793
Greg Clayton99d0faf2010-11-18 23:32:35794UserSettingsControllerSP &
795Target::GetSettingsController ()
796{
797 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:43798 return g_settings_controller;
799}
800
801ArchSpec
802Target::GetDefaultArchitecture ()
803{
Greg Clayton64195a22011-02-23 00:35:02804 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
805
806 if (settings_controller_sp)
807 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
808 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43809}
810
811void
Greg Clayton64195a22011-02-23 00:35:02812Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:43813{
Greg Clayton64195a22011-02-23 00:35:02814 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
815
816 if (settings_controller_sp)
817 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:43818}
819
Greg Clayton0603aa92010-10-04 01:05:56820Target *
821Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
822{
823 // The target can either exist in the "process" of ExecutionContext, or in
824 // the "target_sp" member of SymbolContext. This accessor helper function
825 // will get the target from one of these locations.
826
827 Target *target = NULL;
828 if (sc_ptr != NULL)
829 target = sc_ptr->target_sp.get();
830 if (target == NULL)
831 {
832 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
833 target = &exe_ctx_ptr->process->GetTarget();
834 }
835 return target;
836}
837
838
Caroline Tice1559a462010-09-27 00:30:10839void
840Target::UpdateInstanceName ()
841{
842 StreamString sstr;
843
844 ModuleSP module_sp = GetExecutableModule();
845 if (module_sp)
846 {
Greg Clayton307de252010-10-27 02:06:37847 sstr.Printf ("%s_%s",
848 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton64195a22011-02-23 00:35:02849 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytondbe54502010-11-19 03:46:01850 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
851 sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:10852 }
853}
854
Sean Callanan322f5292010-10-29 00:29:03855const char *
856Target::GetExpressionPrefixContentsAsCString ()
857{
858 return m_expr_prefix_contents.c_str();
859}
860
Greg Clayton8b2fe6d2010-12-14 02:59:59861ExecutionResults
862Target::EvaluateExpression
863(
864 const char *expr_cstr,
865 StackFrame *frame,
866 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:35867 bool keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59868 lldb::ValueObjectSP &result_valobj_sp
869)
870{
871 ExecutionResults execution_results = eExecutionSetupError;
872
873 result_valobj_sp.reset();
874
875 ExecutionContext exe_ctx;
876 if (frame)
877 {
878 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:08879 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:18880 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
881 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
882 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton8b2fe6d2010-12-14 02:59:59883 }
884 else if (m_process_sp)
885 {
886 m_process_sp->CalculateExecutionContext(exe_ctx);
887 }
888 else
889 {
890 CalculateExecutionContext(exe_ctx);
891 }
892
893 if (result_valobj_sp)
894 {
895 execution_results = eExecutionCompleted;
896 // We got a result from the frame variable expression path above...
897 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
898
899 lldb::ValueObjectSP const_valobj_sp;
900
901 // Check in case our value is already a constant value
902 if (result_valobj_sp->GetIsConstant())
903 {
904 const_valobj_sp = result_valobj_sp;
905 const_valobj_sp->SetName (persistent_variable_name);
906 }
907 else
908 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
909 persistent_variable_name);
910
Sean Callanan92adcac2011-01-13 08:53:35911 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
912
Greg Clayton8b2fe6d2010-12-14 02:59:59913 result_valobj_sp = const_valobj_sp;
914
Sean Callanan92adcac2011-01-13 08:53:35915 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
916 assert (clang_expr_variable_sp.get());
917
918 // Set flags and live data as appropriate
919
920 const Value &result_value = live_valobj_sp->GetValue();
921
922 switch (result_value.GetValueType())
923 {
924 case Value::eValueTypeHostAddress:
925 case Value::eValueTypeFileAddress:
926 // we don't do anything with these for now
927 break;
928 case Value::eValueTypeScalar:
929 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
930 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
931 break;
932 case Value::eValueTypeLoadAddress:
933 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
934 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
935 break;
936 }
Greg Clayton8b2fe6d2010-12-14 02:59:59937 }
938 else
939 {
940 // Make sure we aren't just trying to see the value of a persistent
941 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:35942 lldb::ClangExpressionVariableSP persistent_var_sp;
943 // Only check for persistent variables the expression starts with a '$'
944 if (expr_cstr[0] == '$')
945 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
946
Greg Clayton8b2fe6d2010-12-14 02:59:59947 if (persistent_var_sp)
948 {
949 result_valobj_sp = persistent_var_sp->GetValueObject ();
950 execution_results = eExecutionCompleted;
951 }
952 else
953 {
954 const char *prefix = GetExpressionPrefixContentsAsCString();
955
956 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35957 unwind_on_error,
958 keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59959 expr_cstr,
960 prefix,
961 result_valobj_sp);
962 }
963 }
964 return execution_results;
965}
966
Caroline Ticedaccaa92010-09-20 20:44:43967//--------------------------------------------------------------
968// class Target::SettingsController
969//--------------------------------------------------------------
970
971Target::SettingsController::SettingsController () :
972 UserSettingsController ("target", Debugger::GetSettingsController()),
973 m_default_architecture ()
974{
975 m_default_settings.reset (new TargetInstanceSettings (*this, false,
976 InstanceSettings::GetDefaultName().AsCString()));
977}
978
979Target::SettingsController::~SettingsController ()
980{
981}
982
983lldb::InstanceSettingsSP
984Target::SettingsController::CreateInstanceSettings (const char *instance_name)
985{
Greg Claytondbe54502010-11-19 03:46:01986 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
987 false,
988 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:43989 lldb::InstanceSettingsSP new_settings_sp (new_settings);
990 return new_settings_sp;
991}
992
Caroline Ticedaccaa92010-09-20 20:44:43993
Greg Claytonbfe5f3b2011-02-18 01:44:25994#define TSC_DEFAULT_ARCH "default-arch"
995#define TSC_EXPR_PREFIX "expr-prefix"
996#define TSC_EXEC_LEVEL "execution-level"
997#define TSC_EXEC_MODE "execution-mode"
998#define TSC_EXEC_OS_TYPE "execution-os-type"
999
1000
1001static const ConstString &
1002GetSettingNameForDefaultArch ()
1003{
1004 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1005
1006 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431007}
1008
Greg Claytonbfe5f3b2011-02-18 01:44:251009static const ConstString &
1010GetSettingNameForExpressionPrefix ()
1011{
1012 static ConstString g_const_string (TSC_EXPR_PREFIX);
1013 return g_const_string;
1014}
1015
1016static const ConstString &
1017GetSettingNameForExecutionLevel ()
1018{
1019 static ConstString g_const_string (TSC_EXEC_LEVEL);
1020 return g_const_string;
1021}
1022
1023static const ConstString &
1024GetSettingNameForExecutionMode ()
1025{
1026 static ConstString g_const_string (TSC_EXEC_MODE);
1027 return g_const_string;
1028}
1029
1030static const ConstString &
1031GetSettingNameForExecutionOSType ()
1032{
1033 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1034 return g_const_string;
1035}
1036
1037
Caroline Ticedaccaa92010-09-20 20:44:431038bool
1039Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1040 const char *index_value,
1041 const char *value,
1042 const SettingEntry &entry,
1043 const lldb::VarSetOperationType op,
1044 Error&err)
1045{
Greg Claytonbfe5f3b2011-02-18 01:44:251046 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431047 {
Greg Clayton64195a22011-02-23 00:35:021048 m_default_architecture.SetTriple (value);
1049 if (!m_default_architecture.IsValid())
1050 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431051 }
1052 return true;
1053}
1054
1055
1056bool
1057Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1058 StringList &value,
1059 Error &err)
1060{
Greg Claytonbfe5f3b2011-02-18 01:44:251061 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431062 {
Greg Clayton307de252010-10-27 02:06:371063 // If the arch is invalid (the default), don't show a string for it
1064 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021065 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431066 return true;
1067 }
1068 else
1069 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1070
1071 return false;
1072}
1073
1074//--------------------------------------------------------------
1075// class TargetInstanceSettings
1076//--------------------------------------------------------------
1077
Greg Clayton85851dd2010-12-04 00:10:171078TargetInstanceSettings::TargetInstanceSettings
1079(
1080 UserSettingsController &owner,
1081 bool live_instance,
1082 const char *name
1083) :
Greg Claytonbfe5f3b2011-02-18 01:44:251084 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1085 m_expr_prefix_path (),
1086 m_expr_prefix_contents (),
1087 m_execution_level (eExecutionLevelAuto),
1088 m_execution_mode (eExecutionModeAuto),
1089 m_execution_os_type (eExecutionOSTypeAuto)
Caroline Ticedaccaa92010-09-20 20:44:431090{
1091 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1092 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1093 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1094 // This is true for CreateInstanceName() too.
1095
1096 if (GetInstanceName () == InstanceSettings::InvalidName())
1097 {
1098 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1099 m_owner.RegisterInstanceSettings (this);
1100 }
1101
1102 if (live_instance)
1103 {
1104 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1105 CopyInstanceSettings (pending_settings,false);
1106 //m_owner.RemovePendingSettings (m_instance_name);
1107 }
1108}
1109
1110TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:011111 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Ticedaccaa92010-09-20 20:44:431112{
1113 if (m_instance_name != InstanceSettings::GetDefaultName())
1114 {
1115 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1116 CopyInstanceSettings (pending_settings,false);
1117 //m_owner.RemovePendingSettings (m_instance_name);
1118 }
1119}
1120
1121TargetInstanceSettings::~TargetInstanceSettings ()
1122{
1123}
1124
1125TargetInstanceSettings&
1126TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1127{
1128 if (this != &rhs)
1129 {
1130 }
1131
1132 return *this;
1133}
1134
Caroline Ticedaccaa92010-09-20 20:44:431135void
1136TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1137 const char *index_value,
1138 const char *value,
1139 const ConstString &instance_name,
1140 const SettingEntry &entry,
1141 lldb::VarSetOperationType op,
1142 Error &err,
1143 bool pending)
1144{
Greg Claytonbfe5f3b2011-02-18 01:44:251145 int new_enum = -1;
1146
1147 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031148 {
1149 switch (op)
1150 {
1151 default:
1152 err.SetErrorToGenericError ();
1153 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1154 return;
1155 case lldb::eVarSetOperationAssign:
1156 {
1157 FileSpec file_spec(value, true);
1158
1159 if (!file_spec.Exists())
1160 {
1161 err.SetErrorToGenericError ();
1162 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1163 return;
1164 }
1165
Greg Claytonc3f381b2011-02-03 17:47:471166 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan322f5292010-10-29 00:29:031167
Greg Claytonc3f381b2011-02-03 17:47:471168 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan322f5292010-10-29 00:29:031169 {
1170 err.SetErrorToGenericError ();
Greg Claytonc3f381b2011-02-03 17:47:471171 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan322f5292010-10-29 00:29:031172 return;
1173 }
1174
1175 m_expr_prefix_path = value;
Greg Claytonc3f381b2011-02-03 17:47:471176 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan322f5292010-10-29 00:29:031177 }
1178 return;
1179 case lldb::eVarSetOperationAppend:
1180 err.SetErrorToGenericError ();
1181 err.SetErrorString ("Cannot append to a path.\n");
1182 return;
1183 case lldb::eVarSetOperationClear:
1184 m_expr_prefix_path.clear ();
1185 m_expr_prefix_contents.clear ();
1186 return;
1187 }
1188 }
Greg Claytonbfe5f3b2011-02-18 01:44:251189 else if (var_name == GetSettingNameForExecutionLevel ())
1190 {
1191 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1192 if (err.Success())
1193 m_execution_level = (ExecutionLevel)new_enum;
1194 }
1195 else if (var_name == GetSettingNameForExecutionMode ())
1196 {
1197 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1198 if (err.Success())
1199 m_execution_mode = (ExecutionMode)new_enum;
1200 }
1201 else if (var_name == GetSettingNameForExecutionOSType ())
1202 {
1203 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1204 if (err.Success())
1205 m_execution_os_type = (ExecutionOSType)new_enum;
1206 }
Caroline Ticedaccaa92010-09-20 20:44:431207}
1208
1209void
Greg Claytonbfe5f3b2011-02-18 01:44:251210TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431211{
Sean Callanan322f5292010-10-29 00:29:031212 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1213
1214 if (!new_settings_ptr)
1215 return;
1216
Greg Claytonbfe5f3b2011-02-18 01:44:251217 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1218 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
1219 m_execution_level = new_settings_ptr->m_execution_level;
1220 m_execution_mode = new_settings_ptr->m_execution_mode;
1221 m_execution_os_type = new_settings_ptr->m_execution_os_type;
Caroline Ticedaccaa92010-09-20 20:44:431222}
1223
Caroline Tice12cecd72010-09-20 21:37:421224bool
Caroline Ticedaccaa92010-09-20 20:44:431225TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1226 const ConstString &var_name,
1227 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421228 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431229{
Greg Claytonbfe5f3b2011-02-18 01:44:251230 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031231 {
1232 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1233 }
Greg Claytonbfe5f3b2011-02-18 01:44:251234 else if (var_name == GetSettingNameForExecutionLevel ())
1235 {
1236 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_level));
1237 }
1238 else if (var_name == GetSettingNameForExecutionMode ())
1239 {
1240 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_mode));
1241 }
1242 else if (var_name == GetSettingNameForExecutionOSType ())
1243 {
1244 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_os_type));
1245 }
Sean Callanan322f5292010-10-29 00:29:031246 else
1247 {
1248 if (err)
1249 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1250 return false;
1251 }
1252
1253 return true;
Caroline Ticedaccaa92010-09-20 20:44:431254}
1255
1256const ConstString
1257TargetInstanceSettings::CreateInstanceName ()
1258{
Caroline Ticedaccaa92010-09-20 20:44:431259 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:101260 static int instance_count = 1;
1261
Caroline Ticedaccaa92010-09-20 20:44:431262 sstr.Printf ("target_%d", instance_count);
1263 ++instance_count;
1264
1265 const ConstString ret_val (sstr.GetData());
1266 return ret_val;
1267}
1268
1269//--------------------------------------------------
1270// Target::SettingsController Variable Tables
1271//--------------------------------------------------
1272
1273SettingEntry
1274Target::SettingsController::global_settings_table[] =
1275{
Greg Claytonbfe5f3b2011-02-18 01:44:251276 // var-name var-type default enum init'd hidden help-text
1277 // ================= ================== =========== ==== ====== ====== =========================================================================
1278 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1279 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1280};
1281
1282static lldb::OptionEnumValueElement
1283g_execution_level_enums[] =
1284{
1285 { eExecutionLevelAuto , "auto" , "Automatically detect the execution level (user/kernel)." },
1286 { eExecutionLevelKernel , "kernel" , "Treat executables as kernel executables." },
1287 { eExecutionLevelUser , "user" , "Treat executables as user space executables." },
1288 { 0 , NULL , NULL }
1289};
1290
1291static lldb::OptionEnumValueElement
1292g_execution_mode_enums[] =
1293{
1294 { eExecutionModeAuto , "auto" , "Automatically detect the execution mode (static/dynamic)." },
1295 { eExecutionModeStatic , "static" , "All executables are static and addresses at the virtual addresses in the object files." },
1296 { eExecutionModeDynamic , "dynamic" , "Executables and shared libraries are dynamically loaded.." },
1297 { 0 , NULL , NULL }
1298};
1299
1300static lldb::OptionEnumValueElement
1301g_execution_os_enums[] =
1302{
1303 { eExecutionOSTypeAuto , "auto" , "Automatically detect the execution OS (none/halted/live)." },
1304 { eExecutionOSTypeNone , "none" , "There is no operating system available (no processes or threads)." },
1305 { eExecutionOSTypeHalted, "halted" , "There is an operating system, but it is halted when the debugger is halted. "
1306 "Processes and threads must be discovered by accessing symbols and reading "
1307 "memory." },
1308 { eExecutionOSTypeLive , "live" , "There is a live operating system with debug services that can be used to "
1309 "get processes, threads and theirs states." },
1310 { 0, NULL, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431311};
1312
1313SettingEntry
1314Target::SettingsController::instance_settings_table[] =
1315{
Greg Claytonbfe5f3b2011-02-18 01:44:251316 // var-name var-type default enum-table init'd hidden help-text
1317 // ================= ================== =========== ========================= ====== ====== =========================================================================
1318 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL , false, false, "Path to a file containing expressions to be prepended to all expressions." },
1319 { TSC_EXEC_LEVEL , eSetVarTypeEnum , "auto" , g_execution_level_enums , false, false, "Sets the execution level for a target." },
1320 { TSC_EXEC_MODE , eSetVarTypeEnum , "auto" , g_execution_mode_enums , false, false, "Sets the execution mode for a target." },
1321 { TSC_EXEC_OS_TYPE , eSetVarTypeEnum , "auto" , g_execution_os_enums , false, false, "Sets the execution OS for a target." },
1322 { NULL , eSetVarTypeNone , NULL , NULL , false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431323};