blob: 82110db57f9fac39d50573ca9353ff1e87a44bc4 [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;
Greg Claytone996fd32011-03-08 22:40:15415 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
416 assert (executable_objfile);
417 // TODO: remote assertion above after verifying that it doesn't fire off
418 // after the platform changes. The platform is what should be selecting
419 // the right slice of an executable file, and it also should be the one
420 // to resolve any executables in their bundles.
421// if (executable_objfile == NULL)
422// {
423//
424// FileSpec bundle_executable(executable_sp->GetFileSpec());
425// if (Host::ResolveExecutableInBundle (bundle_executable))
426// {
427// ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
428// exe_arch));
429// SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
430// if (bundle_exe_module_sp->GetObjectFile() != NULL)
431// executable_sp = bundle_exe_module_sp;
432// return;
433// }
434// }
Chris Lattner30fdc8d2010-06-08 16:52:24435
436 if (executable_objfile)
437 {
438 executable_objfile->GetDependentModules(dependent_files);
439 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
440 {
441 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
442 exe_arch));
443 if (image_module_sp.get())
444 {
445 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
446 ObjectFile *objfile = image_module_sp->GetObjectFile();
447 if (objfile)
448 objfile->GetDependentModules(dependent_files);
449 }
450 }
451 }
452
453 // Now see if we know the target triple, and if so, create our scratch AST context:
Greg Clayton514487e2011-02-15 21:59:32454 if (m_arch_spec.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24455 {
Greg Clayton514487e2011-02-15 21:59:32456 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch_spec.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:24457 }
458 }
Caroline Tice1559a462010-09-27 00:30:10459
460 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24461}
462
463
Jim Ingham5aee1622010-08-09 23:31:02464bool
465Target::SetArchitecture (const ArchSpec &arch_spec)
466{
467 if (m_arch_spec == arch_spec)
468 {
469 // If we're setting the architecture to our current architecture, we
470 // don't need to do anything.
471 return true;
472 }
473 else if (!m_arch_spec.IsValid())
474 {
475 // If we haven't got a valid arch spec, then we just need to set it.
476 m_arch_spec = arch_spec;
477 return true;
478 }
479 else
480 {
481 // If we have an executable file, try to reset the executable to the desired architecture
482 m_arch_spec = arch_spec;
483 ModuleSP executable_sp = GetExecutableModule ();
484 m_images.Clear();
485 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02486 // Need to do something about unsetting breakpoints.
487
488 if (executable_sp)
489 {
490 FileSpec exec_file_spec = executable_sp->GetFileSpec();
491 Error error = ModuleList::GetSharedModule(exec_file_spec,
492 arch_spec,
493 NULL,
494 NULL,
495 0,
496 executable_sp,
497 NULL,
498 NULL);
499
500 if (!error.Fail() && executable_sp)
501 {
502 SetExecutableModule (executable_sp, true);
503 return true;
504 }
505 else
506 {
507 return false;
508 }
509 }
510 else
511 {
512 return false;
513 }
514 }
515}
Chris Lattner30fdc8d2010-06-08 16:52:24516
Chris Lattner30fdc8d2010-06-08 16:52:24517void
518Target::ModuleAdded (ModuleSP &module_sp)
519{
520 // A module is being added to this target for the first time
521 ModuleList module_list;
522 module_list.Append(module_sp);
523 ModulesDidLoad (module_list);
524}
525
526void
527Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
528{
529 // A module is being added to this target for the first time
530 ModuleList module_list;
531 module_list.Append (old_module_sp);
532 ModulesDidUnload (module_list);
533 module_list.Clear ();
534 module_list.Append (new_module_sp);
535 ModulesDidLoad (module_list);
536}
537
538void
539Target::ModulesDidLoad (ModuleList &module_list)
540{
541 m_breakpoint_list.UpdateBreakpoints (module_list, true);
542 // TODO: make event data that packages up the module_list
543 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
544}
545
546void
547Target::ModulesDidUnload (ModuleList &module_list)
548{
549 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26550
551 // Remove the images from the target image list
552 m_images.Remove(module_list);
553
Chris Lattner30fdc8d2010-06-08 16:52:24554 // TODO: make event data that packages up the module_list
555 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
556}
557
558size_t
Greg Claytondb598232011-01-07 01:57:07559Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
560{
561 const Section *section = addr.GetSection();
562 if (section && section->GetModule())
563 {
564 ObjectFile *objfile = section->GetModule()->GetObjectFile();
565 if (objfile)
566 {
567 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
568 addr.GetOffset(),
569 dst,
570 dst_len);
571 if (bytes_read > 0)
572 return bytes_read;
573 else
574 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
575 }
576 else
577 {
578 error.SetErrorString("address isn't from a object file");
579 }
580 }
581 else
582 {
583 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
584 }
585 return 0;
586}
587
588size_t
589Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24590{
Chris Lattner30fdc8d2010-06-08 16:52:24591 error.Clear();
Greg Claytondb598232011-01-07 01:57:07592
Greg Claytondda4f7b2010-06-30 23:03:03593 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
594
Greg Claytondb598232011-01-07 01:57:07595 size_t bytes_read = 0;
Greg Claytondda4f7b2010-06-30 23:03:03596 Address resolved_addr(addr);
597 if (!resolved_addr.IsSectionOffset())
598 {
599 if (process_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24600 {
Greg Claytonf5e56de2010-09-14 23:36:40601 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03602 }
603 else
604 {
605 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
606 }
607 }
608
Greg Claytondb598232011-01-07 01:57:07609 if (prefer_file_cache)
610 {
611 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
612 if (bytes_read > 0)
613 return bytes_read;
614 }
Greg Claytondda4f7b2010-06-30 23:03:03615
616 if (process_is_valid)
617 {
Greg Claytonf5e56de2010-09-14 23:36:40618 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03619 if (load_addr == LLDB_INVALID_ADDRESS)
620 {
621 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
622 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
623 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
624 resolved_addr.GetFileAddress());
625 else
626 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
627 }
628 else
629 {
Greg Claytondb598232011-01-07 01:57:07630 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24631 if (bytes_read != dst_len)
632 {
633 if (error.Success())
634 {
635 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03636 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24637 else
Greg Claytondda4f7b2010-06-30 23:03:03638 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:24639 }
640 }
Greg Claytondda4f7b2010-06-30 23:03:03641 if (bytes_read)
642 return bytes_read;
643 // If the address is not section offset we have an address that
644 // doesn't resolve to any address in any currently loaded shared
645 // libaries and we failed to read memory so there isn't anything
646 // more we can do. If it is section offset, we might be able to
647 // read cached memory from the object file.
648 if (!resolved_addr.IsSectionOffset())
649 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24650 }
Chris Lattner30fdc8d2010-06-08 16:52:24651 }
Greg Claytondda4f7b2010-06-30 23:03:03652
Greg Claytondb598232011-01-07 01:57:07653 if (!prefer_file_cache)
Greg Claytondda4f7b2010-06-30 23:03:03654 {
Greg Claytondb598232011-01-07 01:57:07655 // If we didn't already try and read from the object file cache, then
656 // try it after failing to read from the process.
657 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03658 }
659 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24660}
661
662
663ModuleSP
664Target::GetSharedModule
665(
666 const FileSpec& file_spec,
667 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10668 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24669 const ConstString *object_name,
670 off_t object_offset,
671 Error *error_ptr
672)
673{
674 // Don't pass in the UUID so we can tell if we have a stale value in our list
675 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
676 bool did_create_module = false;
677 ModuleSP module_sp;
678
679 // If there are image search path entries, try to use them first to acquire a suitable image.
680
681 Error error;
682
683 if (m_image_search_paths.GetSize())
684 {
685 FileSpec transformed_spec;
686 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
687 {
688 transformed_spec.GetFilename() = file_spec.GetFilename();
689 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
690 }
691 }
692
693 // If a module hasn't been found yet, use the unmodified path.
694
695 if (!module_sp)
696 {
697 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
698 }
699
700 if (module_sp)
701 {
702 m_images.Append (module_sp);
703 if (did_create_module)
704 {
705 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
706 ModuleUpdated(old_module_sp, module_sp);
707 else
708 ModuleAdded(module_sp);
709 }
710 }
711 if (error_ptr)
712 *error_ptr = error;
713 return module_sp;
714}
715
716
717Target *
718Target::CalculateTarget ()
719{
720 return this;
721}
722
723Process *
724Target::CalculateProcess ()
725{
726 return NULL;
727}
728
729Thread *
730Target::CalculateThread ()
731{
732 return NULL;
733}
734
735StackFrame *
736Target::CalculateStackFrame ()
737{
738 return NULL;
739}
740
741void
Greg Clayton0603aa92010-10-04 01:05:56742Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24743{
744 exe_ctx.target = this;
745 exe_ctx.process = NULL; // Do NOT fill in process...
746 exe_ctx.thread = NULL;
747 exe_ctx.frame = NULL;
748}
749
750PathMappingList &
751Target::GetImageSearchPathList ()
752{
753 return m_image_search_paths;
754}
755
756void
757Target::ImageSearchPathsChanged
758(
759 const PathMappingList &path_list,
760 void *baton
761)
762{
763 Target *target = (Target *)baton;
764 if (target->m_images.GetSize() > 1)
765 {
766 ModuleSP exe_module_sp (target->GetExecutableModule());
767 if (exe_module_sp)
768 {
769 target->m_images.Clear();
770 target->SetExecutableModule (exe_module_sp, true);
771 }
772 }
773}
774
775ClangASTContext *
776Target::GetScratchClangASTContext()
777{
778 return m_scratch_ast_context_ap.get();
779}
Caroline Ticedaccaa92010-09-20 20:44:43780
Greg Clayton99d0faf2010-11-18 23:32:35781void
Caroline Tice20bd37f2011-03-10 22:14:10782Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43783{
Greg Clayton99d0faf2010-11-18 23:32:35784 UserSettingsControllerSP &usc = GetSettingsController();
785 usc.reset (new SettingsController);
786 UserSettingsController::InitializeSettingsController (usc,
787 SettingsController::global_settings_table,
788 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:10789
790 // Now call SettingsInitialize() on each 'child' setting of Target
791 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35792}
Caroline Ticedaccaa92010-09-20 20:44:43793
Greg Clayton99d0faf2010-11-18 23:32:35794void
Caroline Tice20bd37f2011-03-10 22:14:10795Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35796{
Caroline Tice20bd37f2011-03-10 22:14:10797
798 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
799
800 Process::SettingsTerminate ();
801
802 // Now terminate Target Settings.
803
Greg Clayton99d0faf2010-11-18 23:32:35804 UserSettingsControllerSP &usc = GetSettingsController();
805 UserSettingsController::FinalizeSettingsController (usc);
806 usc.reset();
807}
Caroline Ticedaccaa92010-09-20 20:44:43808
Greg Clayton99d0faf2010-11-18 23:32:35809UserSettingsControllerSP &
810Target::GetSettingsController ()
811{
812 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:43813 return g_settings_controller;
814}
815
816ArchSpec
817Target::GetDefaultArchitecture ()
818{
Greg Clayton64195a22011-02-23 00:35:02819 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
820
821 if (settings_controller_sp)
822 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
823 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43824}
825
826void
Greg Clayton64195a22011-02-23 00:35:02827Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:43828{
Greg Clayton64195a22011-02-23 00:35:02829 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
830
831 if (settings_controller_sp)
832 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:43833}
834
Greg Clayton0603aa92010-10-04 01:05:56835Target *
836Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
837{
838 // The target can either exist in the "process" of ExecutionContext, or in
839 // the "target_sp" member of SymbolContext. This accessor helper function
840 // will get the target from one of these locations.
841
842 Target *target = NULL;
843 if (sc_ptr != NULL)
844 target = sc_ptr->target_sp.get();
845 if (target == NULL)
846 {
847 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
848 target = &exe_ctx_ptr->process->GetTarget();
849 }
850 return target;
851}
852
853
Caroline Tice1559a462010-09-27 00:30:10854void
855Target::UpdateInstanceName ()
856{
857 StreamString sstr;
858
859 ModuleSP module_sp = GetExecutableModule();
860 if (module_sp)
861 {
Greg Clayton307de252010-10-27 02:06:37862 sstr.Printf ("%s_%s",
863 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton64195a22011-02-23 00:35:02864 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytondbe54502010-11-19 03:46:01865 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
866 sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:10867 }
868}
869
Sean Callanan322f5292010-10-29 00:29:03870const char *
871Target::GetExpressionPrefixContentsAsCString ()
872{
873 return m_expr_prefix_contents.c_str();
874}
875
Greg Clayton8b2fe6d2010-12-14 02:59:59876ExecutionResults
877Target::EvaluateExpression
878(
879 const char *expr_cstr,
880 StackFrame *frame,
881 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:35882 bool keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59883 lldb::ValueObjectSP &result_valobj_sp
884)
885{
886 ExecutionResults execution_results = eExecutionSetupError;
887
888 result_valobj_sp.reset();
889
890 ExecutionContext exe_ctx;
891 if (frame)
892 {
893 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:08894 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:18895 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
896 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
897 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton8b2fe6d2010-12-14 02:59:59898 }
899 else if (m_process_sp)
900 {
901 m_process_sp->CalculateExecutionContext(exe_ctx);
902 }
903 else
904 {
905 CalculateExecutionContext(exe_ctx);
906 }
907
908 if (result_valobj_sp)
909 {
910 execution_results = eExecutionCompleted;
911 // We got a result from the frame variable expression path above...
912 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
913
914 lldb::ValueObjectSP const_valobj_sp;
915
916 // Check in case our value is already a constant value
917 if (result_valobj_sp->GetIsConstant())
918 {
919 const_valobj_sp = result_valobj_sp;
920 const_valobj_sp->SetName (persistent_variable_name);
921 }
922 else
923 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
924 persistent_variable_name);
925
Sean Callanan92adcac2011-01-13 08:53:35926 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
927
Greg Clayton8b2fe6d2010-12-14 02:59:59928 result_valobj_sp = const_valobj_sp;
929
Sean Callanan92adcac2011-01-13 08:53:35930 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
931 assert (clang_expr_variable_sp.get());
932
933 // Set flags and live data as appropriate
934
935 const Value &result_value = live_valobj_sp->GetValue();
936
937 switch (result_value.GetValueType())
938 {
939 case Value::eValueTypeHostAddress:
940 case Value::eValueTypeFileAddress:
941 // we don't do anything with these for now
942 break;
943 case Value::eValueTypeScalar:
944 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
945 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
946 break;
947 case Value::eValueTypeLoadAddress:
948 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
949 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
950 break;
951 }
Greg Clayton8b2fe6d2010-12-14 02:59:59952 }
953 else
954 {
955 // Make sure we aren't just trying to see the value of a persistent
956 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:35957 lldb::ClangExpressionVariableSP persistent_var_sp;
958 // Only check for persistent variables the expression starts with a '$'
959 if (expr_cstr[0] == '$')
960 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
961
Greg Clayton8b2fe6d2010-12-14 02:59:59962 if (persistent_var_sp)
963 {
964 result_valobj_sp = persistent_var_sp->GetValueObject ();
965 execution_results = eExecutionCompleted;
966 }
967 else
968 {
969 const char *prefix = GetExpressionPrefixContentsAsCString();
970
971 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35972 unwind_on_error,
973 keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59974 expr_cstr,
975 prefix,
976 result_valobj_sp);
977 }
978 }
979 return execution_results;
980}
981
Caroline Ticedaccaa92010-09-20 20:44:43982//--------------------------------------------------------------
983// class Target::SettingsController
984//--------------------------------------------------------------
985
986Target::SettingsController::SettingsController () :
987 UserSettingsController ("target", Debugger::GetSettingsController()),
988 m_default_architecture ()
989{
990 m_default_settings.reset (new TargetInstanceSettings (*this, false,
991 InstanceSettings::GetDefaultName().AsCString()));
992}
993
994Target::SettingsController::~SettingsController ()
995{
996}
997
998lldb::InstanceSettingsSP
999Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1000{
Greg Claytondbe54502010-11-19 03:46:011001 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1002 false,
1003 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431004 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1005 return new_settings_sp;
1006}
1007
Caroline Ticedaccaa92010-09-20 20:44:431008
Greg Claytonbfe5f3b2011-02-18 01:44:251009#define TSC_DEFAULT_ARCH "default-arch"
1010#define TSC_EXPR_PREFIX "expr-prefix"
1011#define TSC_EXEC_LEVEL "execution-level"
1012#define TSC_EXEC_MODE "execution-mode"
1013#define TSC_EXEC_OS_TYPE "execution-os-type"
1014
1015
1016static const ConstString &
1017GetSettingNameForDefaultArch ()
1018{
1019 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1020
1021 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431022}
1023
Greg Claytonbfe5f3b2011-02-18 01:44:251024static const ConstString &
1025GetSettingNameForExpressionPrefix ()
1026{
1027 static ConstString g_const_string (TSC_EXPR_PREFIX);
1028 return g_const_string;
1029}
1030
1031static const ConstString &
1032GetSettingNameForExecutionLevel ()
1033{
1034 static ConstString g_const_string (TSC_EXEC_LEVEL);
1035 return g_const_string;
1036}
1037
1038static const ConstString &
1039GetSettingNameForExecutionMode ()
1040{
1041 static ConstString g_const_string (TSC_EXEC_MODE);
1042 return g_const_string;
1043}
1044
1045static const ConstString &
1046GetSettingNameForExecutionOSType ()
1047{
1048 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1049 return g_const_string;
1050}
1051
1052
Caroline Ticedaccaa92010-09-20 20:44:431053bool
1054Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1055 const char *index_value,
1056 const char *value,
1057 const SettingEntry &entry,
1058 const lldb::VarSetOperationType op,
1059 Error&err)
1060{
Greg Claytonbfe5f3b2011-02-18 01:44:251061 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431062 {
Greg Clayton64195a22011-02-23 00:35:021063 m_default_architecture.SetTriple (value);
1064 if (!m_default_architecture.IsValid())
1065 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431066 }
1067 return true;
1068}
1069
1070
1071bool
1072Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1073 StringList &value,
1074 Error &err)
1075{
Greg Claytonbfe5f3b2011-02-18 01:44:251076 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431077 {
Greg Clayton307de252010-10-27 02:06:371078 // If the arch is invalid (the default), don't show a string for it
1079 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021080 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431081 return true;
1082 }
1083 else
1084 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1085
1086 return false;
1087}
1088
1089//--------------------------------------------------------------
1090// class TargetInstanceSettings
1091//--------------------------------------------------------------
1092
Greg Clayton85851dd2010-12-04 00:10:171093TargetInstanceSettings::TargetInstanceSettings
1094(
1095 UserSettingsController &owner,
1096 bool live_instance,
1097 const char *name
1098) :
Greg Claytonbfe5f3b2011-02-18 01:44:251099 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1100 m_expr_prefix_path (),
1101 m_expr_prefix_contents (),
1102 m_execution_level (eExecutionLevelAuto),
1103 m_execution_mode (eExecutionModeAuto),
1104 m_execution_os_type (eExecutionOSTypeAuto)
Caroline Ticedaccaa92010-09-20 20:44:431105{
1106 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1107 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1108 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1109 // This is true for CreateInstanceName() too.
1110
1111 if (GetInstanceName () == InstanceSettings::InvalidName())
1112 {
1113 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1114 m_owner.RegisterInstanceSettings (this);
1115 }
1116
1117 if (live_instance)
1118 {
1119 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1120 CopyInstanceSettings (pending_settings,false);
1121 //m_owner.RemovePendingSettings (m_instance_name);
1122 }
1123}
1124
1125TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:011126 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Ticedaccaa92010-09-20 20:44:431127{
1128 if (m_instance_name != InstanceSettings::GetDefaultName())
1129 {
1130 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1131 CopyInstanceSettings (pending_settings,false);
1132 //m_owner.RemovePendingSettings (m_instance_name);
1133 }
1134}
1135
1136TargetInstanceSettings::~TargetInstanceSettings ()
1137{
1138}
1139
1140TargetInstanceSettings&
1141TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1142{
1143 if (this != &rhs)
1144 {
1145 }
1146
1147 return *this;
1148}
1149
Caroline Ticedaccaa92010-09-20 20:44:431150void
1151TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1152 const char *index_value,
1153 const char *value,
1154 const ConstString &instance_name,
1155 const SettingEntry &entry,
1156 lldb::VarSetOperationType op,
1157 Error &err,
1158 bool pending)
1159{
Greg Claytonbfe5f3b2011-02-18 01:44:251160 int new_enum = -1;
1161
1162 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031163 {
1164 switch (op)
1165 {
1166 default:
1167 err.SetErrorToGenericError ();
1168 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1169 return;
1170 case lldb::eVarSetOperationAssign:
1171 {
1172 FileSpec file_spec(value, true);
1173
1174 if (!file_spec.Exists())
1175 {
1176 err.SetErrorToGenericError ();
1177 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1178 return;
1179 }
1180
Greg Claytonc3f381b2011-02-03 17:47:471181 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan322f5292010-10-29 00:29:031182
Greg Claytonc3f381b2011-02-03 17:47:471183 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan322f5292010-10-29 00:29:031184 {
1185 err.SetErrorToGenericError ();
Greg Claytonc3f381b2011-02-03 17:47:471186 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan322f5292010-10-29 00:29:031187 return;
1188 }
1189
1190 m_expr_prefix_path = value;
Greg Claytonc3f381b2011-02-03 17:47:471191 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan322f5292010-10-29 00:29:031192 }
1193 return;
1194 case lldb::eVarSetOperationAppend:
1195 err.SetErrorToGenericError ();
1196 err.SetErrorString ("Cannot append to a path.\n");
1197 return;
1198 case lldb::eVarSetOperationClear:
1199 m_expr_prefix_path.clear ();
1200 m_expr_prefix_contents.clear ();
1201 return;
1202 }
1203 }
Greg Claytonbfe5f3b2011-02-18 01:44:251204 else if (var_name == GetSettingNameForExecutionLevel ())
1205 {
1206 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1207 if (err.Success())
1208 m_execution_level = (ExecutionLevel)new_enum;
1209 }
1210 else if (var_name == GetSettingNameForExecutionMode ())
1211 {
1212 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1213 if (err.Success())
1214 m_execution_mode = (ExecutionMode)new_enum;
1215 }
1216 else if (var_name == GetSettingNameForExecutionOSType ())
1217 {
1218 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1219 if (err.Success())
1220 m_execution_os_type = (ExecutionOSType)new_enum;
1221 }
Caroline Ticedaccaa92010-09-20 20:44:431222}
1223
1224void
Greg Claytonbfe5f3b2011-02-18 01:44:251225TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431226{
Sean Callanan322f5292010-10-29 00:29:031227 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1228
1229 if (!new_settings_ptr)
1230 return;
1231
Greg Claytonbfe5f3b2011-02-18 01:44:251232 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1233 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
1234 m_execution_level = new_settings_ptr->m_execution_level;
1235 m_execution_mode = new_settings_ptr->m_execution_mode;
1236 m_execution_os_type = new_settings_ptr->m_execution_os_type;
Caroline Ticedaccaa92010-09-20 20:44:431237}
1238
Caroline Tice12cecd72010-09-20 21:37:421239bool
Caroline Ticedaccaa92010-09-20 20:44:431240TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1241 const ConstString &var_name,
1242 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421243 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431244{
Greg Claytonbfe5f3b2011-02-18 01:44:251245 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031246 {
1247 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1248 }
Greg Claytonbfe5f3b2011-02-18 01:44:251249 else if (var_name == GetSettingNameForExecutionLevel ())
1250 {
1251 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_level));
1252 }
1253 else if (var_name == GetSettingNameForExecutionMode ())
1254 {
1255 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_mode));
1256 }
1257 else if (var_name == GetSettingNameForExecutionOSType ())
1258 {
1259 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_os_type));
1260 }
Sean Callanan322f5292010-10-29 00:29:031261 else
1262 {
1263 if (err)
1264 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1265 return false;
1266 }
1267
1268 return true;
Caroline Ticedaccaa92010-09-20 20:44:431269}
1270
1271const ConstString
1272TargetInstanceSettings::CreateInstanceName ()
1273{
Caroline Ticedaccaa92010-09-20 20:44:431274 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:101275 static int instance_count = 1;
1276
Caroline Ticedaccaa92010-09-20 20:44:431277 sstr.Printf ("target_%d", instance_count);
1278 ++instance_count;
1279
1280 const ConstString ret_val (sstr.GetData());
1281 return ret_val;
1282}
1283
1284//--------------------------------------------------
1285// Target::SettingsController Variable Tables
1286//--------------------------------------------------
1287
1288SettingEntry
1289Target::SettingsController::global_settings_table[] =
1290{
Greg Claytonbfe5f3b2011-02-18 01:44:251291 // var-name var-type default enum init'd hidden help-text
1292 // ================= ================== =========== ==== ====== ====== =========================================================================
1293 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1294 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1295};
1296
1297static lldb::OptionEnumValueElement
1298g_execution_level_enums[] =
1299{
1300 { eExecutionLevelAuto , "auto" , "Automatically detect the execution level (user/kernel)." },
1301 { eExecutionLevelKernel , "kernel" , "Treat executables as kernel executables." },
1302 { eExecutionLevelUser , "user" , "Treat executables as user space executables." },
1303 { 0 , NULL , NULL }
1304};
1305
1306static lldb::OptionEnumValueElement
1307g_execution_mode_enums[] =
1308{
1309 { eExecutionModeAuto , "auto" , "Automatically detect the execution mode (static/dynamic)." },
1310 { eExecutionModeStatic , "static" , "All executables are static and addresses at the virtual addresses in the object files." },
1311 { eExecutionModeDynamic , "dynamic" , "Executables and shared libraries are dynamically loaded.." },
1312 { 0 , NULL , NULL }
1313};
1314
1315static lldb::OptionEnumValueElement
1316g_execution_os_enums[] =
1317{
1318 { eExecutionOSTypeAuto , "auto" , "Automatically detect the execution OS (none/halted/live)." },
1319 { eExecutionOSTypeNone , "none" , "There is no operating system available (no processes or threads)." },
1320 { eExecutionOSTypeHalted, "halted" , "There is an operating system, but it is halted when the debugger is halted. "
1321 "Processes and threads must be discovered by accessing symbols and reading "
1322 "memory." },
1323 { eExecutionOSTypeLive , "live" , "There is a live operating system with debug services that can be used to "
1324 "get processes, threads and theirs states." },
1325 { 0, NULL, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431326};
1327
1328SettingEntry
1329Target::SettingsController::instance_settings_table[] =
1330{
Greg Claytonbfe5f3b2011-02-18 01:44:251331 // var-name var-type default enum-table init'd hidden help-text
1332 // ================= ================== =========== ========================= ====== ====== =========================================================================
1333 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL , false, false, "Path to a file containing expressions to be prepended to all expressions." },
1334 { TSC_EXEC_LEVEL , eSetVarTypeEnum , "auto" , g_execution_level_enums , false, false, "Sets the execution level for a target." },
1335 { TSC_EXEC_MODE , eSetVarTypeEnum , "auto" , g_execution_mode_enums , false, false, "Sets the execution mode for a target." },
1336 { TSC_EXEC_OS_TYPE , eSetVarTypeEnum , "auto" , g_execution_os_enums , false, false, "Sets the execution OS for a target." },
1337 { NULL , eSetVarTypeNone , NULL , NULL , false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431338};