blob: 036ac0352bfe3ad299bece78e78d6cc816c00c41 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton427f2902010-12-14 02:59:59 +000020#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Caroline Tice4a348082011-05-02 20:41:46 +000023#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000027#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000029#include "lldb/Interpreter/CommandInterpreter.h"
30#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/lldb-private-log.h"
32#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000034#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000035#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
41//----------------------------------------------------------------------
42// Target constructor
43//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000044Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
45 Broadcaster ("lldb.target"),
46 ExecutionContextScope (),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000047 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000048 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000049 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000050 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000051 m_arch (target_arch),
52 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000053 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000054 m_breakpoint_list (false),
55 m_internal_breakpoint_list (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000056 m_process_sp (),
57 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000058 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000059 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000060 m_persistent_variables (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000061 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000062 m_stop_hook_next_id (0),
63 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000064{
Greg Clayton49ce6822010-10-31 03:01:06 +000065 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
66 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
67 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
68
Greg Claytone005f2c2010-11-06 01:53:30 +000069 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000070 if (log)
71 log->Printf ("%p Target::Target()", this);
72}
73
74//----------------------------------------------------------------------
75// Destructor
76//----------------------------------------------------------------------
77Target::~Target()
78{
Greg Claytone005f2c2010-11-06 01:53:30 +000079 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000080 if (log)
81 log->Printf ("%p Target::~Target()", this);
82 DeleteCurrentProcess ();
83}
84
85void
Caroline Tice7826c882010-10-26 03:11:13 +000086Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000087{
Greg Clayton3fed8b92010-10-08 00:21:05 +000088// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000089 if (description_level != lldb::eDescriptionLevelBrief)
90 {
91 s->Indent();
92 s->PutCString("Target\n");
93 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000094 m_images.Dump(s);
95 m_breakpoint_list.Dump(s);
96 m_internal_breakpoint_list.Dump(s);
97 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000098 }
99 else
100 {
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000101 if (GetExecutableModule())
102 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
103 else
104 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000105 }
Chris Lattner24943d22010-06-08 16:52:24 +0000106}
107
108void
109Target::DeleteCurrentProcess ()
110{
111 if (m_process_sp.get())
112 {
Greg Clayton49480b12010-09-14 23:52:43 +0000113 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000114 if (m_process_sp->IsAlive())
115 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000116
117 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000118
119 // Do any cleanup of the target we need to do between process instances.
120 // NB It is better to do this before destroying the process in case the
121 // clean up needs some help from the process.
122 m_breakpoint_list.ClearAllBreakpointSites();
123 m_internal_breakpoint_list.ClearAllBreakpointSites();
124 m_process_sp.reset();
125 }
126}
127
128const lldb::ProcessSP &
129Target::CreateProcess (Listener &listener, const char *plugin_name)
130{
131 DeleteCurrentProcess ();
132 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
133 return m_process_sp;
134}
135
136const lldb::ProcessSP &
137Target::GetProcessSP () const
138{
139 return m_process_sp;
140}
141
142lldb::TargetSP
143Target::GetSP()
144{
Greg Clayton63094e02010-06-23 01:19:29 +0000145 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000146}
147
148BreakpointList &
149Target::GetBreakpointList(bool internal)
150{
151 if (internal)
152 return m_internal_breakpoint_list;
153 else
154 return m_breakpoint_list;
155}
156
157const BreakpointList &
158Target::GetBreakpointList(bool internal) const
159{
160 if (internal)
161 return m_internal_breakpoint_list;
162 else
163 return m_breakpoint_list;
164}
165
166BreakpointSP
167Target::GetBreakpointByID (break_id_t break_id)
168{
169 BreakpointSP bp_sp;
170
171 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
172 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
173 else
174 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
175
176 return bp_sp;
177}
178
179BreakpointSP
180Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
181{
182 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
183 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
184 return CreateBreakpoint (filter_sp, resolver_sp, internal);
185}
186
187
188BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000189Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000190{
Chris Lattner24943d22010-06-08 16:52:24 +0000191 Address so_addr;
192 // Attempt to resolve our load address if possible, though it is ok if
193 // it doesn't resolve to section/offset.
194
Greg Clayton33ed1702010-08-24 00:45:41 +0000195 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000196 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000197 if (!so_addr.IsValid())
198 {
199 // The address didn't resolve, so just set this as an absolute address
200 so_addr.SetOffset (addr);
201 }
202 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000203 return bp_sp;
204}
205
206BreakpointSP
207Target::CreateBreakpoint (Address &addr, bool internal)
208{
209 TargetSP target_sp = this->GetSP();
210 SearchFilterSP filter_sp(new SearchFilter (target_sp));
211 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
212 return CreateBreakpoint (filter_sp, resolver_sp, internal);
213}
214
215BreakpointSP
Greg Clayton7dd98df2011-07-12 17:06:17 +0000216Target::CreateBreakpoint (const FileSpec *containingModule,
217 const char *func_name,
218 uint32_t func_name_type_mask,
219 bool internal,
220 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000221{
Greg Clayton12bec712010-06-28 21:30:43 +0000222 BreakpointSP bp_sp;
223 if (func_name)
224 {
225 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000226
227 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
228 func_name,
229 func_name_type_mask,
230 Breakpoint::Exact,
231 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000232 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
233 }
234 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000235}
236
237
238SearchFilterSP
239Target::GetSearchFilterForModule (const FileSpec *containingModule)
240{
241 SearchFilterSP filter_sp;
242 lldb::TargetSP target_sp = this->GetSP();
243 if (containingModule != NULL)
244 {
245 // TODO: We should look into sharing module based search filters
246 // across many breakpoints like we do for the simple target based one
247 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
248 }
249 else
250 {
251 if (m_search_filter_sp.get() == NULL)
252 m_search_filter_sp.reset (new SearchFilter (target_sp));
253 filter_sp = m_search_filter_sp;
254 }
255 return filter_sp;
256}
257
258BreakpointSP
Greg Clayton7dd98df2011-07-12 17:06:17 +0000259Target::CreateBreakpoint (const FileSpec *containingModule,
260 RegularExpression &func_regex,
261 bool internal,
262 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000263{
264 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000265 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
266 func_regex,
267 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000268
269 return CreateBreakpoint (filter_sp, resolver_sp, internal);
270}
271
272BreakpointSP
273Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
274{
275 BreakpointSP bp_sp;
276 if (filter_sp && resolver_sp)
277 {
278 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
279 resolver_sp->SetBreakpoint (bp_sp.get());
280
281 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000282 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000283 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000284 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000285
Greg Claytone005f2c2010-11-06 01:53:30 +0000286 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000287 if (log)
288 {
289 StreamString s;
290 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
291 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
292 }
293
Chris Lattner24943d22010-06-08 16:52:24 +0000294 bp_sp->ResolveBreakpoint();
295 }
Jim Inghamd1686902010-10-14 23:45:03 +0000296
297 if (!internal && bp_sp)
298 {
299 m_last_created_breakpoint = bp_sp;
300 }
301
Chris Lattner24943d22010-06-08 16:52:24 +0000302 return bp_sp;
303}
304
305void
306Target::RemoveAllBreakpoints (bool internal_also)
307{
Greg Claytone005f2c2010-11-06 01:53:30 +0000308 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000309 if (log)
310 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
311
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000312 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000313 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000314 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000315
316 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000317}
318
319void
320Target::DisableAllBreakpoints (bool internal_also)
321{
Greg Claytone005f2c2010-11-06 01:53:30 +0000322 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000323 if (log)
324 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
325
326 m_breakpoint_list.SetEnabledAll (false);
327 if (internal_also)
328 m_internal_breakpoint_list.SetEnabledAll (false);
329}
330
331void
332Target::EnableAllBreakpoints (bool internal_also)
333{
Greg Claytone005f2c2010-11-06 01:53:30 +0000334 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000335 if (log)
336 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
337
338 m_breakpoint_list.SetEnabledAll (true);
339 if (internal_also)
340 m_internal_breakpoint_list.SetEnabledAll (true);
341}
342
343bool
344Target::RemoveBreakpointByID (break_id_t break_id)
345{
Greg Claytone005f2c2010-11-06 01:53:30 +0000346 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000347 if (log)
348 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
349
350 if (DisableBreakpointByID (break_id))
351 {
352 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000353 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000354 else
Jim Inghamd1686902010-10-14 23:45:03 +0000355 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000356 if (m_last_created_breakpoint)
357 {
358 if (m_last_created_breakpoint->GetID() == break_id)
359 m_last_created_breakpoint.reset();
360 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000361 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000362 }
Chris Lattner24943d22010-06-08 16:52:24 +0000363 return true;
364 }
365 return false;
366}
367
368bool
369Target::DisableBreakpointByID (break_id_t break_id)
370{
Greg Claytone005f2c2010-11-06 01:53:30 +0000371 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000372 if (log)
373 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
374
375 BreakpointSP bp_sp;
376
377 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
378 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
379 else
380 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
381 if (bp_sp)
382 {
383 bp_sp->SetEnabled (false);
384 return true;
385 }
386 return false;
387}
388
389bool
390Target::EnableBreakpointByID (break_id_t break_id)
391{
Greg Claytone005f2c2010-11-06 01:53:30 +0000392 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000393 if (log)
394 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
395 __FUNCTION__,
396 break_id,
397 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
398
399 BreakpointSP bp_sp;
400
401 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
402 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
403 else
404 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
405
406 if (bp_sp)
407 {
408 bp_sp->SetEnabled (true);
409 return true;
410 }
411 return false;
412}
413
414ModuleSP
415Target::GetExecutableModule ()
416{
417 ModuleSP executable_sp;
418 if (m_images.GetSize() > 0)
419 executable_sp = m_images.GetModuleAtIndex(0);
420 return executable_sp;
421}
422
423void
424Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
425{
426 m_images.Clear();
427 m_scratch_ast_context_ap.reset();
428
429 if (executable_sp.get())
430 {
431 Timer scoped_timer (__PRETTY_FUNCTION__,
432 "Target::SetExecutableModule (executable = '%s/%s')",
433 executable_sp->GetFileSpec().GetDirectory().AsCString(),
434 executable_sp->GetFileSpec().GetFilename().AsCString());
435
436 m_images.Append(executable_sp); // The first image is our exectuable file
437
Jim Ingham7508e732010-08-09 23:31:02 +0000438 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000439 if (!m_arch.IsValid())
440 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000441
Chris Lattner24943d22010-06-08 16:52:24 +0000442 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000443 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000444
445 if (executable_objfile)
446 {
447 executable_objfile->GetDependentModules(dependent_files);
448 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
449 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000450 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
451 FileSpec platform_dependent_file_spec;
452 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000453 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000454 else
455 platform_dependent_file_spec = dependent_file_spec;
456
457 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000458 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000459 if (image_module_sp.get())
460 {
Chris Lattner24943d22010-06-08 16:52:24 +0000461 ObjectFile *objfile = image_module_sp->GetObjectFile();
462 if (objfile)
463 objfile->GetDependentModules(dependent_files);
464 }
465 }
466 }
467
468 // Now see if we know the target triple, and if so, create our scratch AST context:
Greg Clayton24bc5d92011-03-30 18:16:51 +0000469 if (m_arch.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000470 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000471 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000472 }
473 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000474
475 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000476}
477
478
Jim Ingham7508e732010-08-09 23:31:02 +0000479bool
480Target::SetArchitecture (const ArchSpec &arch_spec)
481{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000482 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000483 {
484 // If we're setting the architecture to our current architecture, we
485 // don't need to do anything.
486 return true;
487 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000488 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000489 {
490 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000491 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000492 return true;
493 }
494 else
495 {
496 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000497 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000498 ModuleSP executable_sp = GetExecutableModule ();
499 m_images.Clear();
500 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000501 // Need to do something about unsetting breakpoints.
502
503 if (executable_sp)
504 {
505 FileSpec exec_file_spec = executable_sp->GetFileSpec();
506 Error error = ModuleList::GetSharedModule(exec_file_spec,
507 arch_spec,
508 NULL,
509 NULL,
510 0,
511 executable_sp,
512 NULL,
513 NULL);
514
515 if (!error.Fail() && executable_sp)
516 {
517 SetExecutableModule (executable_sp, true);
518 return true;
519 }
520 else
521 {
522 return false;
523 }
524 }
525 else
526 {
527 return false;
528 }
529 }
530}
Chris Lattner24943d22010-06-08 16:52:24 +0000531
Chris Lattner24943d22010-06-08 16:52:24 +0000532void
533Target::ModuleAdded (ModuleSP &module_sp)
534{
535 // A module is being added to this target for the first time
536 ModuleList module_list;
537 module_list.Append(module_sp);
538 ModulesDidLoad (module_list);
539}
540
541void
542Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
543{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000544 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000545 ModuleList module_list;
546 module_list.Append (old_module_sp);
547 ModulesDidUnload (module_list);
548 module_list.Clear ();
549 module_list.Append (new_module_sp);
550 ModulesDidLoad (module_list);
551}
552
553void
554Target::ModulesDidLoad (ModuleList &module_list)
555{
556 m_breakpoint_list.UpdateBreakpoints (module_list, true);
557 // TODO: make event data that packages up the module_list
558 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
559}
560
561void
562Target::ModulesDidUnload (ModuleList &module_list)
563{
564 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000565
566 // Remove the images from the target image list
567 m_images.Remove(module_list);
568
Chris Lattner24943d22010-06-08 16:52:24 +0000569 // TODO: make event data that packages up the module_list
570 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
571}
572
573size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000574Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
575{
576 const Section *section = addr.GetSection();
577 if (section && section->GetModule())
578 {
579 ObjectFile *objfile = section->GetModule()->GetObjectFile();
580 if (objfile)
581 {
582 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
583 addr.GetOffset(),
584 dst,
585 dst_len);
586 if (bytes_read > 0)
587 return bytes_read;
588 else
589 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
590 }
591 else
592 {
593 error.SetErrorString("address isn't from a object file");
594 }
595 }
596 else
597 {
598 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
599 }
600 return 0;
601}
602
603size_t
604Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000605{
Chris Lattner24943d22010-06-08 16:52:24 +0000606 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000607
Greg Clayton70436352010-06-30 23:03:03 +0000608 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
609
Greg Clayton26100dc2011-01-07 01:57:07 +0000610 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000611
612 addr_t load_addr = LLDB_INVALID_ADDRESS;
613 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000614 Address resolved_addr;
615 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000616 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000617 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000618 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000619 // No sections are loaded, so we must assume we are not running
620 // yet and anything we are given is a file address.
621 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
622 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000623 }
Greg Clayton70436352010-06-30 23:03:03 +0000624 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000625 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000626 // We have at least one section loaded. This can be becuase
627 // we have manually loaded some sections with "target modules load ..."
628 // or because we have have a live process that has sections loaded
629 // through the dynamic loader
630 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
631 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000632 }
Greg Clayton70436352010-06-30 23:03:03 +0000633 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000634 if (!resolved_addr.IsValid())
635 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000636
Greg Clayton9b82f862011-07-11 05:12:02 +0000637
Greg Clayton26100dc2011-01-07 01:57:07 +0000638 if (prefer_file_cache)
639 {
640 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
641 if (bytes_read > 0)
642 return bytes_read;
643 }
Greg Clayton70436352010-06-30 23:03:03 +0000644
645 if (process_is_valid)
646 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000647 if (load_addr == LLDB_INVALID_ADDRESS)
648 load_addr = resolved_addr.GetLoadAddress (this);
649
Greg Clayton70436352010-06-30 23:03:03 +0000650 if (load_addr == LLDB_INVALID_ADDRESS)
651 {
652 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
653 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
654 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
655 resolved_addr.GetFileAddress());
656 else
657 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
658 }
659 else
660 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000661 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000662 if (bytes_read != dst_len)
663 {
664 if (error.Success())
665 {
666 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000667 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000668 else
Greg Clayton70436352010-06-30 23:03:03 +0000669 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000670 }
671 }
Greg Clayton70436352010-06-30 23:03:03 +0000672 if (bytes_read)
673 return bytes_read;
674 // If the address is not section offset we have an address that
675 // doesn't resolve to any address in any currently loaded shared
676 // libaries and we failed to read memory so there isn't anything
677 // more we can do. If it is section offset, we might be able to
678 // read cached memory from the object file.
679 if (!resolved_addr.IsSectionOffset())
680 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000681 }
Chris Lattner24943d22010-06-08 16:52:24 +0000682 }
Greg Clayton70436352010-06-30 23:03:03 +0000683
Greg Clayton9b82f862011-07-11 05:12:02 +0000684 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000685 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000686 // If we didn't already try and read from the object file cache, then
687 // try it after failing to read from the process.
688 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000689 }
690 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000691}
692
Greg Clayton7dd98df2011-07-12 17:06:17 +0000693size_t
694Target::ReadScalarIntegerFromMemory (const Address& addr,
695 bool prefer_file_cache,
696 uint32_t byte_size,
697 bool is_signed,
698 Scalar &scalar,
699 Error &error)
700{
701 uint64_t uval;
702
703 if (byte_size <= sizeof(uval))
704 {
705 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
706 if (bytes_read == byte_size)
707 {
708 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
709 uint32_t offset = 0;
710 if (byte_size <= 4)
711 scalar = data.GetMaxU32 (&offset, byte_size);
712 else
713 scalar = data.GetMaxU64 (&offset, byte_size);
714
715 if (is_signed)
716 scalar.SignExtend(byte_size * 8);
717 return bytes_read;
718 }
719 }
720 else
721 {
722 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
723 }
724 return 0;
725}
726
727uint64_t
728Target::ReadUnsignedIntegerFromMemory (const Address& addr,
729 bool prefer_file_cache,
730 size_t integer_byte_size,
731 uint64_t fail_value,
732 Error &error)
733{
734 Scalar scalar;
735 if (ReadScalarIntegerFromMemory (addr,
736 prefer_file_cache,
737 integer_byte_size,
738 false,
739 scalar,
740 error))
741 return scalar.ULongLong(fail_value);
742 return fail_value;
743}
744
745bool
746Target::ReadPointerFromMemory (const Address& addr,
747 bool prefer_file_cache,
748 Error &error,
749 Address &pointer_addr)
750{
751 Scalar scalar;
752 if (ReadScalarIntegerFromMemory (addr,
753 prefer_file_cache,
754 m_arch.GetAddressByteSize(),
755 false,
756 scalar,
757 error))
758 {
759 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
760 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
761 {
762 if (m_section_load_list.IsEmpty())
763 {
764 // No sections are loaded, so we must assume we are not running
765 // yet and anything we are given is a file address.
766 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
767 }
768 else
769 {
770 // We have at least one section loaded. This can be becuase
771 // we have manually loaded some sections with "target modules load ..."
772 // or because we have have a live process that has sections loaded
773 // through the dynamic loader
774 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
775 }
776 // We weren't able to resolve the pointer value, so just return
777 // an address with no section
778 if (!pointer_addr.IsValid())
779 pointer_addr.SetOffset (pointer_vm_addr);
780 return true;
781
782 }
783 }
784 return false;
785}
Chris Lattner24943d22010-06-08 16:52:24 +0000786
787ModuleSP
788Target::GetSharedModule
789(
790 const FileSpec& file_spec,
791 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000792 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000793 const ConstString *object_name,
794 off_t object_offset,
795 Error *error_ptr
796)
797{
798 // Don't pass in the UUID so we can tell if we have a stale value in our list
799 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
800 bool did_create_module = false;
801 ModuleSP module_sp;
802
Chris Lattner24943d22010-06-08 16:52:24 +0000803 Error error;
804
Greg Clayton24bc5d92011-03-30 18:16:51 +0000805 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000806 if (m_image_search_paths.GetSize())
807 {
808 FileSpec transformed_spec;
809 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
810 {
811 transformed_spec.GetFilename() = file_spec.GetFilename();
812 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
813 }
814 }
815
Greg Clayton24bc5d92011-03-30 18:16:51 +0000816 // The platform is responsible for finding and caching an appropriate
817 // module in the shared module cache.
818 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000819 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000820 FileSpec platform_file_spec;
821 error = m_platform_sp->GetSharedModule (file_spec,
822 arch,
823 uuid_ptr,
824 object_name,
825 object_offset,
826 module_sp,
827 &old_module_sp,
828 &did_create_module);
829 }
830 else
831 {
832 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +0000833 }
834
Greg Clayton24bc5d92011-03-30 18:16:51 +0000835 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000836 if (module_sp)
837 {
838 m_images.Append (module_sp);
839 if (did_create_module)
840 {
841 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
842 ModuleUpdated(old_module_sp, module_sp);
843 else
844 ModuleAdded(module_sp);
845 }
846 }
847 if (error_ptr)
848 *error_ptr = error;
849 return module_sp;
850}
851
852
853Target *
854Target::CalculateTarget ()
855{
856 return this;
857}
858
859Process *
860Target::CalculateProcess ()
861{
862 return NULL;
863}
864
865Thread *
866Target::CalculateThread ()
867{
868 return NULL;
869}
870
871StackFrame *
872Target::CalculateStackFrame ()
873{
874 return NULL;
875}
876
877void
Greg Claytona830adb2010-10-04 01:05:56 +0000878Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000879{
880 exe_ctx.target = this;
881 exe_ctx.process = NULL; // Do NOT fill in process...
882 exe_ctx.thread = NULL;
883 exe_ctx.frame = NULL;
884}
885
886PathMappingList &
887Target::GetImageSearchPathList ()
888{
889 return m_image_search_paths;
890}
891
892void
893Target::ImageSearchPathsChanged
894(
895 const PathMappingList &path_list,
896 void *baton
897)
898{
899 Target *target = (Target *)baton;
900 if (target->m_images.GetSize() > 1)
901 {
902 ModuleSP exe_module_sp (target->GetExecutableModule());
903 if (exe_module_sp)
904 {
905 target->m_images.Clear();
906 target->SetExecutableModule (exe_module_sp, true);
907 }
908 }
909}
910
911ClangASTContext *
912Target::GetScratchClangASTContext()
913{
914 return m_scratch_ast_context_ap.get();
915}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000916
Greg Clayton990de7b2010-11-18 23:32:35 +0000917void
Caroline Tice2a456812011-03-10 22:14:10 +0000918Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000919{
Greg Clayton990de7b2010-11-18 23:32:35 +0000920 UserSettingsControllerSP &usc = GetSettingsController();
921 usc.reset (new SettingsController);
922 UserSettingsController::InitializeSettingsController (usc,
923 SettingsController::global_settings_table,
924 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000925
926 // Now call SettingsInitialize() on each 'child' setting of Target
927 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000928}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000929
Greg Clayton990de7b2010-11-18 23:32:35 +0000930void
Caroline Tice2a456812011-03-10 22:14:10 +0000931Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000932{
Caroline Tice2a456812011-03-10 22:14:10 +0000933
934 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
935
936 Process::SettingsTerminate ();
937
938 // Now terminate Target Settings.
939
Greg Clayton990de7b2010-11-18 23:32:35 +0000940 UserSettingsControllerSP &usc = GetSettingsController();
941 UserSettingsController::FinalizeSettingsController (usc);
942 usc.reset();
943}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000944
Greg Clayton990de7b2010-11-18 23:32:35 +0000945UserSettingsControllerSP &
946Target::GetSettingsController ()
947{
948 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000949 return g_settings_controller;
950}
951
952ArchSpec
953Target::GetDefaultArchitecture ()
954{
Greg Clayton940b1032011-02-23 00:35:02 +0000955 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
956
957 if (settings_controller_sp)
958 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
959 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000960}
961
962void
Greg Clayton940b1032011-02-23 00:35:02 +0000963Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000964{
Greg Clayton940b1032011-02-23 00:35:02 +0000965 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
966
967 if (settings_controller_sp)
968 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000969}
970
Greg Claytona830adb2010-10-04 01:05:56 +0000971Target *
972Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
973{
974 // The target can either exist in the "process" of ExecutionContext, or in
975 // the "target_sp" member of SymbolContext. This accessor helper function
976 // will get the target from one of these locations.
977
978 Target *target = NULL;
979 if (sc_ptr != NULL)
980 target = sc_ptr->target_sp.get();
981 if (target == NULL)
982 {
983 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
984 target = &exe_ctx_ptr->process->GetTarget();
985 }
986 return target;
987}
988
989
Caroline Tice1ebef442010-09-27 00:30:10 +0000990void
991Target::UpdateInstanceName ()
992{
993 StreamString sstr;
994
995 ModuleSP module_sp = GetExecutableModule();
996 if (module_sp)
997 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000998 sstr.Printf ("%s_%s",
999 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton940b1032011-02-23 00:35:02 +00001000 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001001 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
1002 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001003 }
1004}
1005
Sean Callanan77e93942010-10-29 00:29:03 +00001006const char *
1007Target::GetExpressionPrefixContentsAsCString ()
1008{
Greg Claytonff44ab42011-04-23 02:04:55 +00001009 if (m_expr_prefix_contents_sp)
1010 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1011 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001012}
1013
Greg Clayton427f2902010-12-14 02:59:59 +00001014ExecutionResults
1015Target::EvaluateExpression
1016(
1017 const char *expr_cstr,
1018 StackFrame *frame,
1019 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001020 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001021 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001022 lldb::ValueObjectSP &result_valobj_sp
1023)
1024{
1025 ExecutionResults execution_results = eExecutionSetupError;
1026
1027 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001028
1029 // We shouldn't run stop hooks in expressions.
1030 // Be sure to reset this if you return anywhere within this function.
1031 bool old_suppress_value = m_suppress_stop_hooks;
1032 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001033
1034 ExecutionContext exe_ctx;
1035 if (frame)
1036 {
1037 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001038 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001039 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
1040 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
Jim Ingham10de7d12011-05-04 03:43:18 +00001041 lldb::VariableSP var_sp;
1042 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1043 use_dynamic,
1044 expr_path_options,
1045 var_sp,
1046 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001047 }
1048 else if (m_process_sp)
1049 {
1050 m_process_sp->CalculateExecutionContext(exe_ctx);
1051 }
1052 else
1053 {
1054 CalculateExecutionContext(exe_ctx);
1055 }
1056
1057 if (result_valobj_sp)
1058 {
1059 execution_results = eExecutionCompleted;
1060 // We got a result from the frame variable expression path above...
1061 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1062
1063 lldb::ValueObjectSP const_valobj_sp;
1064
1065 // Check in case our value is already a constant value
1066 if (result_valobj_sp->GetIsConstant())
1067 {
1068 const_valobj_sp = result_valobj_sp;
1069 const_valobj_sp->SetName (persistent_variable_name);
1070 }
1071 else
Jim Inghame41494a2011-04-16 00:01:13 +00001072 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001073 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001074 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001075 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001076 if (dynamic_sp)
1077 result_valobj_sp = dynamic_sp;
1078 }
1079
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001080 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001081 }
Greg Clayton427f2902010-12-14 02:59:59 +00001082
Sean Callanan6a925532011-01-13 08:53:35 +00001083 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1084
Greg Clayton427f2902010-12-14 02:59:59 +00001085 result_valobj_sp = const_valobj_sp;
1086
Sean Callanan6a925532011-01-13 08:53:35 +00001087 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1088 assert (clang_expr_variable_sp.get());
1089
1090 // Set flags and live data as appropriate
1091
1092 const Value &result_value = live_valobj_sp->GetValue();
1093
1094 switch (result_value.GetValueType())
1095 {
1096 case Value::eValueTypeHostAddress:
1097 case Value::eValueTypeFileAddress:
1098 // we don't do anything with these for now
1099 break;
1100 case Value::eValueTypeScalar:
1101 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1102 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1103 break;
1104 case Value::eValueTypeLoadAddress:
1105 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1106 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1107 break;
1108 }
Greg Clayton427f2902010-12-14 02:59:59 +00001109 }
1110 else
1111 {
1112 // Make sure we aren't just trying to see the value of a persistent
1113 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001114 lldb::ClangExpressionVariableSP persistent_var_sp;
1115 // Only check for persistent variables the expression starts with a '$'
1116 if (expr_cstr[0] == '$')
1117 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1118
Greg Clayton427f2902010-12-14 02:59:59 +00001119 if (persistent_var_sp)
1120 {
1121 result_valobj_sp = persistent_var_sp->GetValueObject ();
1122 execution_results = eExecutionCompleted;
1123 }
1124 else
1125 {
1126 const char *prefix = GetExpressionPrefixContentsAsCString();
1127
1128 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +00001129 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001130 expr_cstr,
1131 prefix,
1132 result_valobj_sp);
1133 }
1134 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001135
1136 m_suppress_stop_hooks = old_suppress_value;
1137
Greg Clayton427f2902010-12-14 02:59:59 +00001138 return execution_results;
1139}
1140
Greg Claytonc0fa5332011-05-22 22:46:53 +00001141lldb::addr_t
1142Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1143{
1144 addr_t code_addr = load_addr;
1145 switch (m_arch.GetMachine())
1146 {
1147 case llvm::Triple::arm:
1148 case llvm::Triple::thumb:
1149 switch (addr_class)
1150 {
1151 case eAddressClassData:
1152 case eAddressClassDebug:
1153 return LLDB_INVALID_ADDRESS;
1154
1155 case eAddressClassUnknown:
1156 case eAddressClassInvalid:
1157 case eAddressClassCode:
1158 case eAddressClassCodeAlternateISA:
1159 case eAddressClassRuntime:
1160 // Check if bit zero it no set?
1161 if ((code_addr & 1ull) == 0)
1162 {
1163 // Bit zero isn't set, check if the address is a multiple of 2?
1164 if (code_addr & 2ull)
1165 {
1166 // The address is a multiple of 2 so it must be thumb, set bit zero
1167 code_addr |= 1ull;
1168 }
1169 else if (addr_class == eAddressClassCodeAlternateISA)
1170 {
1171 // We checked the address and the address claims to be the alternate ISA
1172 // which means thumb, so set bit zero.
1173 code_addr |= 1ull;
1174 }
1175 }
1176 break;
1177 }
1178 break;
1179
1180 default:
1181 break;
1182 }
1183 return code_addr;
1184}
1185
1186lldb::addr_t
1187Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1188{
1189 addr_t opcode_addr = load_addr;
1190 switch (m_arch.GetMachine())
1191 {
1192 case llvm::Triple::arm:
1193 case llvm::Triple::thumb:
1194 switch (addr_class)
1195 {
1196 case eAddressClassData:
1197 case eAddressClassDebug:
1198 return LLDB_INVALID_ADDRESS;
1199
1200 case eAddressClassInvalid:
1201 case eAddressClassUnknown:
1202 case eAddressClassCode:
1203 case eAddressClassCodeAlternateISA:
1204 case eAddressClassRuntime:
1205 opcode_addr &= ~(1ull);
1206 break;
1207 }
1208 break;
1209
1210 default:
1211 break;
1212 }
1213 return opcode_addr;
1214}
1215
Jim Inghamd60d94a2011-03-11 03:53:59 +00001216lldb::user_id_t
1217Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1218{
1219 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1220 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1221 m_stop_hooks[new_uid] = new_hook_sp;
1222 return new_uid;
1223}
1224
1225bool
1226Target::RemoveStopHookByID (lldb::user_id_t user_id)
1227{
1228 size_t num_removed;
1229 num_removed = m_stop_hooks.erase (user_id);
1230 if (num_removed == 0)
1231 return false;
1232 else
1233 return true;
1234}
1235
1236void
1237Target::RemoveAllStopHooks ()
1238{
1239 m_stop_hooks.clear();
1240}
1241
1242Target::StopHookSP
1243Target::GetStopHookByID (lldb::user_id_t user_id)
1244{
1245 StopHookSP found_hook;
1246
1247 StopHookCollection::iterator specified_hook_iter;
1248 specified_hook_iter = m_stop_hooks.find (user_id);
1249 if (specified_hook_iter != m_stop_hooks.end())
1250 found_hook = (*specified_hook_iter).second;
1251 return found_hook;
1252}
1253
1254bool
1255Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1256{
1257 StopHookCollection::iterator specified_hook_iter;
1258 specified_hook_iter = m_stop_hooks.find (user_id);
1259 if (specified_hook_iter == m_stop_hooks.end())
1260 return false;
1261
1262 (*specified_hook_iter).second->SetIsActive (active_state);
1263 return true;
1264}
1265
1266void
1267Target::SetAllStopHooksActiveState (bool active_state)
1268{
1269 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1270 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1271 {
1272 (*pos).second->SetIsActive (active_state);
1273 }
1274}
1275
1276void
1277Target::RunStopHooks ()
1278{
Jim Ingham3613ae12011-05-12 02:06:14 +00001279 if (m_suppress_stop_hooks)
1280 return;
1281
Jim Inghamd60d94a2011-03-11 03:53:59 +00001282 if (!m_process_sp)
1283 return;
1284
1285 if (m_stop_hooks.empty())
1286 return;
1287
1288 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1289
1290 // If there aren't any active stop hooks, don't bother either:
1291 bool any_active_hooks = false;
1292 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1293 {
1294 if ((*pos).second->IsActive())
1295 {
1296 any_active_hooks = true;
1297 break;
1298 }
1299 }
1300 if (!any_active_hooks)
1301 return;
1302
1303 CommandReturnObject result;
1304
1305 std::vector<ExecutionContext> exc_ctx_with_reasons;
1306 std::vector<SymbolContext> sym_ctx_with_reasons;
1307
1308 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1309 size_t num_threads = cur_threadlist.GetSize();
1310 for (size_t i = 0; i < num_threads; i++)
1311 {
1312 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1313 if (cur_thread_sp->ThreadStoppedForAReason())
1314 {
1315 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1316 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1317 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1318 }
1319 }
1320
1321 // If no threads stopped for a reason, don't run the stop-hooks.
1322 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1323 if (num_exe_ctx == 0)
1324 return;
1325
Jim Inghame5ed8e92011-06-02 23:58:26 +00001326 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1327 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001328
1329 bool keep_going = true;
1330 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001331 bool print_hook_header;
1332 bool print_thread_header;
1333
1334 if (num_exe_ctx == 1)
1335 print_thread_header = false;
1336 else
1337 print_thread_header = true;
1338
1339 if (m_stop_hooks.size() == 1)
1340 print_hook_header = false;
1341 else
1342 print_hook_header = true;
1343
Jim Inghamd60d94a2011-03-11 03:53:59 +00001344 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1345 {
1346 // result.Clear();
1347 StopHookSP cur_hook_sp = (*pos).second;
1348 if (!cur_hook_sp->IsActive())
1349 continue;
1350
1351 bool any_thread_matched = false;
1352 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1353 {
1354 if ((cur_hook_sp->GetSpecifier () == NULL
1355 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1356 && (cur_hook_sp->GetThreadSpecifier() == NULL
1357 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1358 {
1359 if (!hooks_ran)
1360 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001361 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001362 hooks_ran = true;
1363 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001364 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001365 {
1366 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1367 any_thread_matched = true;
1368 }
1369
Jim Inghamc54840c2011-03-22 01:47:27 +00001370 if (print_thread_header)
1371 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001372
1373 bool stop_on_continue = true;
1374 bool stop_on_error = true;
1375 bool echo_commands = false;
1376 bool print_results = true;
1377 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001378 &exc_ctx_with_reasons[i],
1379 stop_on_continue,
1380 stop_on_error,
1381 echo_commands,
1382 print_results,
1383 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001384
1385 // If the command started the target going again, we should bag out of
1386 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001387 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1388 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001389 {
1390 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1391 keep_going = false;
1392 }
1393 }
1394 }
1395 }
1396 if (hooks_ran)
1397 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001398
1399 result.GetImmediateOutputStream()->Flush();
1400 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001401}
1402
Greg Claytonbbea1332011-07-08 00:48:09 +00001403bool
1404Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1405{
1406 bool changed = false;
1407 if (module)
1408 {
1409 ObjectFile *object_file = module->GetObjectFile();
1410 if (object_file)
1411 {
1412 SectionList *section_list = object_file->GetSectionList ();
1413 if (section_list)
1414 {
1415 // All sections listed in the dyld image info structure will all
1416 // either be fixed up already, or they will all be off by a single
1417 // slide amount that is determined by finding the first segment
1418 // that is at file offset zero which also has bytes (a file size
1419 // that is greater than zero) in the object file.
1420
1421 // Determine the slide amount (if any)
1422 const size_t num_sections = section_list->GetSize();
1423 size_t sect_idx = 0;
1424 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1425 {
1426 // Iterate through the object file sections to find the
1427 // first section that starts of file offset zero and that
1428 // has bytes in the file...
1429 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1430 if (section)
1431 {
1432 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1433 changed = true;
1434 }
1435 }
1436 }
1437 }
1438 }
1439 return changed;
1440}
1441
1442
Jim Inghamd60d94a2011-03-11 03:53:59 +00001443//--------------------------------------------------------------
1444// class Target::StopHook
1445//--------------------------------------------------------------
1446
1447
1448Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1449 UserID (uid),
1450 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001451 m_commands (),
1452 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001453 m_thread_spec_ap(NULL),
1454 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001455{
1456}
1457
1458Target::StopHook::StopHook (const StopHook &rhs) :
1459 UserID (rhs.GetID()),
1460 m_target_sp (rhs.m_target_sp),
1461 m_commands (rhs.m_commands),
1462 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001463 m_thread_spec_ap (NULL),
1464 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001465{
1466 if (rhs.m_thread_spec_ap.get() != NULL)
1467 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1468}
1469
1470
1471Target::StopHook::~StopHook ()
1472{
1473}
1474
1475void
1476Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1477{
1478 m_thread_spec_ap.reset (specifier);
1479}
1480
1481
1482void
1483Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1484{
1485 int indent_level = s->GetIndentLevel();
1486
1487 s->SetIndentLevel(indent_level + 2);
1488
1489 s->Printf ("Hook: %d\n", GetID());
1490 if (m_active)
1491 s->Indent ("State: enabled\n");
1492 else
1493 s->Indent ("State: disabled\n");
1494
1495 if (m_specifier_sp)
1496 {
1497 s->Indent();
1498 s->PutCString ("Specifier:\n");
1499 s->SetIndentLevel (indent_level + 4);
1500 m_specifier_sp->GetDescription (s, level);
1501 s->SetIndentLevel (indent_level + 2);
1502 }
1503
1504 if (m_thread_spec_ap.get() != NULL)
1505 {
1506 StreamString tmp;
1507 s->Indent("Thread:\n");
1508 m_thread_spec_ap->GetDescription (&tmp, level);
1509 s->SetIndentLevel (indent_level + 4);
1510 s->Indent (tmp.GetData());
1511 s->PutCString ("\n");
1512 s->SetIndentLevel (indent_level + 2);
1513 }
1514
1515 s->Indent ("Commands: \n");
1516 s->SetIndentLevel (indent_level + 4);
1517 uint32_t num_commands = m_commands.GetSize();
1518 for (uint32_t i = 0; i < num_commands; i++)
1519 {
1520 s->Indent(m_commands.GetStringAtIndex(i));
1521 s->PutCString ("\n");
1522 }
1523 s->SetIndentLevel (indent_level);
1524}
1525
1526
Caroline Tice5bc8c972010-09-20 20:44:43 +00001527//--------------------------------------------------------------
1528// class Target::SettingsController
1529//--------------------------------------------------------------
1530
1531Target::SettingsController::SettingsController () :
1532 UserSettingsController ("target", Debugger::GetSettingsController()),
1533 m_default_architecture ()
1534{
1535 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1536 InstanceSettings::GetDefaultName().AsCString()));
1537}
1538
1539Target::SettingsController::~SettingsController ()
1540{
1541}
1542
1543lldb::InstanceSettingsSP
1544Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1545{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001546 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1547 false,
1548 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001549 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1550 return new_settings_sp;
1551}
1552
Caroline Tice5bc8c972010-09-20 20:44:43 +00001553
Jim Inghame41494a2011-04-16 00:01:13 +00001554#define TSC_DEFAULT_ARCH "default-arch"
1555#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001556#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001557#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001558#define TSC_SOURCE_MAP "source-map"
Greg Claytond284b662011-02-18 01:44:25 +00001559
1560
1561static const ConstString &
1562GetSettingNameForDefaultArch ()
1563{
1564 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001565 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001566}
1567
Greg Claytond284b662011-02-18 01:44:25 +00001568static const ConstString &
1569GetSettingNameForExpressionPrefix ()
1570{
1571 static ConstString g_const_string (TSC_EXPR_PREFIX);
1572 return g_const_string;
1573}
1574
1575static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001576GetSettingNameForPreferDynamicValue ()
1577{
1578 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1579 return g_const_string;
1580}
1581
Greg Claytonff44ab42011-04-23 02:04:55 +00001582static const ConstString &
1583GetSettingNameForSourcePathMap ()
1584{
1585 static ConstString g_const_string (TSC_SOURCE_MAP);
1586 return g_const_string;
1587}
Greg Claytond284b662011-02-18 01:44:25 +00001588
Greg Clayton17cd9952011-04-22 03:55:06 +00001589static const ConstString &
1590GetSettingNameForSkipPrologue ()
1591{
1592 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1593 return g_const_string;
1594}
1595
1596
1597
Caroline Tice5bc8c972010-09-20 20:44:43 +00001598bool
1599Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1600 const char *index_value,
1601 const char *value,
1602 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001603 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001604 Error&err)
1605{
Greg Claytond284b662011-02-18 01:44:25 +00001606 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001607 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001608 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001609 if (!m_default_architecture.IsValid())
1610 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001611 }
1612 return true;
1613}
1614
1615
1616bool
1617Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1618 StringList &value,
1619 Error &err)
1620{
Greg Claytond284b662011-02-18 01:44:25 +00001621 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001622 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001623 // If the arch is invalid (the default), don't show a string for it
1624 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001625 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001626 return true;
1627 }
1628 else
1629 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1630
1631 return false;
1632}
1633
1634//--------------------------------------------------------------
1635// class TargetInstanceSettings
1636//--------------------------------------------------------------
1637
Greg Clayton638351a2010-12-04 00:10:17 +00001638TargetInstanceSettings::TargetInstanceSettings
1639(
1640 UserSettingsController &owner,
1641 bool live_instance,
1642 const char *name
1643) :
Greg Claytond284b662011-02-18 01:44:25 +00001644 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001645 m_expr_prefix_file (),
1646 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001647 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001648 m_skip_prologue (true, true),
1649 m_source_map (NULL, NULL)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001650{
1651 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1652 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1653 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1654 // This is true for CreateInstanceName() too.
1655
1656 if (GetInstanceName () == InstanceSettings::InvalidName())
1657 {
1658 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1659 m_owner.RegisterInstanceSettings (this);
1660 }
1661
1662 if (live_instance)
1663 {
1664 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1665 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001666 }
1667}
1668
1669TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001670 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1671 m_expr_prefix_file (rhs.m_expr_prefix_file),
1672 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1673 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1674 m_skip_prologue (rhs.m_skip_prologue),
1675 m_source_map (rhs.m_source_map)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001676{
1677 if (m_instance_name != InstanceSettings::GetDefaultName())
1678 {
1679 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1680 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001681 }
1682}
1683
1684TargetInstanceSettings::~TargetInstanceSettings ()
1685{
1686}
1687
1688TargetInstanceSettings&
1689TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1690{
1691 if (this != &rhs)
1692 {
1693 }
1694
1695 return *this;
1696}
1697
Caroline Tice5bc8c972010-09-20 20:44:43 +00001698void
1699TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1700 const char *index_value,
1701 const char *value,
1702 const ConstString &instance_name,
1703 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001704 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001705 Error &err,
1706 bool pending)
1707{
Greg Claytond284b662011-02-18 01:44:25 +00001708 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001709 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001710 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1711 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001712 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001713 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00001714 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001715 default:
1716 break;
1717 case eVarSetOperationAssign:
1718 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001719 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001720 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1721 {
1722 err.SetErrorToGenericError ();
1723 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1724 return;
1725 }
1726
1727 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1728
1729 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1730 {
1731 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1732 m_expr_prefix_contents_sp.reset();
1733 }
Sean Callanan77e93942010-10-29 00:29:03 +00001734 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001735 break;
1736 case eVarSetOperationClear:
1737 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00001738 }
Sean Callanan77e93942010-10-29 00:29:03 +00001739 }
1740 }
Jim Inghame41494a2011-04-16 00:01:13 +00001741 else if (var_name == GetSettingNameForPreferDynamicValue())
1742 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001743 int new_value;
1744 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1745 if (err.Success())
1746 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00001747 }
1748 else if (var_name == GetSettingNameForSkipPrologue())
1749 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001750 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1751 }
1752 else if (var_name == GetSettingNameForSourcePathMap ())
1753 {
1754 switch (op)
1755 {
1756 case eVarSetOperationReplace:
1757 case eVarSetOperationInsertBefore:
1758 case eVarSetOperationInsertAfter:
1759 case eVarSetOperationRemove:
1760 default:
1761 break;
1762 case eVarSetOperationAssign:
1763 m_source_map.Clear(true);
1764 // Fall through to append....
1765 case eVarSetOperationAppend:
1766 {
1767 Args args(value);
1768 const uint32_t argc = args.GetArgumentCount();
1769 if (argc & 1 || argc == 0)
1770 {
1771 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1772 }
1773 else
1774 {
1775 char resolved_new_path[PATH_MAX];
1776 FileSpec file_spec;
1777 const char *old_path;
1778 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1779 {
1780 const char *new_path = args.GetArgumentAtIndex(idx+1);
1781 assert (new_path); // We have an even number of paths, this shouldn't happen!
1782
1783 file_spec.SetFile(new_path, true);
1784 if (file_spec.Exists())
1785 {
1786 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1787 {
1788 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1789 return;
1790 }
1791 }
1792 else
1793 {
1794 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1795 return;
1796 }
1797 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1798 }
1799 }
1800 }
1801 break;
1802
1803 case eVarSetOperationClear:
1804 m_source_map.Clear(true);
1805 break;
1806 }
Jim Inghame41494a2011-04-16 00:01:13 +00001807 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001808}
1809
1810void
Greg Claytond284b662011-02-18 01:44:25 +00001811TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001812{
Sean Callanan77e93942010-10-29 00:29:03 +00001813 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1814
1815 if (!new_settings_ptr)
1816 return;
1817
Greg Claytonff44ab42011-04-23 02:04:55 +00001818 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1819 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1820 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1821 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001822}
1823
Caroline Ticebcb5b452010-09-20 21:37:42 +00001824bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001825TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1826 const ConstString &var_name,
1827 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001828 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001829{
Greg Claytond284b662011-02-18 01:44:25 +00001830 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001831 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001832 char path[PATH_MAX];
1833 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1834 if (path_len > 0)
1835 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00001836 }
Jim Inghame41494a2011-04-16 00:01:13 +00001837 else if (var_name == GetSettingNameForPreferDynamicValue())
1838 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001839 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00001840 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001841 else if (var_name == GetSettingNameForSkipPrologue())
1842 {
1843 if (m_skip_prologue)
1844 value.AppendString ("true");
1845 else
1846 value.AppendString ("false");
1847 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001848 else if (var_name == GetSettingNameForSourcePathMap ())
1849 {
1850 }
Sean Callanan77e93942010-10-29 00:29:03 +00001851 else
1852 {
1853 if (err)
1854 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1855 return false;
1856 }
1857
1858 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001859}
1860
1861const ConstString
1862TargetInstanceSettings::CreateInstanceName ()
1863{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001864 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001865 static int instance_count = 1;
1866
Caroline Tice5bc8c972010-09-20 20:44:43 +00001867 sstr.Printf ("target_%d", instance_count);
1868 ++instance_count;
1869
1870 const ConstString ret_val (sstr.GetData());
1871 return ret_val;
1872}
1873
1874//--------------------------------------------------
1875// Target::SettingsController Variable Tables
1876//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00001877OptionEnumValueElement
1878TargetInstanceSettings::g_dynamic_value_types[] =
1879{
Greg Clayton577fbc32011-05-30 00:39:48 +00001880{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
1881{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
1882{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00001883{ 0, NULL, NULL }
1884};
Caroline Tice5bc8c972010-09-20 20:44:43 +00001885
1886SettingEntry
1887Target::SettingsController::global_settings_table[] =
1888{
Greg Claytond284b662011-02-18 01:44:25 +00001889 // var-name var-type default enum init'd hidden help-text
1890 // ================= ================== =========== ==== ====== ====== =========================================================================
1891 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1892 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1893};
1894
Caroline Tice5bc8c972010-09-20 20:44:43 +00001895SettingEntry
1896Target::SettingsController::instance_settings_table[] =
1897{
Jim Ingham10de7d12011-05-04 03:43:18 +00001898 // var-name var-type default enum init'd hidden help-text
1899 // ================= ================== =============== ======================= ====== ====== =========================================================================
1900 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
Greg Clayton577fbc32011-05-30 00:39:48 +00001901 { TSC_PREFER_DYNAMIC, eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
1902 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
1903 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
Jim Ingham10de7d12011-05-04 03:43:18 +00001904 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001905};