blob: 442c08e42044f9565fde1a0cec1a0b42430904cb [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham03c8ee52011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton427f2902010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000026#include "lldb/Core/Timer.h"
27#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000028#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000029#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000031#include "lldb/Interpreter/CommandInterpreter.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/lldb-private-log.h"
34#include "lldb/Symbol/ObjectFile.h"
35#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000036#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000037#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
Jim Ingham5a15e692012-02-16 06:50:00 +000043ConstString &
44Target::GetStaticBroadcasterClass ()
45{
46 static ConstString class_name ("lldb.target");
47 return class_name;
48}
49
Chris Lattner24943d22010-06-08 16:52:24 +000050//----------------------------------------------------------------------
51// Target constructor
52//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000053Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Jim Ingham5a15e692012-02-16 06:50:00 +000054 Broadcaster (&debugger, "lldb.target"),
Greg Clayton24bc5d92011-03-30 18:16:51 +000055 ExecutionContextScope (),
Greg Clayton334d33a2012-01-30 07:41:31 +000056 TargetInstanceSettings (GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000057 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000058 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000059 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000060 m_arch (target_arch),
61 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000062 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000063 m_breakpoint_list (false),
64 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000065 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000066 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000067 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000068 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000069 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000070 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000071 m_scratch_ast_source_ap (NULL),
72 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000073 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000074 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000075 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000076 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000077 m_suppress_stop_hooks (false),
78 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000079{
Greg Clayton49ce6822010-10-31 03:01:06 +000080 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
81 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
82 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000083
84 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000085
Greg Claytone005f2c2010-11-06 01:53:30 +000086 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000087 if (log)
88 log->Printf ("%p Target::Target()", this);
89}
90
91//----------------------------------------------------------------------
92// Destructor
93//----------------------------------------------------------------------
94Target::~Target()
95{
Greg Claytone005f2c2010-11-06 01:53:30 +000096 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000097 if (log)
98 log->Printf ("%p Target::~Target()", this);
99 DeleteCurrentProcess ();
100}
101
102void
Caroline Tice7826c882010-10-26 03:11:13 +0000103Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000104{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000105// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000106 if (description_level != lldb::eDescriptionLevelBrief)
107 {
108 s->Indent();
109 s->PutCString("Target\n");
110 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000111 m_images.Dump(s);
112 m_breakpoint_list.Dump(s);
113 m_internal_breakpoint_list.Dump(s);
114 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000115 }
116 else
117 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000118 Module *exe_module = GetExecutableModulePointer();
119 if (exe_module)
120 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000121 else
122 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000123 }
Chris Lattner24943d22010-06-08 16:52:24 +0000124}
125
126void
127Target::DeleteCurrentProcess ()
128{
129 if (m_process_sp.get())
130 {
Greg Clayton49480b12010-09-14 23:52:43 +0000131 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000132 if (m_process_sp->IsAlive())
133 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000134
135 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000136
137 // Do any cleanup of the target we need to do between process instances.
138 // NB It is better to do this before destroying the process in case the
139 // clean up needs some help from the process.
140 m_breakpoint_list.ClearAllBreakpointSites();
141 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000142 // Disable watchpoints just on the debugger side.
Johnny Chen116a5cd2012-02-25 06:44:30 +0000143 Mutex::Locker locker;
144 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000145 DisableAllWatchpoints(false);
Johnny Chen116a5cd2012-02-25 06:44:30 +0000146 ClearAllWatchpointHitCounts();
Chris Lattner24943d22010-06-08 16:52:24 +0000147 m_process_sp.reset();
148 }
149}
150
151const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000152Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000153{
154 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000155 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000156 return m_process_sp;
157}
158
159const lldb::ProcessSP &
160Target::GetProcessSP () const
161{
162 return m_process_sp;
163}
164
Greg Clayton153ccd72011-08-10 02:10:13 +0000165void
166Target::Destroy()
167{
168 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000169 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000170 DeleteCurrentProcess ();
171 m_platform_sp.reset();
172 m_arch.Clear();
173 m_images.Clear();
174 m_section_load_list.Clear();
175 const bool notify = false;
176 m_breakpoint_list.RemoveAll(notify);
177 m_internal_breakpoint_list.RemoveAll(notify);
178 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000179 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000180 m_search_filter_sp.reset();
181 m_image_search_paths.Clear(notify);
182 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000183 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000184 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000185 m_persistent_variables.Clear();
186 m_stop_hooks.clear();
187 m_stop_hook_next_id = 0;
188 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000189 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000190}
191
192
Chris Lattner24943d22010-06-08 16:52:24 +0000193BreakpointList &
194Target::GetBreakpointList(bool internal)
195{
196 if (internal)
197 return m_internal_breakpoint_list;
198 else
199 return m_breakpoint_list;
200}
201
202const BreakpointList &
203Target::GetBreakpointList(bool internal) const
204{
205 if (internal)
206 return m_internal_breakpoint_list;
207 else
208 return m_breakpoint_list;
209}
210
211BreakpointSP
212Target::GetBreakpointByID (break_id_t break_id)
213{
214 BreakpointSP bp_sp;
215
216 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
217 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
218 else
219 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
220
221 return bp_sp;
222}
223
224BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000225Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
226 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000227 RegularExpression &source_regex,
228 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000229{
Jim Inghamd6d47972011-09-23 00:54:11 +0000230 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
231 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000232 return CreateBreakpoint (filter_sp, resolver_sp, internal);
233}
234
235
236BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000237Target::CreateBreakpoint (const FileSpecList *containingModules,
238 const FileSpec &file,
239 uint32_t line_no,
240 bool check_inlines,
241 LazyBool skip_prologue,
242 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000243{
244 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000245
246 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines,
247 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000248 return CreateBreakpoint (filter_sp, resolver_sp, internal);
249}
250
251
252BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000253Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000254{
Chris Lattner24943d22010-06-08 16:52:24 +0000255 Address so_addr;
256 // Attempt to resolve our load address if possible, though it is ok if
257 // it doesn't resolve to section/offset.
258
Greg Clayton33ed1702010-08-24 00:45:41 +0000259 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000260 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000261 if (!so_addr.IsValid())
262 {
263 // The address didn't resolve, so just set this as an absolute address
264 so_addr.SetOffset (addr);
265 }
266 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000267 return bp_sp;
268}
269
270BreakpointSP
271Target::CreateBreakpoint (Address &addr, bool internal)
272{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000273 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000274 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
275 return CreateBreakpoint (filter_sp, resolver_sp, internal);
276}
277
278BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000279Target::CreateBreakpoint (const FileSpecList *containingModules,
280 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000281 const char *func_name,
282 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000283 LazyBool skip_prologue,
284 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000285{
Greg Clayton12bec712010-06-28 21:30:43 +0000286 BreakpointSP bp_sp;
287 if (func_name)
288 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000289 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000290
291 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
292 func_name,
293 func_name_type_mask,
294 Breakpoint::Exact,
295 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000296 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
297 }
298 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000299}
300
Jim Ingham4722b102012-03-06 00:37:27 +0000301lldb::BreakpointSP
302Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000303 const FileSpecList *containingSourceFiles,
304 const std::vector<std::string> &func_names,
305 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000306 LazyBool skip_prologue,
307 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000308{
309 BreakpointSP bp_sp;
310 size_t num_names = func_names.size();
311 if (num_names > 0)
312 {
313 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
314
315 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
316 func_names,
317 func_name_type_mask,
318 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
319 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
320 }
321 return bp_sp;
322}
323
Jim Inghamc1053622012-03-03 02:05:11 +0000324BreakpointSP
325Target::CreateBreakpoint (const FileSpecList *containingModules,
326 const FileSpecList *containingSourceFiles,
327 const char *func_names[],
328 size_t num_names,
329 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000330 LazyBool skip_prologue,
331 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000332{
333 BreakpointSP bp_sp;
334 if (num_names > 0)
335 {
336 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
337
338 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
339 func_names,
340 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000341 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000342 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
343 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
344 }
345 return bp_sp;
346}
Chris Lattner24943d22010-06-08 16:52:24 +0000347
348SearchFilterSP
349Target::GetSearchFilterForModule (const FileSpec *containingModule)
350{
351 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000352 if (containingModule != NULL)
353 {
354 // TODO: We should look into sharing module based search filters
355 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000356 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000357 }
358 else
359 {
360 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000361 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000362 filter_sp = m_search_filter_sp;
363 }
364 return filter_sp;
365}
366
Jim Ingham03c8ee52011-09-21 01:17:13 +0000367SearchFilterSP
368Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
369{
370 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000371 if (containingModules && containingModules->GetSize() != 0)
372 {
373 // TODO: We should look into sharing module based search filters
374 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000375 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000376 }
377 else
378 {
379 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000380 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000381 filter_sp = m_search_filter_sp;
382 }
383 return filter_sp;
384}
385
Jim Inghamd6d47972011-09-23 00:54:11 +0000386SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000387Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
388 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000389{
390 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
391 return GetSearchFilterForModuleList(containingModules);
392
393 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000394 if (containingModules == NULL)
395 {
396 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
397 // but that will take a little reworking.
398
Greg Clayton13d24fb2012-01-29 20:56:30 +0000399 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000400 }
401 else
402 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000403 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000404 }
405 return filter_sp;
406}
407
Chris Lattner24943d22010-06-08 16:52:24 +0000408BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000409Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000410 const FileSpecList *containingSourceFiles,
411 RegularExpression &func_regex,
412 LazyBool skip_prologue,
413 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000414{
Jim Inghamd6d47972011-09-23 00:54:11 +0000415 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000416 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
417 func_regex,
418 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000419
420 return CreateBreakpoint (filter_sp, resolver_sp, internal);
421}
422
Jim Ingham3df164e2012-03-05 04:47:34 +0000423lldb::BreakpointSP
424Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
425{
426 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
427}
428
Chris Lattner24943d22010-06-08 16:52:24 +0000429BreakpointSP
430Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
431{
432 BreakpointSP bp_sp;
433 if (filter_sp && resolver_sp)
434 {
435 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
436 resolver_sp->SetBreakpoint (bp_sp.get());
437
438 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000439 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000440 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000441 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000442
Greg Claytone005f2c2010-11-06 01:53:30 +0000443 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000444 if (log)
445 {
446 StreamString s;
447 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
448 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
449 }
450
Chris Lattner24943d22010-06-08 16:52:24 +0000451 bp_sp->ResolveBreakpoint();
452 }
Jim Inghamd1686902010-10-14 23:45:03 +0000453
454 if (!internal && bp_sp)
455 {
456 m_last_created_breakpoint = bp_sp;
457 }
458
Chris Lattner24943d22010-06-08 16:52:24 +0000459 return bp_sp;
460}
461
Johnny Chenda5a8022011-09-20 23:28:55 +0000462bool
463Target::ProcessIsValid()
464{
465 return (m_process_sp && m_process_sp->IsAlive());
466}
467
Johnny Chenecd4feb2011-10-14 00:42:25 +0000468// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000469// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000470WatchpointSP
471Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type)
Johnny Chen34bbf852011-09-12 23:38:44 +0000472{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000473 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
474 if (log)
475 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
476 __FUNCTION__, addr, size, type);
477
Johnny Chenecd4feb2011-10-14 00:42:25 +0000478 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000479 if (!ProcessIsValid())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000480 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000481 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000482 return wp_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000483
Johnny Chenecd4feb2011-10-14 00:42:25 +0000484 // Currently we only support one watchpoint per address, with total number
485 // of watchpoints limited by the hardware which the inferior is running on.
486 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000487 if (matched_sp)
488 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000489 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000490 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000491 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
492 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000493 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000494 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000495 wp_sp = matched_sp;
496 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000497 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000498 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000499 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000500 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000501 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000502 }
503
Johnny Chenecd4feb2011-10-14 00:42:25 +0000504 if (!wp_sp) {
505 Watchpoint *new_wp = new Watchpoint(addr, size);
506 if (!new_wp) {
507 printf("Watchpoint ctor failed, out of memory?\n");
508 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000509 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000510 new_wp->SetWatchpointType(type);
511 new_wp->SetTarget(this);
512 wp_sp.reset(new_wp);
513 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000514 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000515
Johnny Chenecd4feb2011-10-14 00:42:25 +0000516 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000517 if (log)
518 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
519 __FUNCTION__,
520 rc.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000521 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000522
Johnny Chen155599b2012-03-26 22:00:10 +0000523 if (rc.Fail()) {
524 // Enabling the watchpoint on the device side failed.
525 // Remove the said watchpoint from the list maintained by the target instance.
526 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000527 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000528 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000529 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000530 m_last_created_watchpoint = wp_sp;
531 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000532}
533
Chris Lattner24943d22010-06-08 16:52:24 +0000534void
535Target::RemoveAllBreakpoints (bool internal_also)
536{
Greg Claytone005f2c2010-11-06 01:53:30 +0000537 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000538 if (log)
539 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
540
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000541 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000542 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000543 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000544
545 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000546}
547
548void
549Target::DisableAllBreakpoints (bool internal_also)
550{
Greg Claytone005f2c2010-11-06 01:53:30 +0000551 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000552 if (log)
553 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
554
555 m_breakpoint_list.SetEnabledAll (false);
556 if (internal_also)
557 m_internal_breakpoint_list.SetEnabledAll (false);
558}
559
560void
561Target::EnableAllBreakpoints (bool internal_also)
562{
Greg Claytone005f2c2010-11-06 01:53:30 +0000563 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000564 if (log)
565 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
566
567 m_breakpoint_list.SetEnabledAll (true);
568 if (internal_also)
569 m_internal_breakpoint_list.SetEnabledAll (true);
570}
571
572bool
573Target::RemoveBreakpointByID (break_id_t break_id)
574{
Greg Claytone005f2c2010-11-06 01:53:30 +0000575 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000576 if (log)
577 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
578
579 if (DisableBreakpointByID (break_id))
580 {
581 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000582 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000583 else
Jim Inghamd1686902010-10-14 23:45:03 +0000584 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000585 if (m_last_created_breakpoint)
586 {
587 if (m_last_created_breakpoint->GetID() == break_id)
588 m_last_created_breakpoint.reset();
589 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000590 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000591 }
Chris Lattner24943d22010-06-08 16:52:24 +0000592 return true;
593 }
594 return false;
595}
596
597bool
598Target::DisableBreakpointByID (break_id_t break_id)
599{
Greg Claytone005f2c2010-11-06 01:53:30 +0000600 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000601 if (log)
602 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
603
604 BreakpointSP bp_sp;
605
606 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
607 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
608 else
609 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
610 if (bp_sp)
611 {
612 bp_sp->SetEnabled (false);
613 return true;
614 }
615 return false;
616}
617
618bool
619Target::EnableBreakpointByID (break_id_t break_id)
620{
Greg Claytone005f2c2010-11-06 01:53:30 +0000621 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000622 if (log)
623 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
624 __FUNCTION__,
625 break_id,
626 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
627
628 BreakpointSP bp_sp;
629
630 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
631 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
632 else
633 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
634
635 if (bp_sp)
636 {
637 bp_sp->SetEnabled (true);
638 return true;
639 }
640 return false;
641}
642
Johnny Chenc86582f2011-09-23 21:21:43 +0000643// The flag 'end_to_end', default to true, signifies that the operation is
644// performed end to end, for both the debugger and the debuggee.
645
Johnny Chenecd4feb2011-10-14 00:42:25 +0000646// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
647// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000648bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000649Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000650{
651 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
652 if (log)
653 log->Printf ("Target::%s\n", __FUNCTION__);
654
Johnny Chenc86582f2011-09-23 21:21:43 +0000655 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000656 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000657 return true;
658 }
659
660 // Otherwise, it's an end to end operation.
661
Johnny Chenda5a8022011-09-20 23:28:55 +0000662 if (!ProcessIsValid())
663 return false;
664
Johnny Chenecd4feb2011-10-14 00:42:25 +0000665 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000666 for (size_t i = 0; i < num_watchpoints; ++i)
667 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000668 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
669 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000670 return false;
671
Johnny Chenecd4feb2011-10-14 00:42:25 +0000672 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000673 if (rc.Fail())
674 return false;
675 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000676 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000677 return true; // Success!
678}
679
Johnny Chenecd4feb2011-10-14 00:42:25 +0000680// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
681// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000682bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000683Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000684{
685 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
686 if (log)
687 log->Printf ("Target::%s\n", __FUNCTION__);
688
Johnny Chenc86582f2011-09-23 21:21:43 +0000689 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000690 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000691 return true;
692 }
693
694 // Otherwise, it's an end to end operation.
695
Johnny Chenda5a8022011-09-20 23:28:55 +0000696 if (!ProcessIsValid())
697 return false;
698
Johnny Chenecd4feb2011-10-14 00:42:25 +0000699 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000700 for (size_t i = 0; i < num_watchpoints; ++i)
701 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000702 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
703 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000704 return false;
705
Johnny Chenecd4feb2011-10-14 00:42:25 +0000706 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000707 if (rc.Fail())
708 return false;
709 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000710 return true; // Success!
711}
712
Johnny Chenecd4feb2011-10-14 00:42:25 +0000713// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
714// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000715bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000716Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000717{
718 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
719 if (log)
720 log->Printf ("Target::%s\n", __FUNCTION__);
721
Johnny Chenc86582f2011-09-23 21:21:43 +0000722 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000723 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000724 return true;
725 }
726
727 // Otherwise, it's an end to end operation.
728
Johnny Chenda5a8022011-09-20 23:28:55 +0000729 if (!ProcessIsValid())
730 return false;
731
Johnny Chenecd4feb2011-10-14 00:42:25 +0000732 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000733 for (size_t i = 0; i < num_watchpoints; ++i)
734 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000735 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
736 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000737 return false;
738
Johnny Chenecd4feb2011-10-14 00:42:25 +0000739 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000740 if (rc.Fail())
741 return false;
742 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000743 return true; // Success!
744}
745
Johnny Chen116a5cd2012-02-25 06:44:30 +0000746// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
747bool
748Target::ClearAllWatchpointHitCounts ()
749{
750 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
751 if (log)
752 log->Printf ("Target::%s\n", __FUNCTION__);
753
754 size_t num_watchpoints = m_watchpoint_list.GetSize();
755 for (size_t i = 0; i < num_watchpoints; ++i)
756 {
757 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
758 if (!wp_sp)
759 return false;
760
761 wp_sp->ResetHitCount();
762 }
763 return true; // Success!
764}
765
Johnny Chenecd4feb2011-10-14 00:42:25 +0000766// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000767// during these operations.
768bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000769Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000770{
771 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
772 if (log)
773 log->Printf ("Target::%s\n", __FUNCTION__);
774
775 if (!ProcessIsValid())
776 return false;
777
Johnny Chenecd4feb2011-10-14 00:42:25 +0000778 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000779 for (size_t i = 0; i < num_watchpoints; ++i)
780 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000781 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
782 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000783 return false;
784
Johnny Chenecd4feb2011-10-14 00:42:25 +0000785 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000786 }
787 return true; // Success!
788}
789
Johnny Chenecd4feb2011-10-14 00:42:25 +0000790// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000791bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000792Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000793{
794 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
795 if (log)
796 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
797
798 if (!ProcessIsValid())
799 return false;
800
Johnny Chenecd4feb2011-10-14 00:42:25 +0000801 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
802 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000803 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000804 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000805 if (rc.Success())
806 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000807
Johnny Chen01acfa72011-09-22 18:04:58 +0000808 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000809 }
810 return false;
811}
812
Johnny Chenecd4feb2011-10-14 00:42:25 +0000813// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000814bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000815Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000816{
817 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
818 if (log)
819 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
820
821 if (!ProcessIsValid())
822 return false;
823
Johnny Chenecd4feb2011-10-14 00:42:25 +0000824 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
825 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000826 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000827 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000828 if (rc.Success())
829 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000830
Johnny Chen01acfa72011-09-22 18:04:58 +0000831 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000832 }
833 return false;
834}
835
Johnny Chenecd4feb2011-10-14 00:42:25 +0000836// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000837bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000838Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000839{
840 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
841 if (log)
842 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
843
Johnny Chenecd4feb2011-10-14 00:42:25 +0000844 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000845 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000846 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000847 return true;
848 }
849 return false;
850}
851
Johnny Chenecd4feb2011-10-14 00:42:25 +0000852// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000853bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000854Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000855{
856 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
857 if (log)
858 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
859
860 if (!ProcessIsValid())
861 return false;
862
Johnny Chenecd4feb2011-10-14 00:42:25 +0000863 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
864 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000865 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000866 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000867 return true;
868 }
869 return false;
870}
871
Chris Lattner24943d22010-06-08 16:52:24 +0000872ModuleSP
873Target::GetExecutableModule ()
874{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000875 return m_images.GetModuleAtIndex(0);
876}
877
878Module*
879Target::GetExecutableModulePointer ()
880{
881 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000882}
883
884void
885Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
886{
887 m_images.Clear();
888 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000889 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000890 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000891
892 if (executable_sp.get())
893 {
894 Timer scoped_timer (__PRETTY_FUNCTION__,
895 "Target::SetExecutableModule (executable = '%s/%s')",
896 executable_sp->GetFileSpec().GetDirectory().AsCString(),
897 executable_sp->GetFileSpec().GetFilename().AsCString());
898
899 m_images.Append(executable_sp); // The first image is our exectuable file
900
Jim Ingham7508e732010-08-09 23:31:02 +0000901 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000902 if (!m_arch.IsValid())
903 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000904
Chris Lattner24943d22010-06-08 16:52:24 +0000905 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000906 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000907
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000908 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000909 {
910 executable_objfile->GetDependentModules(dependent_files);
911 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
912 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000913 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
914 FileSpec platform_dependent_file_spec;
915 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000916 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000917 else
918 platform_dependent_file_spec = dependent_file_spec;
919
Greg Clayton444fe992012-02-26 05:51:37 +0000920 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
921 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +0000922 if (image_module_sp.get())
923 {
Chris Lattner24943d22010-06-08 16:52:24 +0000924 ObjectFile *objfile = image_module_sp->GetObjectFile();
925 if (objfile)
926 objfile->GetDependentModules(dependent_files);
927 }
928 }
929 }
Chris Lattner24943d22010-06-08 16:52:24 +0000930 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000931
932 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000933}
934
935
Jim Ingham7508e732010-08-09 23:31:02 +0000936bool
937Target::SetArchitecture (const ArchSpec &arch_spec)
938{
Greg Claytonb170aee2012-05-08 01:45:38 +0000939 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000940 {
Greg Claytonb170aee2012-05-08 01:45:38 +0000941 // If we haven't got a valid arch spec, or the architectures are
942 // compatible, so just update the architecture. Architectures can be
943 // equal, yet the triple OS and vendor might change, so we need to do
944 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000945 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000946 return true;
947 }
948 else
949 {
950 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000951 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000952 ModuleSP executable_sp = GetExecutableModule ();
953 m_images.Clear();
954 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000955 m_scratch_ast_source_ap.reset();
956 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000957 // Need to do something about unsetting breakpoints.
958
959 if (executable_sp)
960 {
Greg Clayton444fe992012-02-26 05:51:37 +0000961 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
962 Error error = ModuleList::GetSharedModule (module_spec,
963 executable_sp,
964 &GetExecutableSearchPaths(),
965 NULL,
966 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +0000967
968 if (!error.Fail() && executable_sp)
969 {
970 SetExecutableModule (executable_sp, true);
971 return true;
972 }
Jim Ingham7508e732010-08-09 23:31:02 +0000973 }
974 }
Greg Claytonb170aee2012-05-08 01:45:38 +0000975 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000976}
Chris Lattner24943d22010-06-08 16:52:24 +0000977
Chris Lattner24943d22010-06-08 16:52:24 +0000978void
979Target::ModuleAdded (ModuleSP &module_sp)
980{
981 // A module is being added to this target for the first time
982 ModuleList module_list;
983 module_list.Append(module_sp);
984 ModulesDidLoad (module_list);
985}
986
987void
988Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
989{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000990 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +0000991 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000992}
993
994void
995Target::ModulesDidLoad (ModuleList &module_list)
996{
997 m_breakpoint_list.UpdateBreakpoints (module_list, true);
998 // TODO: make event data that packages up the module_list
999 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1000}
1001
1002void
1003Target::ModulesDidUnload (ModuleList &module_list)
1004{
1005 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +00001006
1007 // Remove the images from the target image list
1008 m_images.Remove(module_list);
1009
Chris Lattner24943d22010-06-08 16:52:24 +00001010 // TODO: make event data that packages up the module_list
1011 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1012}
1013
Jim Ingham7089d8a2011-10-28 23:14:11 +00001014
Daniel Dunbar705a0982011-10-31 22:50:37 +00001015bool
Greg Clayton444fe992012-02-26 05:51:37 +00001016Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001017{
1018
1019 if (!m_breakpoints_use_platform_avoid)
1020 return false;
1021 else
1022 {
1023 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001024 ModuleSpec module_spec (module_file_spec);
1025 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001026
1027 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1028 // black list.
1029 if (num_modules > 0)
1030 {
1031 for (int i = 0; i < num_modules; i++)
1032 {
1033 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1034 return false;
1035 }
1036 return true;
1037 }
1038 else
1039 return false;
1040 }
1041}
1042
Daniel Dunbar705a0982011-10-31 22:50:37 +00001043bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001044Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1045{
1046 if (!m_breakpoints_use_platform_avoid)
1047 return false;
1048 else if (GetPlatform())
1049 {
1050 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
1051 }
1052 else
1053 return false;
1054}
1055
Chris Lattner24943d22010-06-08 16:52:24 +00001056size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001057Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1058{
Greg Clayton3508c382012-02-24 01:59:29 +00001059 SectionSP section_sp (addr.GetSection());
1060 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001061 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001062 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1063 if (section_sp->IsEncrypted())
1064 {
1065 return 0;
1066 }
Greg Clayton3508c382012-02-24 01:59:29 +00001067 ModuleSP module_sp (section_sp->GetModule());
1068 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001069 {
Greg Clayton3508c382012-02-24 01:59:29 +00001070 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1071 if (objfile)
1072 {
1073 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1074 addr.GetOffset(),
1075 dst,
1076 dst_len);
1077 if (bytes_read > 0)
1078 return bytes_read;
1079 else
1080 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1081 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001082 else
Greg Clayton3508c382012-02-24 01:59:29 +00001083 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001084 }
1085 else
Greg Clayton3508c382012-02-24 01:59:29 +00001086 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001087 }
1088 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001089 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001090
Greg Clayton26100dc2011-01-07 01:57:07 +00001091 return 0;
1092}
1093
1094size_t
Enrico Granata91544802011-09-06 19:20:51 +00001095Target::ReadMemory (const Address& addr,
1096 bool prefer_file_cache,
1097 void *dst,
1098 size_t dst_len,
1099 Error &error,
1100 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001101{
Chris Lattner24943d22010-06-08 16:52:24 +00001102 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001103
Enrico Granata91544802011-09-06 19:20:51 +00001104 // if we end up reading this from process memory, we will fill this
1105 // with the actual load address
1106 if (load_addr_ptr)
1107 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1108
Greg Clayton26100dc2011-01-07 01:57:07 +00001109 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001110
1111 addr_t load_addr = LLDB_INVALID_ADDRESS;
1112 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001113 Address resolved_addr;
1114 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001115 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001116 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001117 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001118 // No sections are loaded, so we must assume we are not running
1119 // yet and anything we are given is a file address.
1120 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1121 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001122 }
Greg Clayton70436352010-06-30 23:03:03 +00001123 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001124 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001125 // We have at least one section loaded. This can be becuase
1126 // we have manually loaded some sections with "target modules load ..."
1127 // or because we have have a live process that has sections loaded
1128 // through the dynamic loader
1129 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1130 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001131 }
Greg Clayton70436352010-06-30 23:03:03 +00001132 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001133 if (!resolved_addr.IsValid())
1134 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001135
Greg Clayton9b82f862011-07-11 05:12:02 +00001136
Greg Clayton26100dc2011-01-07 01:57:07 +00001137 if (prefer_file_cache)
1138 {
1139 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1140 if (bytes_read > 0)
1141 return bytes_read;
1142 }
Greg Clayton70436352010-06-30 23:03:03 +00001143
Johnny Chenda5a8022011-09-20 23:28:55 +00001144 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001145 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001146 if (load_addr == LLDB_INVALID_ADDRESS)
1147 load_addr = resolved_addr.GetLoadAddress (this);
1148
Greg Clayton70436352010-06-30 23:03:03 +00001149 if (load_addr == LLDB_INVALID_ADDRESS)
1150 {
Greg Clayton3508c382012-02-24 01:59:29 +00001151 ModuleSP addr_module_sp (resolved_addr.GetModule());
1152 if (addr_module_sp && addr_module_sp->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001153 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001154 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001155 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001156 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001157 else
Greg Clayton9c236732011-10-26 00:56:27 +00001158 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001159 }
1160 else
1161 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001162 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001163 if (bytes_read != dst_len)
1164 {
1165 if (error.Success())
1166 {
1167 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001168 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001169 else
Greg Clayton9c236732011-10-26 00:56:27 +00001170 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 +00001171 }
1172 }
Greg Clayton70436352010-06-30 23:03:03 +00001173 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001174 {
1175 if (load_addr_ptr)
1176 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001177 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001178 }
Greg Clayton70436352010-06-30 23:03:03 +00001179 // If the address is not section offset we have an address that
1180 // doesn't resolve to any address in any currently loaded shared
1181 // libaries and we failed to read memory so there isn't anything
1182 // more we can do. If it is section offset, we might be able to
1183 // read cached memory from the object file.
1184 if (!resolved_addr.IsSectionOffset())
1185 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001186 }
Chris Lattner24943d22010-06-08 16:52:24 +00001187 }
Greg Clayton70436352010-06-30 23:03:03 +00001188
Greg Clayton9b82f862011-07-11 05:12:02 +00001189 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001190 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001191 // If we didn't already try and read from the object file cache, then
1192 // try it after failing to read from the process.
1193 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001194 }
1195 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001196}
1197
Greg Clayton7dd98df2011-07-12 17:06:17 +00001198size_t
1199Target::ReadScalarIntegerFromMemory (const Address& addr,
1200 bool prefer_file_cache,
1201 uint32_t byte_size,
1202 bool is_signed,
1203 Scalar &scalar,
1204 Error &error)
1205{
1206 uint64_t uval;
1207
1208 if (byte_size <= sizeof(uval))
1209 {
1210 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1211 if (bytes_read == byte_size)
1212 {
1213 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1214 uint32_t offset = 0;
1215 if (byte_size <= 4)
1216 scalar = data.GetMaxU32 (&offset, byte_size);
1217 else
1218 scalar = data.GetMaxU64 (&offset, byte_size);
1219
1220 if (is_signed)
1221 scalar.SignExtend(byte_size * 8);
1222 return bytes_read;
1223 }
1224 }
1225 else
1226 {
1227 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1228 }
1229 return 0;
1230}
1231
1232uint64_t
1233Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1234 bool prefer_file_cache,
1235 size_t integer_byte_size,
1236 uint64_t fail_value,
1237 Error &error)
1238{
1239 Scalar scalar;
1240 if (ReadScalarIntegerFromMemory (addr,
1241 prefer_file_cache,
1242 integer_byte_size,
1243 false,
1244 scalar,
1245 error))
1246 return scalar.ULongLong(fail_value);
1247 return fail_value;
1248}
1249
1250bool
1251Target::ReadPointerFromMemory (const Address& addr,
1252 bool prefer_file_cache,
1253 Error &error,
1254 Address &pointer_addr)
1255{
1256 Scalar scalar;
1257 if (ReadScalarIntegerFromMemory (addr,
1258 prefer_file_cache,
1259 m_arch.GetAddressByteSize(),
1260 false,
1261 scalar,
1262 error))
1263 {
1264 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1265 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1266 {
1267 if (m_section_load_list.IsEmpty())
1268 {
1269 // No sections are loaded, so we must assume we are not running
1270 // yet and anything we are given is a file address.
1271 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1272 }
1273 else
1274 {
1275 // We have at least one section loaded. This can be becuase
1276 // we have manually loaded some sections with "target modules load ..."
1277 // or because we have have a live process that has sections loaded
1278 // through the dynamic loader
1279 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1280 }
1281 // We weren't able to resolve the pointer value, so just return
1282 // an address with no section
1283 if (!pointer_addr.IsValid())
1284 pointer_addr.SetOffset (pointer_vm_addr);
1285 return true;
1286
1287 }
1288 }
1289 return false;
1290}
Chris Lattner24943d22010-06-08 16:52:24 +00001291
1292ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001293Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001294{
Chris Lattner24943d22010-06-08 16:52:24 +00001295 ModuleSP module_sp;
1296
Chris Lattner24943d22010-06-08 16:52:24 +00001297 Error error;
1298
Jim Ingham03e5e512012-05-17 18:38:42 +00001299 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1300 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001301
Jim Ingham03e5e512012-05-17 18:38:42 +00001302 if (module_spec.GetUUID().IsValid())
1303 module_sp = m_images.FindFirstModule(module_spec);
1304
Greg Claytone1ef1e32012-04-27 00:58:27 +00001305 if (!module_sp)
1306 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001307 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1308 bool did_create_module = false;
1309
1310 // If there are image search path entries, try to use them first to acquire a suitable image.
1311 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001312 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001313 ModuleSpec transformed_spec (module_spec);
1314 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1315 {
1316 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1317 error = ModuleList::GetSharedModule (transformed_spec,
1318 module_sp,
1319 &GetExecutableSearchPaths(),
1320 &old_module_sp,
1321 &did_create_module);
1322 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001323 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001324
Greg Claytone1ef1e32012-04-27 00:58:27 +00001325 if (!module_sp)
1326 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001327 // If we have a UUID, we can check our global shared module list in case
1328 // we already have it. If we don't have a valid UUID, then we can't since
1329 // the path in "module_spec" will be a platform path, and we will need to
1330 // let the platform find that file. For example, we could be asking for
1331 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1332 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1333 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1334 // cache.
1335 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001336 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001337 // We have a UUID, it is OK to check the global module list...
1338 error = ModuleList::GetSharedModule (module_spec,
1339 module_sp,
1340 &GetExecutableSearchPaths(),
1341 &old_module_sp,
1342 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001343 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001344
1345 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001346 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001347 // The platform is responsible for finding and caching an appropriate
1348 // module in the shared module cache.
1349 if (m_platform_sp)
1350 {
1351 FileSpec platform_file_spec;
1352 error = m_platform_sp->GetSharedModule (module_spec,
1353 module_sp,
1354 &GetExecutableSearchPaths(),
1355 &old_module_sp,
1356 &did_create_module);
1357 }
1358 else
1359 {
1360 error.SetErrorString("no platform is currently set");
1361 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001362 }
1363 }
Chris Lattner24943d22010-06-08 16:52:24 +00001364
Jim Ingham03e5e512012-05-17 18:38:42 +00001365 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1366 // module in the list already, and if there was, let's remove it.
1367 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001368 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001369 // GetSharedModule is not guaranteed to find the old shared module, for instance
1370 // in the common case where you pass in the UUID, it is only going to find the one
1371 // module matching the UUID. In fact, it has no good way to know what the "old module"
1372 // relevant to this target is, since there might be many copies of a module with this file spec
1373 // in various running debug sessions, but only one of them will belong to this target.
1374 // So let's remove the UUID from the module list, and look in the target's module list.
1375 // Only do this if there is SOMETHING else in the module spec...
1376 if (!old_module_sp)
1377 {
1378 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
1379 {
1380 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1381 module_spec_copy.GetUUID().Clear();
1382
1383 ModuleList found_modules;
1384 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1385 if (num_found == 1)
1386 {
1387 old_module_sp = found_modules.GetModuleAtIndex(0);
1388 }
1389 }
1390 }
1391
1392 m_images.Append (module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001393 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
Jim Ingham03e5e512012-05-17 18:38:42 +00001394 {
Chris Lattner24943d22010-06-08 16:52:24 +00001395 ModuleUpdated(old_module_sp, module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001396 m_images.Remove (old_module_sp);
1397 Module *old_module_ptr = old_module_sp.get();
1398 old_module_sp.reset();
1399 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1400 }
Chris Lattner24943d22010-06-08 16:52:24 +00001401 else
1402 ModuleAdded(module_sp);
1403 }
1404 }
1405 if (error_ptr)
1406 *error_ptr = error;
1407 return module_sp;
1408}
1409
1410
Greg Clayton289afcb2012-02-18 05:35:26 +00001411TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001412Target::CalculateTarget ()
1413{
Greg Clayton289afcb2012-02-18 05:35:26 +00001414 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001415}
1416
Greg Clayton289afcb2012-02-18 05:35:26 +00001417ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001418Target::CalculateProcess ()
1419{
Greg Clayton289afcb2012-02-18 05:35:26 +00001420 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001421}
1422
Greg Clayton289afcb2012-02-18 05:35:26 +00001423ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001424Target::CalculateThread ()
1425{
Greg Clayton289afcb2012-02-18 05:35:26 +00001426 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001427}
1428
Greg Clayton289afcb2012-02-18 05:35:26 +00001429StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001430Target::CalculateStackFrame ()
1431{
Greg Clayton289afcb2012-02-18 05:35:26 +00001432 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001433}
1434
1435void
Greg Claytona830adb2010-10-04 01:05:56 +00001436Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001437{
Greg Clayton567e7f32011-09-22 04:58:26 +00001438 exe_ctx.Clear();
1439 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001440}
1441
1442PathMappingList &
1443Target::GetImageSearchPathList ()
1444{
1445 return m_image_search_paths;
1446}
1447
1448void
1449Target::ImageSearchPathsChanged
1450(
1451 const PathMappingList &path_list,
1452 void *baton
1453)
1454{
1455 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001456 ModuleSP exe_module_sp (target->GetExecutableModule());
1457 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001458 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001459 target->m_images.Clear();
1460 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001461 }
1462}
1463
1464ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001465Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001466{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001467 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001468 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001469 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001470 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001471 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001472 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1473 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1474 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1475 }
Chris Lattner24943d22010-06-08 16:52:24 +00001476 return m_scratch_ast_context_ap.get();
1477}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001478
Sean Callanan4938bd62011-11-16 18:20:47 +00001479ClangASTImporter *
1480Target::GetClangASTImporter()
1481{
1482 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1483
1484 if (!ast_importer)
1485 {
1486 ast_importer = new ClangASTImporter();
1487 m_ast_importer_ap.reset(ast_importer);
1488 }
1489
1490 return ast_importer;
1491}
1492
Greg Clayton990de7b2010-11-18 23:32:35 +00001493void
Caroline Tice2a456812011-03-10 22:14:10 +00001494Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001495{
Greg Clayton334d33a2012-01-30 07:41:31 +00001496 UserSettingsController::InitializeSettingsController (GetSettingsController(),
Greg Clayton990de7b2010-11-18 23:32:35 +00001497 SettingsController::global_settings_table,
1498 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001499
1500 // Now call SettingsInitialize() on each 'child' setting of Target
1501 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001502}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001503
Greg Clayton990de7b2010-11-18 23:32:35 +00001504void
Caroline Tice2a456812011-03-10 22:14:10 +00001505Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001506{
Caroline Tice2a456812011-03-10 22:14:10 +00001507
1508 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1509
1510 Process::SettingsTerminate ();
1511
1512 // Now terminate Target Settings.
1513
Greg Clayton990de7b2010-11-18 23:32:35 +00001514 UserSettingsControllerSP &usc = GetSettingsController();
1515 UserSettingsController::FinalizeSettingsController (usc);
1516 usc.reset();
1517}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001518
Greg Clayton990de7b2010-11-18 23:32:35 +00001519UserSettingsControllerSP &
1520Target::GetSettingsController ()
1521{
Greg Clayton334d33a2012-01-30 07:41:31 +00001522 static UserSettingsControllerSP g_settings_controller_sp;
1523 if (!g_settings_controller_sp)
1524 {
1525 g_settings_controller_sp.reset (new Target::SettingsController);
1526 // The first shared pointer to Target::SettingsController in
1527 // g_settings_controller_sp must be fully created above so that
1528 // the TargetInstanceSettings can use a weak_ptr to refer back
1529 // to the master setttings controller
1530 InstanceSettingsSP default_instance_settings_sp (new TargetInstanceSettings (g_settings_controller_sp,
1531 false,
1532 InstanceSettings::GetDefaultName().AsCString()));
1533 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
1534 }
1535 return g_settings_controller_sp;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001536}
1537
Greg Clayton9ce95382012-02-13 23:10:39 +00001538FileSpecList
1539Target::GetDefaultExecutableSearchPaths ()
1540{
1541 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1542 if (settings_controller_sp)
1543 {
1544 lldb::InstanceSettingsSP instance_settings_sp (settings_controller_sp->GetDefaultInstanceSettings ());
1545 if (instance_settings_sp)
1546 return static_cast<TargetInstanceSettings *>(instance_settings_sp.get())->GetExecutableSearchPaths ();
1547 }
1548 return FileSpecList();
1549}
1550
1551
Caroline Tice5bc8c972010-09-20 20:44:43 +00001552ArchSpec
1553Target::GetDefaultArchitecture ()
1554{
Greg Clayton940b1032011-02-23 00:35:02 +00001555 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
Greg Clayton940b1032011-02-23 00:35:02 +00001556 if (settings_controller_sp)
Greg Clayton469e08d2012-05-15 02:44:13 +00001557 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1558 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001559}
1560
1561void
Greg Clayton940b1032011-02-23 00:35:02 +00001562Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001563{
Greg Clayton940b1032011-02-23 00:35:02 +00001564 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1565
1566 if (settings_controller_sp)
1567 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001568}
1569
Greg Claytona830adb2010-10-04 01:05:56 +00001570Target *
1571Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1572{
1573 // The target can either exist in the "process" of ExecutionContext, or in
1574 // the "target_sp" member of SymbolContext. This accessor helper function
1575 // will get the target from one of these locations.
1576
1577 Target *target = NULL;
1578 if (sc_ptr != NULL)
1579 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001580 if (target == NULL && exe_ctx_ptr)
1581 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001582 return target;
1583}
1584
1585
Caroline Tice1ebef442010-09-27 00:30:10 +00001586void
1587Target::UpdateInstanceName ()
1588{
1589 StreamString sstr;
1590
Greg Clayton5beb99d2011-08-11 02:48:45 +00001591 Module *exe_module = GetExecutableModulePointer();
1592 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001593 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001594 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001595 exe_module->GetFileSpec().GetFilename().AsCString(),
1596 exe_module->GetArchitecture().GetArchitectureName());
1597 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001598 }
1599}
1600
Sean Callanan77e93942010-10-29 00:29:03 +00001601const char *
1602Target::GetExpressionPrefixContentsAsCString ()
1603{
Sean Callanane0b7f942011-11-16 01:54:57 +00001604 if (!m_expr_prefix_contents.empty())
1605 return m_expr_prefix_contents.c_str();
Greg Claytonff44ab42011-04-23 02:04:55 +00001606 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001607}
1608
Greg Clayton427f2902010-12-14 02:59:59 +00001609ExecutionResults
1610Target::EvaluateExpression
1611(
1612 const char *expr_cstr,
1613 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001614 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001615 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001616 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001617 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001618 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001619 lldb::ValueObjectSP &result_valobj_sp
1620)
1621{
1622 ExecutionResults execution_results = eExecutionSetupError;
1623
1624 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001625
1626 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1627 return execution_results;
1628
Jim Ingham3613ae12011-05-12 02:06:14 +00001629 // We shouldn't run stop hooks in expressions.
1630 // Be sure to reset this if you return anywhere within this function.
1631 bool old_suppress_value = m_suppress_stop_hooks;
1632 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001633
1634 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001635
1636 const size_t expr_cstr_len = ::strlen (expr_cstr);
1637
Greg Clayton427f2902010-12-14 02:59:59 +00001638 if (frame)
1639 {
1640 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001641 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001642 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001643 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1644 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001645 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001646
1647 // Make sure we don't have any things that we know a variable expression
1648 // won't be able to deal with before calling into it
1649 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1650 {
1651 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1652 use_dynamic,
1653 expr_path_options,
1654 var_sp,
1655 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001656 // if this expression results in a bitfield, we give up and let the IR handle it
1657 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1658 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001659 }
Greg Clayton427f2902010-12-14 02:59:59 +00001660 }
1661 else if (m_process_sp)
1662 {
1663 m_process_sp->CalculateExecutionContext(exe_ctx);
1664 }
1665 else
1666 {
1667 CalculateExecutionContext(exe_ctx);
1668 }
1669
1670 if (result_valobj_sp)
1671 {
1672 execution_results = eExecutionCompleted;
1673 // We got a result from the frame variable expression path above...
1674 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1675
1676 lldb::ValueObjectSP const_valobj_sp;
1677
1678 // Check in case our value is already a constant value
1679 if (result_valobj_sp->GetIsConstant())
1680 {
1681 const_valobj_sp = result_valobj_sp;
1682 const_valobj_sp->SetName (persistent_variable_name);
1683 }
1684 else
Jim Inghame41494a2011-04-16 00:01:13 +00001685 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001686 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001687 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001688 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001689 if (dynamic_sp)
1690 result_valobj_sp = dynamic_sp;
1691 }
1692
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001693 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001694 }
Greg Clayton427f2902010-12-14 02:59:59 +00001695
Sean Callanan6a925532011-01-13 08:53:35 +00001696 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1697
Greg Clayton427f2902010-12-14 02:59:59 +00001698 result_valobj_sp = const_valobj_sp;
1699
Sean Callanan6a925532011-01-13 08:53:35 +00001700 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1701 assert (clang_expr_variable_sp.get());
1702
1703 // Set flags and live data as appropriate
1704
1705 const Value &result_value = live_valobj_sp->GetValue();
1706
1707 switch (result_value.GetValueType())
1708 {
1709 case Value::eValueTypeHostAddress:
1710 case Value::eValueTypeFileAddress:
1711 // we don't do anything with these for now
1712 break;
1713 case Value::eValueTypeScalar:
1714 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1715 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1716 break;
1717 case Value::eValueTypeLoadAddress:
1718 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1719 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1720 break;
1721 }
Greg Clayton427f2902010-12-14 02:59:59 +00001722 }
1723 else
1724 {
1725 // Make sure we aren't just trying to see the value of a persistent
1726 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001727 lldb::ClangExpressionVariableSP persistent_var_sp;
1728 // Only check for persistent variables the expression starts with a '$'
1729 if (expr_cstr[0] == '$')
1730 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1731
Greg Clayton427f2902010-12-14 02:59:59 +00001732 if (persistent_var_sp)
1733 {
1734 result_valobj_sp = persistent_var_sp->GetValueObject ();
1735 execution_results = eExecutionCompleted;
1736 }
1737 else
1738 {
1739 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001740
Greg Clayton427f2902010-12-14 02:59:59 +00001741 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001742 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001743 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001744 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001745 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001746 expr_cstr,
1747 prefix,
1748 result_valobj_sp);
1749 }
1750 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001751
1752 m_suppress_stop_hooks = old_suppress_value;
1753
Greg Clayton427f2902010-12-14 02:59:59 +00001754 return execution_results;
1755}
1756
Greg Claytonc0fa5332011-05-22 22:46:53 +00001757lldb::addr_t
1758Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1759{
1760 addr_t code_addr = load_addr;
1761 switch (m_arch.GetMachine())
1762 {
1763 case llvm::Triple::arm:
1764 case llvm::Triple::thumb:
1765 switch (addr_class)
1766 {
1767 case eAddressClassData:
1768 case eAddressClassDebug:
1769 return LLDB_INVALID_ADDRESS;
1770
1771 case eAddressClassUnknown:
1772 case eAddressClassInvalid:
1773 case eAddressClassCode:
1774 case eAddressClassCodeAlternateISA:
1775 case eAddressClassRuntime:
1776 // Check if bit zero it no set?
1777 if ((code_addr & 1ull) == 0)
1778 {
1779 // Bit zero isn't set, check if the address is a multiple of 2?
1780 if (code_addr & 2ull)
1781 {
1782 // The address is a multiple of 2 so it must be thumb, set bit zero
1783 code_addr |= 1ull;
1784 }
1785 else if (addr_class == eAddressClassCodeAlternateISA)
1786 {
1787 // We checked the address and the address claims to be the alternate ISA
1788 // which means thumb, so set bit zero.
1789 code_addr |= 1ull;
1790 }
1791 }
1792 break;
1793 }
1794 break;
1795
1796 default:
1797 break;
1798 }
1799 return code_addr;
1800}
1801
1802lldb::addr_t
1803Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1804{
1805 addr_t opcode_addr = load_addr;
1806 switch (m_arch.GetMachine())
1807 {
1808 case llvm::Triple::arm:
1809 case llvm::Triple::thumb:
1810 switch (addr_class)
1811 {
1812 case eAddressClassData:
1813 case eAddressClassDebug:
1814 return LLDB_INVALID_ADDRESS;
1815
1816 case eAddressClassInvalid:
1817 case eAddressClassUnknown:
1818 case eAddressClassCode:
1819 case eAddressClassCodeAlternateISA:
1820 case eAddressClassRuntime:
1821 opcode_addr &= ~(1ull);
1822 break;
1823 }
1824 break;
1825
1826 default:
1827 break;
1828 }
1829 return opcode_addr;
1830}
1831
Jim Inghamd60d94a2011-03-11 03:53:59 +00001832lldb::user_id_t
1833Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1834{
1835 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001836 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001837 m_stop_hooks[new_uid] = new_hook_sp;
1838 return new_uid;
1839}
1840
1841bool
1842Target::RemoveStopHookByID (lldb::user_id_t user_id)
1843{
1844 size_t num_removed;
1845 num_removed = m_stop_hooks.erase (user_id);
1846 if (num_removed == 0)
1847 return false;
1848 else
1849 return true;
1850}
1851
1852void
1853Target::RemoveAllStopHooks ()
1854{
1855 m_stop_hooks.clear();
1856}
1857
1858Target::StopHookSP
1859Target::GetStopHookByID (lldb::user_id_t user_id)
1860{
1861 StopHookSP found_hook;
1862
1863 StopHookCollection::iterator specified_hook_iter;
1864 specified_hook_iter = m_stop_hooks.find (user_id);
1865 if (specified_hook_iter != m_stop_hooks.end())
1866 found_hook = (*specified_hook_iter).second;
1867 return found_hook;
1868}
1869
1870bool
1871Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1872{
1873 StopHookCollection::iterator specified_hook_iter;
1874 specified_hook_iter = m_stop_hooks.find (user_id);
1875 if (specified_hook_iter == m_stop_hooks.end())
1876 return false;
1877
1878 (*specified_hook_iter).second->SetIsActive (active_state);
1879 return true;
1880}
1881
1882void
1883Target::SetAllStopHooksActiveState (bool active_state)
1884{
1885 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1886 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1887 {
1888 (*pos).second->SetIsActive (active_state);
1889 }
1890}
1891
1892void
1893Target::RunStopHooks ()
1894{
Jim Ingham3613ae12011-05-12 02:06:14 +00001895 if (m_suppress_stop_hooks)
1896 return;
1897
Jim Inghamd60d94a2011-03-11 03:53:59 +00001898 if (!m_process_sp)
1899 return;
1900
1901 if (m_stop_hooks.empty())
1902 return;
1903
1904 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1905
1906 // If there aren't any active stop hooks, don't bother either:
1907 bool any_active_hooks = false;
1908 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1909 {
1910 if ((*pos).second->IsActive())
1911 {
1912 any_active_hooks = true;
1913 break;
1914 }
1915 }
1916 if (!any_active_hooks)
1917 return;
1918
1919 CommandReturnObject result;
1920
1921 std::vector<ExecutionContext> exc_ctx_with_reasons;
1922 std::vector<SymbolContext> sym_ctx_with_reasons;
1923
1924 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1925 size_t num_threads = cur_threadlist.GetSize();
1926 for (size_t i = 0; i < num_threads; i++)
1927 {
1928 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1929 if (cur_thread_sp->ThreadStoppedForAReason())
1930 {
1931 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1932 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1933 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1934 }
1935 }
1936
1937 // If no threads stopped for a reason, don't run the stop-hooks.
1938 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1939 if (num_exe_ctx == 0)
1940 return;
1941
Jim Inghame5ed8e92011-06-02 23:58:26 +00001942 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1943 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001944
1945 bool keep_going = true;
1946 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001947 bool print_hook_header;
1948 bool print_thread_header;
1949
1950 if (num_exe_ctx == 1)
1951 print_thread_header = false;
1952 else
1953 print_thread_header = true;
1954
1955 if (m_stop_hooks.size() == 1)
1956 print_hook_header = false;
1957 else
1958 print_hook_header = true;
1959
Jim Inghamd60d94a2011-03-11 03:53:59 +00001960 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1961 {
1962 // result.Clear();
1963 StopHookSP cur_hook_sp = (*pos).second;
1964 if (!cur_hook_sp->IsActive())
1965 continue;
1966
1967 bool any_thread_matched = false;
1968 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1969 {
1970 if ((cur_hook_sp->GetSpecifier () == NULL
1971 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1972 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00001973 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001974 {
1975 if (!hooks_ran)
1976 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001977 hooks_ran = true;
1978 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001979 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001980 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001981 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1982 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1983 NULL);
1984 if (cmd)
1985 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1986 else
1987 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001988 any_thread_matched = true;
1989 }
1990
Jim Inghamc54840c2011-03-22 01:47:27 +00001991 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001992 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001993
1994 bool stop_on_continue = true;
1995 bool stop_on_error = true;
1996 bool echo_commands = false;
1997 bool print_results = true;
1998 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001999 &exc_ctx_with_reasons[i],
2000 stop_on_continue,
2001 stop_on_error,
2002 echo_commands,
2003 print_results,
2004 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002005
2006 // If the command started the target going again, we should bag out of
2007 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002008 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2009 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002010 {
Greg Clayton444e35b2011-10-19 18:09:39 +00002011 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002012 keep_going = false;
2013 }
2014 }
2015 }
2016 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002017
Caroline Tice4a348082011-05-02 20:41:46 +00002018 result.GetImmediateOutputStream()->Flush();
2019 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002020}
2021
Greg Claytonbbea1332011-07-08 00:48:09 +00002022
Jim Inghamd60d94a2011-03-11 03:53:59 +00002023//--------------------------------------------------------------
2024// class Target::StopHook
2025//--------------------------------------------------------------
2026
2027
2028Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2029 UserID (uid),
2030 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002031 m_commands (),
2032 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002033 m_thread_spec_ap(NULL),
2034 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002035{
2036}
2037
2038Target::StopHook::StopHook (const StopHook &rhs) :
2039 UserID (rhs.GetID()),
2040 m_target_sp (rhs.m_target_sp),
2041 m_commands (rhs.m_commands),
2042 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002043 m_thread_spec_ap (NULL),
2044 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002045{
2046 if (rhs.m_thread_spec_ap.get() != NULL)
2047 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2048}
2049
2050
2051Target::StopHook::~StopHook ()
2052{
2053}
2054
2055void
2056Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2057{
2058 m_thread_spec_ap.reset (specifier);
2059}
2060
2061
2062void
2063Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2064{
2065 int indent_level = s->GetIndentLevel();
2066
2067 s->SetIndentLevel(indent_level + 2);
2068
Greg Clayton444e35b2011-10-19 18:09:39 +00002069 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002070 if (m_active)
2071 s->Indent ("State: enabled\n");
2072 else
2073 s->Indent ("State: disabled\n");
2074
2075 if (m_specifier_sp)
2076 {
2077 s->Indent();
2078 s->PutCString ("Specifier:\n");
2079 s->SetIndentLevel (indent_level + 4);
2080 m_specifier_sp->GetDescription (s, level);
2081 s->SetIndentLevel (indent_level + 2);
2082 }
2083
2084 if (m_thread_spec_ap.get() != NULL)
2085 {
2086 StreamString tmp;
2087 s->Indent("Thread:\n");
2088 m_thread_spec_ap->GetDescription (&tmp, level);
2089 s->SetIndentLevel (indent_level + 4);
2090 s->Indent (tmp.GetData());
2091 s->PutCString ("\n");
2092 s->SetIndentLevel (indent_level + 2);
2093 }
2094
2095 s->Indent ("Commands: \n");
2096 s->SetIndentLevel (indent_level + 4);
2097 uint32_t num_commands = m_commands.GetSize();
2098 for (uint32_t i = 0; i < num_commands; i++)
2099 {
2100 s->Indent(m_commands.GetStringAtIndex(i));
2101 s->PutCString ("\n");
2102 }
2103 s->SetIndentLevel (indent_level);
2104}
2105
2106
Caroline Tice5bc8c972010-09-20 20:44:43 +00002107//--------------------------------------------------------------
2108// class Target::SettingsController
2109//--------------------------------------------------------------
2110
2111Target::SettingsController::SettingsController () :
2112 UserSettingsController ("target", Debugger::GetSettingsController()),
2113 m_default_architecture ()
2114{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002115}
2116
2117Target::SettingsController::~SettingsController ()
2118{
2119}
2120
2121lldb::InstanceSettingsSP
2122Target::SettingsController::CreateInstanceSettings (const char *instance_name)
2123{
Greg Clayton334d33a2012-01-30 07:41:31 +00002124 lldb::InstanceSettingsSP new_settings_sp (new TargetInstanceSettings (GetSettingsController(),
2125 false,
2126 instance_name));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002127 return new_settings_sp;
2128}
2129
Caroline Tice5bc8c972010-09-20 20:44:43 +00002130
Greg Claytonabb33022011-11-08 02:43:13 +00002131#define TSC_DEFAULT_ARCH "default-arch"
2132#define TSC_EXPR_PREFIX "expr-prefix"
2133#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Enrico Granatadba1de82012-03-27 02:35:13 +00002134#define TSC_ENABLE_SYNTHETIC "enable-synthetic-value"
Greg Claytonabb33022011-11-08 02:43:13 +00002135#define TSC_SKIP_PROLOGUE "skip-prologue"
2136#define TSC_SOURCE_MAP "source-map"
Greg Clayton9ce95382012-02-13 23:10:39 +00002137#define TSC_EXE_SEARCH_PATHS "exec-search-paths"
Greg Claytonabb33022011-11-08 02:43:13 +00002138#define TSC_MAX_CHILDREN "max-children-count"
2139#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
2140#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
2141#define TSC_RUN_ARGS "run-args"
2142#define TSC_ENV_VARS "env-vars"
2143#define TSC_INHERIT_ENV "inherit-env"
2144#define TSC_STDIN_PATH "input-path"
2145#define TSC_STDOUT_PATH "output-path"
2146#define TSC_STDERR_PATH "error-path"
2147#define TSC_DISABLE_ASLR "disable-aslr"
2148#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytond284b662011-02-18 01:44:25 +00002149
2150
2151static const ConstString &
2152GetSettingNameForDefaultArch ()
2153{
2154 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00002155 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002156}
2157
Greg Claytond284b662011-02-18 01:44:25 +00002158static const ConstString &
2159GetSettingNameForExpressionPrefix ()
2160{
2161 static ConstString g_const_string (TSC_EXPR_PREFIX);
2162 return g_const_string;
2163}
2164
2165static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00002166GetSettingNameForPreferDynamicValue ()
2167{
2168 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2169 return g_const_string;
2170}
2171
Greg Claytonff44ab42011-04-23 02:04:55 +00002172static const ConstString &
Enrico Granatadba1de82012-03-27 02:35:13 +00002173GetSettingNameForEnableSyntheticValue ()
2174{
2175 static ConstString g_const_string (TSC_ENABLE_SYNTHETIC);
2176 return g_const_string;
2177}
2178
2179static const ConstString &
Greg Claytonff44ab42011-04-23 02:04:55 +00002180GetSettingNameForSourcePathMap ()
2181{
2182 static ConstString g_const_string (TSC_SOURCE_MAP);
2183 return g_const_string;
2184}
Greg Claytond284b662011-02-18 01:44:25 +00002185
Greg Clayton17cd9952011-04-22 03:55:06 +00002186static const ConstString &
Greg Clayton9ce95382012-02-13 23:10:39 +00002187GetSettingNameForExecutableSearchPaths ()
2188{
2189 static ConstString g_const_string (TSC_EXE_SEARCH_PATHS);
2190 return g_const_string;
2191}
2192
2193static const ConstString &
Greg Clayton17cd9952011-04-22 03:55:06 +00002194GetSettingNameForSkipPrologue ()
2195{
2196 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2197 return g_const_string;
2198}
2199
Enrico Granata018921d2011-08-12 02:00:06 +00002200static const ConstString &
2201GetSettingNameForMaxChildren ()
2202{
2203 static ConstString g_const_string (TSC_MAX_CHILDREN);
2204 return g_const_string;
2205}
Greg Clayton17cd9952011-04-22 03:55:06 +00002206
Enrico Granata91544802011-09-06 19:20:51 +00002207static const ConstString &
2208GetSettingNameForMaxStringSummaryLength ()
2209{
2210 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2211 return g_const_string;
2212}
Greg Clayton17cd9952011-04-22 03:55:06 +00002213
Jim Ingham7089d8a2011-10-28 23:14:11 +00002214static const ConstString &
2215GetSettingNameForPlatformAvoid ()
2216{
2217 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2218 return g_const_string;
2219}
2220
Greg Claytonabb33022011-11-08 02:43:13 +00002221const ConstString &
2222GetSettingNameForRunArgs ()
2223{
2224 static ConstString g_const_string (TSC_RUN_ARGS);
2225 return g_const_string;
2226}
2227
2228const ConstString &
2229GetSettingNameForEnvVars ()
2230{
2231 static ConstString g_const_string (TSC_ENV_VARS);
2232 return g_const_string;
2233}
2234
2235const ConstString &
2236GetSettingNameForInheritHostEnv ()
2237{
2238 static ConstString g_const_string (TSC_INHERIT_ENV);
2239 return g_const_string;
2240}
2241
2242const ConstString &
2243GetSettingNameForInputPath ()
2244{
2245 static ConstString g_const_string (TSC_STDIN_PATH);
2246 return g_const_string;
2247}
2248
2249const ConstString &
2250GetSettingNameForOutputPath ()
2251{
2252 static ConstString g_const_string (TSC_STDOUT_PATH);
2253 return g_const_string;
2254}
2255
2256const ConstString &
2257GetSettingNameForErrorPath ()
2258{
2259 static ConstString g_const_string (TSC_STDERR_PATH);
2260 return g_const_string;
2261}
2262
2263const ConstString &
2264GetSettingNameForDisableASLR ()
2265{
2266 static ConstString g_const_string (TSC_DISABLE_ASLR);
2267 return g_const_string;
2268}
2269
2270const ConstString &
2271GetSettingNameForDisableSTDIO ()
2272{
2273 static ConstString g_const_string (TSC_DISABLE_STDIO);
2274 return g_const_string;
2275}
Jim Ingham7089d8a2011-10-28 23:14:11 +00002276
Caroline Tice5bc8c972010-09-20 20:44:43 +00002277bool
2278Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2279 const char *index_value,
2280 const char *value,
2281 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002282 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002283 Error&err)
2284{
Greg Claytond284b662011-02-18 01:44:25 +00002285 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002286 {
Greg Claytonb170aee2012-05-08 01:45:38 +00002287 m_default_architecture.SetTriple (value);
Greg Clayton940b1032011-02-23 00:35:02 +00002288 if (!m_default_architecture.IsValid())
2289 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002290 }
2291 return true;
2292}
2293
2294
2295bool
2296Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2297 StringList &value,
2298 Error &err)
2299{
Greg Claytond284b662011-02-18 01:44:25 +00002300 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002301 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002302 // If the arch is invalid (the default), don't show a string for it
2303 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002304 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002305 return true;
2306 }
2307 else
2308 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2309
2310 return false;
2311}
2312
2313//--------------------------------------------------------------
2314// class TargetInstanceSettings
2315//--------------------------------------------------------------
2316
Greg Clayton638351a2010-12-04 00:10:17 +00002317TargetInstanceSettings::TargetInstanceSettings
2318(
Greg Clayton334d33a2012-01-30 07:41:31 +00002319 const lldb::UserSettingsControllerSP &owner_sp,
Greg Clayton638351a2010-12-04 00:10:17 +00002320 bool live_instance,
2321 const char *name
2322) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002323 InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002324 m_expr_prefix_file (),
Sean Callanane0b7f942011-11-16 01:54:57 +00002325 m_expr_prefix_contents (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002326 m_prefer_dynamic_value (2),
Enrico Granatadba1de82012-03-27 02:35:13 +00002327 m_enable_synthetic_value(true, true),
Greg Claytonff44ab42011-04-23 02:04:55 +00002328 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002329 m_source_map (NULL, NULL),
Greg Clayton9ce95382012-02-13 23:10:39 +00002330 m_exe_search_paths (),
Enrico Granata91544802011-09-06 19:20:51 +00002331 m_max_children_display(256),
Jim Ingham7089d8a2011-10-28 23:14:11 +00002332 m_max_strlen_length(1024),
Greg Claytonabb33022011-11-08 02:43:13 +00002333 m_breakpoints_use_platform_avoid (true, true),
2334 m_run_args (),
2335 m_env_vars (),
2336 m_input_path (),
2337 m_output_path (),
2338 m_error_path (),
2339 m_disable_aslr (true),
2340 m_disable_stdio (false),
2341 m_inherit_host_env (true),
2342 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002343{
2344 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2345 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2346 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2347 // This is true for CreateInstanceName() too.
2348
2349 if (GetInstanceName () == InstanceSettings::InvalidName())
2350 {
2351 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Clayton334d33a2012-01-30 07:41:31 +00002352 owner_sp->RegisterInstanceSettings (this);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002353 }
2354
2355 if (live_instance)
2356 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002357 const lldb::InstanceSettingsSP &pending_settings = owner_sp->FindPendingSettings (m_instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002358 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002359 }
2360}
2361
2362TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002363 InstanceSettings (Target::GetSettingsController(), CreateInstanceName().AsCString()),
Greg Claytonff44ab42011-04-23 02:04:55 +00002364 m_expr_prefix_file (rhs.m_expr_prefix_file),
Sean Callanane0b7f942011-11-16 01:54:57 +00002365 m_expr_prefix_contents (rhs.m_expr_prefix_contents),
Greg Claytonff44ab42011-04-23 02:04:55 +00002366 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
Enrico Granatadba1de82012-03-27 02:35:13 +00002367 m_enable_synthetic_value(rhs.m_enable_synthetic_value),
Greg Claytonff44ab42011-04-23 02:04:55 +00002368 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002369 m_source_map (rhs.m_source_map),
Greg Clayton9ce95382012-02-13 23:10:39 +00002370 m_exe_search_paths (rhs.m_exe_search_paths),
Greg Claytonabb33022011-11-08 02:43:13 +00002371 m_max_children_display (rhs.m_max_children_display),
2372 m_max_strlen_length (rhs.m_max_strlen_length),
2373 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2374 m_run_args (rhs.m_run_args),
2375 m_env_vars (rhs.m_env_vars),
2376 m_input_path (rhs.m_input_path),
2377 m_output_path (rhs.m_output_path),
2378 m_error_path (rhs.m_error_path),
2379 m_disable_aslr (rhs.m_disable_aslr),
2380 m_disable_stdio (rhs.m_disable_stdio),
2381 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002382{
2383 if (m_instance_name != InstanceSettings::GetDefaultName())
2384 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002385 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
2386 if (owner_sp)
2387 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name),false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002388 }
2389}
2390
2391TargetInstanceSettings::~TargetInstanceSettings ()
2392{
2393}
2394
2395TargetInstanceSettings&
2396TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2397{
2398 if (this != &rhs)
2399 {
Greg Claytonabb33022011-11-08 02:43:13 +00002400 m_expr_prefix_file = rhs.m_expr_prefix_file;
Sean Callanane0b7f942011-11-16 01:54:57 +00002401 m_expr_prefix_contents = rhs.m_expr_prefix_contents;
Greg Claytonabb33022011-11-08 02:43:13 +00002402 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
Enrico Granatadba1de82012-03-27 02:35:13 +00002403 m_enable_synthetic_value = rhs.m_enable_synthetic_value;
Greg Claytonabb33022011-11-08 02:43:13 +00002404 m_skip_prologue = rhs.m_skip_prologue;
2405 m_source_map = rhs.m_source_map;
Greg Clayton9ce95382012-02-13 23:10:39 +00002406 m_exe_search_paths = rhs.m_exe_search_paths;
Greg Claytonabb33022011-11-08 02:43:13 +00002407 m_max_children_display = rhs.m_max_children_display;
2408 m_max_strlen_length = rhs.m_max_strlen_length;
2409 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2410 m_run_args = rhs.m_run_args;
2411 m_env_vars = rhs.m_env_vars;
2412 m_input_path = rhs.m_input_path;
2413 m_output_path = rhs.m_output_path;
2414 m_error_path = rhs.m_error_path;
2415 m_disable_aslr = rhs.m_disable_aslr;
2416 m_disable_stdio = rhs.m_disable_stdio;
2417 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002418 }
2419
2420 return *this;
2421}
2422
Caroline Tice5bc8c972010-09-20 20:44:43 +00002423void
2424TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2425 const char *index_value,
2426 const char *value,
2427 const ConstString &instance_name,
2428 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002429 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002430 Error &err,
2431 bool pending)
2432{
Greg Claytond284b662011-02-18 01:44:25 +00002433 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002434 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002435 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2436 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002437 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002438 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002439 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002440 default:
2441 break;
2442 case eVarSetOperationAssign:
2443 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002444 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002445 m_expr_prefix_contents.clear();
2446
Greg Claytonff44ab42011-04-23 02:04:55 +00002447 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2448 {
2449 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002450 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002451 return;
2452 }
2453
Greg Clayton4b23ab32012-01-06 02:01:06 +00002454 DataBufferSP file_data_sp (m_expr_prefix_file.GetCurrentValue().ReadFileContents(0, SIZE_MAX, &err));
Sean Callanane0b7f942011-11-16 01:54:57 +00002455
Greg Clayton4b23ab32012-01-06 02:01:06 +00002456 if (err.Success())
Greg Claytonff44ab42011-04-23 02:04:55 +00002457 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002458 if (file_data_sp && file_data_sp->GetByteSize() > 0)
2459 {
2460 m_expr_prefix_contents.assign((const char*)file_data_sp->GetBytes(), file_data_sp->GetByteSize());
2461 }
2462 else
2463 {
2464 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
2465 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002466 }
Sean Callanan77e93942010-10-29 00:29:03 +00002467 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002468 break;
2469 case eVarSetOperationClear:
Sean Callanane0b7f942011-11-16 01:54:57 +00002470 m_expr_prefix_contents.clear();
Sean Callanan77e93942010-10-29 00:29:03 +00002471 }
Sean Callanan77e93942010-10-29 00:29:03 +00002472 }
2473 }
Jim Inghame41494a2011-04-16 00:01:13 +00002474 else if (var_name == GetSettingNameForPreferDynamicValue())
2475 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002476 int new_value;
2477 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2478 if (err.Success())
2479 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002480 }
Enrico Granatadba1de82012-03-27 02:35:13 +00002481 else if (var_name == GetSettingNameForEnableSyntheticValue())
2482 {
2483 bool ok;
2484 bool new_value = Args::StringToBoolean(value, true, &ok);
2485 if (ok)
2486 m_enable_synthetic_value.SetCurrentValue(new_value);
2487 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002488 else if (var_name == GetSettingNameForSkipPrologue())
2489 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002490 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2491 }
Enrico Granata018921d2011-08-12 02:00:06 +00002492 else if (var_name == GetSettingNameForMaxChildren())
2493 {
2494 bool ok;
2495 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2496 if (ok)
2497 m_max_children_display = new_value;
2498 }
Enrico Granata91544802011-09-06 19:20:51 +00002499 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2500 {
2501 bool ok;
2502 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2503 if (ok)
2504 m_max_strlen_length = new_value;
2505 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002506 else if (var_name == GetSettingNameForExecutableSearchPaths())
2507 {
2508 switch (op)
2509 {
2510 case eVarSetOperationReplace:
2511 case eVarSetOperationInsertBefore:
2512 case eVarSetOperationInsertAfter:
2513 case eVarSetOperationRemove:
2514 default:
2515 break;
2516 case eVarSetOperationAssign:
2517 m_exe_search_paths.Clear();
2518 // Fall through to append....
2519 case eVarSetOperationAppend:
2520 {
2521 Args args(value);
2522 const uint32_t argc = args.GetArgumentCount();
2523 if (argc > 0)
2524 {
2525 const char *exe_search_path_dir;
2526 for (uint32_t idx = 0; (exe_search_path_dir = args.GetArgumentAtIndex(idx)) != NULL; ++idx)
2527 {
2528 FileSpec file_spec;
2529 file_spec.GetDirectory().SetCString(exe_search_path_dir);
2530 FileSpec::FileType file_type = file_spec.GetFileType();
2531 if (file_type == FileSpec::eFileTypeDirectory || file_type == FileSpec::eFileTypeInvalid)
2532 {
2533 m_exe_search_paths.Append(file_spec);
2534 }
2535 else
2536 {
2537 err.SetErrorStringWithFormat("executable search path '%s' exists, but it does not resolve to a directory", exe_search_path_dir);
2538 }
2539 }
2540 }
2541 }
2542 break;
2543
2544 case eVarSetOperationClear:
2545 m_exe_search_paths.Clear();
2546 break;
2547 }
2548 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002549 else if (var_name == GetSettingNameForSourcePathMap ())
2550 {
2551 switch (op)
2552 {
2553 case eVarSetOperationReplace:
2554 case eVarSetOperationInsertBefore:
2555 case eVarSetOperationInsertAfter:
2556 case eVarSetOperationRemove:
2557 default:
2558 break;
2559 case eVarSetOperationAssign:
2560 m_source_map.Clear(true);
2561 // Fall through to append....
2562 case eVarSetOperationAppend:
2563 {
2564 Args args(value);
2565 const uint32_t argc = args.GetArgumentCount();
2566 if (argc & 1 || argc == 0)
2567 {
2568 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2569 }
2570 else
2571 {
2572 char resolved_new_path[PATH_MAX];
2573 FileSpec file_spec;
2574 const char *old_path;
2575 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2576 {
2577 const char *new_path = args.GetArgumentAtIndex(idx+1);
2578 assert (new_path); // We have an even number of paths, this shouldn't happen!
2579
2580 file_spec.SetFile(new_path, true);
2581 if (file_spec.Exists())
2582 {
2583 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2584 {
2585 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2586 return;
2587 }
2588 }
2589 else
2590 {
2591 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2592 return;
2593 }
2594 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2595 }
2596 }
2597 }
2598 break;
2599
2600 case eVarSetOperationClear:
2601 m_source_map.Clear(true);
2602 break;
2603 }
Jim Inghame41494a2011-04-16 00:01:13 +00002604 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002605 else if (var_name == GetSettingNameForPlatformAvoid ())
2606 {
2607 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2608 }
Greg Claytonabb33022011-11-08 02:43:13 +00002609 else if (var_name == GetSettingNameForRunArgs())
2610 {
2611 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2612 }
2613 else if (var_name == GetSettingNameForEnvVars())
2614 {
2615 // This is nice for local debugging, but it is isn't correct for
2616 // remote debugging. We need to stop process.env-vars from being
2617 // populated with the host environment and add this as a launch option
2618 // and get the correct environment from the Target's platform.
2619 // GetHostEnvironmentIfNeeded ();
2620 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2621 }
2622 else if (var_name == GetSettingNameForInputPath())
2623 {
2624 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2625 }
2626 else if (var_name == GetSettingNameForOutputPath())
2627 {
2628 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2629 }
2630 else if (var_name == GetSettingNameForErrorPath())
2631 {
2632 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2633 }
2634 else if (var_name == GetSettingNameForDisableASLR())
2635 {
2636 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2637 }
2638 else if (var_name == GetSettingNameForDisableSTDIO ())
2639 {
2640 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2641 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002642}
2643
2644void
Greg Claytond284b662011-02-18 01:44:25 +00002645TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002646{
Sean Callanan77e93942010-10-29 00:29:03 +00002647 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2648
2649 if (!new_settings_ptr)
2650 return;
2651
Greg Claytonabb33022011-11-08 02:43:13 +00002652 *this = *new_settings_ptr;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002653}
2654
Caroline Ticebcb5b452010-09-20 21:37:42 +00002655bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002656TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2657 const ConstString &var_name,
2658 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002659 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002660{
Greg Claytond284b662011-02-18 01:44:25 +00002661 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002662 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002663 char path[PATH_MAX];
2664 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2665 if (path_len > 0)
2666 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002667 }
Jim Inghame41494a2011-04-16 00:01:13 +00002668 else if (var_name == GetSettingNameForPreferDynamicValue())
2669 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002670 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002671 }
Enrico Granatadba1de82012-03-27 02:35:13 +00002672 else if (var_name == GetSettingNameForEnableSyntheticValue())
2673 {
2674 if (m_skip_prologue)
2675 value.AppendString ("true");
2676 else
2677 value.AppendString ("false");
2678 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002679 else if (var_name == GetSettingNameForSkipPrologue())
2680 {
2681 if (m_skip_prologue)
2682 value.AppendString ("true");
2683 else
2684 value.AppendString ("false");
2685 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002686 else if (var_name == GetSettingNameForExecutableSearchPaths())
2687 {
2688 if (m_exe_search_paths.GetSize())
2689 {
2690 for (size_t i = 0, n = m_exe_search_paths.GetSize(); i < n; ++i)
2691 {
2692 value.AppendString(m_exe_search_paths.GetFileSpecAtIndex (i).GetDirectory().AsCString());
2693 }
2694 }
2695 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002696 else if (var_name == GetSettingNameForSourcePathMap ())
2697 {
Johnny Chen931449e2011-12-12 21:59:28 +00002698 if (m_source_map.GetSize())
2699 {
2700 size_t i;
2701 for (i = 0; i < m_source_map.GetSize(); ++i) {
2702 StreamString sstr;
2703 m_source_map.Dump(&sstr, i);
2704 value.AppendString(sstr.GetData());
2705 }
2706 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002707 }
Enrico Granata018921d2011-08-12 02:00:06 +00002708 else if (var_name == GetSettingNameForMaxChildren())
2709 {
2710 StreamString count_str;
2711 count_str.Printf ("%d", m_max_children_display);
2712 value.AppendString (count_str.GetData());
2713 }
Enrico Granata91544802011-09-06 19:20:51 +00002714 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2715 {
2716 StreamString count_str;
2717 count_str.Printf ("%d", m_max_strlen_length);
2718 value.AppendString (count_str.GetData());
2719 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002720 else if (var_name == GetSettingNameForPlatformAvoid())
2721 {
2722 if (m_breakpoints_use_platform_avoid)
2723 value.AppendString ("true");
2724 else
2725 value.AppendString ("false");
2726 }
Greg Claytonabb33022011-11-08 02:43:13 +00002727 else if (var_name == GetSettingNameForRunArgs())
2728 {
2729 if (m_run_args.GetArgumentCount() > 0)
2730 {
2731 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2732 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2733 }
2734 }
2735 else if (var_name == GetSettingNameForEnvVars())
2736 {
2737 GetHostEnvironmentIfNeeded ();
2738
2739 if (m_env_vars.size() > 0)
2740 {
2741 std::map<std::string, std::string>::iterator pos;
2742 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2743 {
2744 StreamString value_str;
2745 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2746 value.AppendString (value_str.GetData());
2747 }
2748 }
2749 }
2750 else if (var_name == GetSettingNameForInputPath())
2751 {
2752 value.AppendString (m_input_path.c_str());
2753 }
2754 else if (var_name == GetSettingNameForOutputPath())
2755 {
2756 value.AppendString (m_output_path.c_str());
2757 }
2758 else if (var_name == GetSettingNameForErrorPath())
2759 {
2760 value.AppendString (m_error_path.c_str());
2761 }
2762 else if (var_name == GetSettingNameForInheritHostEnv())
2763 {
2764 if (m_inherit_host_env)
2765 value.AppendString ("true");
2766 else
2767 value.AppendString ("false");
2768 }
2769 else if (var_name == GetSettingNameForDisableASLR())
2770 {
2771 if (m_disable_aslr)
2772 value.AppendString ("true");
2773 else
2774 value.AppendString ("false");
2775 }
2776 else if (var_name == GetSettingNameForDisableSTDIO())
2777 {
2778 if (m_disable_stdio)
2779 value.AppendString ("true");
2780 else
2781 value.AppendString ("false");
2782 }
Sean Callanan77e93942010-10-29 00:29:03 +00002783 else
2784 {
2785 if (err)
2786 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2787 return false;
2788 }
Sean Callanan77e93942010-10-29 00:29:03 +00002789 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002790}
2791
Greg Claytonabb33022011-11-08 02:43:13 +00002792void
2793Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2794{
2795 if (m_inherit_host_env && !m_got_host_env)
2796 {
2797 m_got_host_env = true;
2798 StringList host_env;
2799 const size_t host_env_count = Host::GetEnvironment (host_env);
2800 for (size_t idx=0; idx<host_env_count; idx++)
2801 {
2802 const char *env_entry = host_env.GetStringAtIndex (idx);
2803 if (env_entry)
2804 {
2805 const char *equal_pos = ::strchr(env_entry, '=');
2806 if (equal_pos)
2807 {
2808 std::string key (env_entry, equal_pos - env_entry);
2809 std::string value (equal_pos + 1);
2810 if (m_env_vars.find (key) == m_env_vars.end())
2811 m_env_vars[key] = value;
2812 }
2813 }
2814 }
2815 }
2816}
2817
2818
2819size_t
2820Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2821{
2822 GetHostEnvironmentIfNeeded ();
2823
2824 dictionary::const_iterator pos, end = m_env_vars.end();
2825 for (pos = m_env_vars.begin(); pos != end; ++pos)
2826 {
2827 std::string env_var_equal_value (pos->first);
2828 env_var_equal_value.append(1, '=');
2829 env_var_equal_value.append (pos->second);
2830 env.AppendArgument (env_var_equal_value.c_str());
2831 }
2832 return env.GetArgumentCount();
2833}
2834
2835
Caroline Tice5bc8c972010-09-20 20:44:43 +00002836const ConstString
2837TargetInstanceSettings::CreateInstanceName ()
2838{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002839 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002840 static int instance_count = 1;
2841
Caroline Tice5bc8c972010-09-20 20:44:43 +00002842 sstr.Printf ("target_%d", instance_count);
2843 ++instance_count;
2844
2845 const ConstString ret_val (sstr.GetData());
2846 return ret_val;
2847}
2848
2849//--------------------------------------------------
2850// Target::SettingsController Variable Tables
2851//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002852OptionEnumValueElement
2853TargetInstanceSettings::g_dynamic_value_types[] =
2854{
Greg Clayton577fbc32011-05-30 00:39:48 +00002855{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2856{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2857{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002858{ 0, NULL, NULL }
2859};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002860
2861SettingEntry
2862Target::SettingsController::global_settings_table[] =
2863{
Greg Claytond284b662011-02-18 01:44:25 +00002864 // var-name var-type default enum init'd hidden help-text
2865 // ================= ================== =========== ==== ====== ====== =========================================================================
2866 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2867 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2868};
2869
Caroline Tice5bc8c972010-09-20 20:44:43 +00002870SettingEntry
2871Target::SettingsController::instance_settings_table[] =
2872{
Enrico Granata91544802011-09-06 19:20:51 +00002873 // var-name var-type default enum init'd hidden help-text
2874 // ================= ================== =============== ======================= ====== ====== =========================================================================
2875 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2876 { 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 +00002877 { TSC_ENABLE_SYNTHETIC , eSetVarTypeBoolean, "true" , NULL, false, false, "Should synthetic values be used by default whenever available." },
Enrico Granata91544802011-09-06 19:20:51 +00002878 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2879 { 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 +00002880 { 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 +00002881 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2882 { 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 +00002883 { 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 +00002884 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2885 { 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." },
2886 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2887 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2888 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2889 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2890// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2891 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2892 { 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 +00002893 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002894};
Jim Ingham5a15e692012-02-16 06:50:00 +00002895
2896const ConstString &
2897Target::TargetEventData::GetFlavorString ()
2898{
2899 static ConstString g_flavor ("Target::TargetEventData");
2900 return g_flavor;
2901}
2902
2903const ConstString &
2904Target::TargetEventData::GetFlavor () const
2905{
2906 return TargetEventData::GetFlavorString ();
2907}
2908
2909Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2910 EventData(),
2911 m_target_sp (new_target_sp)
2912{
2913}
2914
2915Target::TargetEventData::~TargetEventData()
2916{
2917
2918}
2919
2920void
2921Target::TargetEventData::Dump (Stream *s) const
2922{
2923
2924}
2925
2926const TargetSP
2927Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2928{
2929 TargetSP target_sp;
2930
2931 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2932 if (data)
2933 target_sp = data->m_target_sp;
2934
2935 return target_sp;
2936}
2937
2938const Target::TargetEventData *
2939Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2940{
2941 if (event_ptr)
2942 {
2943 const EventData *event_data = event_ptr->GetData();
2944 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2945 return static_cast <const TargetEventData *> (event_ptr->GetData());
2946 }
2947 return NULL;
2948}
2949