blob: ec087d2ed5490aa355721de9648968537c6f60e5 [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 Clayton12bec712010-06-28 21:30:43 +0000216Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000217{
Greg Clayton12bec712010-06-28 21:30:43 +0000218 BreakpointSP bp_sp;
219 if (func_name)
220 {
221 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
222 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
223 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
224 }
225 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000226}
227
228
229SearchFilterSP
230Target::GetSearchFilterForModule (const FileSpec *containingModule)
231{
232 SearchFilterSP filter_sp;
233 lldb::TargetSP target_sp = this->GetSP();
234 if (containingModule != NULL)
235 {
236 // TODO: We should look into sharing module based search filters
237 // across many breakpoints like we do for the simple target based one
238 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
239 }
240 else
241 {
242 if (m_search_filter_sp.get() == NULL)
243 m_search_filter_sp.reset (new SearchFilter (target_sp));
244 filter_sp = m_search_filter_sp;
245 }
246 return filter_sp;
247}
248
249BreakpointSP
250Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
251{
252 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
253 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
254
255 return CreateBreakpoint (filter_sp, resolver_sp, internal);
256}
257
258BreakpointSP
259Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
260{
261 BreakpointSP bp_sp;
262 if (filter_sp && resolver_sp)
263 {
264 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
265 resolver_sp->SetBreakpoint (bp_sp.get());
266
267 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000268 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000269 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000270 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000271
Greg Claytone005f2c2010-11-06 01:53:30 +0000272 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000273 if (log)
274 {
275 StreamString s;
276 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
277 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
278 }
279
Chris Lattner24943d22010-06-08 16:52:24 +0000280 bp_sp->ResolveBreakpoint();
281 }
Jim Inghamd1686902010-10-14 23:45:03 +0000282
283 if (!internal && bp_sp)
284 {
285 m_last_created_breakpoint = bp_sp;
286 }
287
Chris Lattner24943d22010-06-08 16:52:24 +0000288 return bp_sp;
289}
290
291void
292Target::RemoveAllBreakpoints (bool internal_also)
293{
Greg Claytone005f2c2010-11-06 01:53:30 +0000294 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000295 if (log)
296 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
297
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000298 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000299 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000300 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000301
302 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000303}
304
305void
306Target::DisableAllBreakpoints (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
312 m_breakpoint_list.SetEnabledAll (false);
313 if (internal_also)
314 m_internal_breakpoint_list.SetEnabledAll (false);
315}
316
317void
318Target::EnableAllBreakpoints (bool internal_also)
319{
Greg Claytone005f2c2010-11-06 01:53:30 +0000320 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000321 if (log)
322 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
323
324 m_breakpoint_list.SetEnabledAll (true);
325 if (internal_also)
326 m_internal_breakpoint_list.SetEnabledAll (true);
327}
328
329bool
330Target::RemoveBreakpointByID (break_id_t break_id)
331{
Greg Claytone005f2c2010-11-06 01:53:30 +0000332 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000333 if (log)
334 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
335
336 if (DisableBreakpointByID (break_id))
337 {
338 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000339 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000340 else
Jim Inghamd1686902010-10-14 23:45:03 +0000341 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000342 if (m_last_created_breakpoint)
343 {
344 if (m_last_created_breakpoint->GetID() == break_id)
345 m_last_created_breakpoint.reset();
346 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000347 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000348 }
Chris Lattner24943d22010-06-08 16:52:24 +0000349 return true;
350 }
351 return false;
352}
353
354bool
355Target::DisableBreakpointByID (break_id_t break_id)
356{
Greg Claytone005f2c2010-11-06 01:53:30 +0000357 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000358 if (log)
359 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
360
361 BreakpointSP bp_sp;
362
363 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
364 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
365 else
366 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
367 if (bp_sp)
368 {
369 bp_sp->SetEnabled (false);
370 return true;
371 }
372 return false;
373}
374
375bool
376Target::EnableBreakpointByID (break_id_t break_id)
377{
Greg Claytone005f2c2010-11-06 01:53:30 +0000378 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000379 if (log)
380 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
381 __FUNCTION__,
382 break_id,
383 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
384
385 BreakpointSP bp_sp;
386
387 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
388 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
389 else
390 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
391
392 if (bp_sp)
393 {
394 bp_sp->SetEnabled (true);
395 return true;
396 }
397 return false;
398}
399
400ModuleSP
401Target::GetExecutableModule ()
402{
403 ModuleSP executable_sp;
404 if (m_images.GetSize() > 0)
405 executable_sp = m_images.GetModuleAtIndex(0);
406 return executable_sp;
407}
408
409void
410Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
411{
412 m_images.Clear();
413 m_scratch_ast_context_ap.reset();
414
415 if (executable_sp.get())
416 {
417 Timer scoped_timer (__PRETTY_FUNCTION__,
418 "Target::SetExecutableModule (executable = '%s/%s')",
419 executable_sp->GetFileSpec().GetDirectory().AsCString(),
420 executable_sp->GetFileSpec().GetFilename().AsCString());
421
422 m_images.Append(executable_sp); // The first image is our exectuable file
423
Jim Ingham7508e732010-08-09 23:31:02 +0000424 // 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 +0000425 if (!m_arch.IsValid())
426 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000427
Chris Lattner24943d22010-06-08 16:52:24 +0000428 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000429 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000430
431 if (executable_objfile)
432 {
433 executable_objfile->GetDependentModules(dependent_files);
434 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
435 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000436 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
437 FileSpec platform_dependent_file_spec;
438 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000439 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000440 else
441 platform_dependent_file_spec = dependent_file_spec;
442
443 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000444 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000445 if (image_module_sp.get())
446 {
Chris Lattner24943d22010-06-08 16:52:24 +0000447 ObjectFile *objfile = image_module_sp->GetObjectFile();
448 if (objfile)
449 objfile->GetDependentModules(dependent_files);
450 }
451 }
452 }
453
454 // Now see if we know the target triple, and if so, create our scratch AST context:
Greg Clayton24bc5d92011-03-30 18:16:51 +0000455 if (m_arch.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000456 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000457 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000458 }
459 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000460
461 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000462}
463
464
Jim Ingham7508e732010-08-09 23:31:02 +0000465bool
466Target::SetArchitecture (const ArchSpec &arch_spec)
467{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000468 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000469 {
470 // If we're setting the architecture to our current architecture, we
471 // don't need to do anything.
472 return true;
473 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000474 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000475 {
476 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000477 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000478 return true;
479 }
480 else
481 {
482 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000483 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000484 ModuleSP executable_sp = GetExecutableModule ();
485 m_images.Clear();
486 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000487 // Need to do something about unsetting breakpoints.
488
489 if (executable_sp)
490 {
491 FileSpec exec_file_spec = executable_sp->GetFileSpec();
492 Error error = ModuleList::GetSharedModule(exec_file_spec,
493 arch_spec,
494 NULL,
495 NULL,
496 0,
497 executable_sp,
498 NULL,
499 NULL);
500
501 if (!error.Fail() && executable_sp)
502 {
503 SetExecutableModule (executable_sp, true);
504 return true;
505 }
506 else
507 {
508 return false;
509 }
510 }
511 else
512 {
513 return false;
514 }
515 }
516}
Chris Lattner24943d22010-06-08 16:52:24 +0000517
Chris Lattner24943d22010-06-08 16:52:24 +0000518void
519Target::ModuleAdded (ModuleSP &module_sp)
520{
521 // A module is being added to this target for the first time
522 ModuleList module_list;
523 module_list.Append(module_sp);
524 ModulesDidLoad (module_list);
525}
526
527void
528Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
529{
530 // A module is being added to this target for the first time
531 ModuleList module_list;
532 module_list.Append (old_module_sp);
533 ModulesDidUnload (module_list);
534 module_list.Clear ();
535 module_list.Append (new_module_sp);
536 ModulesDidLoad (module_list);
537}
538
539void
540Target::ModulesDidLoad (ModuleList &module_list)
541{
542 m_breakpoint_list.UpdateBreakpoints (module_list, true);
543 // TODO: make event data that packages up the module_list
544 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
545}
546
547void
548Target::ModulesDidUnload (ModuleList &module_list)
549{
550 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000551
552 // Remove the images from the target image list
553 m_images.Remove(module_list);
554
Chris Lattner24943d22010-06-08 16:52:24 +0000555 // TODO: make event data that packages up the module_list
556 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
557}
558
559size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000560Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
561{
562 const Section *section = addr.GetSection();
563 if (section && section->GetModule())
564 {
565 ObjectFile *objfile = section->GetModule()->GetObjectFile();
566 if (objfile)
567 {
568 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
569 addr.GetOffset(),
570 dst,
571 dst_len);
572 if (bytes_read > 0)
573 return bytes_read;
574 else
575 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
576 }
577 else
578 {
579 error.SetErrorString("address isn't from a object file");
580 }
581 }
582 else
583 {
584 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
585 }
586 return 0;
587}
588
589size_t
590Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000591{
Chris Lattner24943d22010-06-08 16:52:24 +0000592 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000593
Greg Clayton70436352010-06-30 23:03:03 +0000594 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
595
Greg Clayton26100dc2011-01-07 01:57:07 +0000596 size_t bytes_read = 0;
Greg Clayton889fbd02011-03-26 19:14:58 +0000597 Address resolved_addr;
598 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000599 {
600 if (process_is_valid)
Greg Claytoneea26402010-09-14 23:36:40 +0000601 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000602 else
Greg Clayton70436352010-06-30 23:03:03 +0000603 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000604 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000605 if (!resolved_addr.IsValid())
606 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000607
Greg Clayton26100dc2011-01-07 01:57:07 +0000608 if (prefer_file_cache)
609 {
610 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
611 if (bytes_read > 0)
612 return bytes_read;
613 }
Greg Clayton70436352010-06-30 23:03:03 +0000614
615 if (process_is_valid)
616 {
Greg Claytoneea26402010-09-14 23:36:40 +0000617 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000618 if (load_addr == LLDB_INVALID_ADDRESS)
619 {
620 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
621 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
622 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
623 resolved_addr.GetFileAddress());
624 else
625 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
626 }
627 else
628 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000629 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000630 if (bytes_read != dst_len)
631 {
632 if (error.Success())
633 {
634 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000635 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000636 else
Greg Clayton70436352010-06-30 23:03:03 +0000637 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 +0000638 }
639 }
Greg Clayton70436352010-06-30 23:03:03 +0000640 if (bytes_read)
641 return bytes_read;
642 // If the address is not section offset we have an address that
643 // doesn't resolve to any address in any currently loaded shared
644 // libaries and we failed to read memory so there isn't anything
645 // more we can do. If it is section offset, we might be able to
646 // read cached memory from the object file.
647 if (!resolved_addr.IsSectionOffset())
648 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000649 }
Chris Lattner24943d22010-06-08 16:52:24 +0000650 }
Greg Clayton70436352010-06-30 23:03:03 +0000651
Greg Clayton26100dc2011-01-07 01:57:07 +0000652 if (!prefer_file_cache)
Greg Clayton70436352010-06-30 23:03:03 +0000653 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000654 // If we didn't already try and read from the object file cache, then
655 // try it after failing to read from the process.
656 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000657 }
658 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000659}
660
661
662ModuleSP
663Target::GetSharedModule
664(
665 const FileSpec& file_spec,
666 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000667 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000668 const ConstString *object_name,
669 off_t object_offset,
670 Error *error_ptr
671)
672{
673 // Don't pass in the UUID so we can tell if we have a stale value in our list
674 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
675 bool did_create_module = false;
676 ModuleSP module_sp;
677
Chris Lattner24943d22010-06-08 16:52:24 +0000678 Error error;
679
Greg Clayton24bc5d92011-03-30 18:16:51 +0000680 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000681 if (m_image_search_paths.GetSize())
682 {
683 FileSpec transformed_spec;
684 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
685 {
686 transformed_spec.GetFilename() = file_spec.GetFilename();
687 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
688 }
689 }
690
Greg Clayton24bc5d92011-03-30 18:16:51 +0000691 // The platform is responsible for finding and caching an appropriate
692 // module in the shared module cache.
693 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000694 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000695 FileSpec platform_file_spec;
696 error = m_platform_sp->GetSharedModule (file_spec,
697 arch,
698 uuid_ptr,
699 object_name,
700 object_offset,
701 module_sp,
702 &old_module_sp,
703 &did_create_module);
704 }
705 else
706 {
707 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +0000708 }
709
Greg Clayton24bc5d92011-03-30 18:16:51 +0000710 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000711 if (module_sp)
712 {
713 m_images.Append (module_sp);
714 if (did_create_module)
715 {
716 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
717 ModuleUpdated(old_module_sp, module_sp);
718 else
719 ModuleAdded(module_sp);
720 }
721 }
722 if (error_ptr)
723 *error_ptr = error;
724 return module_sp;
725}
726
727
728Target *
729Target::CalculateTarget ()
730{
731 return this;
732}
733
734Process *
735Target::CalculateProcess ()
736{
737 return NULL;
738}
739
740Thread *
741Target::CalculateThread ()
742{
743 return NULL;
744}
745
746StackFrame *
747Target::CalculateStackFrame ()
748{
749 return NULL;
750}
751
752void
Greg Claytona830adb2010-10-04 01:05:56 +0000753Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000754{
755 exe_ctx.target = this;
756 exe_ctx.process = NULL; // Do NOT fill in process...
757 exe_ctx.thread = NULL;
758 exe_ctx.frame = NULL;
759}
760
761PathMappingList &
762Target::GetImageSearchPathList ()
763{
764 return m_image_search_paths;
765}
766
767void
768Target::ImageSearchPathsChanged
769(
770 const PathMappingList &path_list,
771 void *baton
772)
773{
774 Target *target = (Target *)baton;
775 if (target->m_images.GetSize() > 1)
776 {
777 ModuleSP exe_module_sp (target->GetExecutableModule());
778 if (exe_module_sp)
779 {
780 target->m_images.Clear();
781 target->SetExecutableModule (exe_module_sp, true);
782 }
783 }
784}
785
786ClangASTContext *
787Target::GetScratchClangASTContext()
788{
789 return m_scratch_ast_context_ap.get();
790}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000791
Greg Clayton990de7b2010-11-18 23:32:35 +0000792void
Caroline Tice2a456812011-03-10 22:14:10 +0000793Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000794{
Greg Clayton990de7b2010-11-18 23:32:35 +0000795 UserSettingsControllerSP &usc = GetSettingsController();
796 usc.reset (new SettingsController);
797 UserSettingsController::InitializeSettingsController (usc,
798 SettingsController::global_settings_table,
799 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000800
801 // Now call SettingsInitialize() on each 'child' setting of Target
802 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000803}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000804
Greg Clayton990de7b2010-11-18 23:32:35 +0000805void
Caroline Tice2a456812011-03-10 22:14:10 +0000806Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000807{
Caroline Tice2a456812011-03-10 22:14:10 +0000808
809 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
810
811 Process::SettingsTerminate ();
812
813 // Now terminate Target Settings.
814
Greg Clayton990de7b2010-11-18 23:32:35 +0000815 UserSettingsControllerSP &usc = GetSettingsController();
816 UserSettingsController::FinalizeSettingsController (usc);
817 usc.reset();
818}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000819
Greg Clayton990de7b2010-11-18 23:32:35 +0000820UserSettingsControllerSP &
821Target::GetSettingsController ()
822{
823 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000824 return g_settings_controller;
825}
826
827ArchSpec
828Target::GetDefaultArchitecture ()
829{
Greg Clayton940b1032011-02-23 00:35:02 +0000830 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
831
832 if (settings_controller_sp)
833 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
834 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000835}
836
837void
Greg Clayton940b1032011-02-23 00:35:02 +0000838Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000839{
Greg Clayton940b1032011-02-23 00:35:02 +0000840 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
841
842 if (settings_controller_sp)
843 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000844}
845
Greg Claytona830adb2010-10-04 01:05:56 +0000846Target *
847Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
848{
849 // The target can either exist in the "process" of ExecutionContext, or in
850 // the "target_sp" member of SymbolContext. This accessor helper function
851 // will get the target from one of these locations.
852
853 Target *target = NULL;
854 if (sc_ptr != NULL)
855 target = sc_ptr->target_sp.get();
856 if (target == NULL)
857 {
858 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
859 target = &exe_ctx_ptr->process->GetTarget();
860 }
861 return target;
862}
863
864
Caroline Tice1ebef442010-09-27 00:30:10 +0000865void
866Target::UpdateInstanceName ()
867{
868 StreamString sstr;
869
870 ModuleSP module_sp = GetExecutableModule();
871 if (module_sp)
872 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000873 sstr.Printf ("%s_%s",
874 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton940b1032011-02-23 00:35:02 +0000875 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000876 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
877 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000878 }
879}
880
Sean Callanan77e93942010-10-29 00:29:03 +0000881const char *
882Target::GetExpressionPrefixContentsAsCString ()
883{
Greg Claytonff44ab42011-04-23 02:04:55 +0000884 if (m_expr_prefix_contents_sp)
885 return (const char *)m_expr_prefix_contents_sp->GetBytes();
886 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +0000887}
888
Greg Clayton427f2902010-12-14 02:59:59 +0000889ExecutionResults
890Target::EvaluateExpression
891(
892 const char *expr_cstr,
893 StackFrame *frame,
894 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +0000895 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +0000896 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +0000897 lldb::ValueObjectSP &result_valobj_sp
898)
899{
900 ExecutionResults execution_results = eExecutionSetupError;
901
902 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +0000903
904 // We shouldn't run stop hooks in expressions.
905 // Be sure to reset this if you return anywhere within this function.
906 bool old_suppress_value = m_suppress_stop_hooks;
907 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +0000908
909 ExecutionContext exe_ctx;
910 if (frame)
911 {
912 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000913 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +0000914 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
915 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
Jim Ingham10de7d12011-05-04 03:43:18 +0000916 lldb::VariableSP var_sp;
917 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
918 use_dynamic,
919 expr_path_options,
920 var_sp,
921 error);
Greg Clayton427f2902010-12-14 02:59:59 +0000922 }
923 else if (m_process_sp)
924 {
925 m_process_sp->CalculateExecutionContext(exe_ctx);
926 }
927 else
928 {
929 CalculateExecutionContext(exe_ctx);
930 }
931
932 if (result_valobj_sp)
933 {
934 execution_results = eExecutionCompleted;
935 // We got a result from the frame variable expression path above...
936 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
937
938 lldb::ValueObjectSP const_valobj_sp;
939
940 // Check in case our value is already a constant value
941 if (result_valobj_sp->GetIsConstant())
942 {
943 const_valobj_sp = result_valobj_sp;
944 const_valobj_sp->SetName (persistent_variable_name);
945 }
946 else
Jim Inghame41494a2011-04-16 00:01:13 +0000947 {
Jim Ingham10de7d12011-05-04 03:43:18 +0000948 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +0000949 {
Jim Ingham10de7d12011-05-04 03:43:18 +0000950 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +0000951 if (dynamic_sp)
952 result_valobj_sp = dynamic_sp;
953 }
954
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000955 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +0000956 }
Greg Clayton427f2902010-12-14 02:59:59 +0000957
Sean Callanan6a925532011-01-13 08:53:35 +0000958 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
959
Greg Clayton427f2902010-12-14 02:59:59 +0000960 result_valobj_sp = const_valobj_sp;
961
Sean Callanan6a925532011-01-13 08:53:35 +0000962 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
963 assert (clang_expr_variable_sp.get());
964
965 // Set flags and live data as appropriate
966
967 const Value &result_value = live_valobj_sp->GetValue();
968
969 switch (result_value.GetValueType())
970 {
971 case Value::eValueTypeHostAddress:
972 case Value::eValueTypeFileAddress:
973 // we don't do anything with these for now
974 break;
975 case Value::eValueTypeScalar:
976 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
977 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
978 break;
979 case Value::eValueTypeLoadAddress:
980 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
981 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
982 break;
983 }
Greg Clayton427f2902010-12-14 02:59:59 +0000984 }
985 else
986 {
987 // Make sure we aren't just trying to see the value of a persistent
988 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +0000989 lldb::ClangExpressionVariableSP persistent_var_sp;
990 // Only check for persistent variables the expression starts with a '$'
991 if (expr_cstr[0] == '$')
992 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
993
Greg Clayton427f2902010-12-14 02:59:59 +0000994 if (persistent_var_sp)
995 {
996 result_valobj_sp = persistent_var_sp->GetValueObject ();
997 execution_results = eExecutionCompleted;
998 }
999 else
1000 {
1001 const char *prefix = GetExpressionPrefixContentsAsCString();
1002
1003 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +00001004 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001005 expr_cstr,
1006 prefix,
1007 result_valobj_sp);
1008 }
1009 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001010
1011 m_suppress_stop_hooks = old_suppress_value;
1012
Greg Clayton427f2902010-12-14 02:59:59 +00001013 return execution_results;
1014}
1015
Greg Claytonc0fa5332011-05-22 22:46:53 +00001016lldb::addr_t
1017Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1018{
1019 addr_t code_addr = load_addr;
1020 switch (m_arch.GetMachine())
1021 {
1022 case llvm::Triple::arm:
1023 case llvm::Triple::thumb:
1024 switch (addr_class)
1025 {
1026 case eAddressClassData:
1027 case eAddressClassDebug:
1028 return LLDB_INVALID_ADDRESS;
1029
1030 case eAddressClassUnknown:
1031 case eAddressClassInvalid:
1032 case eAddressClassCode:
1033 case eAddressClassCodeAlternateISA:
1034 case eAddressClassRuntime:
1035 // Check if bit zero it no set?
1036 if ((code_addr & 1ull) == 0)
1037 {
1038 // Bit zero isn't set, check if the address is a multiple of 2?
1039 if (code_addr & 2ull)
1040 {
1041 // The address is a multiple of 2 so it must be thumb, set bit zero
1042 code_addr |= 1ull;
1043 }
1044 else if (addr_class == eAddressClassCodeAlternateISA)
1045 {
1046 // We checked the address and the address claims to be the alternate ISA
1047 // which means thumb, so set bit zero.
1048 code_addr |= 1ull;
1049 }
1050 }
1051 break;
1052 }
1053 break;
1054
1055 default:
1056 break;
1057 }
1058 return code_addr;
1059}
1060
1061lldb::addr_t
1062Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1063{
1064 addr_t opcode_addr = load_addr;
1065 switch (m_arch.GetMachine())
1066 {
1067 case llvm::Triple::arm:
1068 case llvm::Triple::thumb:
1069 switch (addr_class)
1070 {
1071 case eAddressClassData:
1072 case eAddressClassDebug:
1073 return LLDB_INVALID_ADDRESS;
1074
1075 case eAddressClassInvalid:
1076 case eAddressClassUnknown:
1077 case eAddressClassCode:
1078 case eAddressClassCodeAlternateISA:
1079 case eAddressClassRuntime:
1080 opcode_addr &= ~(1ull);
1081 break;
1082 }
1083 break;
1084
1085 default:
1086 break;
1087 }
1088 return opcode_addr;
1089}
1090
Jim Inghamd60d94a2011-03-11 03:53:59 +00001091lldb::user_id_t
1092Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1093{
1094 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1095 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1096 m_stop_hooks[new_uid] = new_hook_sp;
1097 return new_uid;
1098}
1099
1100bool
1101Target::RemoveStopHookByID (lldb::user_id_t user_id)
1102{
1103 size_t num_removed;
1104 num_removed = m_stop_hooks.erase (user_id);
1105 if (num_removed == 0)
1106 return false;
1107 else
1108 return true;
1109}
1110
1111void
1112Target::RemoveAllStopHooks ()
1113{
1114 m_stop_hooks.clear();
1115}
1116
1117Target::StopHookSP
1118Target::GetStopHookByID (lldb::user_id_t user_id)
1119{
1120 StopHookSP found_hook;
1121
1122 StopHookCollection::iterator specified_hook_iter;
1123 specified_hook_iter = m_stop_hooks.find (user_id);
1124 if (specified_hook_iter != m_stop_hooks.end())
1125 found_hook = (*specified_hook_iter).second;
1126 return found_hook;
1127}
1128
1129bool
1130Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1131{
1132 StopHookCollection::iterator specified_hook_iter;
1133 specified_hook_iter = m_stop_hooks.find (user_id);
1134 if (specified_hook_iter == m_stop_hooks.end())
1135 return false;
1136
1137 (*specified_hook_iter).second->SetIsActive (active_state);
1138 return true;
1139}
1140
1141void
1142Target::SetAllStopHooksActiveState (bool active_state)
1143{
1144 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1145 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1146 {
1147 (*pos).second->SetIsActive (active_state);
1148 }
1149}
1150
1151void
1152Target::RunStopHooks ()
1153{
Jim Ingham3613ae12011-05-12 02:06:14 +00001154 if (m_suppress_stop_hooks)
1155 return;
1156
Jim Inghamd60d94a2011-03-11 03:53:59 +00001157 if (!m_process_sp)
1158 return;
1159
1160 if (m_stop_hooks.empty())
1161 return;
1162
1163 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1164
1165 // If there aren't any active stop hooks, don't bother either:
1166 bool any_active_hooks = false;
1167 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1168 {
1169 if ((*pos).second->IsActive())
1170 {
1171 any_active_hooks = true;
1172 break;
1173 }
1174 }
1175 if (!any_active_hooks)
1176 return;
1177
1178 CommandReturnObject result;
1179
1180 std::vector<ExecutionContext> exc_ctx_with_reasons;
1181 std::vector<SymbolContext> sym_ctx_with_reasons;
1182
1183 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1184 size_t num_threads = cur_threadlist.GetSize();
1185 for (size_t i = 0; i < num_threads; i++)
1186 {
1187 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1188 if (cur_thread_sp->ThreadStoppedForAReason())
1189 {
1190 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1191 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1192 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1193 }
1194 }
1195
1196 // If no threads stopped for a reason, don't run the stop-hooks.
1197 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1198 if (num_exe_ctx == 0)
1199 return;
1200
Jim Inghame5ed8e92011-06-02 23:58:26 +00001201 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1202 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001203
1204 bool keep_going = true;
1205 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001206 bool print_hook_header;
1207 bool print_thread_header;
1208
1209 if (num_exe_ctx == 1)
1210 print_thread_header = false;
1211 else
1212 print_thread_header = true;
1213
1214 if (m_stop_hooks.size() == 1)
1215 print_hook_header = false;
1216 else
1217 print_hook_header = true;
1218
Jim Inghamd60d94a2011-03-11 03:53:59 +00001219 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1220 {
1221 // result.Clear();
1222 StopHookSP cur_hook_sp = (*pos).second;
1223 if (!cur_hook_sp->IsActive())
1224 continue;
1225
1226 bool any_thread_matched = false;
1227 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1228 {
1229 if ((cur_hook_sp->GetSpecifier () == NULL
1230 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1231 && (cur_hook_sp->GetThreadSpecifier() == NULL
1232 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1233 {
1234 if (!hooks_ran)
1235 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001236 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001237 hooks_ran = true;
1238 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001239 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001240 {
1241 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1242 any_thread_matched = true;
1243 }
1244
Jim Inghamc54840c2011-03-22 01:47:27 +00001245 if (print_thread_header)
1246 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001247
1248 bool stop_on_continue = true;
1249 bool stop_on_error = true;
1250 bool echo_commands = false;
1251 bool print_results = true;
1252 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001253 &exc_ctx_with_reasons[i],
1254 stop_on_continue,
1255 stop_on_error,
1256 echo_commands,
1257 print_results,
1258 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001259
1260 // If the command started the target going again, we should bag out of
1261 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001262 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1263 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001264 {
1265 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1266 keep_going = false;
1267 }
1268 }
1269 }
1270 }
1271 if (hooks_ran)
1272 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001273
1274 result.GetImmediateOutputStream()->Flush();
1275 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001276}
1277
1278//--------------------------------------------------------------
1279// class Target::StopHook
1280//--------------------------------------------------------------
1281
1282
1283Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1284 UserID (uid),
1285 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001286 m_commands (),
1287 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001288 m_thread_spec_ap(NULL),
1289 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001290{
1291}
1292
1293Target::StopHook::StopHook (const StopHook &rhs) :
1294 UserID (rhs.GetID()),
1295 m_target_sp (rhs.m_target_sp),
1296 m_commands (rhs.m_commands),
1297 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001298 m_thread_spec_ap (NULL),
1299 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001300{
1301 if (rhs.m_thread_spec_ap.get() != NULL)
1302 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1303}
1304
1305
1306Target::StopHook::~StopHook ()
1307{
1308}
1309
1310void
1311Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1312{
1313 m_thread_spec_ap.reset (specifier);
1314}
1315
1316
1317void
1318Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1319{
1320 int indent_level = s->GetIndentLevel();
1321
1322 s->SetIndentLevel(indent_level + 2);
1323
1324 s->Printf ("Hook: %d\n", GetID());
1325 if (m_active)
1326 s->Indent ("State: enabled\n");
1327 else
1328 s->Indent ("State: disabled\n");
1329
1330 if (m_specifier_sp)
1331 {
1332 s->Indent();
1333 s->PutCString ("Specifier:\n");
1334 s->SetIndentLevel (indent_level + 4);
1335 m_specifier_sp->GetDescription (s, level);
1336 s->SetIndentLevel (indent_level + 2);
1337 }
1338
1339 if (m_thread_spec_ap.get() != NULL)
1340 {
1341 StreamString tmp;
1342 s->Indent("Thread:\n");
1343 m_thread_spec_ap->GetDescription (&tmp, level);
1344 s->SetIndentLevel (indent_level + 4);
1345 s->Indent (tmp.GetData());
1346 s->PutCString ("\n");
1347 s->SetIndentLevel (indent_level + 2);
1348 }
1349
1350 s->Indent ("Commands: \n");
1351 s->SetIndentLevel (indent_level + 4);
1352 uint32_t num_commands = m_commands.GetSize();
1353 for (uint32_t i = 0; i < num_commands; i++)
1354 {
1355 s->Indent(m_commands.GetStringAtIndex(i));
1356 s->PutCString ("\n");
1357 }
1358 s->SetIndentLevel (indent_level);
1359}
1360
1361
Caroline Tice5bc8c972010-09-20 20:44:43 +00001362//--------------------------------------------------------------
1363// class Target::SettingsController
1364//--------------------------------------------------------------
1365
1366Target::SettingsController::SettingsController () :
1367 UserSettingsController ("target", Debugger::GetSettingsController()),
1368 m_default_architecture ()
1369{
1370 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1371 InstanceSettings::GetDefaultName().AsCString()));
1372}
1373
1374Target::SettingsController::~SettingsController ()
1375{
1376}
1377
1378lldb::InstanceSettingsSP
1379Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1380{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001381 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1382 false,
1383 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001384 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1385 return new_settings_sp;
1386}
1387
Caroline Tice5bc8c972010-09-20 20:44:43 +00001388
Jim Inghame41494a2011-04-16 00:01:13 +00001389#define TSC_DEFAULT_ARCH "default-arch"
1390#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001391#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001392#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001393#define TSC_SOURCE_MAP "source-map"
Greg Claytond284b662011-02-18 01:44:25 +00001394
1395
1396static const ConstString &
1397GetSettingNameForDefaultArch ()
1398{
1399 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001400 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001401}
1402
Greg Claytond284b662011-02-18 01:44:25 +00001403static const ConstString &
1404GetSettingNameForExpressionPrefix ()
1405{
1406 static ConstString g_const_string (TSC_EXPR_PREFIX);
1407 return g_const_string;
1408}
1409
1410static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001411GetSettingNameForPreferDynamicValue ()
1412{
1413 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1414 return g_const_string;
1415}
1416
Greg Claytonff44ab42011-04-23 02:04:55 +00001417static const ConstString &
1418GetSettingNameForSourcePathMap ()
1419{
1420 static ConstString g_const_string (TSC_SOURCE_MAP);
1421 return g_const_string;
1422}
Greg Claytond284b662011-02-18 01:44:25 +00001423
Greg Clayton17cd9952011-04-22 03:55:06 +00001424static const ConstString &
1425GetSettingNameForSkipPrologue ()
1426{
1427 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1428 return g_const_string;
1429}
1430
1431
1432
Caroline Tice5bc8c972010-09-20 20:44:43 +00001433bool
1434Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1435 const char *index_value,
1436 const char *value,
1437 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001438 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001439 Error&err)
1440{
Greg Claytond284b662011-02-18 01:44:25 +00001441 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001442 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001443 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001444 if (!m_default_architecture.IsValid())
1445 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001446 }
1447 return true;
1448}
1449
1450
1451bool
1452Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1453 StringList &value,
1454 Error &err)
1455{
Greg Claytond284b662011-02-18 01:44:25 +00001456 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001457 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001458 // If the arch is invalid (the default), don't show a string for it
1459 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001460 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001461 return true;
1462 }
1463 else
1464 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1465
1466 return false;
1467}
1468
1469//--------------------------------------------------------------
1470// class TargetInstanceSettings
1471//--------------------------------------------------------------
1472
Greg Clayton638351a2010-12-04 00:10:17 +00001473TargetInstanceSettings::TargetInstanceSettings
1474(
1475 UserSettingsController &owner,
1476 bool live_instance,
1477 const char *name
1478) :
Greg Claytond284b662011-02-18 01:44:25 +00001479 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001480 m_expr_prefix_file (),
1481 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001482 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001483 m_skip_prologue (true, true),
1484 m_source_map (NULL, NULL)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001485{
1486 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1487 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1488 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1489 // This is true for CreateInstanceName() too.
1490
1491 if (GetInstanceName () == InstanceSettings::InvalidName())
1492 {
1493 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1494 m_owner.RegisterInstanceSettings (this);
1495 }
1496
1497 if (live_instance)
1498 {
1499 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1500 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001501 }
1502}
1503
1504TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001505 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1506 m_expr_prefix_file (rhs.m_expr_prefix_file),
1507 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1508 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1509 m_skip_prologue (rhs.m_skip_prologue),
1510 m_source_map (rhs.m_source_map)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001511{
1512 if (m_instance_name != InstanceSettings::GetDefaultName())
1513 {
1514 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1515 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001516 }
1517}
1518
1519TargetInstanceSettings::~TargetInstanceSettings ()
1520{
1521}
1522
1523TargetInstanceSettings&
1524TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1525{
1526 if (this != &rhs)
1527 {
1528 }
1529
1530 return *this;
1531}
1532
Caroline Tice5bc8c972010-09-20 20:44:43 +00001533void
1534TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1535 const char *index_value,
1536 const char *value,
1537 const ConstString &instance_name,
1538 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001539 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001540 Error &err,
1541 bool pending)
1542{
Greg Claytond284b662011-02-18 01:44:25 +00001543 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001544 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001545 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1546 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001547 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001548 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00001549 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001550 default:
1551 break;
1552 case eVarSetOperationAssign:
1553 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001554 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001555 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1556 {
1557 err.SetErrorToGenericError ();
1558 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1559 return;
1560 }
1561
1562 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1563
1564 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1565 {
1566 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1567 m_expr_prefix_contents_sp.reset();
1568 }
Sean Callanan77e93942010-10-29 00:29:03 +00001569 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001570 break;
1571 case eVarSetOperationClear:
1572 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00001573 }
Sean Callanan77e93942010-10-29 00:29:03 +00001574 }
1575 }
Jim Inghame41494a2011-04-16 00:01:13 +00001576 else if (var_name == GetSettingNameForPreferDynamicValue())
1577 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001578 int new_value;
1579 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1580 if (err.Success())
1581 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00001582 }
1583 else if (var_name == GetSettingNameForSkipPrologue())
1584 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001585 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1586 }
1587 else if (var_name == GetSettingNameForSourcePathMap ())
1588 {
1589 switch (op)
1590 {
1591 case eVarSetOperationReplace:
1592 case eVarSetOperationInsertBefore:
1593 case eVarSetOperationInsertAfter:
1594 case eVarSetOperationRemove:
1595 default:
1596 break;
1597 case eVarSetOperationAssign:
1598 m_source_map.Clear(true);
1599 // Fall through to append....
1600 case eVarSetOperationAppend:
1601 {
1602 Args args(value);
1603 const uint32_t argc = args.GetArgumentCount();
1604 if (argc & 1 || argc == 0)
1605 {
1606 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1607 }
1608 else
1609 {
1610 char resolved_new_path[PATH_MAX];
1611 FileSpec file_spec;
1612 const char *old_path;
1613 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1614 {
1615 const char *new_path = args.GetArgumentAtIndex(idx+1);
1616 assert (new_path); // We have an even number of paths, this shouldn't happen!
1617
1618 file_spec.SetFile(new_path, true);
1619 if (file_spec.Exists())
1620 {
1621 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1622 {
1623 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1624 return;
1625 }
1626 }
1627 else
1628 {
1629 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1630 return;
1631 }
1632 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1633 }
1634 }
1635 }
1636 break;
1637
1638 case eVarSetOperationClear:
1639 m_source_map.Clear(true);
1640 break;
1641 }
Jim Inghame41494a2011-04-16 00:01:13 +00001642 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001643}
1644
1645void
Greg Claytond284b662011-02-18 01:44:25 +00001646TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001647{
Sean Callanan77e93942010-10-29 00:29:03 +00001648 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1649
1650 if (!new_settings_ptr)
1651 return;
1652
Greg Claytonff44ab42011-04-23 02:04:55 +00001653 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1654 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1655 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1656 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001657}
1658
Caroline Ticebcb5b452010-09-20 21:37:42 +00001659bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001660TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1661 const ConstString &var_name,
1662 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001663 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001664{
Greg Claytond284b662011-02-18 01:44:25 +00001665 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001666 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001667 char path[PATH_MAX];
1668 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1669 if (path_len > 0)
1670 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00001671 }
Jim Inghame41494a2011-04-16 00:01:13 +00001672 else if (var_name == GetSettingNameForPreferDynamicValue())
1673 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001674 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00001675 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001676 else if (var_name == GetSettingNameForSkipPrologue())
1677 {
1678 if (m_skip_prologue)
1679 value.AppendString ("true");
1680 else
1681 value.AppendString ("false");
1682 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001683 else if (var_name == GetSettingNameForSourcePathMap ())
1684 {
1685 }
Sean Callanan77e93942010-10-29 00:29:03 +00001686 else
1687 {
1688 if (err)
1689 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1690 return false;
1691 }
1692
1693 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001694}
1695
1696const ConstString
1697TargetInstanceSettings::CreateInstanceName ()
1698{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001699 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001700 static int instance_count = 1;
1701
Caroline Tice5bc8c972010-09-20 20:44:43 +00001702 sstr.Printf ("target_%d", instance_count);
1703 ++instance_count;
1704
1705 const ConstString ret_val (sstr.GetData());
1706 return ret_val;
1707}
1708
1709//--------------------------------------------------
1710// Target::SettingsController Variable Tables
1711//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00001712OptionEnumValueElement
1713TargetInstanceSettings::g_dynamic_value_types[] =
1714{
Greg Clayton577fbc32011-05-30 00:39:48 +00001715{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
1716{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
1717{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00001718{ 0, NULL, NULL }
1719};
Caroline Tice5bc8c972010-09-20 20:44:43 +00001720
1721SettingEntry
1722Target::SettingsController::global_settings_table[] =
1723{
Greg Claytond284b662011-02-18 01:44:25 +00001724 // var-name var-type default enum init'd hidden help-text
1725 // ================= ================== =========== ==== ====== ====== =========================================================================
1726 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1727 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1728};
1729
Caroline Tice5bc8c972010-09-20 20:44:43 +00001730SettingEntry
1731Target::SettingsController::instance_settings_table[] =
1732{
Jim Ingham10de7d12011-05-04 03:43:18 +00001733 // var-name var-type default enum init'd hidden help-text
1734 // ================= ================== =============== ======================= ====== ====== =========================================================================
1735 { 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 +00001736 { TSC_PREFER_DYNAMIC, eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
1737 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
1738 { 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 +00001739 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001740};