blob: 7f8ae45a8d076a04a085d44615a3677dcecfc342 [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"
Jim Ingham03c8ee52011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton427f2902010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000026#include "lldb/Core/Timer.h"
27#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000028#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000029#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000031#include "lldb/Interpreter/CommandInterpreter.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/lldb-private-log.h"
34#include "lldb/Symbol/ObjectFile.h"
35#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000036#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000037#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
Jim Ingham5a15e692012-02-16 06:50:00 +000043ConstString &
44Target::GetStaticBroadcasterClass ()
45{
46 static ConstString class_name ("lldb.target");
47 return class_name;
48}
49
Chris Lattner24943d22010-06-08 16:52:24 +000050//----------------------------------------------------------------------
51// Target constructor
52//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000053Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Jim Ingham5a15e692012-02-16 06:50:00 +000054 Broadcaster (&debugger, "lldb.target"),
Greg Clayton24bc5d92011-03-30 18:16:51 +000055 ExecutionContextScope (),
Greg Clayton334d33a2012-01-30 07:41:31 +000056 TargetInstanceSettings (GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000057 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000058 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000059 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000060 m_arch (target_arch),
61 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000062 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000063 m_breakpoint_list (false),
64 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000065 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000066 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000067 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000068 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000069 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000070 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000071 m_scratch_ast_source_ap (NULL),
72 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000073 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000074 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000075 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000076 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000077 m_suppress_stop_hooks (false),
78 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000079{
Greg Clayton49ce6822010-10-31 03:01:06 +000080 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
81 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
82 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000083
84 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000085
Greg Claytone005f2c2010-11-06 01:53:30 +000086 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000087 if (log)
88 log->Printf ("%p Target::Target()", this);
89}
90
91//----------------------------------------------------------------------
92// Destructor
93//----------------------------------------------------------------------
94Target::~Target()
95{
Greg Claytone005f2c2010-11-06 01:53:30 +000096 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000097 if (log)
98 log->Printf ("%p Target::~Target()", this);
99 DeleteCurrentProcess ();
100}
101
102void
Caroline Tice7826c882010-10-26 03:11:13 +0000103Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000104{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000105// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000106 if (description_level != lldb::eDescriptionLevelBrief)
107 {
108 s->Indent();
109 s->PutCString("Target\n");
110 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000111 m_images.Dump(s);
112 m_breakpoint_list.Dump(s);
113 m_internal_breakpoint_list.Dump(s);
114 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000115 }
116 else
117 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000118 Module *exe_module = GetExecutableModulePointer();
119 if (exe_module)
120 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000121 else
122 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000123 }
Chris Lattner24943d22010-06-08 16:52:24 +0000124}
125
126void
127Target::DeleteCurrentProcess ()
128{
129 if (m_process_sp.get())
130 {
Greg Clayton49480b12010-09-14 23:52:43 +0000131 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000132 if (m_process_sp->IsAlive())
133 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000134
135 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000136
137 // Do any cleanup of the target we need to do between process instances.
138 // NB It is better to do this before destroying the process in case the
139 // clean up needs some help from the process.
140 m_breakpoint_list.ClearAllBreakpointSites();
141 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000142 // Disable watchpoints just on the debugger side.
Johnny Chen116a5cd2012-02-25 06:44:30 +0000143 Mutex::Locker locker;
144 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000145 DisableAllWatchpoints(false);
Johnny Chen116a5cd2012-02-25 06:44:30 +0000146 ClearAllWatchpointHitCounts();
Chris Lattner24943d22010-06-08 16:52:24 +0000147 m_process_sp.reset();
148 }
149}
150
151const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000152Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000153{
154 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000155 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000156 return m_process_sp;
157}
158
159const lldb::ProcessSP &
160Target::GetProcessSP () const
161{
162 return m_process_sp;
163}
164
Greg Clayton153ccd72011-08-10 02:10:13 +0000165void
166Target::Destroy()
167{
168 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000169 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000170 DeleteCurrentProcess ();
171 m_platform_sp.reset();
172 m_arch.Clear();
173 m_images.Clear();
174 m_section_load_list.Clear();
175 const bool notify = false;
176 m_breakpoint_list.RemoveAll(notify);
177 m_internal_breakpoint_list.RemoveAll(notify);
178 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000179 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000180 m_search_filter_sp.reset();
181 m_image_search_paths.Clear(notify);
182 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000183 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000184 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000185 m_persistent_variables.Clear();
186 m_stop_hooks.clear();
187 m_stop_hook_next_id = 0;
188 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000189 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000190}
191
192
Chris Lattner24943d22010-06-08 16:52:24 +0000193BreakpointList &
194Target::GetBreakpointList(bool internal)
195{
196 if (internal)
197 return m_internal_breakpoint_list;
198 else
199 return m_breakpoint_list;
200}
201
202const BreakpointList &
203Target::GetBreakpointList(bool internal) const
204{
205 if (internal)
206 return m_internal_breakpoint_list;
207 else
208 return m_breakpoint_list;
209}
210
211BreakpointSP
212Target::GetBreakpointByID (break_id_t break_id)
213{
214 BreakpointSP bp_sp;
215
216 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
217 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
218 else
219 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
220
221 return bp_sp;
222}
223
224BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000225Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
226 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000227 RegularExpression &source_regex,
228 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000229{
Jim Inghamd6d47972011-09-23 00:54:11 +0000230 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
231 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000232 return CreateBreakpoint (filter_sp, resolver_sp, internal);
233}
234
235
236BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000237Target::CreateBreakpoint (const FileSpecList *containingModules,
238 const FileSpec &file,
239 uint32_t line_no,
240 bool check_inlines,
241 LazyBool skip_prologue,
242 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000243{
244 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000245
246 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines,
247 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000248 return CreateBreakpoint (filter_sp, resolver_sp, internal);
249}
250
251
252BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000253Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000254{
Chris Lattner24943d22010-06-08 16:52:24 +0000255 Address so_addr;
256 // Attempt to resolve our load address if possible, though it is ok if
257 // it doesn't resolve to section/offset.
258
Greg Clayton33ed1702010-08-24 00:45:41 +0000259 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000260 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000261 if (!so_addr.IsValid())
262 {
263 // The address didn't resolve, so just set this as an absolute address
264 so_addr.SetOffset (addr);
265 }
266 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000267 return bp_sp;
268}
269
270BreakpointSP
271Target::CreateBreakpoint (Address &addr, bool internal)
272{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000273 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000274 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
275 return CreateBreakpoint (filter_sp, resolver_sp, internal);
276}
277
278BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000279Target::CreateBreakpoint (const FileSpecList *containingModules,
280 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000281 const char *func_name,
282 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000283 LazyBool skip_prologue,
284 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000285{
Greg Clayton12bec712010-06-28 21:30:43 +0000286 BreakpointSP bp_sp;
287 if (func_name)
288 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000289 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000290
291 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
292 func_name,
293 func_name_type_mask,
294 Breakpoint::Exact,
295 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000296 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
297 }
298 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000299}
300
Jim Ingham4722b102012-03-06 00:37:27 +0000301lldb::BreakpointSP
302Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000303 const FileSpecList *containingSourceFiles,
304 const std::vector<std::string> &func_names,
305 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000306 LazyBool skip_prologue,
307 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000308{
309 BreakpointSP bp_sp;
310 size_t num_names = func_names.size();
311 if (num_names > 0)
312 {
313 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
314
315 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
316 func_names,
317 func_name_type_mask,
318 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
319 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
320 }
321 return bp_sp;
322}
323
Jim Inghamc1053622012-03-03 02:05:11 +0000324BreakpointSP
325Target::CreateBreakpoint (const FileSpecList *containingModules,
326 const FileSpecList *containingSourceFiles,
327 const char *func_names[],
328 size_t num_names,
329 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000330 LazyBool skip_prologue,
331 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000332{
333 BreakpointSP bp_sp;
334 if (num_names > 0)
335 {
336 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
337
338 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
339 func_names,
340 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000341 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000342 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
343 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
344 }
345 return bp_sp;
346}
Chris Lattner24943d22010-06-08 16:52:24 +0000347
348SearchFilterSP
349Target::GetSearchFilterForModule (const FileSpec *containingModule)
350{
351 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000352 if (containingModule != NULL)
353 {
354 // TODO: We should look into sharing module based search filters
355 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000356 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000357 }
358 else
359 {
360 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000361 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000362 filter_sp = m_search_filter_sp;
363 }
364 return filter_sp;
365}
366
Jim Ingham03c8ee52011-09-21 01:17:13 +0000367SearchFilterSP
368Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
369{
370 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000371 if (containingModules && containingModules->GetSize() != 0)
372 {
373 // TODO: We should look into sharing module based search filters
374 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000375 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000376 }
377 else
378 {
379 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000380 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000381 filter_sp = m_search_filter_sp;
382 }
383 return filter_sp;
384}
385
Jim Inghamd6d47972011-09-23 00:54:11 +0000386SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000387Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
388 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000389{
390 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
391 return GetSearchFilterForModuleList(containingModules);
392
393 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000394 if (containingModules == NULL)
395 {
396 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
397 // but that will take a little reworking.
398
Greg Clayton13d24fb2012-01-29 20:56:30 +0000399 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000400 }
401 else
402 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000403 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000404 }
405 return filter_sp;
406}
407
Chris Lattner24943d22010-06-08 16:52:24 +0000408BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000409Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000410 const FileSpecList *containingSourceFiles,
411 RegularExpression &func_regex,
412 LazyBool skip_prologue,
413 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000414{
Jim Inghamd6d47972011-09-23 00:54:11 +0000415 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000416 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
417 func_regex,
418 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000419
420 return CreateBreakpoint (filter_sp, resolver_sp, internal);
421}
422
Jim Ingham3df164e2012-03-05 04:47:34 +0000423lldb::BreakpointSP
424Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
425{
426 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
427}
428
Chris Lattner24943d22010-06-08 16:52:24 +0000429BreakpointSP
430Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
431{
432 BreakpointSP bp_sp;
433 if (filter_sp && resolver_sp)
434 {
435 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
436 resolver_sp->SetBreakpoint (bp_sp.get());
437
438 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000439 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000440 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000441 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000442
Greg Claytone005f2c2010-11-06 01:53:30 +0000443 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000444 if (log)
445 {
446 StreamString s;
447 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
448 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
449 }
450
Chris Lattner24943d22010-06-08 16:52:24 +0000451 bp_sp->ResolveBreakpoint();
452 }
Jim Inghamd1686902010-10-14 23:45:03 +0000453
454 if (!internal && bp_sp)
455 {
456 m_last_created_breakpoint = bp_sp;
457 }
458
Chris Lattner24943d22010-06-08 16:52:24 +0000459 return bp_sp;
460}
461
Johnny Chenda5a8022011-09-20 23:28:55 +0000462bool
463Target::ProcessIsValid()
464{
465 return (m_process_sp && m_process_sp->IsAlive());
466}
467
Johnny Chenecd4feb2011-10-14 00:42:25 +0000468// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000469// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000470WatchpointSP
471Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type)
Johnny Chen34bbf852011-09-12 23:38:44 +0000472{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000473 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
474 if (log)
475 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
476 __FUNCTION__, addr, size, type);
477
Johnny Chenecd4feb2011-10-14 00:42:25 +0000478 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000479 if (!ProcessIsValid())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000480 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000481 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000482 return wp_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000483
Johnny Chenecd4feb2011-10-14 00:42:25 +0000484 // Currently we only support one watchpoint per address, with total number
485 // of watchpoints limited by the hardware which the inferior is running on.
486 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000487 if (matched_sp)
488 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000489 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000490 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000491 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
492 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000493 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000494 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000495 wp_sp = matched_sp;
496 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000497 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000498 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000499 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000500 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000501 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000502 }
503
Johnny Chenecd4feb2011-10-14 00:42:25 +0000504 if (!wp_sp) {
505 Watchpoint *new_wp = new Watchpoint(addr, size);
506 if (!new_wp) {
507 printf("Watchpoint ctor failed, out of memory?\n");
508 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000509 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000510 new_wp->SetWatchpointType(type);
511 new_wp->SetTarget(this);
512 wp_sp.reset(new_wp);
513 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000514 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000515
Johnny Chenecd4feb2011-10-14 00:42:25 +0000516 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000517 if (log)
518 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
519 __FUNCTION__,
520 rc.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000521 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000522
Johnny Chen155599b2012-03-26 22:00:10 +0000523 if (rc.Fail()) {
524 // Enabling the watchpoint on the device side failed.
525 // Remove the said watchpoint from the list maintained by the target instance.
526 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000527 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000528 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000529 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000530 m_last_created_watchpoint = wp_sp;
531 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000532}
533
Chris Lattner24943d22010-06-08 16:52:24 +0000534void
535Target::RemoveAllBreakpoints (bool internal_also)
536{
Greg Claytone005f2c2010-11-06 01:53:30 +0000537 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000538 if (log)
539 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
540
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000541 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000542 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000543 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000544
545 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000546}
547
548void
549Target::DisableAllBreakpoints (bool internal_also)
550{
Greg Claytone005f2c2010-11-06 01:53:30 +0000551 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000552 if (log)
553 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
554
555 m_breakpoint_list.SetEnabledAll (false);
556 if (internal_also)
557 m_internal_breakpoint_list.SetEnabledAll (false);
558}
559
560void
561Target::EnableAllBreakpoints (bool internal_also)
562{
Greg Claytone005f2c2010-11-06 01:53:30 +0000563 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000564 if (log)
565 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
566
567 m_breakpoint_list.SetEnabledAll (true);
568 if (internal_also)
569 m_internal_breakpoint_list.SetEnabledAll (true);
570}
571
572bool
573Target::RemoveBreakpointByID (break_id_t break_id)
574{
Greg Claytone005f2c2010-11-06 01:53:30 +0000575 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000576 if (log)
577 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
578
579 if (DisableBreakpointByID (break_id))
580 {
581 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000582 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000583 else
Jim Inghamd1686902010-10-14 23:45:03 +0000584 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000585 if (m_last_created_breakpoint)
586 {
587 if (m_last_created_breakpoint->GetID() == break_id)
588 m_last_created_breakpoint.reset();
589 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000590 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000591 }
Chris Lattner24943d22010-06-08 16:52:24 +0000592 return true;
593 }
594 return false;
595}
596
597bool
598Target::DisableBreakpointByID (break_id_t break_id)
599{
Greg Claytone005f2c2010-11-06 01:53:30 +0000600 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000601 if (log)
602 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
603
604 BreakpointSP bp_sp;
605
606 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
607 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
608 else
609 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
610 if (bp_sp)
611 {
612 bp_sp->SetEnabled (false);
613 return true;
614 }
615 return false;
616}
617
618bool
619Target::EnableBreakpointByID (break_id_t break_id)
620{
Greg Claytone005f2c2010-11-06 01:53:30 +0000621 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000622 if (log)
623 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
624 __FUNCTION__,
625 break_id,
626 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
627
628 BreakpointSP bp_sp;
629
630 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
631 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
632 else
633 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
634
635 if (bp_sp)
636 {
637 bp_sp->SetEnabled (true);
638 return true;
639 }
640 return false;
641}
642
Johnny Chenc86582f2011-09-23 21:21:43 +0000643// The flag 'end_to_end', default to true, signifies that the operation is
644// performed end to end, for both the debugger and the debuggee.
645
Johnny Chenecd4feb2011-10-14 00:42:25 +0000646// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
647// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000648bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000649Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000650{
651 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
652 if (log)
653 log->Printf ("Target::%s\n", __FUNCTION__);
654
Johnny Chenc86582f2011-09-23 21:21:43 +0000655 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000656 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000657 return true;
658 }
659
660 // Otherwise, it's an end to end operation.
661
Johnny Chenda5a8022011-09-20 23:28:55 +0000662 if (!ProcessIsValid())
663 return false;
664
Johnny Chenecd4feb2011-10-14 00:42:25 +0000665 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000666 for (size_t i = 0; i < num_watchpoints; ++i)
667 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000668 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
669 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000670 return false;
671
Johnny Chenecd4feb2011-10-14 00:42:25 +0000672 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000673 if (rc.Fail())
674 return false;
675 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000676 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000677 return true; // Success!
678}
679
Johnny Chenecd4feb2011-10-14 00:42:25 +0000680// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
681// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000682bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000683Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000684{
685 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
686 if (log)
687 log->Printf ("Target::%s\n", __FUNCTION__);
688
Johnny Chenc86582f2011-09-23 21:21:43 +0000689 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000690 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000691 return true;
692 }
693
694 // Otherwise, it's an end to end operation.
695
Johnny Chenda5a8022011-09-20 23:28:55 +0000696 if (!ProcessIsValid())
697 return false;
698
Johnny Chenecd4feb2011-10-14 00:42:25 +0000699 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000700 for (size_t i = 0; i < num_watchpoints; ++i)
701 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000702 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
703 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000704 return false;
705
Johnny Chenecd4feb2011-10-14 00:42:25 +0000706 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000707 if (rc.Fail())
708 return false;
709 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000710 return true; // Success!
711}
712
Johnny Chenecd4feb2011-10-14 00:42:25 +0000713// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
714// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000715bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000716Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000717{
718 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
719 if (log)
720 log->Printf ("Target::%s\n", __FUNCTION__);
721
Johnny Chenc86582f2011-09-23 21:21:43 +0000722 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000723 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000724 return true;
725 }
726
727 // Otherwise, it's an end to end operation.
728
Johnny Chenda5a8022011-09-20 23:28:55 +0000729 if (!ProcessIsValid())
730 return false;
731
Johnny Chenecd4feb2011-10-14 00:42:25 +0000732 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000733 for (size_t i = 0; i < num_watchpoints; ++i)
734 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000735 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
736 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000737 return false;
738
Johnny Chenecd4feb2011-10-14 00:42:25 +0000739 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000740 if (rc.Fail())
741 return false;
742 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000743 return true; // Success!
744}
745
Johnny Chen116a5cd2012-02-25 06:44:30 +0000746// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
747bool
748Target::ClearAllWatchpointHitCounts ()
749{
750 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
751 if (log)
752 log->Printf ("Target::%s\n", __FUNCTION__);
753
754 size_t num_watchpoints = m_watchpoint_list.GetSize();
755 for (size_t i = 0; i < num_watchpoints; ++i)
756 {
757 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
758 if (!wp_sp)
759 return false;
760
761 wp_sp->ResetHitCount();
762 }
763 return true; // Success!
764}
765
Johnny Chenecd4feb2011-10-14 00:42:25 +0000766// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000767// during these operations.
768bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000769Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000770{
771 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
772 if (log)
773 log->Printf ("Target::%s\n", __FUNCTION__);
774
775 if (!ProcessIsValid())
776 return false;
777
Johnny Chenecd4feb2011-10-14 00:42:25 +0000778 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000779 for (size_t i = 0; i < num_watchpoints; ++i)
780 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000781 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
782 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000783 return false;
784
Johnny Chenecd4feb2011-10-14 00:42:25 +0000785 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000786 }
787 return true; // Success!
788}
789
Johnny Chenecd4feb2011-10-14 00:42:25 +0000790// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000791bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000792Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000793{
794 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
795 if (log)
796 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
797
798 if (!ProcessIsValid())
799 return false;
800
Johnny Chenecd4feb2011-10-14 00:42:25 +0000801 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
802 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000803 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000804 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000805 if (rc.Success())
806 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000807
Johnny Chen01acfa72011-09-22 18:04:58 +0000808 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000809 }
810 return false;
811}
812
Johnny Chenecd4feb2011-10-14 00:42:25 +0000813// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000814bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000815Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000816{
817 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
818 if (log)
819 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
820
821 if (!ProcessIsValid())
822 return false;
823
Johnny Chenecd4feb2011-10-14 00:42:25 +0000824 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
825 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000826 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000827 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000828 if (rc.Success())
829 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000830
Johnny Chen01acfa72011-09-22 18:04:58 +0000831 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000832 }
833 return false;
834}
835
Johnny Chenecd4feb2011-10-14 00:42:25 +0000836// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000837bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000838Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000839{
840 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
841 if (log)
842 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
843
Johnny Chenecd4feb2011-10-14 00:42:25 +0000844 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000845 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000846 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000847 return true;
848 }
849 return false;
850}
851
Johnny Chenecd4feb2011-10-14 00:42:25 +0000852// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000853bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000854Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000855{
856 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
857 if (log)
858 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
859
860 if (!ProcessIsValid())
861 return false;
862
Johnny Chenecd4feb2011-10-14 00:42:25 +0000863 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
864 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000865 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000866 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000867 return true;
868 }
869 return false;
870}
871
Chris Lattner24943d22010-06-08 16:52:24 +0000872ModuleSP
873Target::GetExecutableModule ()
874{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000875 return m_images.GetModuleAtIndex(0);
876}
877
878Module*
879Target::GetExecutableModulePointer ()
880{
881 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000882}
883
884void
885Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
886{
887 m_images.Clear();
888 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000889 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000890 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000891
892 if (executable_sp.get())
893 {
894 Timer scoped_timer (__PRETTY_FUNCTION__,
895 "Target::SetExecutableModule (executable = '%s/%s')",
896 executable_sp->GetFileSpec().GetDirectory().AsCString(),
897 executable_sp->GetFileSpec().GetFilename().AsCString());
898
899 m_images.Append(executable_sp); // The first image is our exectuable file
900
Jim Ingham7508e732010-08-09 23:31:02 +0000901 // 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 +0000902 if (!m_arch.IsValid())
903 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000904
Chris Lattner24943d22010-06-08 16:52:24 +0000905 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000906 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000907
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000908 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000909 {
910 executable_objfile->GetDependentModules(dependent_files);
911 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
912 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000913 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
914 FileSpec platform_dependent_file_spec;
915 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000916 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000917 else
918 platform_dependent_file_spec = dependent_file_spec;
919
Greg Clayton444fe992012-02-26 05:51:37 +0000920 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
921 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +0000922 if (image_module_sp.get())
923 {
Chris Lattner24943d22010-06-08 16:52:24 +0000924 ObjectFile *objfile = image_module_sp->GetObjectFile();
925 if (objfile)
926 objfile->GetDependentModules(dependent_files);
927 }
928 }
929 }
Chris Lattner24943d22010-06-08 16:52:24 +0000930 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000931
932 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000933}
934
935
Jim Ingham7508e732010-08-09 23:31:02 +0000936bool
937Target::SetArchitecture (const ArchSpec &arch_spec)
938{
Greg Claytonb170aee2012-05-08 01:45:38 +0000939 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000940 {
Greg Claytonb170aee2012-05-08 01:45:38 +0000941 // If we haven't got a valid arch spec, or the architectures are
942 // compatible, so just update the architecture. Architectures can be
943 // equal, yet the triple OS and vendor might change, so we need to do
944 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000945 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000946 return true;
947 }
948 else
949 {
950 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000951 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000952 ModuleSP executable_sp = GetExecutableModule ();
953 m_images.Clear();
954 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000955 m_scratch_ast_source_ap.reset();
956 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000957 // Need to do something about unsetting breakpoints.
958
959 if (executable_sp)
960 {
Greg Clayton444fe992012-02-26 05:51:37 +0000961 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
962 Error error = ModuleList::GetSharedModule (module_spec,
963 executable_sp,
964 &GetExecutableSearchPaths(),
965 NULL,
966 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +0000967
968 if (!error.Fail() && executable_sp)
969 {
970 SetExecutableModule (executable_sp, true);
971 return true;
972 }
Jim Ingham7508e732010-08-09 23:31:02 +0000973 }
974 }
Greg Claytonb170aee2012-05-08 01:45:38 +0000975 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000976}
Chris Lattner24943d22010-06-08 16:52:24 +0000977
Chris Lattner24943d22010-06-08 16:52:24 +0000978void
979Target::ModuleAdded (ModuleSP &module_sp)
980{
981 // A module is being added to this target for the first time
982 ModuleList module_list;
983 module_list.Append(module_sp);
984 ModulesDidLoad (module_list);
985}
986
987void
988Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
989{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000990 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +0000991 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000992}
993
994void
995Target::ModulesDidLoad (ModuleList &module_list)
996{
997 m_breakpoint_list.UpdateBreakpoints (module_list, true);
998 // TODO: make event data that packages up the module_list
999 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1000}
1001
1002void
1003Target::ModulesDidUnload (ModuleList &module_list)
1004{
1005 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +00001006
1007 // Remove the images from the target image list
1008 m_images.Remove(module_list);
1009
Chris Lattner24943d22010-06-08 16:52:24 +00001010 // TODO: make event data that packages up the module_list
1011 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1012}
1013
Jim Ingham7089d8a2011-10-28 23:14:11 +00001014
Daniel Dunbar705a0982011-10-31 22:50:37 +00001015bool
Greg Clayton444fe992012-02-26 05:51:37 +00001016Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001017{
1018
1019 if (!m_breakpoints_use_platform_avoid)
1020 return false;
1021 else
1022 {
1023 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001024 ModuleSpec module_spec (module_file_spec);
1025 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001026
1027 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1028 // black list.
1029 if (num_modules > 0)
1030 {
1031 for (int i = 0; i < num_modules; i++)
1032 {
1033 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1034 return false;
1035 }
1036 return true;
1037 }
1038 else
1039 return false;
1040 }
1041}
1042
Daniel Dunbar705a0982011-10-31 22:50:37 +00001043bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001044Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1045{
1046 if (!m_breakpoints_use_platform_avoid)
1047 return false;
1048 else if (GetPlatform())
1049 {
1050 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
1051 }
1052 else
1053 return false;
1054}
1055
Chris Lattner24943d22010-06-08 16:52:24 +00001056size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001057Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1058{
Greg Clayton3508c382012-02-24 01:59:29 +00001059 SectionSP section_sp (addr.GetSection());
1060 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001061 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001062 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1063 if (section_sp->IsEncrypted())
1064 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001065 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001066 return 0;
1067 }
Greg Clayton3508c382012-02-24 01:59:29 +00001068 ModuleSP module_sp (section_sp->GetModule());
1069 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001070 {
Greg Clayton3508c382012-02-24 01:59:29 +00001071 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1072 if (objfile)
1073 {
1074 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1075 addr.GetOffset(),
1076 dst,
1077 dst_len);
1078 if (bytes_read > 0)
1079 return bytes_read;
1080 else
1081 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1082 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001083 else
Greg Clayton3508c382012-02-24 01:59:29 +00001084 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001085 }
1086 else
Greg Clayton3508c382012-02-24 01:59:29 +00001087 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001088 }
1089 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001090 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001091
Greg Clayton26100dc2011-01-07 01:57:07 +00001092 return 0;
1093}
1094
1095size_t
Enrico Granata91544802011-09-06 19:20:51 +00001096Target::ReadMemory (const Address& addr,
1097 bool prefer_file_cache,
1098 void *dst,
1099 size_t dst_len,
1100 Error &error,
1101 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001102{
Chris Lattner24943d22010-06-08 16:52:24 +00001103 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001104
Enrico Granata91544802011-09-06 19:20:51 +00001105 // if we end up reading this from process memory, we will fill this
1106 // with the actual load address
1107 if (load_addr_ptr)
1108 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1109
Greg Clayton26100dc2011-01-07 01:57:07 +00001110 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001111
1112 addr_t load_addr = LLDB_INVALID_ADDRESS;
1113 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001114 Address resolved_addr;
1115 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001116 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001117 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001118 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001119 // No sections are loaded, so we must assume we are not running
1120 // yet and anything we are given is a file address.
1121 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1122 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001123 }
Greg Clayton70436352010-06-30 23:03:03 +00001124 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001125 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001126 // We have at least one section loaded. This can be becuase
1127 // we have manually loaded some sections with "target modules load ..."
1128 // or because we have have a live process that has sections loaded
1129 // through the dynamic loader
1130 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1131 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001132 }
Greg Clayton70436352010-06-30 23:03:03 +00001133 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001134 if (!resolved_addr.IsValid())
1135 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001136
Greg Clayton9b82f862011-07-11 05:12:02 +00001137
Greg Clayton26100dc2011-01-07 01:57:07 +00001138 if (prefer_file_cache)
1139 {
1140 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1141 if (bytes_read > 0)
1142 return bytes_read;
1143 }
Greg Clayton70436352010-06-30 23:03:03 +00001144
Johnny Chenda5a8022011-09-20 23:28:55 +00001145 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001146 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001147 if (load_addr == LLDB_INVALID_ADDRESS)
1148 load_addr = resolved_addr.GetLoadAddress (this);
1149
Greg Clayton70436352010-06-30 23:03:03 +00001150 if (load_addr == LLDB_INVALID_ADDRESS)
1151 {
Greg Clayton3508c382012-02-24 01:59:29 +00001152 ModuleSP addr_module_sp (resolved_addr.GetModule());
1153 if (addr_module_sp && addr_module_sp->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001154 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001155 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001156 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001157 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001158 else
Greg Clayton9c236732011-10-26 00:56:27 +00001159 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001160 }
1161 else
1162 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001163 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001164 if (bytes_read != dst_len)
1165 {
1166 if (error.Success())
1167 {
1168 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001169 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001170 else
Greg Clayton9c236732011-10-26 00:56:27 +00001171 error.SetErrorStringWithFormat("only %zu of %zu bytes were read from memory at 0x%llx", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001172 }
1173 }
Greg Clayton70436352010-06-30 23:03:03 +00001174 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001175 {
1176 if (load_addr_ptr)
1177 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001178 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001179 }
Greg Clayton70436352010-06-30 23:03:03 +00001180 // If the address is not section offset we have an address that
1181 // doesn't resolve to any address in any currently loaded shared
1182 // libaries and we failed to read memory so there isn't anything
1183 // more we can do. If it is section offset, we might be able to
1184 // read cached memory from the object file.
1185 if (!resolved_addr.IsSectionOffset())
1186 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001187 }
Chris Lattner24943d22010-06-08 16:52:24 +00001188 }
Greg Clayton70436352010-06-30 23:03:03 +00001189
Greg Clayton9b82f862011-07-11 05:12:02 +00001190 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001191 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001192 // If we didn't already try and read from the object file cache, then
1193 // try it after failing to read from the process.
1194 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001195 }
1196 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001197}
1198
Greg Clayton7dd98df2011-07-12 17:06:17 +00001199size_t
1200Target::ReadScalarIntegerFromMemory (const Address& addr,
1201 bool prefer_file_cache,
1202 uint32_t byte_size,
1203 bool is_signed,
1204 Scalar &scalar,
1205 Error &error)
1206{
1207 uint64_t uval;
1208
1209 if (byte_size <= sizeof(uval))
1210 {
1211 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1212 if (bytes_read == byte_size)
1213 {
1214 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1215 uint32_t offset = 0;
1216 if (byte_size <= 4)
1217 scalar = data.GetMaxU32 (&offset, byte_size);
1218 else
1219 scalar = data.GetMaxU64 (&offset, byte_size);
1220
1221 if (is_signed)
1222 scalar.SignExtend(byte_size * 8);
1223 return bytes_read;
1224 }
1225 }
1226 else
1227 {
1228 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1229 }
1230 return 0;
1231}
1232
1233uint64_t
1234Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1235 bool prefer_file_cache,
1236 size_t integer_byte_size,
1237 uint64_t fail_value,
1238 Error &error)
1239{
1240 Scalar scalar;
1241 if (ReadScalarIntegerFromMemory (addr,
1242 prefer_file_cache,
1243 integer_byte_size,
1244 false,
1245 scalar,
1246 error))
1247 return scalar.ULongLong(fail_value);
1248 return fail_value;
1249}
1250
1251bool
1252Target::ReadPointerFromMemory (const Address& addr,
1253 bool prefer_file_cache,
1254 Error &error,
1255 Address &pointer_addr)
1256{
1257 Scalar scalar;
1258 if (ReadScalarIntegerFromMemory (addr,
1259 prefer_file_cache,
1260 m_arch.GetAddressByteSize(),
1261 false,
1262 scalar,
1263 error))
1264 {
1265 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1266 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1267 {
1268 if (m_section_load_list.IsEmpty())
1269 {
1270 // No sections are loaded, so we must assume we are not running
1271 // yet and anything we are given is a file address.
1272 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1273 }
1274 else
1275 {
1276 // We have at least one section loaded. This can be becuase
1277 // we have manually loaded some sections with "target modules load ..."
1278 // or because we have have a live process that has sections loaded
1279 // through the dynamic loader
1280 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1281 }
1282 // We weren't able to resolve the pointer value, so just return
1283 // an address with no section
1284 if (!pointer_addr.IsValid())
1285 pointer_addr.SetOffset (pointer_vm_addr);
1286 return true;
1287
1288 }
1289 }
1290 return false;
1291}
Chris Lattner24943d22010-06-08 16:52:24 +00001292
1293ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001294Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001295{
Chris Lattner24943d22010-06-08 16:52:24 +00001296 ModuleSP module_sp;
1297
Chris Lattner24943d22010-06-08 16:52:24 +00001298 Error error;
1299
Jim Ingham03e5e512012-05-17 18:38:42 +00001300 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1301 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001302
Jim Ingham03e5e512012-05-17 18:38:42 +00001303 if (module_spec.GetUUID().IsValid())
1304 module_sp = m_images.FindFirstModule(module_spec);
1305
Greg Claytone1ef1e32012-04-27 00:58:27 +00001306 if (!module_sp)
1307 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001308 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1309 bool did_create_module = false;
1310
1311 // If there are image search path entries, try to use them first to acquire a suitable image.
1312 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001313 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001314 ModuleSpec transformed_spec (module_spec);
1315 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1316 {
1317 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1318 error = ModuleList::GetSharedModule (transformed_spec,
1319 module_sp,
1320 &GetExecutableSearchPaths(),
1321 &old_module_sp,
1322 &did_create_module);
1323 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001324 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001325
Greg Claytone1ef1e32012-04-27 00:58:27 +00001326 if (!module_sp)
1327 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001328 // If we have a UUID, we can check our global shared module list in case
1329 // we already have it. If we don't have a valid UUID, then we can't since
1330 // the path in "module_spec" will be a platform path, and we will need to
1331 // let the platform find that file. For example, we could be asking for
1332 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1333 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1334 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1335 // cache.
1336 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001337 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001338 // We have a UUID, it is OK to check the global module list...
1339 error = ModuleList::GetSharedModule (module_spec,
1340 module_sp,
1341 &GetExecutableSearchPaths(),
1342 &old_module_sp,
1343 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001344 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001345
1346 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001347 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001348 // The platform is responsible for finding and caching an appropriate
1349 // module in the shared module cache.
1350 if (m_platform_sp)
1351 {
1352 FileSpec platform_file_spec;
1353 error = m_platform_sp->GetSharedModule (module_spec,
1354 module_sp,
1355 &GetExecutableSearchPaths(),
1356 &old_module_sp,
1357 &did_create_module);
1358 }
1359 else
1360 {
1361 error.SetErrorString("no platform is currently set");
1362 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001363 }
1364 }
Chris Lattner24943d22010-06-08 16:52:24 +00001365
Jim Ingham03e5e512012-05-17 18:38:42 +00001366 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1367 // module in the list already, and if there was, let's remove it.
1368 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001369 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001370 // GetSharedModule is not guaranteed to find the old shared module, for instance
1371 // in the common case where you pass in the UUID, it is only going to find the one
1372 // module matching the UUID. In fact, it has no good way to know what the "old module"
1373 // relevant to this target is, since there might be many copies of a module with this file spec
1374 // in various running debug sessions, but only one of them will belong to this target.
1375 // So let's remove the UUID from the module list, and look in the target's module list.
1376 // Only do this if there is SOMETHING else in the module spec...
1377 if (!old_module_sp)
1378 {
1379 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
1380 {
1381 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1382 module_spec_copy.GetUUID().Clear();
1383
1384 ModuleList found_modules;
1385 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1386 if (num_found == 1)
1387 {
1388 old_module_sp = found_modules.GetModuleAtIndex(0);
1389 }
1390 }
1391 }
1392
1393 m_images.Append (module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001394 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
Jim Ingham03e5e512012-05-17 18:38:42 +00001395 {
Chris Lattner24943d22010-06-08 16:52:24 +00001396 ModuleUpdated(old_module_sp, module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001397 m_images.Remove (old_module_sp);
1398 Module *old_module_ptr = old_module_sp.get();
1399 old_module_sp.reset();
1400 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1401 }
Chris Lattner24943d22010-06-08 16:52:24 +00001402 else
1403 ModuleAdded(module_sp);
1404 }
1405 }
1406 if (error_ptr)
1407 *error_ptr = error;
1408 return module_sp;
1409}
1410
1411
Greg Clayton289afcb2012-02-18 05:35:26 +00001412TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001413Target::CalculateTarget ()
1414{
Greg Clayton289afcb2012-02-18 05:35:26 +00001415 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001416}
1417
Greg Clayton289afcb2012-02-18 05:35:26 +00001418ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001419Target::CalculateProcess ()
1420{
Greg Clayton289afcb2012-02-18 05:35:26 +00001421 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001422}
1423
Greg Clayton289afcb2012-02-18 05:35:26 +00001424ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001425Target::CalculateThread ()
1426{
Greg Clayton289afcb2012-02-18 05:35:26 +00001427 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001428}
1429
Greg Clayton289afcb2012-02-18 05:35:26 +00001430StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001431Target::CalculateStackFrame ()
1432{
Greg Clayton289afcb2012-02-18 05:35:26 +00001433 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001434}
1435
1436void
Greg Claytona830adb2010-10-04 01:05:56 +00001437Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001438{
Greg Clayton567e7f32011-09-22 04:58:26 +00001439 exe_ctx.Clear();
1440 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001441}
1442
1443PathMappingList &
1444Target::GetImageSearchPathList ()
1445{
1446 return m_image_search_paths;
1447}
1448
1449void
1450Target::ImageSearchPathsChanged
1451(
1452 const PathMappingList &path_list,
1453 void *baton
1454)
1455{
1456 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001457 ModuleSP exe_module_sp (target->GetExecutableModule());
1458 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001459 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001460 target->m_images.Clear();
1461 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001462 }
1463}
1464
1465ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001466Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001467{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001468 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001469 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001470 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001471 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001472 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001473 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1474 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1475 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1476 }
Chris Lattner24943d22010-06-08 16:52:24 +00001477 return m_scratch_ast_context_ap.get();
1478}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001479
Sean Callanan4938bd62011-11-16 18:20:47 +00001480ClangASTImporter *
1481Target::GetClangASTImporter()
1482{
1483 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1484
1485 if (!ast_importer)
1486 {
1487 ast_importer = new ClangASTImporter();
1488 m_ast_importer_ap.reset(ast_importer);
1489 }
1490
1491 return ast_importer;
1492}
1493
Greg Clayton990de7b2010-11-18 23:32:35 +00001494void
Caroline Tice2a456812011-03-10 22:14:10 +00001495Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001496{
Greg Clayton334d33a2012-01-30 07:41:31 +00001497 UserSettingsController::InitializeSettingsController (GetSettingsController(),
Greg Clayton990de7b2010-11-18 23:32:35 +00001498 SettingsController::global_settings_table,
1499 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001500
1501 // Now call SettingsInitialize() on each 'child' setting of Target
1502 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001503}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001504
Greg Clayton990de7b2010-11-18 23:32:35 +00001505void
Caroline Tice2a456812011-03-10 22:14:10 +00001506Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001507{
Caroline Tice2a456812011-03-10 22:14:10 +00001508
1509 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1510
1511 Process::SettingsTerminate ();
1512
1513 // Now terminate Target Settings.
1514
Greg Clayton990de7b2010-11-18 23:32:35 +00001515 UserSettingsControllerSP &usc = GetSettingsController();
1516 UserSettingsController::FinalizeSettingsController (usc);
1517 usc.reset();
1518}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001519
Greg Clayton990de7b2010-11-18 23:32:35 +00001520UserSettingsControllerSP &
1521Target::GetSettingsController ()
1522{
Greg Clayton334d33a2012-01-30 07:41:31 +00001523 static UserSettingsControllerSP g_settings_controller_sp;
1524 if (!g_settings_controller_sp)
1525 {
1526 g_settings_controller_sp.reset (new Target::SettingsController);
1527 // The first shared pointer to Target::SettingsController in
1528 // g_settings_controller_sp must be fully created above so that
1529 // the TargetInstanceSettings can use a weak_ptr to refer back
1530 // to the master setttings controller
1531 InstanceSettingsSP default_instance_settings_sp (new TargetInstanceSettings (g_settings_controller_sp,
1532 false,
1533 InstanceSettings::GetDefaultName().AsCString()));
1534 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
1535 }
1536 return g_settings_controller_sp;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001537}
1538
Greg Clayton9ce95382012-02-13 23:10:39 +00001539FileSpecList
1540Target::GetDefaultExecutableSearchPaths ()
1541{
1542 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1543 if (settings_controller_sp)
1544 {
1545 lldb::InstanceSettingsSP instance_settings_sp (settings_controller_sp->GetDefaultInstanceSettings ());
1546 if (instance_settings_sp)
1547 return static_cast<TargetInstanceSettings *>(instance_settings_sp.get())->GetExecutableSearchPaths ();
1548 }
1549 return FileSpecList();
1550}
1551
1552
Caroline Tice5bc8c972010-09-20 20:44:43 +00001553ArchSpec
1554Target::GetDefaultArchitecture ()
1555{
Greg Clayton940b1032011-02-23 00:35:02 +00001556 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
Greg Clayton940b1032011-02-23 00:35:02 +00001557 if (settings_controller_sp)
Greg Clayton469e08d2012-05-15 02:44:13 +00001558 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1559 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001560}
1561
1562void
Greg Clayton940b1032011-02-23 00:35:02 +00001563Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001564{
Greg Clayton940b1032011-02-23 00:35:02 +00001565 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1566
1567 if (settings_controller_sp)
1568 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001569}
1570
Greg Claytona830adb2010-10-04 01:05:56 +00001571Target *
1572Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1573{
1574 // The target can either exist in the "process" of ExecutionContext, or in
1575 // the "target_sp" member of SymbolContext. This accessor helper function
1576 // will get the target from one of these locations.
1577
1578 Target *target = NULL;
1579 if (sc_ptr != NULL)
1580 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001581 if (target == NULL && exe_ctx_ptr)
1582 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001583 return target;
1584}
1585
1586
Caroline Tice1ebef442010-09-27 00:30:10 +00001587void
1588Target::UpdateInstanceName ()
1589{
1590 StreamString sstr;
1591
Greg Clayton5beb99d2011-08-11 02:48:45 +00001592 Module *exe_module = GetExecutableModulePointer();
1593 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001594 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001595 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001596 exe_module->GetFileSpec().GetFilename().AsCString(),
1597 exe_module->GetArchitecture().GetArchitectureName());
1598 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001599 }
1600}
1601
Sean Callanan77e93942010-10-29 00:29:03 +00001602const char *
1603Target::GetExpressionPrefixContentsAsCString ()
1604{
Sean Callanane0b7f942011-11-16 01:54:57 +00001605 if (!m_expr_prefix_contents.empty())
1606 return m_expr_prefix_contents.c_str();
Greg Claytonff44ab42011-04-23 02:04:55 +00001607 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001608}
1609
Greg Clayton427f2902010-12-14 02:59:59 +00001610ExecutionResults
1611Target::EvaluateExpression
1612(
1613 const char *expr_cstr,
1614 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001615 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001616 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001617 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001618 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001619 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001620 lldb::ValueObjectSP &result_valobj_sp
1621)
1622{
1623 ExecutionResults execution_results = eExecutionSetupError;
1624
1625 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001626
1627 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1628 return execution_results;
1629
Jim Ingham3613ae12011-05-12 02:06:14 +00001630 // We shouldn't run stop hooks in expressions.
1631 // Be sure to reset this if you return anywhere within this function.
1632 bool old_suppress_value = m_suppress_stop_hooks;
1633 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001634
1635 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001636
1637 const size_t expr_cstr_len = ::strlen (expr_cstr);
1638
Greg Clayton427f2902010-12-14 02:59:59 +00001639 if (frame)
1640 {
1641 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001642 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001643 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001644 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1645 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001646 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001647
1648 // Make sure we don't have any things that we know a variable expression
1649 // won't be able to deal with before calling into it
1650 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1651 {
1652 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1653 use_dynamic,
1654 expr_path_options,
1655 var_sp,
1656 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001657 // if this expression results in a bitfield, we give up and let the IR handle it
1658 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1659 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001660 }
Greg Clayton427f2902010-12-14 02:59:59 +00001661 }
1662 else if (m_process_sp)
1663 {
1664 m_process_sp->CalculateExecutionContext(exe_ctx);
1665 }
1666 else
1667 {
1668 CalculateExecutionContext(exe_ctx);
1669 }
1670
1671 if (result_valobj_sp)
1672 {
1673 execution_results = eExecutionCompleted;
1674 // We got a result from the frame variable expression path above...
1675 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1676
1677 lldb::ValueObjectSP const_valobj_sp;
1678
1679 // Check in case our value is already a constant value
1680 if (result_valobj_sp->GetIsConstant())
1681 {
1682 const_valobj_sp = result_valobj_sp;
1683 const_valobj_sp->SetName (persistent_variable_name);
1684 }
1685 else
Jim Inghame41494a2011-04-16 00:01:13 +00001686 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001687 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001688 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001689 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001690 if (dynamic_sp)
1691 result_valobj_sp = dynamic_sp;
1692 }
1693
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001694 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001695 }
Greg Clayton427f2902010-12-14 02:59:59 +00001696
Sean Callanan6a925532011-01-13 08:53:35 +00001697 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1698
Greg Clayton427f2902010-12-14 02:59:59 +00001699 result_valobj_sp = const_valobj_sp;
1700
Sean Callanan6a925532011-01-13 08:53:35 +00001701 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1702 assert (clang_expr_variable_sp.get());
1703
1704 // Set flags and live data as appropriate
1705
1706 const Value &result_value = live_valobj_sp->GetValue();
1707
1708 switch (result_value.GetValueType())
1709 {
1710 case Value::eValueTypeHostAddress:
1711 case Value::eValueTypeFileAddress:
1712 // we don't do anything with these for now
1713 break;
1714 case Value::eValueTypeScalar:
1715 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1716 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1717 break;
1718 case Value::eValueTypeLoadAddress:
1719 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1720 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1721 break;
1722 }
Greg Clayton427f2902010-12-14 02:59:59 +00001723 }
1724 else
1725 {
1726 // Make sure we aren't just trying to see the value of a persistent
1727 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001728 lldb::ClangExpressionVariableSP persistent_var_sp;
1729 // Only check for persistent variables the expression starts with a '$'
1730 if (expr_cstr[0] == '$')
1731 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1732
Greg Clayton427f2902010-12-14 02:59:59 +00001733 if (persistent_var_sp)
1734 {
1735 result_valobj_sp = persistent_var_sp->GetValueObject ();
1736 execution_results = eExecutionCompleted;
1737 }
1738 else
1739 {
1740 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001741
Greg Clayton427f2902010-12-14 02:59:59 +00001742 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001743 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001744 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001745 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001746 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001747 expr_cstr,
1748 prefix,
1749 result_valobj_sp);
1750 }
1751 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001752
1753 m_suppress_stop_hooks = old_suppress_value;
1754
Greg Clayton427f2902010-12-14 02:59:59 +00001755 return execution_results;
1756}
1757
Greg Claytonc0fa5332011-05-22 22:46:53 +00001758lldb::addr_t
1759Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1760{
1761 addr_t code_addr = load_addr;
1762 switch (m_arch.GetMachine())
1763 {
1764 case llvm::Triple::arm:
1765 case llvm::Triple::thumb:
1766 switch (addr_class)
1767 {
1768 case eAddressClassData:
1769 case eAddressClassDebug:
1770 return LLDB_INVALID_ADDRESS;
1771
1772 case eAddressClassUnknown:
1773 case eAddressClassInvalid:
1774 case eAddressClassCode:
1775 case eAddressClassCodeAlternateISA:
1776 case eAddressClassRuntime:
1777 // Check if bit zero it no set?
1778 if ((code_addr & 1ull) == 0)
1779 {
1780 // Bit zero isn't set, check if the address is a multiple of 2?
1781 if (code_addr & 2ull)
1782 {
1783 // The address is a multiple of 2 so it must be thumb, set bit zero
1784 code_addr |= 1ull;
1785 }
1786 else if (addr_class == eAddressClassCodeAlternateISA)
1787 {
1788 // We checked the address and the address claims to be the alternate ISA
1789 // which means thumb, so set bit zero.
1790 code_addr |= 1ull;
1791 }
1792 }
1793 break;
1794 }
1795 break;
1796
1797 default:
1798 break;
1799 }
1800 return code_addr;
1801}
1802
1803lldb::addr_t
1804Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1805{
1806 addr_t opcode_addr = load_addr;
1807 switch (m_arch.GetMachine())
1808 {
1809 case llvm::Triple::arm:
1810 case llvm::Triple::thumb:
1811 switch (addr_class)
1812 {
1813 case eAddressClassData:
1814 case eAddressClassDebug:
1815 return LLDB_INVALID_ADDRESS;
1816
1817 case eAddressClassInvalid:
1818 case eAddressClassUnknown:
1819 case eAddressClassCode:
1820 case eAddressClassCodeAlternateISA:
1821 case eAddressClassRuntime:
1822 opcode_addr &= ~(1ull);
1823 break;
1824 }
1825 break;
1826
1827 default:
1828 break;
1829 }
1830 return opcode_addr;
1831}
1832
Jim Inghamd60d94a2011-03-11 03:53:59 +00001833lldb::user_id_t
1834Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1835{
1836 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001837 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001838 m_stop_hooks[new_uid] = new_hook_sp;
1839 return new_uid;
1840}
1841
1842bool
1843Target::RemoveStopHookByID (lldb::user_id_t user_id)
1844{
1845 size_t num_removed;
1846 num_removed = m_stop_hooks.erase (user_id);
1847 if (num_removed == 0)
1848 return false;
1849 else
1850 return true;
1851}
1852
1853void
1854Target::RemoveAllStopHooks ()
1855{
1856 m_stop_hooks.clear();
1857}
1858
1859Target::StopHookSP
1860Target::GetStopHookByID (lldb::user_id_t user_id)
1861{
1862 StopHookSP found_hook;
1863
1864 StopHookCollection::iterator specified_hook_iter;
1865 specified_hook_iter = m_stop_hooks.find (user_id);
1866 if (specified_hook_iter != m_stop_hooks.end())
1867 found_hook = (*specified_hook_iter).second;
1868 return found_hook;
1869}
1870
1871bool
1872Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1873{
1874 StopHookCollection::iterator specified_hook_iter;
1875 specified_hook_iter = m_stop_hooks.find (user_id);
1876 if (specified_hook_iter == m_stop_hooks.end())
1877 return false;
1878
1879 (*specified_hook_iter).second->SetIsActive (active_state);
1880 return true;
1881}
1882
1883void
1884Target::SetAllStopHooksActiveState (bool active_state)
1885{
1886 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1887 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1888 {
1889 (*pos).second->SetIsActive (active_state);
1890 }
1891}
1892
1893void
1894Target::RunStopHooks ()
1895{
Jim Ingham3613ae12011-05-12 02:06:14 +00001896 if (m_suppress_stop_hooks)
1897 return;
1898
Jim Inghamd60d94a2011-03-11 03:53:59 +00001899 if (!m_process_sp)
1900 return;
1901
1902 if (m_stop_hooks.empty())
1903 return;
1904
1905 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1906
1907 // If there aren't any active stop hooks, don't bother either:
1908 bool any_active_hooks = false;
1909 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1910 {
1911 if ((*pos).second->IsActive())
1912 {
1913 any_active_hooks = true;
1914 break;
1915 }
1916 }
1917 if (!any_active_hooks)
1918 return;
1919
1920 CommandReturnObject result;
1921
1922 std::vector<ExecutionContext> exc_ctx_with_reasons;
1923 std::vector<SymbolContext> sym_ctx_with_reasons;
1924
1925 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1926 size_t num_threads = cur_threadlist.GetSize();
1927 for (size_t i = 0; i < num_threads; i++)
1928 {
1929 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1930 if (cur_thread_sp->ThreadStoppedForAReason())
1931 {
1932 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1933 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1934 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1935 }
1936 }
1937
1938 // If no threads stopped for a reason, don't run the stop-hooks.
1939 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1940 if (num_exe_ctx == 0)
1941 return;
1942
Jim Inghame5ed8e92011-06-02 23:58:26 +00001943 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1944 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001945
1946 bool keep_going = true;
1947 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001948 bool print_hook_header;
1949 bool print_thread_header;
1950
1951 if (num_exe_ctx == 1)
1952 print_thread_header = false;
1953 else
1954 print_thread_header = true;
1955
1956 if (m_stop_hooks.size() == 1)
1957 print_hook_header = false;
1958 else
1959 print_hook_header = true;
1960
Jim Inghamd60d94a2011-03-11 03:53:59 +00001961 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1962 {
1963 // result.Clear();
1964 StopHookSP cur_hook_sp = (*pos).second;
1965 if (!cur_hook_sp->IsActive())
1966 continue;
1967
1968 bool any_thread_matched = false;
1969 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1970 {
1971 if ((cur_hook_sp->GetSpecifier () == NULL
1972 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1973 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00001974 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001975 {
1976 if (!hooks_ran)
1977 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001978 hooks_ran = true;
1979 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001980 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001981 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001982 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1983 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1984 NULL);
1985 if (cmd)
1986 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1987 else
1988 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001989 any_thread_matched = true;
1990 }
1991
Jim Inghamc54840c2011-03-22 01:47:27 +00001992 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001993 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001994
1995 bool stop_on_continue = true;
1996 bool stop_on_error = true;
1997 bool echo_commands = false;
1998 bool print_results = true;
1999 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002000 &exc_ctx_with_reasons[i],
2001 stop_on_continue,
2002 stop_on_error,
2003 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002004 print_results,
2005 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002006 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002007
2008 // If the command started the target going again, we should bag out of
2009 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002010 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2011 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002012 {
Greg Clayton444e35b2011-10-19 18:09:39 +00002013 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002014 keep_going = false;
2015 }
2016 }
2017 }
2018 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002019
Caroline Tice4a348082011-05-02 20:41:46 +00002020 result.GetImmediateOutputStream()->Flush();
2021 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002022}
2023
Greg Claytonbbea1332011-07-08 00:48:09 +00002024
Jim Inghamd60d94a2011-03-11 03:53:59 +00002025//--------------------------------------------------------------
2026// class Target::StopHook
2027//--------------------------------------------------------------
2028
2029
2030Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2031 UserID (uid),
2032 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002033 m_commands (),
2034 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002035 m_thread_spec_ap(NULL),
2036 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002037{
2038}
2039
2040Target::StopHook::StopHook (const StopHook &rhs) :
2041 UserID (rhs.GetID()),
2042 m_target_sp (rhs.m_target_sp),
2043 m_commands (rhs.m_commands),
2044 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002045 m_thread_spec_ap (NULL),
2046 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002047{
2048 if (rhs.m_thread_spec_ap.get() != NULL)
2049 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2050}
2051
2052
2053Target::StopHook::~StopHook ()
2054{
2055}
2056
2057void
2058Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2059{
2060 m_thread_spec_ap.reset (specifier);
2061}
2062
2063
2064void
2065Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2066{
2067 int indent_level = s->GetIndentLevel();
2068
2069 s->SetIndentLevel(indent_level + 2);
2070
Greg Clayton444e35b2011-10-19 18:09:39 +00002071 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002072 if (m_active)
2073 s->Indent ("State: enabled\n");
2074 else
2075 s->Indent ("State: disabled\n");
2076
2077 if (m_specifier_sp)
2078 {
2079 s->Indent();
2080 s->PutCString ("Specifier:\n");
2081 s->SetIndentLevel (indent_level + 4);
2082 m_specifier_sp->GetDescription (s, level);
2083 s->SetIndentLevel (indent_level + 2);
2084 }
2085
2086 if (m_thread_spec_ap.get() != NULL)
2087 {
2088 StreamString tmp;
2089 s->Indent("Thread:\n");
2090 m_thread_spec_ap->GetDescription (&tmp, level);
2091 s->SetIndentLevel (indent_level + 4);
2092 s->Indent (tmp.GetData());
2093 s->PutCString ("\n");
2094 s->SetIndentLevel (indent_level + 2);
2095 }
2096
2097 s->Indent ("Commands: \n");
2098 s->SetIndentLevel (indent_level + 4);
2099 uint32_t num_commands = m_commands.GetSize();
2100 for (uint32_t i = 0; i < num_commands; i++)
2101 {
2102 s->Indent(m_commands.GetStringAtIndex(i));
2103 s->PutCString ("\n");
2104 }
2105 s->SetIndentLevel (indent_level);
2106}
2107
2108
Caroline Tice5bc8c972010-09-20 20:44:43 +00002109//--------------------------------------------------------------
2110// class Target::SettingsController
2111//--------------------------------------------------------------
2112
2113Target::SettingsController::SettingsController () :
2114 UserSettingsController ("target", Debugger::GetSettingsController()),
2115 m_default_architecture ()
2116{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002117}
2118
2119Target::SettingsController::~SettingsController ()
2120{
2121}
2122
2123lldb::InstanceSettingsSP
2124Target::SettingsController::CreateInstanceSettings (const char *instance_name)
2125{
Greg Clayton334d33a2012-01-30 07:41:31 +00002126 lldb::InstanceSettingsSP new_settings_sp (new TargetInstanceSettings (GetSettingsController(),
2127 false,
2128 instance_name));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002129 return new_settings_sp;
2130}
2131
Caroline Tice5bc8c972010-09-20 20:44:43 +00002132
Greg Claytonabb33022011-11-08 02:43:13 +00002133#define TSC_DEFAULT_ARCH "default-arch"
2134#define TSC_EXPR_PREFIX "expr-prefix"
2135#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Enrico Granatadba1de82012-03-27 02:35:13 +00002136#define TSC_ENABLE_SYNTHETIC "enable-synthetic-value"
Greg Claytonabb33022011-11-08 02:43:13 +00002137#define TSC_SKIP_PROLOGUE "skip-prologue"
2138#define TSC_SOURCE_MAP "source-map"
Greg Clayton9ce95382012-02-13 23:10:39 +00002139#define TSC_EXE_SEARCH_PATHS "exec-search-paths"
Greg Claytonabb33022011-11-08 02:43:13 +00002140#define TSC_MAX_CHILDREN "max-children-count"
2141#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
2142#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
2143#define TSC_RUN_ARGS "run-args"
2144#define TSC_ENV_VARS "env-vars"
2145#define TSC_INHERIT_ENV "inherit-env"
2146#define TSC_STDIN_PATH "input-path"
2147#define TSC_STDOUT_PATH "output-path"
2148#define TSC_STDERR_PATH "error-path"
2149#define TSC_DISABLE_ASLR "disable-aslr"
2150#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytond284b662011-02-18 01:44:25 +00002151
2152
2153static const ConstString &
2154GetSettingNameForDefaultArch ()
2155{
2156 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00002157 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002158}
2159
Greg Claytond284b662011-02-18 01:44:25 +00002160static const ConstString &
2161GetSettingNameForExpressionPrefix ()
2162{
2163 static ConstString g_const_string (TSC_EXPR_PREFIX);
2164 return g_const_string;
2165}
2166
2167static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00002168GetSettingNameForPreferDynamicValue ()
2169{
2170 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2171 return g_const_string;
2172}
2173
Greg Claytonff44ab42011-04-23 02:04:55 +00002174static const ConstString &
Enrico Granatadba1de82012-03-27 02:35:13 +00002175GetSettingNameForEnableSyntheticValue ()
2176{
2177 static ConstString g_const_string (TSC_ENABLE_SYNTHETIC);
2178 return g_const_string;
2179}
2180
2181static const ConstString &
Greg Claytonff44ab42011-04-23 02:04:55 +00002182GetSettingNameForSourcePathMap ()
2183{
2184 static ConstString g_const_string (TSC_SOURCE_MAP);
2185 return g_const_string;
2186}
Greg Claytond284b662011-02-18 01:44:25 +00002187
Greg Clayton17cd9952011-04-22 03:55:06 +00002188static const ConstString &
Greg Clayton9ce95382012-02-13 23:10:39 +00002189GetSettingNameForExecutableSearchPaths ()
2190{
2191 static ConstString g_const_string (TSC_EXE_SEARCH_PATHS);
2192 return g_const_string;
2193}
2194
2195static const ConstString &
Greg Clayton17cd9952011-04-22 03:55:06 +00002196GetSettingNameForSkipPrologue ()
2197{
2198 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2199 return g_const_string;
2200}
2201
Enrico Granata018921d2011-08-12 02:00:06 +00002202static const ConstString &
2203GetSettingNameForMaxChildren ()
2204{
2205 static ConstString g_const_string (TSC_MAX_CHILDREN);
2206 return g_const_string;
2207}
Greg Clayton17cd9952011-04-22 03:55:06 +00002208
Enrico Granata91544802011-09-06 19:20:51 +00002209static const ConstString &
2210GetSettingNameForMaxStringSummaryLength ()
2211{
2212 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2213 return g_const_string;
2214}
Greg Clayton17cd9952011-04-22 03:55:06 +00002215
Jim Ingham7089d8a2011-10-28 23:14:11 +00002216static const ConstString &
2217GetSettingNameForPlatformAvoid ()
2218{
2219 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2220 return g_const_string;
2221}
2222
Greg Claytonabb33022011-11-08 02:43:13 +00002223const ConstString &
2224GetSettingNameForRunArgs ()
2225{
2226 static ConstString g_const_string (TSC_RUN_ARGS);
2227 return g_const_string;
2228}
2229
2230const ConstString &
2231GetSettingNameForEnvVars ()
2232{
2233 static ConstString g_const_string (TSC_ENV_VARS);
2234 return g_const_string;
2235}
2236
2237const ConstString &
2238GetSettingNameForInheritHostEnv ()
2239{
2240 static ConstString g_const_string (TSC_INHERIT_ENV);
2241 return g_const_string;
2242}
2243
2244const ConstString &
2245GetSettingNameForInputPath ()
2246{
2247 static ConstString g_const_string (TSC_STDIN_PATH);
2248 return g_const_string;
2249}
2250
2251const ConstString &
2252GetSettingNameForOutputPath ()
2253{
2254 static ConstString g_const_string (TSC_STDOUT_PATH);
2255 return g_const_string;
2256}
2257
2258const ConstString &
2259GetSettingNameForErrorPath ()
2260{
2261 static ConstString g_const_string (TSC_STDERR_PATH);
2262 return g_const_string;
2263}
2264
2265const ConstString &
2266GetSettingNameForDisableASLR ()
2267{
2268 static ConstString g_const_string (TSC_DISABLE_ASLR);
2269 return g_const_string;
2270}
2271
2272const ConstString &
2273GetSettingNameForDisableSTDIO ()
2274{
2275 static ConstString g_const_string (TSC_DISABLE_STDIO);
2276 return g_const_string;
2277}
Jim Ingham7089d8a2011-10-28 23:14:11 +00002278
Caroline Tice5bc8c972010-09-20 20:44:43 +00002279bool
2280Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2281 const char *index_value,
2282 const char *value,
2283 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002284 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002285 Error&err)
2286{
Greg Claytond284b662011-02-18 01:44:25 +00002287 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002288 {
Greg Claytonb170aee2012-05-08 01:45:38 +00002289 m_default_architecture.SetTriple (value);
Greg Clayton940b1032011-02-23 00:35:02 +00002290 if (!m_default_architecture.IsValid())
2291 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002292 }
2293 return true;
2294}
2295
2296
2297bool
2298Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2299 StringList &value,
2300 Error &err)
2301{
Greg Claytond284b662011-02-18 01:44:25 +00002302 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002303 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002304 // If the arch is invalid (the default), don't show a string for it
2305 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002306 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002307 return true;
2308 }
2309 else
2310 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2311
2312 return false;
2313}
2314
2315//--------------------------------------------------------------
2316// class TargetInstanceSettings
2317//--------------------------------------------------------------
2318
Greg Clayton638351a2010-12-04 00:10:17 +00002319TargetInstanceSettings::TargetInstanceSettings
2320(
Greg Clayton334d33a2012-01-30 07:41:31 +00002321 const lldb::UserSettingsControllerSP &owner_sp,
Greg Clayton638351a2010-12-04 00:10:17 +00002322 bool live_instance,
2323 const char *name
2324) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002325 InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002326 m_expr_prefix_file (),
Sean Callanane0b7f942011-11-16 01:54:57 +00002327 m_expr_prefix_contents (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002328 m_prefer_dynamic_value (2),
Enrico Granatadba1de82012-03-27 02:35:13 +00002329 m_enable_synthetic_value(true, true),
Greg Claytonff44ab42011-04-23 02:04:55 +00002330 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002331 m_source_map (NULL, NULL),
Greg Clayton9ce95382012-02-13 23:10:39 +00002332 m_exe_search_paths (),
Enrico Granata91544802011-09-06 19:20:51 +00002333 m_max_children_display(256),
Jim Ingham7089d8a2011-10-28 23:14:11 +00002334 m_max_strlen_length(1024),
Greg Claytonabb33022011-11-08 02:43:13 +00002335 m_breakpoints_use_platform_avoid (true, true),
2336 m_run_args (),
2337 m_env_vars (),
2338 m_input_path (),
2339 m_output_path (),
2340 m_error_path (),
2341 m_disable_aslr (true),
2342 m_disable_stdio (false),
2343 m_inherit_host_env (true),
2344 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002345{
2346 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2347 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2348 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2349 // This is true for CreateInstanceName() too.
2350
2351 if (GetInstanceName () == InstanceSettings::InvalidName())
2352 {
2353 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Clayton334d33a2012-01-30 07:41:31 +00002354 owner_sp->RegisterInstanceSettings (this);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002355 }
2356
2357 if (live_instance)
2358 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002359 const lldb::InstanceSettingsSP &pending_settings = owner_sp->FindPendingSettings (m_instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002360 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002361 }
2362}
2363
2364TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002365 InstanceSettings (Target::GetSettingsController(), CreateInstanceName().AsCString()),
Greg Claytonff44ab42011-04-23 02:04:55 +00002366 m_expr_prefix_file (rhs.m_expr_prefix_file),
Sean Callanane0b7f942011-11-16 01:54:57 +00002367 m_expr_prefix_contents (rhs.m_expr_prefix_contents),
Greg Claytonff44ab42011-04-23 02:04:55 +00002368 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
Enrico Granatadba1de82012-03-27 02:35:13 +00002369 m_enable_synthetic_value(rhs.m_enable_synthetic_value),
Greg Claytonff44ab42011-04-23 02:04:55 +00002370 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002371 m_source_map (rhs.m_source_map),
Greg Clayton9ce95382012-02-13 23:10:39 +00002372 m_exe_search_paths (rhs.m_exe_search_paths),
Greg Claytonabb33022011-11-08 02:43:13 +00002373 m_max_children_display (rhs.m_max_children_display),
2374 m_max_strlen_length (rhs.m_max_strlen_length),
2375 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2376 m_run_args (rhs.m_run_args),
2377 m_env_vars (rhs.m_env_vars),
2378 m_input_path (rhs.m_input_path),
2379 m_output_path (rhs.m_output_path),
2380 m_error_path (rhs.m_error_path),
2381 m_disable_aslr (rhs.m_disable_aslr),
2382 m_disable_stdio (rhs.m_disable_stdio),
2383 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002384{
2385 if (m_instance_name != InstanceSettings::GetDefaultName())
2386 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002387 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
2388 if (owner_sp)
2389 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name),false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002390 }
2391}
2392
2393TargetInstanceSettings::~TargetInstanceSettings ()
2394{
2395}
2396
2397TargetInstanceSettings&
2398TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2399{
2400 if (this != &rhs)
2401 {
Greg Claytonabb33022011-11-08 02:43:13 +00002402 m_expr_prefix_file = rhs.m_expr_prefix_file;
Sean Callanane0b7f942011-11-16 01:54:57 +00002403 m_expr_prefix_contents = rhs.m_expr_prefix_contents;
Greg Claytonabb33022011-11-08 02:43:13 +00002404 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
Enrico Granatadba1de82012-03-27 02:35:13 +00002405 m_enable_synthetic_value = rhs.m_enable_synthetic_value;
Greg Claytonabb33022011-11-08 02:43:13 +00002406 m_skip_prologue = rhs.m_skip_prologue;
2407 m_source_map = rhs.m_source_map;
Greg Clayton9ce95382012-02-13 23:10:39 +00002408 m_exe_search_paths = rhs.m_exe_search_paths;
Greg Claytonabb33022011-11-08 02:43:13 +00002409 m_max_children_display = rhs.m_max_children_display;
2410 m_max_strlen_length = rhs.m_max_strlen_length;
2411 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2412 m_run_args = rhs.m_run_args;
2413 m_env_vars = rhs.m_env_vars;
2414 m_input_path = rhs.m_input_path;
2415 m_output_path = rhs.m_output_path;
2416 m_error_path = rhs.m_error_path;
2417 m_disable_aslr = rhs.m_disable_aslr;
2418 m_disable_stdio = rhs.m_disable_stdio;
2419 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002420 }
2421
2422 return *this;
2423}
2424
Caroline Tice5bc8c972010-09-20 20:44:43 +00002425void
2426TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2427 const char *index_value,
2428 const char *value,
2429 const ConstString &instance_name,
2430 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002431 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002432 Error &err,
2433 bool pending)
2434{
Greg Claytond284b662011-02-18 01:44:25 +00002435 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002436 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002437 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2438 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002439 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002440 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002441 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002442 default:
2443 break;
2444 case eVarSetOperationAssign:
2445 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002446 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002447 m_expr_prefix_contents.clear();
2448
Greg Claytonff44ab42011-04-23 02:04:55 +00002449 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2450 {
2451 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002452 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002453 return;
2454 }
2455
Greg Clayton4b23ab32012-01-06 02:01:06 +00002456 DataBufferSP file_data_sp (m_expr_prefix_file.GetCurrentValue().ReadFileContents(0, SIZE_MAX, &err));
Sean Callanane0b7f942011-11-16 01:54:57 +00002457
Greg Clayton4b23ab32012-01-06 02:01:06 +00002458 if (err.Success())
Greg Claytonff44ab42011-04-23 02:04:55 +00002459 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002460 if (file_data_sp && file_data_sp->GetByteSize() > 0)
2461 {
2462 m_expr_prefix_contents.assign((const char*)file_data_sp->GetBytes(), file_data_sp->GetByteSize());
2463 }
2464 else
2465 {
2466 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
2467 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002468 }
Sean Callanan77e93942010-10-29 00:29:03 +00002469 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002470 break;
2471 case eVarSetOperationClear:
Sean Callanane0b7f942011-11-16 01:54:57 +00002472 m_expr_prefix_contents.clear();
Sean Callanan77e93942010-10-29 00:29:03 +00002473 }
Sean Callanan77e93942010-10-29 00:29:03 +00002474 }
2475 }
Jim Inghame41494a2011-04-16 00:01:13 +00002476 else if (var_name == GetSettingNameForPreferDynamicValue())
2477 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002478 int new_value;
2479 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2480 if (err.Success())
2481 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002482 }
Enrico Granatadba1de82012-03-27 02:35:13 +00002483 else if (var_name == GetSettingNameForEnableSyntheticValue())
2484 {
2485 bool ok;
2486 bool new_value = Args::StringToBoolean(value, true, &ok);
2487 if (ok)
2488 m_enable_synthetic_value.SetCurrentValue(new_value);
2489 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002490 else if (var_name == GetSettingNameForSkipPrologue())
2491 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002492 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2493 }
Enrico Granata018921d2011-08-12 02:00:06 +00002494 else if (var_name == GetSettingNameForMaxChildren())
2495 {
2496 bool ok;
2497 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2498 if (ok)
2499 m_max_children_display = new_value;
2500 }
Enrico Granata91544802011-09-06 19:20:51 +00002501 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2502 {
2503 bool ok;
2504 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2505 if (ok)
2506 m_max_strlen_length = new_value;
2507 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002508 else if (var_name == GetSettingNameForExecutableSearchPaths())
2509 {
2510 switch (op)
2511 {
2512 case eVarSetOperationReplace:
2513 case eVarSetOperationInsertBefore:
2514 case eVarSetOperationInsertAfter:
2515 case eVarSetOperationRemove:
2516 default:
2517 break;
2518 case eVarSetOperationAssign:
2519 m_exe_search_paths.Clear();
2520 // Fall through to append....
2521 case eVarSetOperationAppend:
2522 {
2523 Args args(value);
2524 const uint32_t argc = args.GetArgumentCount();
2525 if (argc > 0)
2526 {
2527 const char *exe_search_path_dir;
2528 for (uint32_t idx = 0; (exe_search_path_dir = args.GetArgumentAtIndex(idx)) != NULL; ++idx)
2529 {
2530 FileSpec file_spec;
2531 file_spec.GetDirectory().SetCString(exe_search_path_dir);
2532 FileSpec::FileType file_type = file_spec.GetFileType();
2533 if (file_type == FileSpec::eFileTypeDirectory || file_type == FileSpec::eFileTypeInvalid)
2534 {
2535 m_exe_search_paths.Append(file_spec);
2536 }
2537 else
2538 {
2539 err.SetErrorStringWithFormat("executable search path '%s' exists, but it does not resolve to a directory", exe_search_path_dir);
2540 }
2541 }
2542 }
2543 }
2544 break;
2545
2546 case eVarSetOperationClear:
2547 m_exe_search_paths.Clear();
2548 break;
2549 }
2550 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002551 else if (var_name == GetSettingNameForSourcePathMap ())
2552 {
2553 switch (op)
2554 {
2555 case eVarSetOperationReplace:
2556 case eVarSetOperationInsertBefore:
2557 case eVarSetOperationInsertAfter:
2558 case eVarSetOperationRemove:
2559 default:
2560 break;
2561 case eVarSetOperationAssign:
2562 m_source_map.Clear(true);
2563 // Fall through to append....
2564 case eVarSetOperationAppend:
2565 {
2566 Args args(value);
2567 const uint32_t argc = args.GetArgumentCount();
2568 if (argc & 1 || argc == 0)
2569 {
2570 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2571 }
2572 else
2573 {
2574 char resolved_new_path[PATH_MAX];
2575 FileSpec file_spec;
2576 const char *old_path;
2577 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2578 {
2579 const char *new_path = args.GetArgumentAtIndex(idx+1);
2580 assert (new_path); // We have an even number of paths, this shouldn't happen!
2581
2582 file_spec.SetFile(new_path, true);
2583 if (file_spec.Exists())
2584 {
2585 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2586 {
2587 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2588 return;
2589 }
2590 }
2591 else
2592 {
2593 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2594 return;
2595 }
2596 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2597 }
2598 }
2599 }
2600 break;
2601
2602 case eVarSetOperationClear:
2603 m_source_map.Clear(true);
2604 break;
2605 }
Jim Inghame41494a2011-04-16 00:01:13 +00002606 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002607 else if (var_name == GetSettingNameForPlatformAvoid ())
2608 {
2609 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2610 }
Greg Claytonabb33022011-11-08 02:43:13 +00002611 else if (var_name == GetSettingNameForRunArgs())
2612 {
2613 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2614 }
2615 else if (var_name == GetSettingNameForEnvVars())
2616 {
2617 // This is nice for local debugging, but it is isn't correct for
2618 // remote debugging. We need to stop process.env-vars from being
2619 // populated with the host environment and add this as a launch option
2620 // and get the correct environment from the Target's platform.
2621 // GetHostEnvironmentIfNeeded ();
2622 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2623 }
2624 else if (var_name == GetSettingNameForInputPath())
2625 {
2626 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2627 }
2628 else if (var_name == GetSettingNameForOutputPath())
2629 {
2630 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2631 }
2632 else if (var_name == GetSettingNameForErrorPath())
2633 {
2634 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2635 }
2636 else if (var_name == GetSettingNameForDisableASLR())
2637 {
2638 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2639 }
2640 else if (var_name == GetSettingNameForDisableSTDIO ())
2641 {
2642 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2643 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002644}
2645
2646void
Greg Claytond284b662011-02-18 01:44:25 +00002647TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002648{
Sean Callanan77e93942010-10-29 00:29:03 +00002649 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2650
2651 if (!new_settings_ptr)
2652 return;
2653
Greg Claytonabb33022011-11-08 02:43:13 +00002654 *this = *new_settings_ptr;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002655}
2656
Caroline Ticebcb5b452010-09-20 21:37:42 +00002657bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002658TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2659 const ConstString &var_name,
2660 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002661 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002662{
Greg Claytond284b662011-02-18 01:44:25 +00002663 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002664 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002665 char path[PATH_MAX];
2666 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2667 if (path_len > 0)
2668 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002669 }
Jim Inghame41494a2011-04-16 00:01:13 +00002670 else if (var_name == GetSettingNameForPreferDynamicValue())
2671 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002672 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002673 }
Enrico Granatadba1de82012-03-27 02:35:13 +00002674 else if (var_name == GetSettingNameForEnableSyntheticValue())
2675 {
2676 if (m_skip_prologue)
2677 value.AppendString ("true");
2678 else
2679 value.AppendString ("false");
2680 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002681 else if (var_name == GetSettingNameForSkipPrologue())
2682 {
2683 if (m_skip_prologue)
2684 value.AppendString ("true");
2685 else
2686 value.AppendString ("false");
2687 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002688 else if (var_name == GetSettingNameForExecutableSearchPaths())
2689 {
2690 if (m_exe_search_paths.GetSize())
2691 {
2692 for (size_t i = 0, n = m_exe_search_paths.GetSize(); i < n; ++i)
2693 {
2694 value.AppendString(m_exe_search_paths.GetFileSpecAtIndex (i).GetDirectory().AsCString());
2695 }
2696 }
2697 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002698 else if (var_name == GetSettingNameForSourcePathMap ())
2699 {
Johnny Chen931449e2011-12-12 21:59:28 +00002700 if (m_source_map.GetSize())
2701 {
2702 size_t i;
2703 for (i = 0; i < m_source_map.GetSize(); ++i) {
2704 StreamString sstr;
2705 m_source_map.Dump(&sstr, i);
2706 value.AppendString(sstr.GetData());
2707 }
2708 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002709 }
Enrico Granata018921d2011-08-12 02:00:06 +00002710 else if (var_name == GetSettingNameForMaxChildren())
2711 {
2712 StreamString count_str;
2713 count_str.Printf ("%d", m_max_children_display);
2714 value.AppendString (count_str.GetData());
2715 }
Enrico Granata91544802011-09-06 19:20:51 +00002716 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2717 {
2718 StreamString count_str;
2719 count_str.Printf ("%d", m_max_strlen_length);
2720 value.AppendString (count_str.GetData());
2721 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002722 else if (var_name == GetSettingNameForPlatformAvoid())
2723 {
2724 if (m_breakpoints_use_platform_avoid)
2725 value.AppendString ("true");
2726 else
2727 value.AppendString ("false");
2728 }
Greg Claytonabb33022011-11-08 02:43:13 +00002729 else if (var_name == GetSettingNameForRunArgs())
2730 {
2731 if (m_run_args.GetArgumentCount() > 0)
2732 {
2733 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2734 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2735 }
2736 }
2737 else if (var_name == GetSettingNameForEnvVars())
2738 {
2739 GetHostEnvironmentIfNeeded ();
2740
2741 if (m_env_vars.size() > 0)
2742 {
2743 std::map<std::string, std::string>::iterator pos;
2744 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2745 {
2746 StreamString value_str;
2747 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2748 value.AppendString (value_str.GetData());
2749 }
2750 }
2751 }
2752 else if (var_name == GetSettingNameForInputPath())
2753 {
2754 value.AppendString (m_input_path.c_str());
2755 }
2756 else if (var_name == GetSettingNameForOutputPath())
2757 {
2758 value.AppendString (m_output_path.c_str());
2759 }
2760 else if (var_name == GetSettingNameForErrorPath())
2761 {
2762 value.AppendString (m_error_path.c_str());
2763 }
2764 else if (var_name == GetSettingNameForInheritHostEnv())
2765 {
2766 if (m_inherit_host_env)
2767 value.AppendString ("true");
2768 else
2769 value.AppendString ("false");
2770 }
2771 else if (var_name == GetSettingNameForDisableASLR())
2772 {
2773 if (m_disable_aslr)
2774 value.AppendString ("true");
2775 else
2776 value.AppendString ("false");
2777 }
2778 else if (var_name == GetSettingNameForDisableSTDIO())
2779 {
2780 if (m_disable_stdio)
2781 value.AppendString ("true");
2782 else
2783 value.AppendString ("false");
2784 }
Sean Callanan77e93942010-10-29 00:29:03 +00002785 else
2786 {
2787 if (err)
2788 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2789 return false;
2790 }
Sean Callanan77e93942010-10-29 00:29:03 +00002791 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002792}
2793
Greg Claytonabb33022011-11-08 02:43:13 +00002794void
2795Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2796{
2797 if (m_inherit_host_env && !m_got_host_env)
2798 {
2799 m_got_host_env = true;
2800 StringList host_env;
2801 const size_t host_env_count = Host::GetEnvironment (host_env);
2802 for (size_t idx=0; idx<host_env_count; idx++)
2803 {
2804 const char *env_entry = host_env.GetStringAtIndex (idx);
2805 if (env_entry)
2806 {
2807 const char *equal_pos = ::strchr(env_entry, '=');
2808 if (equal_pos)
2809 {
2810 std::string key (env_entry, equal_pos - env_entry);
2811 std::string value (equal_pos + 1);
2812 if (m_env_vars.find (key) == m_env_vars.end())
2813 m_env_vars[key] = value;
2814 }
2815 }
2816 }
2817 }
2818}
2819
2820
2821size_t
2822Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2823{
2824 GetHostEnvironmentIfNeeded ();
2825
2826 dictionary::const_iterator pos, end = m_env_vars.end();
2827 for (pos = m_env_vars.begin(); pos != end; ++pos)
2828 {
2829 std::string env_var_equal_value (pos->first);
2830 env_var_equal_value.append(1, '=');
2831 env_var_equal_value.append (pos->second);
2832 env.AppendArgument (env_var_equal_value.c_str());
2833 }
2834 return env.GetArgumentCount();
2835}
2836
2837
Caroline Tice5bc8c972010-09-20 20:44:43 +00002838const ConstString
2839TargetInstanceSettings::CreateInstanceName ()
2840{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002841 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002842 static int instance_count = 1;
2843
Caroline Tice5bc8c972010-09-20 20:44:43 +00002844 sstr.Printf ("target_%d", instance_count);
2845 ++instance_count;
2846
2847 const ConstString ret_val (sstr.GetData());
2848 return ret_val;
2849}
2850
2851//--------------------------------------------------
2852// Target::SettingsController Variable Tables
2853//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002854OptionEnumValueElement
2855TargetInstanceSettings::g_dynamic_value_types[] =
2856{
Greg Clayton577fbc32011-05-30 00:39:48 +00002857{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2858{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2859{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002860{ 0, NULL, NULL }
2861};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002862
2863SettingEntry
2864Target::SettingsController::global_settings_table[] =
2865{
Greg Claytond284b662011-02-18 01:44:25 +00002866 // var-name var-type default enum init'd hidden help-text
2867 // ================= ================== =========== ==== ====== ====== =========================================================================
2868 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2869 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2870};
2871
Caroline Tice5bc8c972010-09-20 20:44:43 +00002872SettingEntry
2873Target::SettingsController::instance_settings_table[] =
2874{
Enrico Granata91544802011-09-06 19:20:51 +00002875 // var-name var-type default enum init'd hidden help-text
2876 // ================= ================== =============== ======================= ====== ====== =========================================================================
2877 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2878 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
Enrico Granatadba1de82012-03-27 02:35:13 +00002879 { TSC_ENABLE_SYNTHETIC , eSetVarTypeBoolean, "true" , NULL, false, false, "Should synthetic values be used by default whenever available." },
Enrico Granata91544802011-09-06 19:20:51 +00002880 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2881 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
Greg Clayton9ce95382012-02-13 23:10:39 +00002882 { TSC_EXE_SEARCH_PATHS , eSetVarTypeArray , NULL , NULL, false, false, "Executable search paths to use when locating executable files whose paths don't match the local file system." },
Enrico Granata91544802011-09-06 19:20:51 +00002883 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2884 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
Jim Ingham7089d8a2011-10-28 23:14:11 +00002885 { TSC_PLATFORM_AVOID , eSetVarTypeBoolean, "true" , NULL, false, false, "Consult the platform module avoid list when setting non-module specific breakpoints." },
Greg Claytonabb33022011-11-08 02:43:13 +00002886 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2887 { TSC_ENV_VARS , eSetVarTypeDictionary, NULL , NULL, false, false, "A list of all the environment variables to be passed to the executable's environment, and their values." },
2888 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2889 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2890 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2891 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2892// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2893 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2894 { TSC_DISABLE_STDIO , eSetVarTypeBoolean, "false" , NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" },
Enrico Granata91544802011-09-06 19:20:51 +00002895 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002896};
Jim Ingham5a15e692012-02-16 06:50:00 +00002897
2898const ConstString &
2899Target::TargetEventData::GetFlavorString ()
2900{
2901 static ConstString g_flavor ("Target::TargetEventData");
2902 return g_flavor;
2903}
2904
2905const ConstString &
2906Target::TargetEventData::GetFlavor () const
2907{
2908 return TargetEventData::GetFlavorString ();
2909}
2910
2911Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2912 EventData(),
2913 m_target_sp (new_target_sp)
2914{
2915}
2916
2917Target::TargetEventData::~TargetEventData()
2918{
2919
2920}
2921
2922void
2923Target::TargetEventData::Dump (Stream *s) const
2924{
2925
2926}
2927
2928const TargetSP
2929Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2930{
2931 TargetSP target_sp;
2932
2933 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2934 if (data)
2935 target_sp = data->m_target_sp;
2936
2937 return target_sp;
2938}
2939
2940const Target::TargetEventData *
2941Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2942{
2943 if (event_ptr)
2944 {
2945 const EventData *event_data = event_ptr->GetData();
2946 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2947 return static_cast <const TargetEventData *> (event_ptr->GetData());
2948 }
2949 return NULL;
2950}
2951