blob: a5103b483ed96bfd9ad29f1ee102bc82773c892c [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
Chris Lattner24943d22010-06-08 16:52:24 +0000468 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000469
470 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000471}
472
473
Jim Ingham7508e732010-08-09 23:31:02 +0000474bool
475Target::SetArchitecture (const ArchSpec &arch_spec)
476{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000477 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000478 {
479 // If we're setting the architecture to our current architecture, we
480 // don't need to do anything.
481 return true;
482 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000483 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000484 {
485 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000486 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000487 return true;
488 }
489 else
490 {
491 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000492 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000493 ModuleSP executable_sp = GetExecutableModule ();
494 m_images.Clear();
495 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000496 // Need to do something about unsetting breakpoints.
497
498 if (executable_sp)
499 {
500 FileSpec exec_file_spec = executable_sp->GetFileSpec();
501 Error error = ModuleList::GetSharedModule(exec_file_spec,
502 arch_spec,
503 NULL,
504 NULL,
505 0,
506 executable_sp,
507 NULL,
508 NULL);
509
510 if (!error.Fail() && executable_sp)
511 {
512 SetExecutableModule (executable_sp, true);
513 return true;
514 }
515 else
516 {
517 return false;
518 }
519 }
520 else
521 {
522 return false;
523 }
524 }
525}
Chris Lattner24943d22010-06-08 16:52:24 +0000526
Chris Lattner24943d22010-06-08 16:52:24 +0000527void
528Target::ModuleAdded (ModuleSP &module_sp)
529{
530 // A module is being added to this target for the first time
531 ModuleList module_list;
532 module_list.Append(module_sp);
533 ModulesDidLoad (module_list);
534}
535
536void
537Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
538{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000539 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000540 ModuleList module_list;
541 module_list.Append (old_module_sp);
542 ModulesDidUnload (module_list);
543 module_list.Clear ();
544 module_list.Append (new_module_sp);
545 ModulesDidLoad (module_list);
546}
547
548void
549Target::ModulesDidLoad (ModuleList &module_list)
550{
551 m_breakpoint_list.UpdateBreakpoints (module_list, true);
552 // TODO: make event data that packages up the module_list
553 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
554}
555
556void
557Target::ModulesDidUnload (ModuleList &module_list)
558{
559 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000560
561 // Remove the images from the target image list
562 m_images.Remove(module_list);
563
Chris Lattner24943d22010-06-08 16:52:24 +0000564 // TODO: make event data that packages up the module_list
565 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
566}
567
568size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000569Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
570{
571 const Section *section = addr.GetSection();
572 if (section && section->GetModule())
573 {
574 ObjectFile *objfile = section->GetModule()->GetObjectFile();
575 if (objfile)
576 {
577 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
578 addr.GetOffset(),
579 dst,
580 dst_len);
581 if (bytes_read > 0)
582 return bytes_read;
583 else
584 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
585 }
586 else
587 {
588 error.SetErrorString("address isn't from a object file");
589 }
590 }
591 else
592 {
593 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
594 }
595 return 0;
596}
597
598size_t
599Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000600{
Chris Lattner24943d22010-06-08 16:52:24 +0000601 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000602
Greg Clayton70436352010-06-30 23:03:03 +0000603 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
604
Greg Clayton26100dc2011-01-07 01:57:07 +0000605 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000606
607 addr_t load_addr = LLDB_INVALID_ADDRESS;
608 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000609 Address resolved_addr;
610 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000611 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000612 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000613 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000614 // No sections are loaded, so we must assume we are not running
615 // yet and anything we are given is a file address.
616 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
617 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000618 }
Greg Clayton70436352010-06-30 23:03:03 +0000619 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000620 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000621 // We have at least one section loaded. This can be becuase
622 // we have manually loaded some sections with "target modules load ..."
623 // or because we have have a live process that has sections loaded
624 // through the dynamic loader
625 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
626 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000627 }
Greg Clayton70436352010-06-30 23:03:03 +0000628 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000629 if (!resolved_addr.IsValid())
630 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000631
Greg Clayton9b82f862011-07-11 05:12:02 +0000632
Greg Clayton26100dc2011-01-07 01:57:07 +0000633 if (prefer_file_cache)
634 {
635 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
636 if (bytes_read > 0)
637 return bytes_read;
638 }
Greg Clayton70436352010-06-30 23:03:03 +0000639
640 if (process_is_valid)
641 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000642 if (load_addr == LLDB_INVALID_ADDRESS)
643 load_addr = resolved_addr.GetLoadAddress (this);
644
Greg Clayton70436352010-06-30 23:03:03 +0000645 if (load_addr == LLDB_INVALID_ADDRESS)
646 {
647 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
648 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
649 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
650 resolved_addr.GetFileAddress());
651 else
652 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
653 }
654 else
655 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000656 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000657 if (bytes_read != dst_len)
658 {
659 if (error.Success())
660 {
661 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000662 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000663 else
Greg Clayton70436352010-06-30 23:03:03 +0000664 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 +0000665 }
666 }
Greg Clayton70436352010-06-30 23:03:03 +0000667 if (bytes_read)
668 return bytes_read;
669 // If the address is not section offset we have an address that
670 // doesn't resolve to any address in any currently loaded shared
671 // libaries and we failed to read memory so there isn't anything
672 // more we can do. If it is section offset, we might be able to
673 // read cached memory from the object file.
674 if (!resolved_addr.IsSectionOffset())
675 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000676 }
Chris Lattner24943d22010-06-08 16:52:24 +0000677 }
Greg Clayton70436352010-06-30 23:03:03 +0000678
Greg Clayton9b82f862011-07-11 05:12:02 +0000679 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000680 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000681 // If we didn't already try and read from the object file cache, then
682 // try it after failing to read from the process.
683 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000684 }
685 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000686}
687
Greg Clayton7dd98df2011-07-12 17:06:17 +0000688size_t
689Target::ReadScalarIntegerFromMemory (const Address& addr,
690 bool prefer_file_cache,
691 uint32_t byte_size,
692 bool is_signed,
693 Scalar &scalar,
694 Error &error)
695{
696 uint64_t uval;
697
698 if (byte_size <= sizeof(uval))
699 {
700 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
701 if (bytes_read == byte_size)
702 {
703 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
704 uint32_t offset = 0;
705 if (byte_size <= 4)
706 scalar = data.GetMaxU32 (&offset, byte_size);
707 else
708 scalar = data.GetMaxU64 (&offset, byte_size);
709
710 if (is_signed)
711 scalar.SignExtend(byte_size * 8);
712 return bytes_read;
713 }
714 }
715 else
716 {
717 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
718 }
719 return 0;
720}
721
722uint64_t
723Target::ReadUnsignedIntegerFromMemory (const Address& addr,
724 bool prefer_file_cache,
725 size_t integer_byte_size,
726 uint64_t fail_value,
727 Error &error)
728{
729 Scalar scalar;
730 if (ReadScalarIntegerFromMemory (addr,
731 prefer_file_cache,
732 integer_byte_size,
733 false,
734 scalar,
735 error))
736 return scalar.ULongLong(fail_value);
737 return fail_value;
738}
739
740bool
741Target::ReadPointerFromMemory (const Address& addr,
742 bool prefer_file_cache,
743 Error &error,
744 Address &pointer_addr)
745{
746 Scalar scalar;
747 if (ReadScalarIntegerFromMemory (addr,
748 prefer_file_cache,
749 m_arch.GetAddressByteSize(),
750 false,
751 scalar,
752 error))
753 {
754 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
755 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
756 {
757 if (m_section_load_list.IsEmpty())
758 {
759 // No sections are loaded, so we must assume we are not running
760 // yet and anything we are given is a file address.
761 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
762 }
763 else
764 {
765 // We have at least one section loaded. This can be becuase
766 // we have manually loaded some sections with "target modules load ..."
767 // or because we have have a live process that has sections loaded
768 // through the dynamic loader
769 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
770 }
771 // We weren't able to resolve the pointer value, so just return
772 // an address with no section
773 if (!pointer_addr.IsValid())
774 pointer_addr.SetOffset (pointer_vm_addr);
775 return true;
776
777 }
778 }
779 return false;
780}
Chris Lattner24943d22010-06-08 16:52:24 +0000781
782ModuleSP
783Target::GetSharedModule
784(
785 const FileSpec& file_spec,
786 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000787 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000788 const ConstString *object_name,
789 off_t object_offset,
790 Error *error_ptr
791)
792{
793 // Don't pass in the UUID so we can tell if we have a stale value in our list
794 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
795 bool did_create_module = false;
796 ModuleSP module_sp;
797
Chris Lattner24943d22010-06-08 16:52:24 +0000798 Error error;
799
Greg Clayton24bc5d92011-03-30 18:16:51 +0000800 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000801 if (m_image_search_paths.GetSize())
802 {
803 FileSpec transformed_spec;
804 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
805 {
806 transformed_spec.GetFilename() = file_spec.GetFilename();
807 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
808 }
809 }
810
Greg Clayton24bc5d92011-03-30 18:16:51 +0000811 // The platform is responsible for finding and caching an appropriate
812 // module in the shared module cache.
813 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000814 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000815 FileSpec platform_file_spec;
816 error = m_platform_sp->GetSharedModule (file_spec,
817 arch,
818 uuid_ptr,
819 object_name,
820 object_offset,
821 module_sp,
822 &old_module_sp,
823 &did_create_module);
824 }
825 else
826 {
827 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +0000828 }
829
Greg Clayton24bc5d92011-03-30 18:16:51 +0000830 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000831 if (module_sp)
832 {
833 m_images.Append (module_sp);
834 if (did_create_module)
835 {
836 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
837 ModuleUpdated(old_module_sp, module_sp);
838 else
839 ModuleAdded(module_sp);
840 }
841 }
842 if (error_ptr)
843 *error_ptr = error;
844 return module_sp;
845}
846
847
848Target *
849Target::CalculateTarget ()
850{
851 return this;
852}
853
854Process *
855Target::CalculateProcess ()
856{
857 return NULL;
858}
859
860Thread *
861Target::CalculateThread ()
862{
863 return NULL;
864}
865
866StackFrame *
867Target::CalculateStackFrame ()
868{
869 return NULL;
870}
871
872void
Greg Claytona830adb2010-10-04 01:05:56 +0000873Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000874{
875 exe_ctx.target = this;
876 exe_ctx.process = NULL; // Do NOT fill in process...
877 exe_ctx.thread = NULL;
878 exe_ctx.frame = NULL;
879}
880
881PathMappingList &
882Target::GetImageSearchPathList ()
883{
884 return m_image_search_paths;
885}
886
887void
888Target::ImageSearchPathsChanged
889(
890 const PathMappingList &path_list,
891 void *baton
892)
893{
894 Target *target = (Target *)baton;
895 if (target->m_images.GetSize() > 1)
896 {
897 ModuleSP exe_module_sp (target->GetExecutableModule());
898 if (exe_module_sp)
899 {
900 target->m_images.Clear();
901 target->SetExecutableModule (exe_module_sp, true);
902 }
903 }
904}
905
906ClangASTContext *
907Target::GetScratchClangASTContext()
908{
Greg Clayton34ce4ea2011-08-03 01:23:55 +0000909 // Now see if we know the target triple, and if so, create our scratch AST context:
910 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
911 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000912 return m_scratch_ast_context_ap.get();
913}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000914
Greg Clayton990de7b2010-11-18 23:32:35 +0000915void
Caroline Tice2a456812011-03-10 22:14:10 +0000916Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000917{
Greg Clayton990de7b2010-11-18 23:32:35 +0000918 UserSettingsControllerSP &usc = GetSettingsController();
919 usc.reset (new SettingsController);
920 UserSettingsController::InitializeSettingsController (usc,
921 SettingsController::global_settings_table,
922 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000923
924 // Now call SettingsInitialize() on each 'child' setting of Target
925 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000926}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000927
Greg Clayton990de7b2010-11-18 23:32:35 +0000928void
Caroline Tice2a456812011-03-10 22:14:10 +0000929Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000930{
Caroline Tice2a456812011-03-10 22:14:10 +0000931
932 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
933
934 Process::SettingsTerminate ();
935
936 // Now terminate Target Settings.
937
Greg Clayton990de7b2010-11-18 23:32:35 +0000938 UserSettingsControllerSP &usc = GetSettingsController();
939 UserSettingsController::FinalizeSettingsController (usc);
940 usc.reset();
941}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000942
Greg Clayton990de7b2010-11-18 23:32:35 +0000943UserSettingsControllerSP &
944Target::GetSettingsController ()
945{
946 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000947 return g_settings_controller;
948}
949
950ArchSpec
951Target::GetDefaultArchitecture ()
952{
Greg Clayton940b1032011-02-23 00:35:02 +0000953 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
954
955 if (settings_controller_sp)
956 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
957 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000958}
959
960void
Greg Clayton940b1032011-02-23 00:35:02 +0000961Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000962{
Greg Clayton940b1032011-02-23 00:35:02 +0000963 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
964
965 if (settings_controller_sp)
966 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000967}
968
Greg Claytona830adb2010-10-04 01:05:56 +0000969Target *
970Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
971{
972 // The target can either exist in the "process" of ExecutionContext, or in
973 // the "target_sp" member of SymbolContext. This accessor helper function
974 // will get the target from one of these locations.
975
976 Target *target = NULL;
977 if (sc_ptr != NULL)
978 target = sc_ptr->target_sp.get();
979 if (target == NULL)
980 {
981 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
982 target = &exe_ctx_ptr->process->GetTarget();
983 }
984 return target;
985}
986
987
Caroline Tice1ebef442010-09-27 00:30:10 +0000988void
989Target::UpdateInstanceName ()
990{
991 StreamString sstr;
992
993 ModuleSP module_sp = GetExecutableModule();
994 if (module_sp)
995 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000996 sstr.Printf ("%s_%s",
997 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton940b1032011-02-23 00:35:02 +0000998 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000999 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
1000 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001001 }
1002}
1003
Sean Callanan77e93942010-10-29 00:29:03 +00001004const char *
1005Target::GetExpressionPrefixContentsAsCString ()
1006{
Greg Claytonff44ab42011-04-23 02:04:55 +00001007 if (m_expr_prefix_contents_sp)
1008 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1009 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001010}
1011
Greg Clayton427f2902010-12-14 02:59:59 +00001012ExecutionResults
1013Target::EvaluateExpression
1014(
1015 const char *expr_cstr,
1016 StackFrame *frame,
1017 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001018 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001019 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001020 lldb::ValueObjectSP &result_valobj_sp
1021)
1022{
1023 ExecutionResults execution_results = eExecutionSetupError;
1024
1025 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001026
1027 // We shouldn't run stop hooks in expressions.
1028 // Be sure to reset this if you return anywhere within this function.
1029 bool old_suppress_value = m_suppress_stop_hooks;
1030 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001031
1032 ExecutionContext exe_ctx;
1033 if (frame)
1034 {
1035 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001036 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001037 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001038 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1039 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001040 lldb::VariableSP var_sp;
1041 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1042 use_dynamic,
1043 expr_path_options,
1044 var_sp,
1045 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001046 }
1047 else if (m_process_sp)
1048 {
1049 m_process_sp->CalculateExecutionContext(exe_ctx);
1050 }
1051 else
1052 {
1053 CalculateExecutionContext(exe_ctx);
1054 }
1055
1056 if (result_valobj_sp)
1057 {
1058 execution_results = eExecutionCompleted;
1059 // We got a result from the frame variable expression path above...
1060 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1061
1062 lldb::ValueObjectSP const_valobj_sp;
1063
1064 // Check in case our value is already a constant value
1065 if (result_valobj_sp->GetIsConstant())
1066 {
1067 const_valobj_sp = result_valobj_sp;
1068 const_valobj_sp->SetName (persistent_variable_name);
1069 }
1070 else
Jim Inghame41494a2011-04-16 00:01:13 +00001071 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001072 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001073 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001074 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001075 if (dynamic_sp)
1076 result_valobj_sp = dynamic_sp;
1077 }
1078
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001079 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001080 }
Greg Clayton427f2902010-12-14 02:59:59 +00001081
Sean Callanan6a925532011-01-13 08:53:35 +00001082 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1083
Greg Clayton427f2902010-12-14 02:59:59 +00001084 result_valobj_sp = const_valobj_sp;
1085
Sean Callanan6a925532011-01-13 08:53:35 +00001086 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1087 assert (clang_expr_variable_sp.get());
1088
1089 // Set flags and live data as appropriate
1090
1091 const Value &result_value = live_valobj_sp->GetValue();
1092
1093 switch (result_value.GetValueType())
1094 {
1095 case Value::eValueTypeHostAddress:
1096 case Value::eValueTypeFileAddress:
1097 // we don't do anything with these for now
1098 break;
1099 case Value::eValueTypeScalar:
1100 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1101 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1102 break;
1103 case Value::eValueTypeLoadAddress:
1104 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1105 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1106 break;
1107 }
Greg Clayton427f2902010-12-14 02:59:59 +00001108 }
1109 else
1110 {
1111 // Make sure we aren't just trying to see the value of a persistent
1112 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001113 lldb::ClangExpressionVariableSP persistent_var_sp;
1114 // Only check for persistent variables the expression starts with a '$'
1115 if (expr_cstr[0] == '$')
1116 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1117
Greg Clayton427f2902010-12-14 02:59:59 +00001118 if (persistent_var_sp)
1119 {
1120 result_valobj_sp = persistent_var_sp->GetValueObject ();
1121 execution_results = eExecutionCompleted;
1122 }
1123 else
1124 {
1125 const char *prefix = GetExpressionPrefixContentsAsCString();
1126
1127 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +00001128 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001129 expr_cstr,
1130 prefix,
1131 result_valobj_sp);
1132 }
1133 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001134
1135 m_suppress_stop_hooks = old_suppress_value;
1136
Greg Clayton427f2902010-12-14 02:59:59 +00001137 return execution_results;
1138}
1139
Greg Claytonc0fa5332011-05-22 22:46:53 +00001140lldb::addr_t
1141Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1142{
1143 addr_t code_addr = load_addr;
1144 switch (m_arch.GetMachine())
1145 {
1146 case llvm::Triple::arm:
1147 case llvm::Triple::thumb:
1148 switch (addr_class)
1149 {
1150 case eAddressClassData:
1151 case eAddressClassDebug:
1152 return LLDB_INVALID_ADDRESS;
1153
1154 case eAddressClassUnknown:
1155 case eAddressClassInvalid:
1156 case eAddressClassCode:
1157 case eAddressClassCodeAlternateISA:
1158 case eAddressClassRuntime:
1159 // Check if bit zero it no set?
1160 if ((code_addr & 1ull) == 0)
1161 {
1162 // Bit zero isn't set, check if the address is a multiple of 2?
1163 if (code_addr & 2ull)
1164 {
1165 // The address is a multiple of 2 so it must be thumb, set bit zero
1166 code_addr |= 1ull;
1167 }
1168 else if (addr_class == eAddressClassCodeAlternateISA)
1169 {
1170 // We checked the address and the address claims to be the alternate ISA
1171 // which means thumb, so set bit zero.
1172 code_addr |= 1ull;
1173 }
1174 }
1175 break;
1176 }
1177 break;
1178
1179 default:
1180 break;
1181 }
1182 return code_addr;
1183}
1184
1185lldb::addr_t
1186Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1187{
1188 addr_t opcode_addr = load_addr;
1189 switch (m_arch.GetMachine())
1190 {
1191 case llvm::Triple::arm:
1192 case llvm::Triple::thumb:
1193 switch (addr_class)
1194 {
1195 case eAddressClassData:
1196 case eAddressClassDebug:
1197 return LLDB_INVALID_ADDRESS;
1198
1199 case eAddressClassInvalid:
1200 case eAddressClassUnknown:
1201 case eAddressClassCode:
1202 case eAddressClassCodeAlternateISA:
1203 case eAddressClassRuntime:
1204 opcode_addr &= ~(1ull);
1205 break;
1206 }
1207 break;
1208
1209 default:
1210 break;
1211 }
1212 return opcode_addr;
1213}
1214
Jim Inghamd60d94a2011-03-11 03:53:59 +00001215lldb::user_id_t
1216Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1217{
1218 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1219 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1220 m_stop_hooks[new_uid] = new_hook_sp;
1221 return new_uid;
1222}
1223
1224bool
1225Target::RemoveStopHookByID (lldb::user_id_t user_id)
1226{
1227 size_t num_removed;
1228 num_removed = m_stop_hooks.erase (user_id);
1229 if (num_removed == 0)
1230 return false;
1231 else
1232 return true;
1233}
1234
1235void
1236Target::RemoveAllStopHooks ()
1237{
1238 m_stop_hooks.clear();
1239}
1240
1241Target::StopHookSP
1242Target::GetStopHookByID (lldb::user_id_t user_id)
1243{
1244 StopHookSP found_hook;
1245
1246 StopHookCollection::iterator specified_hook_iter;
1247 specified_hook_iter = m_stop_hooks.find (user_id);
1248 if (specified_hook_iter != m_stop_hooks.end())
1249 found_hook = (*specified_hook_iter).second;
1250 return found_hook;
1251}
1252
1253bool
1254Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1255{
1256 StopHookCollection::iterator specified_hook_iter;
1257 specified_hook_iter = m_stop_hooks.find (user_id);
1258 if (specified_hook_iter == m_stop_hooks.end())
1259 return false;
1260
1261 (*specified_hook_iter).second->SetIsActive (active_state);
1262 return true;
1263}
1264
1265void
1266Target::SetAllStopHooksActiveState (bool active_state)
1267{
1268 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1269 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1270 {
1271 (*pos).second->SetIsActive (active_state);
1272 }
1273}
1274
1275void
1276Target::RunStopHooks ()
1277{
Jim Ingham3613ae12011-05-12 02:06:14 +00001278 if (m_suppress_stop_hooks)
1279 return;
1280
Jim Inghamd60d94a2011-03-11 03:53:59 +00001281 if (!m_process_sp)
1282 return;
1283
1284 if (m_stop_hooks.empty())
1285 return;
1286
1287 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1288
1289 // If there aren't any active stop hooks, don't bother either:
1290 bool any_active_hooks = false;
1291 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1292 {
1293 if ((*pos).second->IsActive())
1294 {
1295 any_active_hooks = true;
1296 break;
1297 }
1298 }
1299 if (!any_active_hooks)
1300 return;
1301
1302 CommandReturnObject result;
1303
1304 std::vector<ExecutionContext> exc_ctx_with_reasons;
1305 std::vector<SymbolContext> sym_ctx_with_reasons;
1306
1307 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1308 size_t num_threads = cur_threadlist.GetSize();
1309 for (size_t i = 0; i < num_threads; i++)
1310 {
1311 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1312 if (cur_thread_sp->ThreadStoppedForAReason())
1313 {
1314 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1315 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1316 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1317 }
1318 }
1319
1320 // If no threads stopped for a reason, don't run the stop-hooks.
1321 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1322 if (num_exe_ctx == 0)
1323 return;
1324
Jim Inghame5ed8e92011-06-02 23:58:26 +00001325 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1326 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001327
1328 bool keep_going = true;
1329 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001330 bool print_hook_header;
1331 bool print_thread_header;
1332
1333 if (num_exe_ctx == 1)
1334 print_thread_header = false;
1335 else
1336 print_thread_header = true;
1337
1338 if (m_stop_hooks.size() == 1)
1339 print_hook_header = false;
1340 else
1341 print_hook_header = true;
1342
Jim Inghamd60d94a2011-03-11 03:53:59 +00001343 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1344 {
1345 // result.Clear();
1346 StopHookSP cur_hook_sp = (*pos).second;
1347 if (!cur_hook_sp->IsActive())
1348 continue;
1349
1350 bool any_thread_matched = false;
1351 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1352 {
1353 if ((cur_hook_sp->GetSpecifier () == NULL
1354 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1355 && (cur_hook_sp->GetThreadSpecifier() == NULL
1356 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1357 {
1358 if (!hooks_ran)
1359 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001360 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001361 hooks_ran = true;
1362 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001363 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001364 {
1365 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1366 any_thread_matched = true;
1367 }
1368
Jim Inghamc54840c2011-03-22 01:47:27 +00001369 if (print_thread_header)
1370 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001371
1372 bool stop_on_continue = true;
1373 bool stop_on_error = true;
1374 bool echo_commands = false;
1375 bool print_results = true;
1376 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001377 &exc_ctx_with_reasons[i],
1378 stop_on_continue,
1379 stop_on_error,
1380 echo_commands,
1381 print_results,
1382 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001383
1384 // If the command started the target going again, we should bag out of
1385 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001386 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1387 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001388 {
1389 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1390 keep_going = false;
1391 }
1392 }
1393 }
1394 }
1395 if (hooks_ran)
1396 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001397
1398 result.GetImmediateOutputStream()->Flush();
1399 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001400}
1401
Greg Claytonbbea1332011-07-08 00:48:09 +00001402bool
1403Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1404{
1405 bool changed = false;
1406 if (module)
1407 {
1408 ObjectFile *object_file = module->GetObjectFile();
1409 if (object_file)
1410 {
1411 SectionList *section_list = object_file->GetSectionList ();
1412 if (section_list)
1413 {
1414 // All sections listed in the dyld image info structure will all
1415 // either be fixed up already, or they will all be off by a single
1416 // slide amount that is determined by finding the first segment
1417 // that is at file offset zero which also has bytes (a file size
1418 // that is greater than zero) in the object file.
1419
1420 // Determine the slide amount (if any)
1421 const size_t num_sections = section_list->GetSize();
1422 size_t sect_idx = 0;
1423 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1424 {
1425 // Iterate through the object file sections to find the
1426 // first section that starts of file offset zero and that
1427 // has bytes in the file...
1428 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1429 if (section)
1430 {
1431 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1432 changed = true;
1433 }
1434 }
1435 }
1436 }
1437 }
1438 return changed;
1439}
1440
1441
Jim Inghamd60d94a2011-03-11 03:53:59 +00001442//--------------------------------------------------------------
1443// class Target::StopHook
1444//--------------------------------------------------------------
1445
1446
1447Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1448 UserID (uid),
1449 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001450 m_commands (),
1451 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001452 m_thread_spec_ap(NULL),
1453 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001454{
1455}
1456
1457Target::StopHook::StopHook (const StopHook &rhs) :
1458 UserID (rhs.GetID()),
1459 m_target_sp (rhs.m_target_sp),
1460 m_commands (rhs.m_commands),
1461 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001462 m_thread_spec_ap (NULL),
1463 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001464{
1465 if (rhs.m_thread_spec_ap.get() != NULL)
1466 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1467}
1468
1469
1470Target::StopHook::~StopHook ()
1471{
1472}
1473
1474void
1475Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1476{
1477 m_thread_spec_ap.reset (specifier);
1478}
1479
1480
1481void
1482Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1483{
1484 int indent_level = s->GetIndentLevel();
1485
1486 s->SetIndentLevel(indent_level + 2);
1487
1488 s->Printf ("Hook: %d\n", GetID());
1489 if (m_active)
1490 s->Indent ("State: enabled\n");
1491 else
1492 s->Indent ("State: disabled\n");
1493
1494 if (m_specifier_sp)
1495 {
1496 s->Indent();
1497 s->PutCString ("Specifier:\n");
1498 s->SetIndentLevel (indent_level + 4);
1499 m_specifier_sp->GetDescription (s, level);
1500 s->SetIndentLevel (indent_level + 2);
1501 }
1502
1503 if (m_thread_spec_ap.get() != NULL)
1504 {
1505 StreamString tmp;
1506 s->Indent("Thread:\n");
1507 m_thread_spec_ap->GetDescription (&tmp, level);
1508 s->SetIndentLevel (indent_level + 4);
1509 s->Indent (tmp.GetData());
1510 s->PutCString ("\n");
1511 s->SetIndentLevel (indent_level + 2);
1512 }
1513
1514 s->Indent ("Commands: \n");
1515 s->SetIndentLevel (indent_level + 4);
1516 uint32_t num_commands = m_commands.GetSize();
1517 for (uint32_t i = 0; i < num_commands; i++)
1518 {
1519 s->Indent(m_commands.GetStringAtIndex(i));
1520 s->PutCString ("\n");
1521 }
1522 s->SetIndentLevel (indent_level);
1523}
1524
1525
Caroline Tice5bc8c972010-09-20 20:44:43 +00001526//--------------------------------------------------------------
1527// class Target::SettingsController
1528//--------------------------------------------------------------
1529
1530Target::SettingsController::SettingsController () :
1531 UserSettingsController ("target", Debugger::GetSettingsController()),
1532 m_default_architecture ()
1533{
1534 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1535 InstanceSettings::GetDefaultName().AsCString()));
1536}
1537
1538Target::SettingsController::~SettingsController ()
1539{
1540}
1541
1542lldb::InstanceSettingsSP
1543Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1544{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001545 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1546 false,
1547 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001548 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1549 return new_settings_sp;
1550}
1551
Caroline Tice5bc8c972010-09-20 20:44:43 +00001552
Jim Inghame41494a2011-04-16 00:01:13 +00001553#define TSC_DEFAULT_ARCH "default-arch"
1554#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001555#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001556#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001557#define TSC_SOURCE_MAP "source-map"
Greg Claytond284b662011-02-18 01:44:25 +00001558
1559
1560static const ConstString &
1561GetSettingNameForDefaultArch ()
1562{
1563 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001564 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001565}
1566
Greg Claytond284b662011-02-18 01:44:25 +00001567static const ConstString &
1568GetSettingNameForExpressionPrefix ()
1569{
1570 static ConstString g_const_string (TSC_EXPR_PREFIX);
1571 return g_const_string;
1572}
1573
1574static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001575GetSettingNameForPreferDynamicValue ()
1576{
1577 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1578 return g_const_string;
1579}
1580
Greg Claytonff44ab42011-04-23 02:04:55 +00001581static const ConstString &
1582GetSettingNameForSourcePathMap ()
1583{
1584 static ConstString g_const_string (TSC_SOURCE_MAP);
1585 return g_const_string;
1586}
Greg Claytond284b662011-02-18 01:44:25 +00001587
Greg Clayton17cd9952011-04-22 03:55:06 +00001588static const ConstString &
1589GetSettingNameForSkipPrologue ()
1590{
1591 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1592 return g_const_string;
1593}
1594
1595
1596
Caroline Tice5bc8c972010-09-20 20:44:43 +00001597bool
1598Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1599 const char *index_value,
1600 const char *value,
1601 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001602 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001603 Error&err)
1604{
Greg Claytond284b662011-02-18 01:44:25 +00001605 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001606 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001607 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001608 if (!m_default_architecture.IsValid())
1609 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001610 }
1611 return true;
1612}
1613
1614
1615bool
1616Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1617 StringList &value,
1618 Error &err)
1619{
Greg Claytond284b662011-02-18 01:44:25 +00001620 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001621 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001622 // If the arch is invalid (the default), don't show a string for it
1623 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001624 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001625 return true;
1626 }
1627 else
1628 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1629
1630 return false;
1631}
1632
1633//--------------------------------------------------------------
1634// class TargetInstanceSettings
1635//--------------------------------------------------------------
1636
Greg Clayton638351a2010-12-04 00:10:17 +00001637TargetInstanceSettings::TargetInstanceSettings
1638(
1639 UserSettingsController &owner,
1640 bool live_instance,
1641 const char *name
1642) :
Greg Claytond284b662011-02-18 01:44:25 +00001643 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001644 m_expr_prefix_file (),
1645 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001646 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001647 m_skip_prologue (true, true),
1648 m_source_map (NULL, NULL)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001649{
1650 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1651 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1652 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1653 // This is true for CreateInstanceName() too.
1654
1655 if (GetInstanceName () == InstanceSettings::InvalidName())
1656 {
1657 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1658 m_owner.RegisterInstanceSettings (this);
1659 }
1660
1661 if (live_instance)
1662 {
1663 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1664 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001665 }
1666}
1667
1668TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001669 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1670 m_expr_prefix_file (rhs.m_expr_prefix_file),
1671 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1672 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1673 m_skip_prologue (rhs.m_skip_prologue),
1674 m_source_map (rhs.m_source_map)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001675{
1676 if (m_instance_name != InstanceSettings::GetDefaultName())
1677 {
1678 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1679 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001680 }
1681}
1682
1683TargetInstanceSettings::~TargetInstanceSettings ()
1684{
1685}
1686
1687TargetInstanceSettings&
1688TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1689{
1690 if (this != &rhs)
1691 {
1692 }
1693
1694 return *this;
1695}
1696
Caroline Tice5bc8c972010-09-20 20:44:43 +00001697void
1698TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1699 const char *index_value,
1700 const char *value,
1701 const ConstString &instance_name,
1702 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001703 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001704 Error &err,
1705 bool pending)
1706{
Greg Claytond284b662011-02-18 01:44:25 +00001707 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001708 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001709 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1710 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001711 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001712 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00001713 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001714 default:
1715 break;
1716 case eVarSetOperationAssign:
1717 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001718 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001719 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1720 {
1721 err.SetErrorToGenericError ();
1722 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1723 return;
1724 }
1725
1726 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1727
1728 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1729 {
1730 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1731 m_expr_prefix_contents_sp.reset();
1732 }
Sean Callanan77e93942010-10-29 00:29:03 +00001733 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001734 break;
1735 case eVarSetOperationClear:
1736 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00001737 }
Sean Callanan77e93942010-10-29 00:29:03 +00001738 }
1739 }
Jim Inghame41494a2011-04-16 00:01:13 +00001740 else if (var_name == GetSettingNameForPreferDynamicValue())
1741 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001742 int new_value;
1743 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1744 if (err.Success())
1745 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00001746 }
1747 else if (var_name == GetSettingNameForSkipPrologue())
1748 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001749 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1750 }
1751 else if (var_name == GetSettingNameForSourcePathMap ())
1752 {
1753 switch (op)
1754 {
1755 case eVarSetOperationReplace:
1756 case eVarSetOperationInsertBefore:
1757 case eVarSetOperationInsertAfter:
1758 case eVarSetOperationRemove:
1759 default:
1760 break;
1761 case eVarSetOperationAssign:
1762 m_source_map.Clear(true);
1763 // Fall through to append....
1764 case eVarSetOperationAppend:
1765 {
1766 Args args(value);
1767 const uint32_t argc = args.GetArgumentCount();
1768 if (argc & 1 || argc == 0)
1769 {
1770 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1771 }
1772 else
1773 {
1774 char resolved_new_path[PATH_MAX];
1775 FileSpec file_spec;
1776 const char *old_path;
1777 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1778 {
1779 const char *new_path = args.GetArgumentAtIndex(idx+1);
1780 assert (new_path); // We have an even number of paths, this shouldn't happen!
1781
1782 file_spec.SetFile(new_path, true);
1783 if (file_spec.Exists())
1784 {
1785 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1786 {
1787 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1788 return;
1789 }
1790 }
1791 else
1792 {
1793 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1794 return;
1795 }
1796 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1797 }
1798 }
1799 }
1800 break;
1801
1802 case eVarSetOperationClear:
1803 m_source_map.Clear(true);
1804 break;
1805 }
Jim Inghame41494a2011-04-16 00:01:13 +00001806 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001807}
1808
1809void
Greg Claytond284b662011-02-18 01:44:25 +00001810TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001811{
Sean Callanan77e93942010-10-29 00:29:03 +00001812 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1813
1814 if (!new_settings_ptr)
1815 return;
1816
Greg Claytonff44ab42011-04-23 02:04:55 +00001817 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1818 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1819 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1820 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001821}
1822
Caroline Ticebcb5b452010-09-20 21:37:42 +00001823bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001824TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1825 const ConstString &var_name,
1826 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001827 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001828{
Greg Claytond284b662011-02-18 01:44:25 +00001829 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001830 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001831 char path[PATH_MAX];
1832 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1833 if (path_len > 0)
1834 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00001835 }
Jim Inghame41494a2011-04-16 00:01:13 +00001836 else if (var_name == GetSettingNameForPreferDynamicValue())
1837 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001838 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00001839 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001840 else if (var_name == GetSettingNameForSkipPrologue())
1841 {
1842 if (m_skip_prologue)
1843 value.AppendString ("true");
1844 else
1845 value.AppendString ("false");
1846 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001847 else if (var_name == GetSettingNameForSourcePathMap ())
1848 {
1849 }
Sean Callanan77e93942010-10-29 00:29:03 +00001850 else
1851 {
1852 if (err)
1853 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1854 return false;
1855 }
1856
1857 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001858}
1859
1860const ConstString
1861TargetInstanceSettings::CreateInstanceName ()
1862{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001863 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001864 static int instance_count = 1;
1865
Caroline Tice5bc8c972010-09-20 20:44:43 +00001866 sstr.Printf ("target_%d", instance_count);
1867 ++instance_count;
1868
1869 const ConstString ret_val (sstr.GetData());
1870 return ret_val;
1871}
1872
1873//--------------------------------------------------
1874// Target::SettingsController Variable Tables
1875//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00001876OptionEnumValueElement
1877TargetInstanceSettings::g_dynamic_value_types[] =
1878{
Greg Clayton577fbc32011-05-30 00:39:48 +00001879{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
1880{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
1881{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00001882{ 0, NULL, NULL }
1883};
Caroline Tice5bc8c972010-09-20 20:44:43 +00001884
1885SettingEntry
1886Target::SettingsController::global_settings_table[] =
1887{
Greg Claytond284b662011-02-18 01:44:25 +00001888 // var-name var-type default enum init'd hidden help-text
1889 // ================= ================== =========== ==== ====== ====== =========================================================================
1890 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1891 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1892};
1893
Caroline Tice5bc8c972010-09-20 20:44:43 +00001894SettingEntry
1895Target::SettingsController::instance_settings_table[] =
1896{
Jim Ingham10de7d12011-05-04 03:43:18 +00001897 // var-name var-type default enum init'd hidden help-text
1898 // ================= ================== =============== ======================= ====== ====== =========================================================================
1899 { 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 +00001900 { TSC_PREFER_DYNAMIC, eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
1901 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
1902 { 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 +00001903 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001904};