blob: 3ce42d82e21fb9bf02cceaf05966617e5a0cf0e3 [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 Claytondbe54502010-11-19 03:46:01804 lldb::UserSettingsControllerSP &settings_controller = GetSettingsController();
Caroline Ticedaccaa92010-09-20 20:44:43805 lldb::SettableVariableType var_type;
806 Error err;
807 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
808
809 const char *default_name = "";
810 if (result.GetSize() == 1 && err.Success())
811 default_name = result.GetStringAtIndex (0);
812
813 ArchSpec default_arch (default_name);
814 return default_arch;
815}
816
817void
818Target::SetDefaultArchitecture (ArchSpec new_arch)
819{
820 if (new_arch.IsValid())
Greg Claytondbe54502010-11-19 03:46:01821 GetSettingsController ()->SetVariable ("target.default-arch",
822 new_arch.AsCString(),
823 lldb::eVarSetOperationAssign,
824 false,
825 "[]");
Caroline Ticedaccaa92010-09-20 20:44:43826}
827
Greg Clayton0603aa92010-10-04 01:05:56828Target *
829Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
830{
831 // The target can either exist in the "process" of ExecutionContext, or in
832 // the "target_sp" member of SymbolContext. This accessor helper function
833 // will get the target from one of these locations.
834
835 Target *target = NULL;
836 if (sc_ptr != NULL)
837 target = sc_ptr->target_sp.get();
838 if (target == NULL)
839 {
840 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
841 target = &exe_ctx_ptr->process->GetTarget();
842 }
843 return target;
844}
845
846
Caroline Tice1559a462010-09-27 00:30:10847void
848Target::UpdateInstanceName ()
849{
850 StreamString sstr;
851
852 ModuleSP module_sp = GetExecutableModule();
853 if (module_sp)
854 {
Greg Clayton307de252010-10-27 02:06:37855 sstr.Printf ("%s_%s",
856 module_sp->GetFileSpec().GetFilename().AsCString(),
Caroline Tice1559a462010-09-27 00:30:10857 module_sp->GetArchitecture().AsCString());
Greg Claytondbe54502010-11-19 03:46:01858 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
859 sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:10860 }
861}
862
Sean Callanan322f5292010-10-29 00:29:03863const char *
864Target::GetExpressionPrefixContentsAsCString ()
865{
866 return m_expr_prefix_contents.c_str();
867}
868
Greg Clayton8b2fe6d2010-12-14 02:59:59869ExecutionResults
870Target::EvaluateExpression
871(
872 const char *expr_cstr,
873 StackFrame *frame,
874 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:35875 bool keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59876 lldb::ValueObjectSP &result_valobj_sp
877)
878{
879 ExecutionResults execution_results = eExecutionSetupError;
880
881 result_valobj_sp.reset();
882
883 ExecutionContext exe_ctx;
884 if (frame)
885 {
886 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:08887 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:18888 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
889 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
890 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton8b2fe6d2010-12-14 02:59:59891 }
892 else if (m_process_sp)
893 {
894 m_process_sp->CalculateExecutionContext(exe_ctx);
895 }
896 else
897 {
898 CalculateExecutionContext(exe_ctx);
899 }
900
901 if (result_valobj_sp)
902 {
903 execution_results = eExecutionCompleted;
904 // We got a result from the frame variable expression path above...
905 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
906
907 lldb::ValueObjectSP const_valobj_sp;
908
909 // Check in case our value is already a constant value
910 if (result_valobj_sp->GetIsConstant())
911 {
912 const_valobj_sp = result_valobj_sp;
913 const_valobj_sp->SetName (persistent_variable_name);
914 }
915 else
916 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
917 persistent_variable_name);
918
Sean Callanan92adcac2011-01-13 08:53:35919 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
920
Greg Clayton8b2fe6d2010-12-14 02:59:59921 result_valobj_sp = const_valobj_sp;
922
Sean Callanan92adcac2011-01-13 08:53:35923 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
924 assert (clang_expr_variable_sp.get());
925
926 // Set flags and live data as appropriate
927
928 const Value &result_value = live_valobj_sp->GetValue();
929
930 switch (result_value.GetValueType())
931 {
932 case Value::eValueTypeHostAddress:
933 case Value::eValueTypeFileAddress:
934 // we don't do anything with these for now
935 break;
936 case Value::eValueTypeScalar:
937 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
938 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
939 break;
940 case Value::eValueTypeLoadAddress:
941 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
942 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
943 break;
944 }
Greg Clayton8b2fe6d2010-12-14 02:59:59945 }
946 else
947 {
948 // Make sure we aren't just trying to see the value of a persistent
949 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:35950 lldb::ClangExpressionVariableSP persistent_var_sp;
951 // Only check for persistent variables the expression starts with a '$'
952 if (expr_cstr[0] == '$')
953 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
954
Greg Clayton8b2fe6d2010-12-14 02:59:59955 if (persistent_var_sp)
956 {
957 result_valobj_sp = persistent_var_sp->GetValueObject ();
958 execution_results = eExecutionCompleted;
959 }
960 else
961 {
962 const char *prefix = GetExpressionPrefixContentsAsCString();
963
964 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35965 unwind_on_error,
966 keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59967 expr_cstr,
968 prefix,
969 result_valobj_sp);
970 }
971 }
972 return execution_results;
973}
974
Caroline Ticedaccaa92010-09-20 20:44:43975//--------------------------------------------------------------
976// class Target::SettingsController
977//--------------------------------------------------------------
978
979Target::SettingsController::SettingsController () :
980 UserSettingsController ("target", Debugger::GetSettingsController()),
981 m_default_architecture ()
982{
983 m_default_settings.reset (new TargetInstanceSettings (*this, false,
984 InstanceSettings::GetDefaultName().AsCString()));
985}
986
987Target::SettingsController::~SettingsController ()
988{
989}
990
991lldb::InstanceSettingsSP
992Target::SettingsController::CreateInstanceSettings (const char *instance_name)
993{
Greg Claytondbe54502010-11-19 03:46:01994 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
995 false,
996 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:43997 lldb::InstanceSettingsSP new_settings_sp (new_settings);
998 return new_settings_sp;
999}
1000
Caroline Ticedaccaa92010-09-20 20:44:431001
Greg Claytonbfe5f3b2011-02-18 01:44:251002#define TSC_DEFAULT_ARCH "default-arch"
1003#define TSC_EXPR_PREFIX "expr-prefix"
1004#define TSC_EXEC_LEVEL "execution-level"
1005#define TSC_EXEC_MODE "execution-mode"
1006#define TSC_EXEC_OS_TYPE "execution-os-type"
1007
1008
1009static const ConstString &
1010GetSettingNameForDefaultArch ()
1011{
1012 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1013
1014 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431015}
1016
Greg Claytonbfe5f3b2011-02-18 01:44:251017static const ConstString &
1018GetSettingNameForExpressionPrefix ()
1019{
1020 static ConstString g_const_string (TSC_EXPR_PREFIX);
1021 return g_const_string;
1022}
1023
1024static const ConstString &
1025GetSettingNameForExecutionLevel ()
1026{
1027 static ConstString g_const_string (TSC_EXEC_LEVEL);
1028 return g_const_string;
1029}
1030
1031static const ConstString &
1032GetSettingNameForExecutionMode ()
1033{
1034 static ConstString g_const_string (TSC_EXEC_MODE);
1035 return g_const_string;
1036}
1037
1038static const ConstString &
1039GetSettingNameForExecutionOSType ()
1040{
1041 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1042 return g_const_string;
1043}
1044
1045
Caroline Ticedaccaa92010-09-20 20:44:431046bool
1047Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1048 const char *index_value,
1049 const char *value,
1050 const SettingEntry &entry,
1051 const lldb::VarSetOperationType op,
1052 Error&err)
1053{
Greg Claytonbfe5f3b2011-02-18 01:44:251054 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431055 {
1056 ArchSpec tmp_spec (value);
1057 if (tmp_spec.IsValid())
1058 m_default_architecture = tmp_spec;
1059 else
1060 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
1061 }
1062 return true;
1063}
1064
1065
1066bool
1067Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1068 StringList &value,
1069 Error &err)
1070{
Greg Claytonbfe5f3b2011-02-18 01:44:251071 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431072 {
Greg Clayton307de252010-10-27 02:06:371073 // If the arch is invalid (the default), don't show a string for it
1074 if (m_default_architecture.IsValid())
1075 value.AppendString (m_default_architecture.AsCString());
Caroline Ticedaccaa92010-09-20 20:44:431076 return true;
1077 }
1078 else
1079 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1080
1081 return false;
1082}
1083
1084//--------------------------------------------------------------
1085// class TargetInstanceSettings
1086//--------------------------------------------------------------
1087
Greg Clayton85851dd2010-12-04 00:10:171088TargetInstanceSettings::TargetInstanceSettings
1089(
1090 UserSettingsController &owner,
1091 bool live_instance,
1092 const char *name
1093) :
Greg Claytonbfe5f3b2011-02-18 01:44:251094 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1095 m_expr_prefix_path (),
1096 m_expr_prefix_contents (),
1097 m_execution_level (eExecutionLevelAuto),
1098 m_execution_mode (eExecutionModeAuto),
1099 m_execution_os_type (eExecutionOSTypeAuto)
Caroline Ticedaccaa92010-09-20 20:44:431100{
1101 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1102 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1103 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1104 // This is true for CreateInstanceName() too.
1105
1106 if (GetInstanceName () == InstanceSettings::InvalidName())
1107 {
1108 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1109 m_owner.RegisterInstanceSettings (this);
1110 }
1111
1112 if (live_instance)
1113 {
1114 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1115 CopyInstanceSettings (pending_settings,false);
1116 //m_owner.RemovePendingSettings (m_instance_name);
1117 }
1118}
1119
1120TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:011121 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Ticedaccaa92010-09-20 20:44:431122{
1123 if (m_instance_name != InstanceSettings::GetDefaultName())
1124 {
1125 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1126 CopyInstanceSettings (pending_settings,false);
1127 //m_owner.RemovePendingSettings (m_instance_name);
1128 }
1129}
1130
1131TargetInstanceSettings::~TargetInstanceSettings ()
1132{
1133}
1134
1135TargetInstanceSettings&
1136TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1137{
1138 if (this != &rhs)
1139 {
1140 }
1141
1142 return *this;
1143}
1144
Caroline Ticedaccaa92010-09-20 20:44:431145void
1146TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1147 const char *index_value,
1148 const char *value,
1149 const ConstString &instance_name,
1150 const SettingEntry &entry,
1151 lldb::VarSetOperationType op,
1152 Error &err,
1153 bool pending)
1154{
Greg Claytonbfe5f3b2011-02-18 01:44:251155 int new_enum = -1;
1156
1157 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031158 {
1159 switch (op)
1160 {
1161 default:
1162 err.SetErrorToGenericError ();
1163 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1164 return;
1165 case lldb::eVarSetOperationAssign:
1166 {
1167 FileSpec file_spec(value, true);
1168
1169 if (!file_spec.Exists())
1170 {
1171 err.SetErrorToGenericError ();
1172 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1173 return;
1174 }
1175
Greg Claytonc3f381b2011-02-03 17:47:471176 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan322f5292010-10-29 00:29:031177
Greg Claytonc3f381b2011-02-03 17:47:471178 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan322f5292010-10-29 00:29:031179 {
1180 err.SetErrorToGenericError ();
Greg Claytonc3f381b2011-02-03 17:47:471181 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan322f5292010-10-29 00:29:031182 return;
1183 }
1184
1185 m_expr_prefix_path = value;
Greg Claytonc3f381b2011-02-03 17:47:471186 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan322f5292010-10-29 00:29:031187 }
1188 return;
1189 case lldb::eVarSetOperationAppend:
1190 err.SetErrorToGenericError ();
1191 err.SetErrorString ("Cannot append to a path.\n");
1192 return;
1193 case lldb::eVarSetOperationClear:
1194 m_expr_prefix_path.clear ();
1195 m_expr_prefix_contents.clear ();
1196 return;
1197 }
1198 }
Greg Claytonbfe5f3b2011-02-18 01:44:251199 else if (var_name == GetSettingNameForExecutionLevel ())
1200 {
1201 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1202 if (err.Success())
1203 m_execution_level = (ExecutionLevel)new_enum;
1204 }
1205 else if (var_name == GetSettingNameForExecutionMode ())
1206 {
1207 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1208 if (err.Success())
1209 m_execution_mode = (ExecutionMode)new_enum;
1210 }
1211 else if (var_name == GetSettingNameForExecutionOSType ())
1212 {
1213 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1214 if (err.Success())
1215 m_execution_os_type = (ExecutionOSType)new_enum;
1216 }
Caroline Ticedaccaa92010-09-20 20:44:431217}
1218
1219void
Greg Claytonbfe5f3b2011-02-18 01:44:251220TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431221{
Sean Callanan322f5292010-10-29 00:29:031222 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1223
1224 if (!new_settings_ptr)
1225 return;
1226
Greg Claytonbfe5f3b2011-02-18 01:44:251227 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1228 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
1229 m_execution_level = new_settings_ptr->m_execution_level;
1230 m_execution_mode = new_settings_ptr->m_execution_mode;
1231 m_execution_os_type = new_settings_ptr->m_execution_os_type;
Caroline Ticedaccaa92010-09-20 20:44:431232}
1233
Caroline Tice12cecd72010-09-20 21:37:421234bool
Caroline Ticedaccaa92010-09-20 20:44:431235TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1236 const ConstString &var_name,
1237 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421238 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431239{
Greg Claytonbfe5f3b2011-02-18 01:44:251240 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031241 {
1242 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1243 }
Greg Claytonbfe5f3b2011-02-18 01:44:251244 else if (var_name == GetSettingNameForExecutionLevel ())
1245 {
1246 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_level));
1247 }
1248 else if (var_name == GetSettingNameForExecutionMode ())
1249 {
1250 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_mode));
1251 }
1252 else if (var_name == GetSettingNameForExecutionOSType ())
1253 {
1254 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_os_type));
1255 }
Sean Callanan322f5292010-10-29 00:29:031256 else
1257 {
1258 if (err)
1259 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1260 return false;
1261 }
1262
1263 return true;
Caroline Ticedaccaa92010-09-20 20:44:431264}
1265
1266const ConstString
1267TargetInstanceSettings::CreateInstanceName ()
1268{
Caroline Ticedaccaa92010-09-20 20:44:431269 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:101270 static int instance_count = 1;
1271
Caroline Ticedaccaa92010-09-20 20:44:431272 sstr.Printf ("target_%d", instance_count);
1273 ++instance_count;
1274
1275 const ConstString ret_val (sstr.GetData());
1276 return ret_val;
1277}
1278
1279//--------------------------------------------------
1280// Target::SettingsController Variable Tables
1281//--------------------------------------------------
1282
1283SettingEntry
1284Target::SettingsController::global_settings_table[] =
1285{
Greg Claytonbfe5f3b2011-02-18 01:44:251286 // var-name var-type default enum init'd hidden help-text
1287 // ================= ================== =========== ==== ====== ====== =========================================================================
1288 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1289 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1290};
1291
1292static lldb::OptionEnumValueElement
1293g_execution_level_enums[] =
1294{
1295 { eExecutionLevelAuto , "auto" , "Automatically detect the execution level (user/kernel)." },
1296 { eExecutionLevelKernel , "kernel" , "Treat executables as kernel executables." },
1297 { eExecutionLevelUser , "user" , "Treat executables as user space executables." },
1298 { 0 , NULL , NULL }
1299};
1300
1301static lldb::OptionEnumValueElement
1302g_execution_mode_enums[] =
1303{
1304 { eExecutionModeAuto , "auto" , "Automatically detect the execution mode (static/dynamic)." },
1305 { eExecutionModeStatic , "static" , "All executables are static and addresses at the virtual addresses in the object files." },
1306 { eExecutionModeDynamic , "dynamic" , "Executables and shared libraries are dynamically loaded.." },
1307 { 0 , NULL , NULL }
1308};
1309
1310static lldb::OptionEnumValueElement
1311g_execution_os_enums[] =
1312{
1313 { eExecutionOSTypeAuto , "auto" , "Automatically detect the execution OS (none/halted/live)." },
1314 { eExecutionOSTypeNone , "none" , "There is no operating system available (no processes or threads)." },
1315 { eExecutionOSTypeHalted, "halted" , "There is an operating system, but it is halted when the debugger is halted. "
1316 "Processes and threads must be discovered by accessing symbols and reading "
1317 "memory." },
1318 { eExecutionOSTypeLive , "live" , "There is a live operating system with debug services that can be used to "
1319 "get processes, threads and theirs states." },
1320 { 0, NULL, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431321};
1322
1323SettingEntry
1324Target::SettingsController::instance_settings_table[] =
1325{
Greg Claytonbfe5f3b2011-02-18 01:44:251326 // var-name var-type default enum-table init'd hidden help-text
1327 // ================= ================== =========== ========================= ====== ====== =========================================================================
1328 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL , false, false, "Path to a file containing expressions to be prepended to all expressions." },
1329 { TSC_EXEC_LEVEL , eSetVarTypeEnum , "auto" , g_execution_level_enums , false, false, "Sets the execution level for a target." },
1330 { TSC_EXEC_MODE , eSetVarTypeEnum , "auto" , g_execution_mode_enums , false, false, "Sets the execution mode for a target." },
1331 { TSC_EXEC_OS_TYPE , eSetVarTypeEnum , "auto" , g_execution_os_enums , false, false, "Sets the execution OS for a target." },
1332 { NULL , eSetVarTypeNone , NULL , NULL , false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431333};