blob: 0fce9ae06d8b43ae1dfd594b47b55b54ee568ba8 [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.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000486
487 // Grab the list mutex while doing operations.
488 Mutex::Locker locker;
489 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000490 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000491 if (matched_sp)
492 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000493 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000494 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000495 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
496 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000497 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000498 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000499 wp_sp = matched_sp;
500 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000501 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000502 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000503 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000504 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000505 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000506 }
507
Johnny Chenecd4feb2011-10-14 00:42:25 +0000508 if (!wp_sp) {
509 Watchpoint *new_wp = new Watchpoint(addr, size);
510 if (!new_wp) {
511 printf("Watchpoint ctor failed, out of memory?\n");
512 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000513 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000514 new_wp->SetWatchpointType(type);
515 new_wp->SetTarget(this);
516 wp_sp.reset(new_wp);
517 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000518 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000519
Johnny Chenecd4feb2011-10-14 00:42:25 +0000520 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000521 if (log)
522 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
523 __FUNCTION__,
524 rc.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000525 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000526
Johnny Chen155599b2012-03-26 22:00:10 +0000527 if (rc.Fail()) {
528 // Enabling the watchpoint on the device side failed.
529 // Remove the said watchpoint from the list maintained by the target instance.
530 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000531 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000532 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000533 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000534 m_last_created_watchpoint = wp_sp;
535 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000536}
537
Chris Lattner24943d22010-06-08 16:52:24 +0000538void
539Target::RemoveAllBreakpoints (bool internal_also)
540{
Greg Claytone005f2c2010-11-06 01:53:30 +0000541 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000542 if (log)
543 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
544
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000545 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000546 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000547 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000548
549 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000550}
551
552void
553Target::DisableAllBreakpoints (bool internal_also)
554{
Greg Claytone005f2c2010-11-06 01:53:30 +0000555 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000556 if (log)
557 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
558
559 m_breakpoint_list.SetEnabledAll (false);
560 if (internal_also)
561 m_internal_breakpoint_list.SetEnabledAll (false);
562}
563
564void
565Target::EnableAllBreakpoints (bool internal_also)
566{
Greg Claytone005f2c2010-11-06 01:53:30 +0000567 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000568 if (log)
569 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
570
571 m_breakpoint_list.SetEnabledAll (true);
572 if (internal_also)
573 m_internal_breakpoint_list.SetEnabledAll (true);
574}
575
576bool
577Target::RemoveBreakpointByID (break_id_t break_id)
578{
Greg Claytone005f2c2010-11-06 01:53:30 +0000579 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000580 if (log)
581 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
582
583 if (DisableBreakpointByID (break_id))
584 {
585 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000586 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000587 else
Jim Inghamd1686902010-10-14 23:45:03 +0000588 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000589 if (m_last_created_breakpoint)
590 {
591 if (m_last_created_breakpoint->GetID() == break_id)
592 m_last_created_breakpoint.reset();
593 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000594 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000595 }
Chris Lattner24943d22010-06-08 16:52:24 +0000596 return true;
597 }
598 return false;
599}
600
601bool
602Target::DisableBreakpointByID (break_id_t break_id)
603{
Greg Claytone005f2c2010-11-06 01:53:30 +0000604 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000605 if (log)
606 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
607
608 BreakpointSP bp_sp;
609
610 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
611 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
612 else
613 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
614 if (bp_sp)
615 {
616 bp_sp->SetEnabled (false);
617 return true;
618 }
619 return false;
620}
621
622bool
623Target::EnableBreakpointByID (break_id_t break_id)
624{
Greg Claytone005f2c2010-11-06 01:53:30 +0000625 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000626 if (log)
627 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
628 __FUNCTION__,
629 break_id,
630 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
631
632 BreakpointSP bp_sp;
633
634 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
635 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
636 else
637 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
638
639 if (bp_sp)
640 {
641 bp_sp->SetEnabled (true);
642 return true;
643 }
644 return false;
645}
646
Johnny Chenc86582f2011-09-23 21:21:43 +0000647// The flag 'end_to_end', default to true, signifies that the operation is
648// performed end to end, for both the debugger and the debuggee.
649
Johnny Chenecd4feb2011-10-14 00:42:25 +0000650// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
651// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000652bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000653Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000654{
655 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
656 if (log)
657 log->Printf ("Target::%s\n", __FUNCTION__);
658
Johnny Chenc86582f2011-09-23 21:21:43 +0000659 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000660 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000661 return true;
662 }
663
664 // Otherwise, it's an end to end operation.
665
Johnny Chenda5a8022011-09-20 23:28:55 +0000666 if (!ProcessIsValid())
667 return false;
668
Johnny Chenecd4feb2011-10-14 00:42:25 +0000669 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000670 for (size_t i = 0; i < num_watchpoints; ++i)
671 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000672 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
673 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000674 return false;
675
Johnny Chenecd4feb2011-10-14 00:42:25 +0000676 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000677 if (rc.Fail())
678 return false;
679 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000680 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000681 return true; // Success!
682}
683
Johnny Chenecd4feb2011-10-14 00:42:25 +0000684// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
685// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000686bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000687Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000688{
689 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
690 if (log)
691 log->Printf ("Target::%s\n", __FUNCTION__);
692
Johnny Chenc86582f2011-09-23 21:21:43 +0000693 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000694 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000695 return true;
696 }
697
698 // Otherwise, it's an end to end operation.
699
Johnny Chenda5a8022011-09-20 23:28:55 +0000700 if (!ProcessIsValid())
701 return false;
702
Johnny Chenecd4feb2011-10-14 00:42:25 +0000703 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000704 for (size_t i = 0; i < num_watchpoints; ++i)
705 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000706 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
707 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000708 return false;
709
Johnny Chenecd4feb2011-10-14 00:42:25 +0000710 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000711 if (rc.Fail())
712 return false;
713 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000714 return true; // Success!
715}
716
Johnny Chenecd4feb2011-10-14 00:42:25 +0000717// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
718// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000719bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000720Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000721{
722 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
723 if (log)
724 log->Printf ("Target::%s\n", __FUNCTION__);
725
Johnny Chenc86582f2011-09-23 21:21:43 +0000726 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000727 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000728 return true;
729 }
730
731 // Otherwise, it's an end to end operation.
732
Johnny Chenda5a8022011-09-20 23:28:55 +0000733 if (!ProcessIsValid())
734 return false;
735
Johnny Chenecd4feb2011-10-14 00:42:25 +0000736 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000737 for (size_t i = 0; i < num_watchpoints; ++i)
738 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000739 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
740 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000741 return false;
742
Johnny Chenecd4feb2011-10-14 00:42:25 +0000743 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000744 if (rc.Fail())
745 return false;
746 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000747 return true; // Success!
748}
749
Johnny Chen116a5cd2012-02-25 06:44:30 +0000750// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
751bool
752Target::ClearAllWatchpointHitCounts ()
753{
754 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
755 if (log)
756 log->Printf ("Target::%s\n", __FUNCTION__);
757
758 size_t num_watchpoints = m_watchpoint_list.GetSize();
759 for (size_t i = 0; i < num_watchpoints; ++i)
760 {
761 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
762 if (!wp_sp)
763 return false;
764
765 wp_sp->ResetHitCount();
766 }
767 return true; // Success!
768}
769
Johnny Chenecd4feb2011-10-14 00:42:25 +0000770// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000771// during these operations.
772bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000773Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000774{
775 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
776 if (log)
777 log->Printf ("Target::%s\n", __FUNCTION__);
778
779 if (!ProcessIsValid())
780 return false;
781
Johnny Chenecd4feb2011-10-14 00:42:25 +0000782 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000783 for (size_t i = 0; i < num_watchpoints; ++i)
784 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000785 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
786 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000787 return false;
788
Johnny Chenecd4feb2011-10-14 00:42:25 +0000789 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000790 }
791 return true; // Success!
792}
793
Johnny Chenecd4feb2011-10-14 00:42:25 +0000794// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000795bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000796Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000797{
798 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
799 if (log)
800 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
801
802 if (!ProcessIsValid())
803 return false;
804
Johnny Chenecd4feb2011-10-14 00:42:25 +0000805 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
806 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000807 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000808 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000809 if (rc.Success())
810 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000811
Johnny Chen01acfa72011-09-22 18:04:58 +0000812 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000813 }
814 return false;
815}
816
Johnny Chenecd4feb2011-10-14 00:42:25 +0000817// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000818bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000819Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000820{
821 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
822 if (log)
823 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
824
825 if (!ProcessIsValid())
826 return false;
827
Johnny Chenecd4feb2011-10-14 00:42:25 +0000828 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
829 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000830 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000831 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000832 if (rc.Success())
833 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000834
Johnny Chen01acfa72011-09-22 18:04:58 +0000835 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000836 }
837 return false;
838}
839
Johnny Chenecd4feb2011-10-14 00:42:25 +0000840// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000841bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000842Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000843{
844 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
845 if (log)
846 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
847
Johnny Chenecd4feb2011-10-14 00:42:25 +0000848 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000849 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000850 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000851 return true;
852 }
853 return false;
854}
855
Johnny Chenecd4feb2011-10-14 00:42:25 +0000856// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000857bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000858Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000859{
860 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
861 if (log)
862 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
863
864 if (!ProcessIsValid())
865 return false;
866
Johnny Chenecd4feb2011-10-14 00:42:25 +0000867 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
868 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000869 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000870 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000871 return true;
872 }
873 return false;
874}
875
Chris Lattner24943d22010-06-08 16:52:24 +0000876ModuleSP
877Target::GetExecutableModule ()
878{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000879 return m_images.GetModuleAtIndex(0);
880}
881
882Module*
883Target::GetExecutableModulePointer ()
884{
885 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000886}
887
888void
889Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
890{
891 m_images.Clear();
892 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000893 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000894 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000895
896 if (executable_sp.get())
897 {
898 Timer scoped_timer (__PRETTY_FUNCTION__,
899 "Target::SetExecutableModule (executable = '%s/%s')",
900 executable_sp->GetFileSpec().GetDirectory().AsCString(),
901 executable_sp->GetFileSpec().GetFilename().AsCString());
902
903 m_images.Append(executable_sp); // The first image is our exectuable file
904
Jim Ingham7508e732010-08-09 23:31:02 +0000905 // 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 +0000906 if (!m_arch.IsValid())
907 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000908
Chris Lattner24943d22010-06-08 16:52:24 +0000909 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000910 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000911
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000912 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000913 {
914 executable_objfile->GetDependentModules(dependent_files);
915 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
916 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000917 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
918 FileSpec platform_dependent_file_spec;
919 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000920 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000921 else
922 platform_dependent_file_spec = dependent_file_spec;
923
Greg Clayton444fe992012-02-26 05:51:37 +0000924 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
925 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +0000926 if (image_module_sp.get())
927 {
Chris Lattner24943d22010-06-08 16:52:24 +0000928 ObjectFile *objfile = image_module_sp->GetObjectFile();
929 if (objfile)
930 objfile->GetDependentModules(dependent_files);
931 }
932 }
933 }
Chris Lattner24943d22010-06-08 16:52:24 +0000934 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000935
936 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000937}
938
939
Jim Ingham7508e732010-08-09 23:31:02 +0000940bool
941Target::SetArchitecture (const ArchSpec &arch_spec)
942{
Greg Claytonb170aee2012-05-08 01:45:38 +0000943 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000944 {
Greg Claytonb170aee2012-05-08 01:45:38 +0000945 // If we haven't got a valid arch spec, or the architectures are
946 // compatible, so just update the architecture. Architectures can be
947 // equal, yet the triple OS and vendor might change, so we need to do
948 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000949 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000950 return true;
951 }
952 else
953 {
954 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000955 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000956 ModuleSP executable_sp = GetExecutableModule ();
957 m_images.Clear();
958 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000959 m_scratch_ast_source_ap.reset();
960 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000961 // Need to do something about unsetting breakpoints.
962
963 if (executable_sp)
964 {
Greg Clayton444fe992012-02-26 05:51:37 +0000965 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
966 Error error = ModuleList::GetSharedModule (module_spec,
967 executable_sp,
968 &GetExecutableSearchPaths(),
969 NULL,
970 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +0000971
972 if (!error.Fail() && executable_sp)
973 {
974 SetExecutableModule (executable_sp, true);
975 return true;
976 }
Jim Ingham7508e732010-08-09 23:31:02 +0000977 }
978 }
Greg Claytonb170aee2012-05-08 01:45:38 +0000979 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000980}
Chris Lattner24943d22010-06-08 16:52:24 +0000981
Chris Lattner24943d22010-06-08 16:52:24 +0000982void
983Target::ModuleAdded (ModuleSP &module_sp)
984{
985 // A module is being added to this target for the first time
986 ModuleList module_list;
987 module_list.Append(module_sp);
988 ModulesDidLoad (module_list);
989}
990
991void
992Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
993{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000994 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +0000995 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000996}
997
998void
999Target::ModulesDidLoad (ModuleList &module_list)
1000{
1001 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1002 // TODO: make event data that packages up the module_list
1003 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1004}
1005
1006void
1007Target::ModulesDidUnload (ModuleList &module_list)
1008{
1009 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +00001010
1011 // Remove the images from the target image list
1012 m_images.Remove(module_list);
1013
Chris Lattner24943d22010-06-08 16:52:24 +00001014 // TODO: make event data that packages up the module_list
1015 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1016}
1017
Jim Ingham7089d8a2011-10-28 23:14:11 +00001018
Daniel Dunbar705a0982011-10-31 22:50:37 +00001019bool
Greg Clayton444fe992012-02-26 05:51:37 +00001020Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001021{
1022
1023 if (!m_breakpoints_use_platform_avoid)
1024 return false;
1025 else
1026 {
1027 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001028 ModuleSpec module_spec (module_file_spec);
1029 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001030
1031 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1032 // black list.
1033 if (num_modules > 0)
1034 {
1035 for (int i = 0; i < num_modules; i++)
1036 {
1037 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1038 return false;
1039 }
1040 return true;
1041 }
1042 else
1043 return false;
1044 }
1045}
1046
Daniel Dunbar705a0982011-10-31 22:50:37 +00001047bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001048Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1049{
1050 if (!m_breakpoints_use_platform_avoid)
1051 return false;
1052 else if (GetPlatform())
1053 {
1054 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
1055 }
1056 else
1057 return false;
1058}
1059
Chris Lattner24943d22010-06-08 16:52:24 +00001060size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001061Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1062{
Greg Clayton3508c382012-02-24 01:59:29 +00001063 SectionSP section_sp (addr.GetSection());
1064 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001065 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001066 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1067 if (section_sp->IsEncrypted())
1068 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001069 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001070 return 0;
1071 }
Greg Clayton3508c382012-02-24 01:59:29 +00001072 ModuleSP module_sp (section_sp->GetModule());
1073 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001074 {
Greg Clayton3508c382012-02-24 01:59:29 +00001075 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1076 if (objfile)
1077 {
1078 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1079 addr.GetOffset(),
1080 dst,
1081 dst_len);
1082 if (bytes_read > 0)
1083 return bytes_read;
1084 else
1085 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1086 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001087 else
Greg Clayton3508c382012-02-24 01:59:29 +00001088 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001089 }
1090 else
Greg Clayton3508c382012-02-24 01:59:29 +00001091 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001092 }
1093 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001094 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001095
Greg Clayton26100dc2011-01-07 01:57:07 +00001096 return 0;
1097}
1098
1099size_t
Enrico Granata91544802011-09-06 19:20:51 +00001100Target::ReadMemory (const Address& addr,
1101 bool prefer_file_cache,
1102 void *dst,
1103 size_t dst_len,
1104 Error &error,
1105 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001106{
Chris Lattner24943d22010-06-08 16:52:24 +00001107 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001108
Enrico Granata91544802011-09-06 19:20:51 +00001109 // if we end up reading this from process memory, we will fill this
1110 // with the actual load address
1111 if (load_addr_ptr)
1112 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1113
Greg Clayton26100dc2011-01-07 01:57:07 +00001114 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001115
1116 addr_t load_addr = LLDB_INVALID_ADDRESS;
1117 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001118 Address resolved_addr;
1119 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001120 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001121 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001122 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001123 // No sections are loaded, so we must assume we are not running
1124 // yet and anything we are given is a file address.
1125 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1126 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001127 }
Greg Clayton70436352010-06-30 23:03:03 +00001128 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001129 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001130 // We have at least one section loaded. This can be becuase
1131 // we have manually loaded some sections with "target modules load ..."
1132 // or because we have have a live process that has sections loaded
1133 // through the dynamic loader
1134 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1135 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001136 }
Greg Clayton70436352010-06-30 23:03:03 +00001137 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001138 if (!resolved_addr.IsValid())
1139 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001140
Greg Clayton9b82f862011-07-11 05:12:02 +00001141
Greg Clayton26100dc2011-01-07 01:57:07 +00001142 if (prefer_file_cache)
1143 {
1144 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1145 if (bytes_read > 0)
1146 return bytes_read;
1147 }
Greg Clayton70436352010-06-30 23:03:03 +00001148
Johnny Chenda5a8022011-09-20 23:28:55 +00001149 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001150 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001151 if (load_addr == LLDB_INVALID_ADDRESS)
1152 load_addr = resolved_addr.GetLoadAddress (this);
1153
Greg Clayton70436352010-06-30 23:03:03 +00001154 if (load_addr == LLDB_INVALID_ADDRESS)
1155 {
Greg Clayton3508c382012-02-24 01:59:29 +00001156 ModuleSP addr_module_sp (resolved_addr.GetModule());
1157 if (addr_module_sp && addr_module_sp->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001158 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001159 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001160 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001161 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001162 else
Greg Clayton9c236732011-10-26 00:56:27 +00001163 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001164 }
1165 else
1166 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001167 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001168 if (bytes_read != dst_len)
1169 {
1170 if (error.Success())
1171 {
1172 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001173 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001174 else
Greg Clayton9c236732011-10-26 00:56:27 +00001175 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 +00001176 }
1177 }
Greg Clayton70436352010-06-30 23:03:03 +00001178 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001179 {
1180 if (load_addr_ptr)
1181 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001182 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001183 }
Greg Clayton70436352010-06-30 23:03:03 +00001184 // If the address is not section offset we have an address that
1185 // doesn't resolve to any address in any currently loaded shared
1186 // libaries and we failed to read memory so there isn't anything
1187 // more we can do. If it is section offset, we might be able to
1188 // read cached memory from the object file.
1189 if (!resolved_addr.IsSectionOffset())
1190 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001191 }
Chris Lattner24943d22010-06-08 16:52:24 +00001192 }
Greg Clayton70436352010-06-30 23:03:03 +00001193
Greg Clayton9b82f862011-07-11 05:12:02 +00001194 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001195 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001196 // If we didn't already try and read from the object file cache, then
1197 // try it after failing to read from the process.
1198 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001199 }
1200 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001201}
1202
Greg Clayton7dd98df2011-07-12 17:06:17 +00001203size_t
1204Target::ReadScalarIntegerFromMemory (const Address& addr,
1205 bool prefer_file_cache,
1206 uint32_t byte_size,
1207 bool is_signed,
1208 Scalar &scalar,
1209 Error &error)
1210{
1211 uint64_t uval;
1212
1213 if (byte_size <= sizeof(uval))
1214 {
1215 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1216 if (bytes_read == byte_size)
1217 {
1218 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1219 uint32_t offset = 0;
1220 if (byte_size <= 4)
1221 scalar = data.GetMaxU32 (&offset, byte_size);
1222 else
1223 scalar = data.GetMaxU64 (&offset, byte_size);
1224
1225 if (is_signed)
1226 scalar.SignExtend(byte_size * 8);
1227 return bytes_read;
1228 }
1229 }
1230 else
1231 {
1232 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1233 }
1234 return 0;
1235}
1236
1237uint64_t
1238Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1239 bool prefer_file_cache,
1240 size_t integer_byte_size,
1241 uint64_t fail_value,
1242 Error &error)
1243{
1244 Scalar scalar;
1245 if (ReadScalarIntegerFromMemory (addr,
1246 prefer_file_cache,
1247 integer_byte_size,
1248 false,
1249 scalar,
1250 error))
1251 return scalar.ULongLong(fail_value);
1252 return fail_value;
1253}
1254
1255bool
1256Target::ReadPointerFromMemory (const Address& addr,
1257 bool prefer_file_cache,
1258 Error &error,
1259 Address &pointer_addr)
1260{
1261 Scalar scalar;
1262 if (ReadScalarIntegerFromMemory (addr,
1263 prefer_file_cache,
1264 m_arch.GetAddressByteSize(),
1265 false,
1266 scalar,
1267 error))
1268 {
1269 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1270 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1271 {
1272 if (m_section_load_list.IsEmpty())
1273 {
1274 // No sections are loaded, so we must assume we are not running
1275 // yet and anything we are given is a file address.
1276 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1277 }
1278 else
1279 {
1280 // We have at least one section loaded. This can be becuase
1281 // we have manually loaded some sections with "target modules load ..."
1282 // or because we have have a live process that has sections loaded
1283 // through the dynamic loader
1284 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1285 }
1286 // We weren't able to resolve the pointer value, so just return
1287 // an address with no section
1288 if (!pointer_addr.IsValid())
1289 pointer_addr.SetOffset (pointer_vm_addr);
1290 return true;
1291
1292 }
1293 }
1294 return false;
1295}
Chris Lattner24943d22010-06-08 16:52:24 +00001296
1297ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001298Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001299{
Chris Lattner24943d22010-06-08 16:52:24 +00001300 ModuleSP module_sp;
1301
Chris Lattner24943d22010-06-08 16:52:24 +00001302 Error error;
1303
Jim Ingham03e5e512012-05-17 18:38:42 +00001304 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1305 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001306
Jim Ingham03e5e512012-05-17 18:38:42 +00001307 if (module_spec.GetUUID().IsValid())
1308 module_sp = m_images.FindFirstModule(module_spec);
1309
Greg Claytone1ef1e32012-04-27 00:58:27 +00001310 if (!module_sp)
1311 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001312 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1313 bool did_create_module = false;
1314
1315 // If there are image search path entries, try to use them first to acquire a suitable image.
1316 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001317 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001318 ModuleSpec transformed_spec (module_spec);
1319 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1320 {
1321 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1322 error = ModuleList::GetSharedModule (transformed_spec,
1323 module_sp,
1324 &GetExecutableSearchPaths(),
1325 &old_module_sp,
1326 &did_create_module);
1327 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001328 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001329
Greg Claytone1ef1e32012-04-27 00:58:27 +00001330 if (!module_sp)
1331 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001332 // If we have a UUID, we can check our global shared module list in case
1333 // we already have it. If we don't have a valid UUID, then we can't since
1334 // the path in "module_spec" will be a platform path, and we will need to
1335 // let the platform find that file. For example, we could be asking for
1336 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1337 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1338 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1339 // cache.
1340 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001341 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001342 // We have a UUID, it is OK to check the global module list...
1343 error = ModuleList::GetSharedModule (module_spec,
1344 module_sp,
1345 &GetExecutableSearchPaths(),
1346 &old_module_sp,
1347 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001348 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001349
1350 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001351 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001352 // The platform is responsible for finding and caching an appropriate
1353 // module in the shared module cache.
1354 if (m_platform_sp)
1355 {
1356 FileSpec platform_file_spec;
1357 error = m_platform_sp->GetSharedModule (module_spec,
1358 module_sp,
1359 &GetExecutableSearchPaths(),
1360 &old_module_sp,
1361 &did_create_module);
1362 }
1363 else
1364 {
1365 error.SetErrorString("no platform is currently set");
1366 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001367 }
1368 }
Chris Lattner24943d22010-06-08 16:52:24 +00001369
Jim Ingham03e5e512012-05-17 18:38:42 +00001370 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1371 // module in the list already, and if there was, let's remove it.
1372 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001373 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001374 // GetSharedModule is not guaranteed to find the old shared module, for instance
1375 // in the common case where you pass in the UUID, it is only going to find the one
1376 // module matching the UUID. In fact, it has no good way to know what the "old module"
1377 // relevant to this target is, since there might be many copies of a module with this file spec
1378 // in various running debug sessions, but only one of them will belong to this target.
1379 // So let's remove the UUID from the module list, and look in the target's module list.
1380 // Only do this if there is SOMETHING else in the module spec...
1381 if (!old_module_sp)
1382 {
1383 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
1384 {
1385 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1386 module_spec_copy.GetUUID().Clear();
1387
1388 ModuleList found_modules;
1389 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1390 if (num_found == 1)
1391 {
1392 old_module_sp = found_modules.GetModuleAtIndex(0);
1393 }
1394 }
1395 }
1396
1397 m_images.Append (module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001398 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
Jim Ingham03e5e512012-05-17 18:38:42 +00001399 {
Chris Lattner24943d22010-06-08 16:52:24 +00001400 ModuleUpdated(old_module_sp, module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001401 m_images.Remove (old_module_sp);
1402 Module *old_module_ptr = old_module_sp.get();
1403 old_module_sp.reset();
1404 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1405 }
Chris Lattner24943d22010-06-08 16:52:24 +00001406 else
1407 ModuleAdded(module_sp);
1408 }
1409 }
1410 if (error_ptr)
1411 *error_ptr = error;
1412 return module_sp;
1413}
1414
1415
Greg Clayton289afcb2012-02-18 05:35:26 +00001416TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001417Target::CalculateTarget ()
1418{
Greg Clayton289afcb2012-02-18 05:35:26 +00001419 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001420}
1421
Greg Clayton289afcb2012-02-18 05:35:26 +00001422ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001423Target::CalculateProcess ()
1424{
Greg Clayton289afcb2012-02-18 05:35:26 +00001425 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001426}
1427
Greg Clayton289afcb2012-02-18 05:35:26 +00001428ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001429Target::CalculateThread ()
1430{
Greg Clayton289afcb2012-02-18 05:35:26 +00001431 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001432}
1433
Greg Clayton289afcb2012-02-18 05:35:26 +00001434StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001435Target::CalculateStackFrame ()
1436{
Greg Clayton289afcb2012-02-18 05:35:26 +00001437 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001438}
1439
1440void
Greg Claytona830adb2010-10-04 01:05:56 +00001441Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001442{
Greg Clayton567e7f32011-09-22 04:58:26 +00001443 exe_ctx.Clear();
1444 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001445}
1446
1447PathMappingList &
1448Target::GetImageSearchPathList ()
1449{
1450 return m_image_search_paths;
1451}
1452
1453void
1454Target::ImageSearchPathsChanged
1455(
1456 const PathMappingList &path_list,
1457 void *baton
1458)
1459{
1460 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001461 ModuleSP exe_module_sp (target->GetExecutableModule());
1462 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001463 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001464 target->m_images.Clear();
1465 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001466 }
1467}
1468
1469ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001470Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001471{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001472 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001473 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001474 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001475 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001476 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001477 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1478 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1479 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1480 }
Chris Lattner24943d22010-06-08 16:52:24 +00001481 return m_scratch_ast_context_ap.get();
1482}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001483
Sean Callanan4938bd62011-11-16 18:20:47 +00001484ClangASTImporter *
1485Target::GetClangASTImporter()
1486{
1487 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1488
1489 if (!ast_importer)
1490 {
1491 ast_importer = new ClangASTImporter();
1492 m_ast_importer_ap.reset(ast_importer);
1493 }
1494
1495 return ast_importer;
1496}
1497
Greg Clayton990de7b2010-11-18 23:32:35 +00001498void
Caroline Tice2a456812011-03-10 22:14:10 +00001499Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001500{
Greg Clayton334d33a2012-01-30 07:41:31 +00001501 UserSettingsController::InitializeSettingsController (GetSettingsController(),
Greg Clayton990de7b2010-11-18 23:32:35 +00001502 SettingsController::global_settings_table,
1503 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001504
1505 // Now call SettingsInitialize() on each 'child' setting of Target
1506 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001507}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001508
Greg Clayton990de7b2010-11-18 23:32:35 +00001509void
Caroline Tice2a456812011-03-10 22:14:10 +00001510Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001511{
Caroline Tice2a456812011-03-10 22:14:10 +00001512
1513 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1514
1515 Process::SettingsTerminate ();
1516
1517 // Now terminate Target Settings.
1518
Greg Clayton990de7b2010-11-18 23:32:35 +00001519 UserSettingsControllerSP &usc = GetSettingsController();
1520 UserSettingsController::FinalizeSettingsController (usc);
1521 usc.reset();
1522}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001523
Greg Clayton990de7b2010-11-18 23:32:35 +00001524UserSettingsControllerSP &
1525Target::GetSettingsController ()
1526{
Greg Clayton334d33a2012-01-30 07:41:31 +00001527 static UserSettingsControllerSP g_settings_controller_sp;
1528 if (!g_settings_controller_sp)
1529 {
1530 g_settings_controller_sp.reset (new Target::SettingsController);
1531 // The first shared pointer to Target::SettingsController in
1532 // g_settings_controller_sp must be fully created above so that
1533 // the TargetInstanceSettings can use a weak_ptr to refer back
1534 // to the master setttings controller
1535 InstanceSettingsSP default_instance_settings_sp (new TargetInstanceSettings (g_settings_controller_sp,
1536 false,
1537 InstanceSettings::GetDefaultName().AsCString()));
1538 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
1539 }
1540 return g_settings_controller_sp;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001541}
1542
Greg Clayton9ce95382012-02-13 23:10:39 +00001543FileSpecList
1544Target::GetDefaultExecutableSearchPaths ()
1545{
1546 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1547 if (settings_controller_sp)
1548 {
1549 lldb::InstanceSettingsSP instance_settings_sp (settings_controller_sp->GetDefaultInstanceSettings ());
1550 if (instance_settings_sp)
1551 return static_cast<TargetInstanceSettings *>(instance_settings_sp.get())->GetExecutableSearchPaths ();
1552 }
1553 return FileSpecList();
1554}
1555
1556
Caroline Tice5bc8c972010-09-20 20:44:43 +00001557ArchSpec
1558Target::GetDefaultArchitecture ()
1559{
Greg Clayton940b1032011-02-23 00:35:02 +00001560 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
Greg Clayton940b1032011-02-23 00:35:02 +00001561 if (settings_controller_sp)
Greg Clayton469e08d2012-05-15 02:44:13 +00001562 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1563 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001564}
1565
1566void
Greg Clayton940b1032011-02-23 00:35:02 +00001567Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001568{
Greg Clayton940b1032011-02-23 00:35:02 +00001569 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1570
1571 if (settings_controller_sp)
1572 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001573}
1574
Greg Claytona830adb2010-10-04 01:05:56 +00001575Target *
1576Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1577{
1578 // The target can either exist in the "process" of ExecutionContext, or in
1579 // the "target_sp" member of SymbolContext. This accessor helper function
1580 // will get the target from one of these locations.
1581
1582 Target *target = NULL;
1583 if (sc_ptr != NULL)
1584 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001585 if (target == NULL && exe_ctx_ptr)
1586 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001587 return target;
1588}
1589
1590
Caroline Tice1ebef442010-09-27 00:30:10 +00001591void
1592Target::UpdateInstanceName ()
1593{
1594 StreamString sstr;
1595
Greg Clayton5beb99d2011-08-11 02:48:45 +00001596 Module *exe_module = GetExecutableModulePointer();
1597 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001598 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001599 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001600 exe_module->GetFileSpec().GetFilename().AsCString(),
1601 exe_module->GetArchitecture().GetArchitectureName());
1602 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001603 }
1604}
1605
Sean Callanan77e93942010-10-29 00:29:03 +00001606const char *
1607Target::GetExpressionPrefixContentsAsCString ()
1608{
Sean Callanane0b7f942011-11-16 01:54:57 +00001609 if (!m_expr_prefix_contents.empty())
1610 return m_expr_prefix_contents.c_str();
Greg Claytonff44ab42011-04-23 02:04:55 +00001611 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001612}
1613
Greg Clayton427f2902010-12-14 02:59:59 +00001614ExecutionResults
1615Target::EvaluateExpression
1616(
1617 const char *expr_cstr,
1618 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001619 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001620 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001621 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001622 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001623 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001624 lldb::ValueObjectSP &result_valobj_sp
1625)
1626{
1627 ExecutionResults execution_results = eExecutionSetupError;
1628
1629 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001630
1631 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1632 return execution_results;
1633
Jim Ingham3613ae12011-05-12 02:06:14 +00001634 // We shouldn't run stop hooks in expressions.
1635 // Be sure to reset this if you return anywhere within this function.
1636 bool old_suppress_value = m_suppress_stop_hooks;
1637 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001638
1639 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001640
1641 const size_t expr_cstr_len = ::strlen (expr_cstr);
1642
Greg Clayton427f2902010-12-14 02:59:59 +00001643 if (frame)
1644 {
1645 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001646 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001647 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001648 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1649 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001650 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001651
1652 // Make sure we don't have any things that we know a variable expression
1653 // won't be able to deal with before calling into it
1654 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1655 {
1656 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1657 use_dynamic,
1658 expr_path_options,
1659 var_sp,
1660 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001661 // if this expression results in a bitfield, we give up and let the IR handle it
1662 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1663 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001664 }
Greg Clayton427f2902010-12-14 02:59:59 +00001665 }
1666 else if (m_process_sp)
1667 {
1668 m_process_sp->CalculateExecutionContext(exe_ctx);
1669 }
1670 else
1671 {
1672 CalculateExecutionContext(exe_ctx);
1673 }
1674
1675 if (result_valobj_sp)
1676 {
1677 execution_results = eExecutionCompleted;
1678 // We got a result from the frame variable expression path above...
1679 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1680
1681 lldb::ValueObjectSP const_valobj_sp;
1682
1683 // Check in case our value is already a constant value
1684 if (result_valobj_sp->GetIsConstant())
1685 {
1686 const_valobj_sp = result_valobj_sp;
1687 const_valobj_sp->SetName (persistent_variable_name);
1688 }
1689 else
Jim Inghame41494a2011-04-16 00:01:13 +00001690 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001691 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001692 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001693 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001694 if (dynamic_sp)
1695 result_valobj_sp = dynamic_sp;
1696 }
1697
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001698 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001699 }
Greg Clayton427f2902010-12-14 02:59:59 +00001700
Sean Callanan6a925532011-01-13 08:53:35 +00001701 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1702
Greg Clayton427f2902010-12-14 02:59:59 +00001703 result_valobj_sp = const_valobj_sp;
1704
Sean Callanan6a925532011-01-13 08:53:35 +00001705 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1706 assert (clang_expr_variable_sp.get());
1707
1708 // Set flags and live data as appropriate
1709
1710 const Value &result_value = live_valobj_sp->GetValue();
1711
1712 switch (result_value.GetValueType())
1713 {
1714 case Value::eValueTypeHostAddress:
1715 case Value::eValueTypeFileAddress:
1716 // we don't do anything with these for now
1717 break;
1718 case Value::eValueTypeScalar:
1719 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1720 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1721 break;
1722 case Value::eValueTypeLoadAddress:
1723 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1724 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1725 break;
1726 }
Greg Clayton427f2902010-12-14 02:59:59 +00001727 }
1728 else
1729 {
1730 // Make sure we aren't just trying to see the value of a persistent
1731 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001732 lldb::ClangExpressionVariableSP persistent_var_sp;
1733 // Only check for persistent variables the expression starts with a '$'
1734 if (expr_cstr[0] == '$')
1735 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1736
Greg Clayton427f2902010-12-14 02:59:59 +00001737 if (persistent_var_sp)
1738 {
1739 result_valobj_sp = persistent_var_sp->GetValueObject ();
1740 execution_results = eExecutionCompleted;
1741 }
1742 else
1743 {
1744 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001745
Greg Clayton427f2902010-12-14 02:59:59 +00001746 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001747 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001748 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001749 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001750 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001751 expr_cstr,
1752 prefix,
1753 result_valobj_sp);
1754 }
1755 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001756
1757 m_suppress_stop_hooks = old_suppress_value;
1758
Greg Clayton427f2902010-12-14 02:59:59 +00001759 return execution_results;
1760}
1761
Greg Claytonc0fa5332011-05-22 22:46:53 +00001762lldb::addr_t
1763Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1764{
1765 addr_t code_addr = load_addr;
1766 switch (m_arch.GetMachine())
1767 {
1768 case llvm::Triple::arm:
1769 case llvm::Triple::thumb:
1770 switch (addr_class)
1771 {
1772 case eAddressClassData:
1773 case eAddressClassDebug:
1774 return LLDB_INVALID_ADDRESS;
1775
1776 case eAddressClassUnknown:
1777 case eAddressClassInvalid:
1778 case eAddressClassCode:
1779 case eAddressClassCodeAlternateISA:
1780 case eAddressClassRuntime:
1781 // Check if bit zero it no set?
1782 if ((code_addr & 1ull) == 0)
1783 {
1784 // Bit zero isn't set, check if the address is a multiple of 2?
1785 if (code_addr & 2ull)
1786 {
1787 // The address is a multiple of 2 so it must be thumb, set bit zero
1788 code_addr |= 1ull;
1789 }
1790 else if (addr_class == eAddressClassCodeAlternateISA)
1791 {
1792 // We checked the address and the address claims to be the alternate ISA
1793 // which means thumb, so set bit zero.
1794 code_addr |= 1ull;
1795 }
1796 }
1797 break;
1798 }
1799 break;
1800
1801 default:
1802 break;
1803 }
1804 return code_addr;
1805}
1806
1807lldb::addr_t
1808Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1809{
1810 addr_t opcode_addr = load_addr;
1811 switch (m_arch.GetMachine())
1812 {
1813 case llvm::Triple::arm:
1814 case llvm::Triple::thumb:
1815 switch (addr_class)
1816 {
1817 case eAddressClassData:
1818 case eAddressClassDebug:
1819 return LLDB_INVALID_ADDRESS;
1820
1821 case eAddressClassInvalid:
1822 case eAddressClassUnknown:
1823 case eAddressClassCode:
1824 case eAddressClassCodeAlternateISA:
1825 case eAddressClassRuntime:
1826 opcode_addr &= ~(1ull);
1827 break;
1828 }
1829 break;
1830
1831 default:
1832 break;
1833 }
1834 return opcode_addr;
1835}
1836
Jim Inghamd60d94a2011-03-11 03:53:59 +00001837lldb::user_id_t
1838Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1839{
1840 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001841 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001842 m_stop_hooks[new_uid] = new_hook_sp;
1843 return new_uid;
1844}
1845
1846bool
1847Target::RemoveStopHookByID (lldb::user_id_t user_id)
1848{
1849 size_t num_removed;
1850 num_removed = m_stop_hooks.erase (user_id);
1851 if (num_removed == 0)
1852 return false;
1853 else
1854 return true;
1855}
1856
1857void
1858Target::RemoveAllStopHooks ()
1859{
1860 m_stop_hooks.clear();
1861}
1862
1863Target::StopHookSP
1864Target::GetStopHookByID (lldb::user_id_t user_id)
1865{
1866 StopHookSP found_hook;
1867
1868 StopHookCollection::iterator specified_hook_iter;
1869 specified_hook_iter = m_stop_hooks.find (user_id);
1870 if (specified_hook_iter != m_stop_hooks.end())
1871 found_hook = (*specified_hook_iter).second;
1872 return found_hook;
1873}
1874
1875bool
1876Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1877{
1878 StopHookCollection::iterator specified_hook_iter;
1879 specified_hook_iter = m_stop_hooks.find (user_id);
1880 if (specified_hook_iter == m_stop_hooks.end())
1881 return false;
1882
1883 (*specified_hook_iter).second->SetIsActive (active_state);
1884 return true;
1885}
1886
1887void
1888Target::SetAllStopHooksActiveState (bool active_state)
1889{
1890 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1891 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1892 {
1893 (*pos).second->SetIsActive (active_state);
1894 }
1895}
1896
1897void
1898Target::RunStopHooks ()
1899{
Jim Ingham3613ae12011-05-12 02:06:14 +00001900 if (m_suppress_stop_hooks)
1901 return;
1902
Jim Inghamd60d94a2011-03-11 03:53:59 +00001903 if (!m_process_sp)
1904 return;
1905
1906 if (m_stop_hooks.empty())
1907 return;
1908
1909 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1910
1911 // If there aren't any active stop hooks, don't bother either:
1912 bool any_active_hooks = false;
1913 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1914 {
1915 if ((*pos).second->IsActive())
1916 {
1917 any_active_hooks = true;
1918 break;
1919 }
1920 }
1921 if (!any_active_hooks)
1922 return;
1923
1924 CommandReturnObject result;
1925
1926 std::vector<ExecutionContext> exc_ctx_with_reasons;
1927 std::vector<SymbolContext> sym_ctx_with_reasons;
1928
1929 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1930 size_t num_threads = cur_threadlist.GetSize();
1931 for (size_t i = 0; i < num_threads; i++)
1932 {
1933 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1934 if (cur_thread_sp->ThreadStoppedForAReason())
1935 {
1936 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1937 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1938 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1939 }
1940 }
1941
1942 // If no threads stopped for a reason, don't run the stop-hooks.
1943 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1944 if (num_exe_ctx == 0)
1945 return;
1946
Jim Inghame5ed8e92011-06-02 23:58:26 +00001947 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1948 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001949
1950 bool keep_going = true;
1951 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001952 bool print_hook_header;
1953 bool print_thread_header;
1954
1955 if (num_exe_ctx == 1)
1956 print_thread_header = false;
1957 else
1958 print_thread_header = true;
1959
1960 if (m_stop_hooks.size() == 1)
1961 print_hook_header = false;
1962 else
1963 print_hook_header = true;
1964
Jim Inghamd60d94a2011-03-11 03:53:59 +00001965 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1966 {
1967 // result.Clear();
1968 StopHookSP cur_hook_sp = (*pos).second;
1969 if (!cur_hook_sp->IsActive())
1970 continue;
1971
1972 bool any_thread_matched = false;
1973 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1974 {
1975 if ((cur_hook_sp->GetSpecifier () == NULL
1976 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1977 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00001978 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001979 {
1980 if (!hooks_ran)
1981 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001982 hooks_ran = true;
1983 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001984 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001985 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001986 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1987 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1988 NULL);
1989 if (cmd)
1990 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1991 else
1992 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001993 any_thread_matched = true;
1994 }
1995
Jim Inghamc54840c2011-03-22 01:47:27 +00001996 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001997 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001998
1999 bool stop_on_continue = true;
2000 bool stop_on_error = true;
2001 bool echo_commands = false;
2002 bool print_results = true;
2003 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002004 &exc_ctx_with_reasons[i],
2005 stop_on_continue,
2006 stop_on_error,
2007 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002008 print_results,
2009 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002010 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002011
2012 // If the command started the target going again, we should bag out of
2013 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002014 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2015 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002016 {
Greg Clayton444e35b2011-10-19 18:09:39 +00002017 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002018 keep_going = false;
2019 }
2020 }
2021 }
2022 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002023
Caroline Tice4a348082011-05-02 20:41:46 +00002024 result.GetImmediateOutputStream()->Flush();
2025 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002026}
2027
Greg Claytonbbea1332011-07-08 00:48:09 +00002028
Jim Inghamd60d94a2011-03-11 03:53:59 +00002029//--------------------------------------------------------------
2030// class Target::StopHook
2031//--------------------------------------------------------------
2032
2033
2034Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2035 UserID (uid),
2036 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002037 m_commands (),
2038 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002039 m_thread_spec_ap(NULL),
2040 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002041{
2042}
2043
2044Target::StopHook::StopHook (const StopHook &rhs) :
2045 UserID (rhs.GetID()),
2046 m_target_sp (rhs.m_target_sp),
2047 m_commands (rhs.m_commands),
2048 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002049 m_thread_spec_ap (NULL),
2050 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002051{
2052 if (rhs.m_thread_spec_ap.get() != NULL)
2053 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2054}
2055
2056
2057Target::StopHook::~StopHook ()
2058{
2059}
2060
2061void
2062Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2063{
2064 m_thread_spec_ap.reset (specifier);
2065}
2066
2067
2068void
2069Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2070{
2071 int indent_level = s->GetIndentLevel();
2072
2073 s->SetIndentLevel(indent_level + 2);
2074
Greg Clayton444e35b2011-10-19 18:09:39 +00002075 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002076 if (m_active)
2077 s->Indent ("State: enabled\n");
2078 else
2079 s->Indent ("State: disabled\n");
2080
2081 if (m_specifier_sp)
2082 {
2083 s->Indent();
2084 s->PutCString ("Specifier:\n");
2085 s->SetIndentLevel (indent_level + 4);
2086 m_specifier_sp->GetDescription (s, level);
2087 s->SetIndentLevel (indent_level + 2);
2088 }
2089
2090 if (m_thread_spec_ap.get() != NULL)
2091 {
2092 StreamString tmp;
2093 s->Indent("Thread:\n");
2094 m_thread_spec_ap->GetDescription (&tmp, level);
2095 s->SetIndentLevel (indent_level + 4);
2096 s->Indent (tmp.GetData());
2097 s->PutCString ("\n");
2098 s->SetIndentLevel (indent_level + 2);
2099 }
2100
2101 s->Indent ("Commands: \n");
2102 s->SetIndentLevel (indent_level + 4);
2103 uint32_t num_commands = m_commands.GetSize();
2104 for (uint32_t i = 0; i < num_commands; i++)
2105 {
2106 s->Indent(m_commands.GetStringAtIndex(i));
2107 s->PutCString ("\n");
2108 }
2109 s->SetIndentLevel (indent_level);
2110}
2111
2112
Caroline Tice5bc8c972010-09-20 20:44:43 +00002113//--------------------------------------------------------------
2114// class Target::SettingsController
2115//--------------------------------------------------------------
2116
2117Target::SettingsController::SettingsController () :
2118 UserSettingsController ("target", Debugger::GetSettingsController()),
2119 m_default_architecture ()
2120{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002121}
2122
2123Target::SettingsController::~SettingsController ()
2124{
2125}
2126
2127lldb::InstanceSettingsSP
2128Target::SettingsController::CreateInstanceSettings (const char *instance_name)
2129{
Greg Clayton334d33a2012-01-30 07:41:31 +00002130 lldb::InstanceSettingsSP new_settings_sp (new TargetInstanceSettings (GetSettingsController(),
2131 false,
2132 instance_name));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002133 return new_settings_sp;
2134}
2135
Caroline Tice5bc8c972010-09-20 20:44:43 +00002136
Greg Claytonabb33022011-11-08 02:43:13 +00002137#define TSC_DEFAULT_ARCH "default-arch"
2138#define TSC_EXPR_PREFIX "expr-prefix"
2139#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Enrico Granatadba1de82012-03-27 02:35:13 +00002140#define TSC_ENABLE_SYNTHETIC "enable-synthetic-value"
Greg Claytonabb33022011-11-08 02:43:13 +00002141#define TSC_SKIP_PROLOGUE "skip-prologue"
2142#define TSC_SOURCE_MAP "source-map"
Greg Clayton9ce95382012-02-13 23:10:39 +00002143#define TSC_EXE_SEARCH_PATHS "exec-search-paths"
Greg Claytonabb33022011-11-08 02:43:13 +00002144#define TSC_MAX_CHILDREN "max-children-count"
2145#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
2146#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
2147#define TSC_RUN_ARGS "run-args"
2148#define TSC_ENV_VARS "env-vars"
2149#define TSC_INHERIT_ENV "inherit-env"
2150#define TSC_STDIN_PATH "input-path"
2151#define TSC_STDOUT_PATH "output-path"
2152#define TSC_STDERR_PATH "error-path"
2153#define TSC_DISABLE_ASLR "disable-aslr"
2154#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytond284b662011-02-18 01:44:25 +00002155
2156
2157static const ConstString &
2158GetSettingNameForDefaultArch ()
2159{
2160 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00002161 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002162}
2163
Greg Claytond284b662011-02-18 01:44:25 +00002164static const ConstString &
2165GetSettingNameForExpressionPrefix ()
2166{
2167 static ConstString g_const_string (TSC_EXPR_PREFIX);
2168 return g_const_string;
2169}
2170
2171static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00002172GetSettingNameForPreferDynamicValue ()
2173{
2174 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2175 return g_const_string;
2176}
2177
Greg Claytonff44ab42011-04-23 02:04:55 +00002178static const ConstString &
Enrico Granatadba1de82012-03-27 02:35:13 +00002179GetSettingNameForEnableSyntheticValue ()
2180{
2181 static ConstString g_const_string (TSC_ENABLE_SYNTHETIC);
2182 return g_const_string;
2183}
2184
2185static const ConstString &
Greg Claytonff44ab42011-04-23 02:04:55 +00002186GetSettingNameForSourcePathMap ()
2187{
2188 static ConstString g_const_string (TSC_SOURCE_MAP);
2189 return g_const_string;
2190}
Greg Claytond284b662011-02-18 01:44:25 +00002191
Greg Clayton17cd9952011-04-22 03:55:06 +00002192static const ConstString &
Greg Clayton9ce95382012-02-13 23:10:39 +00002193GetSettingNameForExecutableSearchPaths ()
2194{
2195 static ConstString g_const_string (TSC_EXE_SEARCH_PATHS);
2196 return g_const_string;
2197}
2198
2199static const ConstString &
Greg Clayton17cd9952011-04-22 03:55:06 +00002200GetSettingNameForSkipPrologue ()
2201{
2202 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2203 return g_const_string;
2204}
2205
Enrico Granata018921d2011-08-12 02:00:06 +00002206static const ConstString &
2207GetSettingNameForMaxChildren ()
2208{
2209 static ConstString g_const_string (TSC_MAX_CHILDREN);
2210 return g_const_string;
2211}
Greg Clayton17cd9952011-04-22 03:55:06 +00002212
Enrico Granata91544802011-09-06 19:20:51 +00002213static const ConstString &
2214GetSettingNameForMaxStringSummaryLength ()
2215{
2216 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2217 return g_const_string;
2218}
Greg Clayton17cd9952011-04-22 03:55:06 +00002219
Jim Ingham7089d8a2011-10-28 23:14:11 +00002220static const ConstString &
2221GetSettingNameForPlatformAvoid ()
2222{
2223 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2224 return g_const_string;
2225}
2226
Greg Claytonabb33022011-11-08 02:43:13 +00002227const ConstString &
2228GetSettingNameForRunArgs ()
2229{
2230 static ConstString g_const_string (TSC_RUN_ARGS);
2231 return g_const_string;
2232}
2233
2234const ConstString &
2235GetSettingNameForEnvVars ()
2236{
2237 static ConstString g_const_string (TSC_ENV_VARS);
2238 return g_const_string;
2239}
2240
2241const ConstString &
2242GetSettingNameForInheritHostEnv ()
2243{
2244 static ConstString g_const_string (TSC_INHERIT_ENV);
2245 return g_const_string;
2246}
2247
2248const ConstString &
2249GetSettingNameForInputPath ()
2250{
2251 static ConstString g_const_string (TSC_STDIN_PATH);
2252 return g_const_string;
2253}
2254
2255const ConstString &
2256GetSettingNameForOutputPath ()
2257{
2258 static ConstString g_const_string (TSC_STDOUT_PATH);
2259 return g_const_string;
2260}
2261
2262const ConstString &
2263GetSettingNameForErrorPath ()
2264{
2265 static ConstString g_const_string (TSC_STDERR_PATH);
2266 return g_const_string;
2267}
2268
2269const ConstString &
2270GetSettingNameForDisableASLR ()
2271{
2272 static ConstString g_const_string (TSC_DISABLE_ASLR);
2273 return g_const_string;
2274}
2275
2276const ConstString &
2277GetSettingNameForDisableSTDIO ()
2278{
2279 static ConstString g_const_string (TSC_DISABLE_STDIO);
2280 return g_const_string;
2281}
Jim Ingham7089d8a2011-10-28 23:14:11 +00002282
Caroline Tice5bc8c972010-09-20 20:44:43 +00002283bool
2284Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2285 const char *index_value,
2286 const char *value,
2287 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002288 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002289 Error&err)
2290{
Greg Claytond284b662011-02-18 01:44:25 +00002291 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002292 {
Greg Claytonb170aee2012-05-08 01:45:38 +00002293 m_default_architecture.SetTriple (value);
Greg Clayton940b1032011-02-23 00:35:02 +00002294 if (!m_default_architecture.IsValid())
2295 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002296 }
2297 return true;
2298}
2299
2300
2301bool
2302Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2303 StringList &value,
2304 Error &err)
2305{
Greg Claytond284b662011-02-18 01:44:25 +00002306 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002307 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002308 // If the arch is invalid (the default), don't show a string for it
2309 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002310 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002311 return true;
2312 }
2313 else
2314 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2315
2316 return false;
2317}
2318
2319//--------------------------------------------------------------
2320// class TargetInstanceSettings
2321//--------------------------------------------------------------
2322
Greg Clayton638351a2010-12-04 00:10:17 +00002323TargetInstanceSettings::TargetInstanceSettings
2324(
Greg Clayton334d33a2012-01-30 07:41:31 +00002325 const lldb::UserSettingsControllerSP &owner_sp,
Greg Clayton638351a2010-12-04 00:10:17 +00002326 bool live_instance,
2327 const char *name
2328) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002329 InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002330 m_expr_prefix_file (),
Sean Callanane0b7f942011-11-16 01:54:57 +00002331 m_expr_prefix_contents (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002332 m_prefer_dynamic_value (2),
Enrico Granatadba1de82012-03-27 02:35:13 +00002333 m_enable_synthetic_value(true, true),
Greg Claytonff44ab42011-04-23 02:04:55 +00002334 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002335 m_source_map (NULL, NULL),
Greg Clayton9ce95382012-02-13 23:10:39 +00002336 m_exe_search_paths (),
Enrico Granata91544802011-09-06 19:20:51 +00002337 m_max_children_display(256),
Jim Ingham7089d8a2011-10-28 23:14:11 +00002338 m_max_strlen_length(1024),
Greg Claytonabb33022011-11-08 02:43:13 +00002339 m_breakpoints_use_platform_avoid (true, true),
2340 m_run_args (),
2341 m_env_vars (),
2342 m_input_path (),
2343 m_output_path (),
2344 m_error_path (),
2345 m_disable_aslr (true),
2346 m_disable_stdio (false),
2347 m_inherit_host_env (true),
2348 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002349{
2350 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2351 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2352 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2353 // This is true for CreateInstanceName() too.
2354
2355 if (GetInstanceName () == InstanceSettings::InvalidName())
2356 {
2357 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Clayton334d33a2012-01-30 07:41:31 +00002358 owner_sp->RegisterInstanceSettings (this);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002359 }
2360
2361 if (live_instance)
2362 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002363 const lldb::InstanceSettingsSP &pending_settings = owner_sp->FindPendingSettings (m_instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002364 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002365 }
2366}
2367
2368TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002369 InstanceSettings (Target::GetSettingsController(), CreateInstanceName().AsCString()),
Greg Claytonff44ab42011-04-23 02:04:55 +00002370 m_expr_prefix_file (rhs.m_expr_prefix_file),
Sean Callanane0b7f942011-11-16 01:54:57 +00002371 m_expr_prefix_contents (rhs.m_expr_prefix_contents),
Greg Claytonff44ab42011-04-23 02:04:55 +00002372 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
Enrico Granatadba1de82012-03-27 02:35:13 +00002373 m_enable_synthetic_value(rhs.m_enable_synthetic_value),
Greg Claytonff44ab42011-04-23 02:04:55 +00002374 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002375 m_source_map (rhs.m_source_map),
Greg Clayton9ce95382012-02-13 23:10:39 +00002376 m_exe_search_paths (rhs.m_exe_search_paths),
Greg Claytonabb33022011-11-08 02:43:13 +00002377 m_max_children_display (rhs.m_max_children_display),
2378 m_max_strlen_length (rhs.m_max_strlen_length),
2379 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2380 m_run_args (rhs.m_run_args),
2381 m_env_vars (rhs.m_env_vars),
2382 m_input_path (rhs.m_input_path),
2383 m_output_path (rhs.m_output_path),
2384 m_error_path (rhs.m_error_path),
2385 m_disable_aslr (rhs.m_disable_aslr),
2386 m_disable_stdio (rhs.m_disable_stdio),
2387 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002388{
2389 if (m_instance_name != InstanceSettings::GetDefaultName())
2390 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002391 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
2392 if (owner_sp)
2393 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name),false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002394 }
2395}
2396
2397TargetInstanceSettings::~TargetInstanceSettings ()
2398{
2399}
2400
2401TargetInstanceSettings&
2402TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2403{
2404 if (this != &rhs)
2405 {
Greg Claytonabb33022011-11-08 02:43:13 +00002406 m_expr_prefix_file = rhs.m_expr_prefix_file;
Sean Callanane0b7f942011-11-16 01:54:57 +00002407 m_expr_prefix_contents = rhs.m_expr_prefix_contents;
Greg Claytonabb33022011-11-08 02:43:13 +00002408 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
Enrico Granatadba1de82012-03-27 02:35:13 +00002409 m_enable_synthetic_value = rhs.m_enable_synthetic_value;
Greg Claytonabb33022011-11-08 02:43:13 +00002410 m_skip_prologue = rhs.m_skip_prologue;
2411 m_source_map = rhs.m_source_map;
Greg Clayton9ce95382012-02-13 23:10:39 +00002412 m_exe_search_paths = rhs.m_exe_search_paths;
Greg Claytonabb33022011-11-08 02:43:13 +00002413 m_max_children_display = rhs.m_max_children_display;
2414 m_max_strlen_length = rhs.m_max_strlen_length;
2415 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2416 m_run_args = rhs.m_run_args;
2417 m_env_vars = rhs.m_env_vars;
2418 m_input_path = rhs.m_input_path;
2419 m_output_path = rhs.m_output_path;
2420 m_error_path = rhs.m_error_path;
2421 m_disable_aslr = rhs.m_disable_aslr;
2422 m_disable_stdio = rhs.m_disable_stdio;
2423 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002424 }
2425
2426 return *this;
2427}
2428
Caroline Tice5bc8c972010-09-20 20:44:43 +00002429void
2430TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2431 const char *index_value,
2432 const char *value,
2433 const ConstString &instance_name,
2434 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002435 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002436 Error &err,
2437 bool pending)
2438{
Greg Claytond284b662011-02-18 01:44:25 +00002439 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002440 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002441 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2442 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002443 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002444 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002445 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002446 default:
2447 break;
2448 case eVarSetOperationAssign:
2449 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002450 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002451 m_expr_prefix_contents.clear();
2452
Greg Claytonff44ab42011-04-23 02:04:55 +00002453 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2454 {
2455 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002456 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002457 return;
2458 }
2459
Greg Clayton4b23ab32012-01-06 02:01:06 +00002460 DataBufferSP file_data_sp (m_expr_prefix_file.GetCurrentValue().ReadFileContents(0, SIZE_MAX, &err));
Sean Callanane0b7f942011-11-16 01:54:57 +00002461
Greg Clayton4b23ab32012-01-06 02:01:06 +00002462 if (err.Success())
Greg Claytonff44ab42011-04-23 02:04:55 +00002463 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002464 if (file_data_sp && file_data_sp->GetByteSize() > 0)
2465 {
2466 m_expr_prefix_contents.assign((const char*)file_data_sp->GetBytes(), file_data_sp->GetByteSize());
2467 }
2468 else
2469 {
2470 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
2471 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002472 }
Sean Callanan77e93942010-10-29 00:29:03 +00002473 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002474 break;
2475 case eVarSetOperationClear:
Sean Callanane0b7f942011-11-16 01:54:57 +00002476 m_expr_prefix_contents.clear();
Sean Callanan77e93942010-10-29 00:29:03 +00002477 }
Sean Callanan77e93942010-10-29 00:29:03 +00002478 }
2479 }
Jim Inghame41494a2011-04-16 00:01:13 +00002480 else if (var_name == GetSettingNameForPreferDynamicValue())
2481 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002482 int new_value;
2483 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2484 if (err.Success())
2485 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002486 }
Enrico Granatadba1de82012-03-27 02:35:13 +00002487 else if (var_name == GetSettingNameForEnableSyntheticValue())
2488 {
2489 bool ok;
2490 bool new_value = Args::StringToBoolean(value, true, &ok);
2491 if (ok)
2492 m_enable_synthetic_value.SetCurrentValue(new_value);
2493 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002494 else if (var_name == GetSettingNameForSkipPrologue())
2495 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002496 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2497 }
Enrico Granata018921d2011-08-12 02:00:06 +00002498 else if (var_name == GetSettingNameForMaxChildren())
2499 {
2500 bool ok;
2501 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2502 if (ok)
2503 m_max_children_display = new_value;
2504 }
Enrico Granata91544802011-09-06 19:20:51 +00002505 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2506 {
2507 bool ok;
2508 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2509 if (ok)
2510 m_max_strlen_length = new_value;
2511 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002512 else if (var_name == GetSettingNameForExecutableSearchPaths())
2513 {
2514 switch (op)
2515 {
2516 case eVarSetOperationReplace:
2517 case eVarSetOperationInsertBefore:
2518 case eVarSetOperationInsertAfter:
2519 case eVarSetOperationRemove:
2520 default:
2521 break;
2522 case eVarSetOperationAssign:
2523 m_exe_search_paths.Clear();
2524 // Fall through to append....
2525 case eVarSetOperationAppend:
2526 {
2527 Args args(value);
2528 const uint32_t argc = args.GetArgumentCount();
2529 if (argc > 0)
2530 {
2531 const char *exe_search_path_dir;
2532 for (uint32_t idx = 0; (exe_search_path_dir = args.GetArgumentAtIndex(idx)) != NULL; ++idx)
2533 {
2534 FileSpec file_spec;
2535 file_spec.GetDirectory().SetCString(exe_search_path_dir);
2536 FileSpec::FileType file_type = file_spec.GetFileType();
2537 if (file_type == FileSpec::eFileTypeDirectory || file_type == FileSpec::eFileTypeInvalid)
2538 {
2539 m_exe_search_paths.Append(file_spec);
2540 }
2541 else
2542 {
2543 err.SetErrorStringWithFormat("executable search path '%s' exists, but it does not resolve to a directory", exe_search_path_dir);
2544 }
2545 }
2546 }
2547 }
2548 break;
2549
2550 case eVarSetOperationClear:
2551 m_exe_search_paths.Clear();
2552 break;
2553 }
2554 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002555 else if (var_name == GetSettingNameForSourcePathMap ())
2556 {
2557 switch (op)
2558 {
2559 case eVarSetOperationReplace:
2560 case eVarSetOperationInsertBefore:
2561 case eVarSetOperationInsertAfter:
2562 case eVarSetOperationRemove:
2563 default:
2564 break;
2565 case eVarSetOperationAssign:
2566 m_source_map.Clear(true);
2567 // Fall through to append....
2568 case eVarSetOperationAppend:
2569 {
2570 Args args(value);
2571 const uint32_t argc = args.GetArgumentCount();
2572 if (argc & 1 || argc == 0)
2573 {
2574 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2575 }
2576 else
2577 {
2578 char resolved_new_path[PATH_MAX];
2579 FileSpec file_spec;
2580 const char *old_path;
2581 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2582 {
2583 const char *new_path = args.GetArgumentAtIndex(idx+1);
2584 assert (new_path); // We have an even number of paths, this shouldn't happen!
2585
2586 file_spec.SetFile(new_path, true);
2587 if (file_spec.Exists())
2588 {
2589 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2590 {
2591 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2592 return;
2593 }
2594 }
2595 else
2596 {
2597 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2598 return;
2599 }
2600 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2601 }
2602 }
2603 }
2604 break;
2605
2606 case eVarSetOperationClear:
2607 m_source_map.Clear(true);
2608 break;
2609 }
Jim Inghame41494a2011-04-16 00:01:13 +00002610 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002611 else if (var_name == GetSettingNameForPlatformAvoid ())
2612 {
2613 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2614 }
Greg Claytonabb33022011-11-08 02:43:13 +00002615 else if (var_name == GetSettingNameForRunArgs())
2616 {
2617 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2618 }
2619 else if (var_name == GetSettingNameForEnvVars())
2620 {
2621 // This is nice for local debugging, but it is isn't correct for
2622 // remote debugging. We need to stop process.env-vars from being
2623 // populated with the host environment and add this as a launch option
2624 // and get the correct environment from the Target's platform.
2625 // GetHostEnvironmentIfNeeded ();
2626 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2627 }
2628 else if (var_name == GetSettingNameForInputPath())
2629 {
2630 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2631 }
2632 else if (var_name == GetSettingNameForOutputPath())
2633 {
2634 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2635 }
2636 else if (var_name == GetSettingNameForErrorPath())
2637 {
2638 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2639 }
2640 else if (var_name == GetSettingNameForDisableASLR())
2641 {
2642 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2643 }
2644 else if (var_name == GetSettingNameForDisableSTDIO ())
2645 {
2646 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2647 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002648}
2649
2650void
Greg Claytond284b662011-02-18 01:44:25 +00002651TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002652{
Sean Callanan77e93942010-10-29 00:29:03 +00002653 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2654
2655 if (!new_settings_ptr)
2656 return;
2657
Greg Claytonabb33022011-11-08 02:43:13 +00002658 *this = *new_settings_ptr;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002659}
2660
Caroline Ticebcb5b452010-09-20 21:37:42 +00002661bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002662TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2663 const ConstString &var_name,
2664 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002665 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002666{
Greg Claytond284b662011-02-18 01:44:25 +00002667 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002668 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002669 char path[PATH_MAX];
2670 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2671 if (path_len > 0)
2672 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002673 }
Jim Inghame41494a2011-04-16 00:01:13 +00002674 else if (var_name == GetSettingNameForPreferDynamicValue())
2675 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002676 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002677 }
Enrico Granatadba1de82012-03-27 02:35:13 +00002678 else if (var_name == GetSettingNameForEnableSyntheticValue())
2679 {
2680 if (m_skip_prologue)
2681 value.AppendString ("true");
2682 else
2683 value.AppendString ("false");
2684 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002685 else if (var_name == GetSettingNameForSkipPrologue())
2686 {
2687 if (m_skip_prologue)
2688 value.AppendString ("true");
2689 else
2690 value.AppendString ("false");
2691 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002692 else if (var_name == GetSettingNameForExecutableSearchPaths())
2693 {
2694 if (m_exe_search_paths.GetSize())
2695 {
2696 for (size_t i = 0, n = m_exe_search_paths.GetSize(); i < n; ++i)
2697 {
2698 value.AppendString(m_exe_search_paths.GetFileSpecAtIndex (i).GetDirectory().AsCString());
2699 }
2700 }
2701 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002702 else if (var_name == GetSettingNameForSourcePathMap ())
2703 {
Johnny Chen931449e2011-12-12 21:59:28 +00002704 if (m_source_map.GetSize())
2705 {
2706 size_t i;
2707 for (i = 0; i < m_source_map.GetSize(); ++i) {
2708 StreamString sstr;
2709 m_source_map.Dump(&sstr, i);
2710 value.AppendString(sstr.GetData());
2711 }
2712 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002713 }
Enrico Granata018921d2011-08-12 02:00:06 +00002714 else if (var_name == GetSettingNameForMaxChildren())
2715 {
2716 StreamString count_str;
2717 count_str.Printf ("%d", m_max_children_display);
2718 value.AppendString (count_str.GetData());
2719 }
Enrico Granata91544802011-09-06 19:20:51 +00002720 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2721 {
2722 StreamString count_str;
2723 count_str.Printf ("%d", m_max_strlen_length);
2724 value.AppendString (count_str.GetData());
2725 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002726 else if (var_name == GetSettingNameForPlatformAvoid())
2727 {
2728 if (m_breakpoints_use_platform_avoid)
2729 value.AppendString ("true");
2730 else
2731 value.AppendString ("false");
2732 }
Greg Claytonabb33022011-11-08 02:43:13 +00002733 else if (var_name == GetSettingNameForRunArgs())
2734 {
2735 if (m_run_args.GetArgumentCount() > 0)
2736 {
2737 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2738 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2739 }
2740 }
2741 else if (var_name == GetSettingNameForEnvVars())
2742 {
2743 GetHostEnvironmentIfNeeded ();
2744
2745 if (m_env_vars.size() > 0)
2746 {
2747 std::map<std::string, std::string>::iterator pos;
2748 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2749 {
2750 StreamString value_str;
2751 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2752 value.AppendString (value_str.GetData());
2753 }
2754 }
2755 }
2756 else if (var_name == GetSettingNameForInputPath())
2757 {
2758 value.AppendString (m_input_path.c_str());
2759 }
2760 else if (var_name == GetSettingNameForOutputPath())
2761 {
2762 value.AppendString (m_output_path.c_str());
2763 }
2764 else if (var_name == GetSettingNameForErrorPath())
2765 {
2766 value.AppendString (m_error_path.c_str());
2767 }
2768 else if (var_name == GetSettingNameForInheritHostEnv())
2769 {
2770 if (m_inherit_host_env)
2771 value.AppendString ("true");
2772 else
2773 value.AppendString ("false");
2774 }
2775 else if (var_name == GetSettingNameForDisableASLR())
2776 {
2777 if (m_disable_aslr)
2778 value.AppendString ("true");
2779 else
2780 value.AppendString ("false");
2781 }
2782 else if (var_name == GetSettingNameForDisableSTDIO())
2783 {
2784 if (m_disable_stdio)
2785 value.AppendString ("true");
2786 else
2787 value.AppendString ("false");
2788 }
Sean Callanan77e93942010-10-29 00:29:03 +00002789 else
2790 {
2791 if (err)
2792 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2793 return false;
2794 }
Sean Callanan77e93942010-10-29 00:29:03 +00002795 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002796}
2797
Greg Claytonabb33022011-11-08 02:43:13 +00002798void
2799Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2800{
2801 if (m_inherit_host_env && !m_got_host_env)
2802 {
2803 m_got_host_env = true;
2804 StringList host_env;
2805 const size_t host_env_count = Host::GetEnvironment (host_env);
2806 for (size_t idx=0; idx<host_env_count; idx++)
2807 {
2808 const char *env_entry = host_env.GetStringAtIndex (idx);
2809 if (env_entry)
2810 {
2811 const char *equal_pos = ::strchr(env_entry, '=');
2812 if (equal_pos)
2813 {
2814 std::string key (env_entry, equal_pos - env_entry);
2815 std::string value (equal_pos + 1);
2816 if (m_env_vars.find (key) == m_env_vars.end())
2817 m_env_vars[key] = value;
2818 }
2819 }
2820 }
2821 }
2822}
2823
2824
2825size_t
2826Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2827{
2828 GetHostEnvironmentIfNeeded ();
2829
2830 dictionary::const_iterator pos, end = m_env_vars.end();
2831 for (pos = m_env_vars.begin(); pos != end; ++pos)
2832 {
2833 std::string env_var_equal_value (pos->first);
2834 env_var_equal_value.append(1, '=');
2835 env_var_equal_value.append (pos->second);
2836 env.AppendArgument (env_var_equal_value.c_str());
2837 }
2838 return env.GetArgumentCount();
2839}
2840
2841
Caroline Tice5bc8c972010-09-20 20:44:43 +00002842const ConstString
2843TargetInstanceSettings::CreateInstanceName ()
2844{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002845 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002846 static int instance_count = 1;
2847
Caroline Tice5bc8c972010-09-20 20:44:43 +00002848 sstr.Printf ("target_%d", instance_count);
2849 ++instance_count;
2850
2851 const ConstString ret_val (sstr.GetData());
2852 return ret_val;
2853}
2854
2855//--------------------------------------------------
2856// Target::SettingsController Variable Tables
2857//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002858OptionEnumValueElement
2859TargetInstanceSettings::g_dynamic_value_types[] =
2860{
Greg Clayton577fbc32011-05-30 00:39:48 +00002861{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2862{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2863{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002864{ 0, NULL, NULL }
2865};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002866
2867SettingEntry
2868Target::SettingsController::global_settings_table[] =
2869{
Greg Claytond284b662011-02-18 01:44:25 +00002870 // var-name var-type default enum init'd hidden help-text
2871 // ================= ================== =========== ==== ====== ====== =========================================================================
2872 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2873 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2874};
2875
Caroline Tice5bc8c972010-09-20 20:44:43 +00002876SettingEntry
2877Target::SettingsController::instance_settings_table[] =
2878{
Enrico Granata91544802011-09-06 19:20:51 +00002879 // var-name var-type default enum init'd hidden help-text
2880 // ================= ================== =============== ======================= ====== ====== =========================================================================
2881 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2882 { 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 +00002883 { TSC_ENABLE_SYNTHETIC , eSetVarTypeBoolean, "true" , NULL, false, false, "Should synthetic values be used by default whenever available." },
Enrico Granata91544802011-09-06 19:20:51 +00002884 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2885 { 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 +00002886 { 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 +00002887 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2888 { 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 +00002889 { 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 +00002890 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2891 { 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." },
2892 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2893 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2894 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2895 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2896// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2897 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2898 { 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 +00002899 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002900};
Jim Ingham5a15e692012-02-16 06:50:00 +00002901
2902const ConstString &
2903Target::TargetEventData::GetFlavorString ()
2904{
2905 static ConstString g_flavor ("Target::TargetEventData");
2906 return g_flavor;
2907}
2908
2909const ConstString &
2910Target::TargetEventData::GetFlavor () const
2911{
2912 return TargetEventData::GetFlavorString ();
2913}
2914
2915Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2916 EventData(),
2917 m_target_sp (new_target_sp)
2918{
2919}
2920
2921Target::TargetEventData::~TargetEventData()
2922{
2923
2924}
2925
2926void
2927Target::TargetEventData::Dump (Stream *s) const
2928{
2929
2930}
2931
2932const TargetSP
2933Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2934{
2935 TargetSP target_sp;
2936
2937 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2938 if (data)
2939 target_sp = data->m_target_sp;
2940
2941 return target_sp;
2942}
2943
2944const Target::TargetEventData *
2945Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2946{
2947 if (event_ptr)
2948 {
2949 const EventData *event_data = event_ptr->GetData();
2950 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2951 return static_cast <const TargetEventData *> (event_ptr->GetData());
2952 }
2953 return NULL;
2954}
2955