blob: 81601749a3d52949b18780e1bd512f563c9c90eb [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"
Johnny Chen3f883492012-06-04 23:19:54 +000033#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034#include "lldb/lldb-private-log.h"
35#include "lldb/Symbol/ObjectFile.h"
36#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000037#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000038#include "lldb/Target/Thread.h"
39#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000040
41using namespace lldb;
42using namespace lldb_private;
43
Jim Ingham5a15e692012-02-16 06:50:00 +000044ConstString &
45Target::GetStaticBroadcasterClass ()
46{
47 static ConstString class_name ("lldb.target");
48 return class_name;
49}
50
Chris Lattner24943d22010-06-08 16:52:24 +000051//----------------------------------------------------------------------
52// Target constructor
53//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000054Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Jim Ingham5a15e692012-02-16 06:50:00 +000055 Broadcaster (&debugger, "lldb.target"),
Greg Clayton24bc5d92011-03-30 18:16:51 +000056 ExecutionContextScope (),
Greg Clayton334d33a2012-01-30 07:41:31 +000057 TargetInstanceSettings (GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000058 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000059 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000060 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000061 m_arch (target_arch),
62 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000063 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000064 m_breakpoint_list (false),
65 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000066 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000067 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000068 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000069 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000070 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000071 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000072 m_scratch_ast_source_ap (NULL),
73 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000074 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000075 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000076 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000077 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000078 m_suppress_stop_hooks (false),
79 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000080{
Greg Clayton49ce6822010-10-31 03:01:06 +000081 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
82 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
83 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000084
85 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000086
Greg Claytone005f2c2010-11-06 01:53:30 +000087 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000088 if (log)
89 log->Printf ("%p Target::Target()", this);
90}
91
92//----------------------------------------------------------------------
93// Destructor
94//----------------------------------------------------------------------
95Target::~Target()
96{
Greg Claytone005f2c2010-11-06 01:53:30 +000097 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000098 if (log)
99 log->Printf ("%p Target::~Target()", this);
100 DeleteCurrentProcess ();
101}
102
103void
Caroline Tice7826c882010-10-26 03:11:13 +0000104Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000105{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000106// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000107 if (description_level != lldb::eDescriptionLevelBrief)
108 {
109 s->Indent();
110 s->PutCString("Target\n");
111 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000112 m_images.Dump(s);
113 m_breakpoint_list.Dump(s);
114 m_internal_breakpoint_list.Dump(s);
115 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000116 }
117 else
118 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000119 Module *exe_module = GetExecutableModulePointer();
120 if (exe_module)
121 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000122 else
123 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000124 }
Chris Lattner24943d22010-06-08 16:52:24 +0000125}
126
127void
128Target::DeleteCurrentProcess ()
129{
130 if (m_process_sp.get())
131 {
Greg Clayton49480b12010-09-14 23:52:43 +0000132 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000133 if (m_process_sp->IsAlive())
134 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000135
136 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000137
138 // Do any cleanup of the target we need to do between process instances.
139 // NB It is better to do this before destroying the process in case the
140 // clean up needs some help from the process.
141 m_breakpoint_list.ClearAllBreakpointSites();
142 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000143 // Disable watchpoints just on the debugger side.
Johnny Chen116a5cd2012-02-25 06:44:30 +0000144 Mutex::Locker locker;
145 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000146 DisableAllWatchpoints(false);
Johnny Chen116a5cd2012-02-25 06:44:30 +0000147 ClearAllWatchpointHitCounts();
Chris Lattner24943d22010-06-08 16:52:24 +0000148 m_process_sp.reset();
149 }
150}
151
152const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000153Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000154{
155 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000156 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000157 return m_process_sp;
158}
159
160const lldb::ProcessSP &
161Target::GetProcessSP () const
162{
163 return m_process_sp;
164}
165
Greg Clayton153ccd72011-08-10 02:10:13 +0000166void
167Target::Destroy()
168{
169 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000170 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000171 DeleteCurrentProcess ();
172 m_platform_sp.reset();
173 m_arch.Clear();
174 m_images.Clear();
175 m_section_load_list.Clear();
176 const bool notify = false;
177 m_breakpoint_list.RemoveAll(notify);
178 m_internal_breakpoint_list.RemoveAll(notify);
179 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000180 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000181 m_search_filter_sp.reset();
182 m_image_search_paths.Clear(notify);
183 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000184 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000185 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000186 m_persistent_variables.Clear();
187 m_stop_hooks.clear();
188 m_stop_hook_next_id = 0;
189 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000190 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000191}
192
193
Chris Lattner24943d22010-06-08 16:52:24 +0000194BreakpointList &
195Target::GetBreakpointList(bool internal)
196{
197 if (internal)
198 return m_internal_breakpoint_list;
199 else
200 return m_breakpoint_list;
201}
202
203const BreakpointList &
204Target::GetBreakpointList(bool internal) const
205{
206 if (internal)
207 return m_internal_breakpoint_list;
208 else
209 return m_breakpoint_list;
210}
211
212BreakpointSP
213Target::GetBreakpointByID (break_id_t break_id)
214{
215 BreakpointSP bp_sp;
216
217 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
218 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
219 else
220 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
221
222 return bp_sp;
223}
224
225BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000226Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
227 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000228 RegularExpression &source_regex,
229 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000230{
Jim Inghamd6d47972011-09-23 00:54:11 +0000231 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
232 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000233 return CreateBreakpoint (filter_sp, resolver_sp, internal);
234}
235
236
237BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000238Target::CreateBreakpoint (const FileSpecList *containingModules,
239 const FileSpec &file,
240 uint32_t line_no,
241 bool check_inlines,
242 LazyBool skip_prologue,
243 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000244{
245 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000246
247 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines,
248 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000249 return CreateBreakpoint (filter_sp, resolver_sp, internal);
250}
251
252
253BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000254Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000255{
Chris Lattner24943d22010-06-08 16:52:24 +0000256 Address so_addr;
257 // Attempt to resolve our load address if possible, though it is ok if
258 // it doesn't resolve to section/offset.
259
Greg Clayton33ed1702010-08-24 00:45:41 +0000260 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000261 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000262 if (!so_addr.IsValid())
263 {
264 // The address didn't resolve, so just set this as an absolute address
265 so_addr.SetOffset (addr);
266 }
267 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000268 return bp_sp;
269}
270
271BreakpointSP
272Target::CreateBreakpoint (Address &addr, bool internal)
273{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000274 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000275 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
276 return CreateBreakpoint (filter_sp, resolver_sp, internal);
277}
278
279BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000280Target::CreateBreakpoint (const FileSpecList *containingModules,
281 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000282 const char *func_name,
283 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000284 LazyBool skip_prologue,
285 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000286{
Greg Clayton12bec712010-06-28 21:30:43 +0000287 BreakpointSP bp_sp;
288 if (func_name)
289 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000290 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000291
292 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
293 func_name,
294 func_name_type_mask,
295 Breakpoint::Exact,
296 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000297 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
298 }
299 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000300}
301
Jim Ingham4722b102012-03-06 00:37:27 +0000302lldb::BreakpointSP
303Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000304 const FileSpecList *containingSourceFiles,
305 const std::vector<std::string> &func_names,
306 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000307 LazyBool skip_prologue,
308 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000309{
310 BreakpointSP bp_sp;
311 size_t num_names = func_names.size();
312 if (num_names > 0)
313 {
314 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
315
316 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
317 func_names,
318 func_name_type_mask,
319 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
320 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
321 }
322 return bp_sp;
323}
324
Jim Inghamc1053622012-03-03 02:05:11 +0000325BreakpointSP
326Target::CreateBreakpoint (const FileSpecList *containingModules,
327 const FileSpecList *containingSourceFiles,
328 const char *func_names[],
329 size_t num_names,
330 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000331 LazyBool skip_prologue,
332 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000333{
334 BreakpointSP bp_sp;
335 if (num_names > 0)
336 {
337 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
338
339 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
340 func_names,
341 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000342 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000343 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
344 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
345 }
346 return bp_sp;
347}
Chris Lattner24943d22010-06-08 16:52:24 +0000348
349SearchFilterSP
350Target::GetSearchFilterForModule (const FileSpec *containingModule)
351{
352 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000353 if (containingModule != NULL)
354 {
355 // TODO: We should look into sharing module based search filters
356 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000357 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000358 }
359 else
360 {
361 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000362 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000363 filter_sp = m_search_filter_sp;
364 }
365 return filter_sp;
366}
367
Jim Ingham03c8ee52011-09-21 01:17:13 +0000368SearchFilterSP
369Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
370{
371 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000372 if (containingModules && containingModules->GetSize() != 0)
373 {
374 // TODO: We should look into sharing module based search filters
375 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000376 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000377 }
378 else
379 {
380 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000381 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000382 filter_sp = m_search_filter_sp;
383 }
384 return filter_sp;
385}
386
Jim Inghamd6d47972011-09-23 00:54:11 +0000387SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000388Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
389 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000390{
391 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
392 return GetSearchFilterForModuleList(containingModules);
393
394 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000395 if (containingModules == NULL)
396 {
397 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
398 // but that will take a little reworking.
399
Greg Clayton13d24fb2012-01-29 20:56:30 +0000400 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000401 }
402 else
403 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000404 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000405 }
406 return filter_sp;
407}
408
Chris Lattner24943d22010-06-08 16:52:24 +0000409BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000410Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000411 const FileSpecList *containingSourceFiles,
412 RegularExpression &func_regex,
413 LazyBool skip_prologue,
414 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000415{
Jim Inghamd6d47972011-09-23 00:54:11 +0000416 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000417 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
418 func_regex,
419 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000420
421 return CreateBreakpoint (filter_sp, resolver_sp, internal);
422}
423
Jim Ingham3df164e2012-03-05 04:47:34 +0000424lldb::BreakpointSP
425Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
426{
427 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
428}
429
Chris Lattner24943d22010-06-08 16:52:24 +0000430BreakpointSP
431Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
432{
433 BreakpointSP bp_sp;
434 if (filter_sp && resolver_sp)
435 {
436 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
437 resolver_sp->SetBreakpoint (bp_sp.get());
438
439 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000440 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000441 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000442 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000443
Greg Claytone005f2c2010-11-06 01:53:30 +0000444 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000445 if (log)
446 {
447 StreamString s;
448 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
449 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
450 }
451
Chris Lattner24943d22010-06-08 16:52:24 +0000452 bp_sp->ResolveBreakpoint();
453 }
Jim Inghamd1686902010-10-14 23:45:03 +0000454
455 if (!internal && bp_sp)
456 {
457 m_last_created_breakpoint = bp_sp;
458 }
459
Chris Lattner24943d22010-06-08 16:52:24 +0000460 return bp_sp;
461}
462
Johnny Chenda5a8022011-09-20 23:28:55 +0000463bool
464Target::ProcessIsValid()
465{
466 return (m_process_sp && m_process_sp->IsAlive());
467}
468
Johnny Chen3f883492012-06-04 23:19:54 +0000469static bool
470CheckIfWatchpointsExhausted(Target *target, Error &error)
471{
472 uint32_t num_supported_hardware_watchpoints;
473 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
474 if (rc.Success())
475 {
476 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
477 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
478 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
479 num_supported_hardware_watchpoints);
480 }
481 return false;
482}
483
Johnny Chenecd4feb2011-10-14 00:42:25 +0000484// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000485// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000486WatchpointSP
Johnny Chen3f883492012-06-04 23:19:54 +0000487Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000488{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000489 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
490 if (log)
491 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
492 __FUNCTION__, addr, size, type);
493
Johnny Chenecd4feb2011-10-14 00:42:25 +0000494 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000495 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000496 {
497 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000498 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000499 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000500 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000501 {
502 if (size == 0)
503 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
504 else
505 error.SetErrorStringWithFormat("invalid watch address: %llu", addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000506 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000507 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000508
Johnny Chenecd4feb2011-10-14 00:42:25 +0000509 // Currently we only support one watchpoint per address, with total number
510 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000511
512 // Grab the list mutex while doing operations.
513 Mutex::Locker locker;
514 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000515 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000516 if (matched_sp)
517 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000518 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000519 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000520 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
521 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000522 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000523 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000524 wp_sp = matched_sp;
525 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000526 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000527 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000528 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000529 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000530 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000531 }
532
Johnny Chenecd4feb2011-10-14 00:42:25 +0000533 if (!wp_sp) {
534 Watchpoint *new_wp = new Watchpoint(addr, size);
535 if (!new_wp) {
536 printf("Watchpoint ctor failed, out of memory?\n");
537 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000538 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000539 new_wp->SetWatchpointType(type);
540 new_wp->SetTarget(this);
541 wp_sp.reset(new_wp);
542 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000543 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000544
Johnny Chen3f883492012-06-04 23:19:54 +0000545 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000546 if (log)
547 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
548 __FUNCTION__,
Johnny Chen3f883492012-06-04 23:19:54 +0000549 error.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000550 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000551
Johnny Chen3f883492012-06-04 23:19:54 +0000552 if (error.Fail()) {
Johnny Chen155599b2012-03-26 22:00:10 +0000553 // Enabling the watchpoint on the device side failed.
554 // Remove the said watchpoint from the list maintained by the target instance.
555 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000556 // See if we could provide more helpful error message.
557 if (!CheckIfWatchpointsExhausted(this, error))
558 {
559 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
560 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
561 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000562 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000563 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000564 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000565 m_last_created_watchpoint = wp_sp;
566 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000567}
568
Chris Lattner24943d22010-06-08 16:52:24 +0000569void
570Target::RemoveAllBreakpoints (bool internal_also)
571{
Greg Claytone005f2c2010-11-06 01:53:30 +0000572 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000573 if (log)
574 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
575
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000576 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000577 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000578 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000579
580 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000581}
582
583void
584Target::DisableAllBreakpoints (bool internal_also)
585{
Greg Claytone005f2c2010-11-06 01:53:30 +0000586 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000587 if (log)
588 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
589
590 m_breakpoint_list.SetEnabledAll (false);
591 if (internal_also)
592 m_internal_breakpoint_list.SetEnabledAll (false);
593}
594
595void
596Target::EnableAllBreakpoints (bool internal_also)
597{
Greg Claytone005f2c2010-11-06 01:53:30 +0000598 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000599 if (log)
600 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
601
602 m_breakpoint_list.SetEnabledAll (true);
603 if (internal_also)
604 m_internal_breakpoint_list.SetEnabledAll (true);
605}
606
607bool
608Target::RemoveBreakpointByID (break_id_t break_id)
609{
Greg Claytone005f2c2010-11-06 01:53:30 +0000610 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000611 if (log)
612 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
613
614 if (DisableBreakpointByID (break_id))
615 {
616 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000617 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000618 else
Jim Inghamd1686902010-10-14 23:45:03 +0000619 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000620 if (m_last_created_breakpoint)
621 {
622 if (m_last_created_breakpoint->GetID() == break_id)
623 m_last_created_breakpoint.reset();
624 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000625 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000626 }
Chris Lattner24943d22010-06-08 16:52:24 +0000627 return true;
628 }
629 return false;
630}
631
632bool
633Target::DisableBreakpointByID (break_id_t break_id)
634{
Greg Claytone005f2c2010-11-06 01:53:30 +0000635 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000636 if (log)
637 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
638
639 BreakpointSP bp_sp;
640
641 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
642 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
643 else
644 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
645 if (bp_sp)
646 {
647 bp_sp->SetEnabled (false);
648 return true;
649 }
650 return false;
651}
652
653bool
654Target::EnableBreakpointByID (break_id_t break_id)
655{
Greg Claytone005f2c2010-11-06 01:53:30 +0000656 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000657 if (log)
658 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
659 __FUNCTION__,
660 break_id,
661 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
662
663 BreakpointSP bp_sp;
664
665 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
666 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
667 else
668 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
669
670 if (bp_sp)
671 {
672 bp_sp->SetEnabled (true);
673 return true;
674 }
675 return false;
676}
677
Johnny Chenc86582f2011-09-23 21:21:43 +0000678// The flag 'end_to_end', default to true, signifies that the operation is
679// performed end to end, for both the debugger and the debuggee.
680
Johnny Chenecd4feb2011-10-14 00:42:25 +0000681// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
682// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000683bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000684Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000685{
686 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
687 if (log)
688 log->Printf ("Target::%s\n", __FUNCTION__);
689
Johnny Chenc86582f2011-09-23 21:21:43 +0000690 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000691 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000692 return true;
693 }
694
695 // Otherwise, it's an end to end operation.
696
Johnny Chenda5a8022011-09-20 23:28:55 +0000697 if (!ProcessIsValid())
698 return false;
699
Johnny Chenecd4feb2011-10-14 00:42:25 +0000700 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000701 for (size_t i = 0; i < num_watchpoints; ++i)
702 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000703 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
704 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000705 return false;
706
Johnny Chenecd4feb2011-10-14 00:42:25 +0000707 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000708 if (rc.Fail())
709 return false;
710 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000711 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000712 return true; // Success!
713}
714
Johnny Chenecd4feb2011-10-14 00:42:25 +0000715// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
716// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000717bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000718Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000719{
720 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
721 if (log)
722 log->Printf ("Target::%s\n", __FUNCTION__);
723
Johnny Chenc86582f2011-09-23 21:21:43 +0000724 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000725 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000726 return true;
727 }
728
729 // Otherwise, it's an end to end operation.
730
Johnny Chenda5a8022011-09-20 23:28:55 +0000731 if (!ProcessIsValid())
732 return false;
733
Johnny Chenecd4feb2011-10-14 00:42:25 +0000734 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000735 for (size_t i = 0; i < num_watchpoints; ++i)
736 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000737 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
738 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000739 return false;
740
Johnny Chenecd4feb2011-10-14 00:42:25 +0000741 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000742 if (rc.Fail())
743 return false;
744 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000745 return true; // Success!
746}
747
Johnny Chenecd4feb2011-10-14 00:42:25 +0000748// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
749// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000750bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000751Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000752{
753 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
754 if (log)
755 log->Printf ("Target::%s\n", __FUNCTION__);
756
Johnny Chenc86582f2011-09-23 21:21:43 +0000757 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000758 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000759 return true;
760 }
761
762 // Otherwise, it's an end to end operation.
763
Johnny Chenda5a8022011-09-20 23:28:55 +0000764 if (!ProcessIsValid())
765 return false;
766
Johnny Chenecd4feb2011-10-14 00:42:25 +0000767 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000768 for (size_t i = 0; i < num_watchpoints; ++i)
769 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000770 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
771 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000772 return false;
773
Johnny Chenecd4feb2011-10-14 00:42:25 +0000774 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000775 if (rc.Fail())
776 return false;
777 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000778 return true; // Success!
779}
780
Johnny Chen116a5cd2012-02-25 06:44:30 +0000781// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
782bool
783Target::ClearAllWatchpointHitCounts ()
784{
785 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
786 if (log)
787 log->Printf ("Target::%s\n", __FUNCTION__);
788
789 size_t num_watchpoints = m_watchpoint_list.GetSize();
790 for (size_t i = 0; i < num_watchpoints; ++i)
791 {
792 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
793 if (!wp_sp)
794 return false;
795
796 wp_sp->ResetHitCount();
797 }
798 return true; // Success!
799}
800
Johnny Chenecd4feb2011-10-14 00:42:25 +0000801// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000802// during these operations.
803bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000804Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000805{
806 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
807 if (log)
808 log->Printf ("Target::%s\n", __FUNCTION__);
809
810 if (!ProcessIsValid())
811 return false;
812
Johnny Chenecd4feb2011-10-14 00:42:25 +0000813 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000814 for (size_t i = 0; i < num_watchpoints; ++i)
815 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000816 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
817 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000818 return false;
819
Johnny Chenecd4feb2011-10-14 00:42:25 +0000820 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000821 }
822 return true; // Success!
823}
824
Johnny Chenecd4feb2011-10-14 00:42:25 +0000825// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000826bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000827Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000828{
829 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
830 if (log)
831 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
832
833 if (!ProcessIsValid())
834 return false;
835
Johnny Chenecd4feb2011-10-14 00:42:25 +0000836 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
837 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000838 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000839 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000840 if (rc.Success())
841 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000842
Johnny Chen01acfa72011-09-22 18:04:58 +0000843 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000844 }
845 return false;
846}
847
Johnny Chenecd4feb2011-10-14 00:42:25 +0000848// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000849bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000850Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000851{
852 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
853 if (log)
854 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
855
856 if (!ProcessIsValid())
857 return false;
858
Johnny Chenecd4feb2011-10-14 00:42:25 +0000859 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
860 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000861 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000862 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000863 if (rc.Success())
864 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000865
Johnny Chen01acfa72011-09-22 18:04:58 +0000866 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000867 }
868 return false;
869}
870
Johnny Chenecd4feb2011-10-14 00:42:25 +0000871// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000872bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000873Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000874{
875 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
876 if (log)
877 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
878
Johnny Chenecd4feb2011-10-14 00:42:25 +0000879 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000880 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000881 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000882 return true;
883 }
884 return false;
885}
886
Johnny Chenecd4feb2011-10-14 00:42:25 +0000887// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000888bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000889Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000890{
891 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
892 if (log)
893 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
894
895 if (!ProcessIsValid())
896 return false;
897
Johnny Chenecd4feb2011-10-14 00:42:25 +0000898 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
899 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000900 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000901 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000902 return true;
903 }
904 return false;
905}
906
Chris Lattner24943d22010-06-08 16:52:24 +0000907ModuleSP
908Target::GetExecutableModule ()
909{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000910 return m_images.GetModuleAtIndex(0);
911}
912
913Module*
914Target::GetExecutableModulePointer ()
915{
916 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000917}
918
919void
920Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
921{
922 m_images.Clear();
923 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000924 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000925 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000926
927 if (executable_sp.get())
928 {
929 Timer scoped_timer (__PRETTY_FUNCTION__,
930 "Target::SetExecutableModule (executable = '%s/%s')",
931 executable_sp->GetFileSpec().GetDirectory().AsCString(),
932 executable_sp->GetFileSpec().GetFilename().AsCString());
933
934 m_images.Append(executable_sp); // The first image is our exectuable file
935
Jim Ingham7508e732010-08-09 23:31:02 +0000936 // 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 +0000937 if (!m_arch.IsValid())
938 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000939
Chris Lattner24943d22010-06-08 16:52:24 +0000940 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000941 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000942
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000943 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000944 {
945 executable_objfile->GetDependentModules(dependent_files);
946 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
947 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000948 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
949 FileSpec platform_dependent_file_spec;
950 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000951 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000952 else
953 platform_dependent_file_spec = dependent_file_spec;
954
Greg Clayton444fe992012-02-26 05:51:37 +0000955 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
956 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +0000957 if (image_module_sp.get())
958 {
Chris Lattner24943d22010-06-08 16:52:24 +0000959 ObjectFile *objfile = image_module_sp->GetObjectFile();
960 if (objfile)
961 objfile->GetDependentModules(dependent_files);
962 }
963 }
964 }
Chris Lattner24943d22010-06-08 16:52:24 +0000965 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000966
967 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000968}
969
970
Jim Ingham7508e732010-08-09 23:31:02 +0000971bool
972Target::SetArchitecture (const ArchSpec &arch_spec)
973{
Greg Claytonb170aee2012-05-08 01:45:38 +0000974 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000975 {
Greg Claytonb170aee2012-05-08 01:45:38 +0000976 // If we haven't got a valid arch spec, or the architectures are
977 // compatible, so just update the architecture. Architectures can be
978 // equal, yet the triple OS and vendor might change, so we need to do
979 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000980 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000981 return true;
982 }
983 else
984 {
985 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000986 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000987 ModuleSP executable_sp = GetExecutableModule ();
988 m_images.Clear();
989 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000990 m_scratch_ast_source_ap.reset();
991 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000992 // Need to do something about unsetting breakpoints.
993
994 if (executable_sp)
995 {
Greg Clayton444fe992012-02-26 05:51:37 +0000996 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
997 Error error = ModuleList::GetSharedModule (module_spec,
998 executable_sp,
999 &GetExecutableSearchPaths(),
1000 NULL,
1001 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001002
1003 if (!error.Fail() && executable_sp)
1004 {
1005 SetExecutableModule (executable_sp, true);
1006 return true;
1007 }
Jim Ingham7508e732010-08-09 23:31:02 +00001008 }
1009 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001010 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001011}
Chris Lattner24943d22010-06-08 16:52:24 +00001012
Chris Lattner24943d22010-06-08 16:52:24 +00001013void
1014Target::ModuleAdded (ModuleSP &module_sp)
1015{
1016 // A module is being added to this target for the first time
1017 ModuleList module_list;
1018 module_list.Append(module_sp);
1019 ModulesDidLoad (module_list);
1020}
1021
1022void
1023Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
1024{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001025 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001026 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001027}
1028
1029void
1030Target::ModulesDidLoad (ModuleList &module_list)
1031{
1032 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1033 // TODO: make event data that packages up the module_list
1034 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1035}
1036
1037void
1038Target::ModulesDidUnload (ModuleList &module_list)
1039{
1040 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +00001041
1042 // Remove the images from the target image list
1043 m_images.Remove(module_list);
1044
Chris Lattner24943d22010-06-08 16:52:24 +00001045 // TODO: make event data that packages up the module_list
1046 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1047}
1048
Jim Ingham7089d8a2011-10-28 23:14:11 +00001049
Daniel Dunbar705a0982011-10-31 22:50:37 +00001050bool
Greg Clayton444fe992012-02-26 05:51:37 +00001051Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001052{
1053
1054 if (!m_breakpoints_use_platform_avoid)
1055 return false;
1056 else
1057 {
1058 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001059 ModuleSpec module_spec (module_file_spec);
1060 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001061
1062 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1063 // black list.
1064 if (num_modules > 0)
1065 {
1066 for (int i = 0; i < num_modules; i++)
1067 {
1068 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1069 return false;
1070 }
1071 return true;
1072 }
1073 else
1074 return false;
1075 }
1076}
1077
Daniel Dunbar705a0982011-10-31 22:50:37 +00001078bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001079Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1080{
1081 if (!m_breakpoints_use_platform_avoid)
1082 return false;
1083 else if (GetPlatform())
1084 {
1085 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
1086 }
1087 else
1088 return false;
1089}
1090
Chris Lattner24943d22010-06-08 16:52:24 +00001091size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001092Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1093{
Greg Clayton3508c382012-02-24 01:59:29 +00001094 SectionSP section_sp (addr.GetSection());
1095 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001096 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001097 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1098 if (section_sp->IsEncrypted())
1099 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001100 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001101 return 0;
1102 }
Greg Clayton3508c382012-02-24 01:59:29 +00001103 ModuleSP module_sp (section_sp->GetModule());
1104 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001105 {
Greg Clayton3508c382012-02-24 01:59:29 +00001106 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1107 if (objfile)
1108 {
1109 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1110 addr.GetOffset(),
1111 dst,
1112 dst_len);
1113 if (bytes_read > 0)
1114 return bytes_read;
1115 else
1116 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1117 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001118 else
Greg Clayton3508c382012-02-24 01:59:29 +00001119 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001120 }
1121 else
Greg Clayton3508c382012-02-24 01:59:29 +00001122 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001123 }
1124 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001125 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001126
Greg Clayton26100dc2011-01-07 01:57:07 +00001127 return 0;
1128}
1129
1130size_t
Enrico Granata91544802011-09-06 19:20:51 +00001131Target::ReadMemory (const Address& addr,
1132 bool prefer_file_cache,
1133 void *dst,
1134 size_t dst_len,
1135 Error &error,
1136 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001137{
Chris Lattner24943d22010-06-08 16:52:24 +00001138 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001139
Enrico Granata91544802011-09-06 19:20:51 +00001140 // if we end up reading this from process memory, we will fill this
1141 // with the actual load address
1142 if (load_addr_ptr)
1143 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1144
Greg Clayton26100dc2011-01-07 01:57:07 +00001145 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001146
1147 addr_t load_addr = LLDB_INVALID_ADDRESS;
1148 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001149 Address resolved_addr;
1150 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001151 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001152 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001153 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001154 // No sections are loaded, so we must assume we are not running
1155 // yet and anything we are given is a file address.
1156 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1157 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001158 }
Greg Clayton70436352010-06-30 23:03:03 +00001159 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001160 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001161 // We have at least one section loaded. This can be becuase
1162 // we have manually loaded some sections with "target modules load ..."
1163 // or because we have have a live process that has sections loaded
1164 // through the dynamic loader
1165 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1166 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001167 }
Greg Clayton70436352010-06-30 23:03:03 +00001168 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001169 if (!resolved_addr.IsValid())
1170 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001171
Greg Clayton9b82f862011-07-11 05:12:02 +00001172
Greg Clayton26100dc2011-01-07 01:57:07 +00001173 if (prefer_file_cache)
1174 {
1175 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1176 if (bytes_read > 0)
1177 return bytes_read;
1178 }
Greg Clayton70436352010-06-30 23:03:03 +00001179
Johnny Chenda5a8022011-09-20 23:28:55 +00001180 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001181 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001182 if (load_addr == LLDB_INVALID_ADDRESS)
1183 load_addr = resolved_addr.GetLoadAddress (this);
1184
Greg Clayton70436352010-06-30 23:03:03 +00001185 if (load_addr == LLDB_INVALID_ADDRESS)
1186 {
Greg Clayton3508c382012-02-24 01:59:29 +00001187 ModuleSP addr_module_sp (resolved_addr.GetModule());
1188 if (addr_module_sp && addr_module_sp->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001189 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001190 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001191 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001192 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001193 else
Greg Clayton9c236732011-10-26 00:56:27 +00001194 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001195 }
1196 else
1197 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001198 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001199 if (bytes_read != dst_len)
1200 {
1201 if (error.Success())
1202 {
1203 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001204 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001205 else
Greg Clayton9c236732011-10-26 00:56:27 +00001206 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 +00001207 }
1208 }
Greg Clayton70436352010-06-30 23:03:03 +00001209 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001210 {
1211 if (load_addr_ptr)
1212 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001213 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001214 }
Greg Clayton70436352010-06-30 23:03:03 +00001215 // If the address is not section offset we have an address that
1216 // doesn't resolve to any address in any currently loaded shared
1217 // libaries and we failed to read memory so there isn't anything
1218 // more we can do. If it is section offset, we might be able to
1219 // read cached memory from the object file.
1220 if (!resolved_addr.IsSectionOffset())
1221 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001222 }
Chris Lattner24943d22010-06-08 16:52:24 +00001223 }
Greg Clayton70436352010-06-30 23:03:03 +00001224
Greg Clayton9b82f862011-07-11 05:12:02 +00001225 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001226 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001227 // If we didn't already try and read from the object file cache, then
1228 // try it after failing to read from the process.
1229 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001230 }
1231 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001232}
1233
Greg Clayton7dd98df2011-07-12 17:06:17 +00001234size_t
1235Target::ReadScalarIntegerFromMemory (const Address& addr,
1236 bool prefer_file_cache,
1237 uint32_t byte_size,
1238 bool is_signed,
1239 Scalar &scalar,
1240 Error &error)
1241{
1242 uint64_t uval;
1243
1244 if (byte_size <= sizeof(uval))
1245 {
1246 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1247 if (bytes_read == byte_size)
1248 {
1249 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1250 uint32_t offset = 0;
1251 if (byte_size <= 4)
1252 scalar = data.GetMaxU32 (&offset, byte_size);
1253 else
1254 scalar = data.GetMaxU64 (&offset, byte_size);
1255
1256 if (is_signed)
1257 scalar.SignExtend(byte_size * 8);
1258 return bytes_read;
1259 }
1260 }
1261 else
1262 {
1263 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1264 }
1265 return 0;
1266}
1267
1268uint64_t
1269Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1270 bool prefer_file_cache,
1271 size_t integer_byte_size,
1272 uint64_t fail_value,
1273 Error &error)
1274{
1275 Scalar scalar;
1276 if (ReadScalarIntegerFromMemory (addr,
1277 prefer_file_cache,
1278 integer_byte_size,
1279 false,
1280 scalar,
1281 error))
1282 return scalar.ULongLong(fail_value);
1283 return fail_value;
1284}
1285
1286bool
1287Target::ReadPointerFromMemory (const Address& addr,
1288 bool prefer_file_cache,
1289 Error &error,
1290 Address &pointer_addr)
1291{
1292 Scalar scalar;
1293 if (ReadScalarIntegerFromMemory (addr,
1294 prefer_file_cache,
1295 m_arch.GetAddressByteSize(),
1296 false,
1297 scalar,
1298 error))
1299 {
1300 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1301 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1302 {
1303 if (m_section_load_list.IsEmpty())
1304 {
1305 // No sections are loaded, so we must assume we are not running
1306 // yet and anything we are given is a file address.
1307 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1308 }
1309 else
1310 {
1311 // We have at least one section loaded. This can be becuase
1312 // we have manually loaded some sections with "target modules load ..."
1313 // or because we have have a live process that has sections loaded
1314 // through the dynamic loader
1315 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1316 }
1317 // We weren't able to resolve the pointer value, so just return
1318 // an address with no section
1319 if (!pointer_addr.IsValid())
1320 pointer_addr.SetOffset (pointer_vm_addr);
1321 return true;
1322
1323 }
1324 }
1325 return false;
1326}
Chris Lattner24943d22010-06-08 16:52:24 +00001327
1328ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001329Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001330{
Chris Lattner24943d22010-06-08 16:52:24 +00001331 ModuleSP module_sp;
1332
Chris Lattner24943d22010-06-08 16:52:24 +00001333 Error error;
1334
Jim Ingham03e5e512012-05-17 18:38:42 +00001335 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1336 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001337
Jim Ingham03e5e512012-05-17 18:38:42 +00001338 if (module_spec.GetUUID().IsValid())
1339 module_sp = m_images.FindFirstModule(module_spec);
1340
Greg Claytone1ef1e32012-04-27 00:58:27 +00001341 if (!module_sp)
1342 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001343 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1344 bool did_create_module = false;
1345
1346 // If there are image search path entries, try to use them first to acquire a suitable image.
1347 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001348 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001349 ModuleSpec transformed_spec (module_spec);
1350 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1351 {
1352 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1353 error = ModuleList::GetSharedModule (transformed_spec,
1354 module_sp,
1355 &GetExecutableSearchPaths(),
1356 &old_module_sp,
1357 &did_create_module);
1358 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001359 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001360
Greg Claytone1ef1e32012-04-27 00:58:27 +00001361 if (!module_sp)
1362 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001363 // If we have a UUID, we can check our global shared module list in case
1364 // we already have it. If we don't have a valid UUID, then we can't since
1365 // the path in "module_spec" will be a platform path, and we will need to
1366 // let the platform find that file. For example, we could be asking for
1367 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1368 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1369 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1370 // cache.
1371 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001372 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001373 // We have a UUID, it is OK to check the global module list...
1374 error = ModuleList::GetSharedModule (module_spec,
1375 module_sp,
1376 &GetExecutableSearchPaths(),
1377 &old_module_sp,
1378 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001379 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001380
1381 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001382 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001383 // The platform is responsible for finding and caching an appropriate
1384 // module in the shared module cache.
1385 if (m_platform_sp)
1386 {
1387 FileSpec platform_file_spec;
1388 error = m_platform_sp->GetSharedModule (module_spec,
1389 module_sp,
1390 &GetExecutableSearchPaths(),
1391 &old_module_sp,
1392 &did_create_module);
1393 }
1394 else
1395 {
1396 error.SetErrorString("no platform is currently set");
1397 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001398 }
1399 }
Chris Lattner24943d22010-06-08 16:52:24 +00001400
Jim Ingham03e5e512012-05-17 18:38:42 +00001401 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1402 // module in the list already, and if there was, let's remove it.
1403 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001404 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001405 // GetSharedModule is not guaranteed to find the old shared module, for instance
1406 // in the common case where you pass in the UUID, it is only going to find the one
1407 // module matching the UUID. In fact, it has no good way to know what the "old module"
1408 // relevant to this target is, since there might be many copies of a module with this file spec
1409 // in various running debug sessions, but only one of them will belong to this target.
1410 // So let's remove the UUID from the module list, and look in the target's module list.
1411 // Only do this if there is SOMETHING else in the module spec...
1412 if (!old_module_sp)
1413 {
1414 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
1415 {
1416 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1417 module_spec_copy.GetUUID().Clear();
1418
1419 ModuleList found_modules;
1420 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1421 if (num_found == 1)
1422 {
1423 old_module_sp = found_modules.GetModuleAtIndex(0);
1424 }
1425 }
1426 }
1427
1428 m_images.Append (module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001429 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
Jim Ingham03e5e512012-05-17 18:38:42 +00001430 {
Chris Lattner24943d22010-06-08 16:52:24 +00001431 ModuleUpdated(old_module_sp, module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001432 m_images.Remove (old_module_sp);
1433 Module *old_module_ptr = old_module_sp.get();
1434 old_module_sp.reset();
1435 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1436 }
Chris Lattner24943d22010-06-08 16:52:24 +00001437 else
1438 ModuleAdded(module_sp);
1439 }
1440 }
1441 if (error_ptr)
1442 *error_ptr = error;
1443 return module_sp;
1444}
1445
1446
Greg Clayton289afcb2012-02-18 05:35:26 +00001447TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001448Target::CalculateTarget ()
1449{
Greg Clayton289afcb2012-02-18 05:35:26 +00001450 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001451}
1452
Greg Clayton289afcb2012-02-18 05:35:26 +00001453ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001454Target::CalculateProcess ()
1455{
Greg Clayton289afcb2012-02-18 05:35:26 +00001456 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001457}
1458
Greg Clayton289afcb2012-02-18 05:35:26 +00001459ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001460Target::CalculateThread ()
1461{
Greg Clayton289afcb2012-02-18 05:35:26 +00001462 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001463}
1464
Greg Clayton289afcb2012-02-18 05:35:26 +00001465StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001466Target::CalculateStackFrame ()
1467{
Greg Clayton289afcb2012-02-18 05:35:26 +00001468 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001469}
1470
1471void
Greg Claytona830adb2010-10-04 01:05:56 +00001472Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001473{
Greg Clayton567e7f32011-09-22 04:58:26 +00001474 exe_ctx.Clear();
1475 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001476}
1477
1478PathMappingList &
1479Target::GetImageSearchPathList ()
1480{
1481 return m_image_search_paths;
1482}
1483
1484void
1485Target::ImageSearchPathsChanged
1486(
1487 const PathMappingList &path_list,
1488 void *baton
1489)
1490{
1491 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001492 ModuleSP exe_module_sp (target->GetExecutableModule());
1493 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001494 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001495 target->m_images.Clear();
1496 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001497 }
1498}
1499
1500ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001501Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001502{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001503 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001504 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001505 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001506 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001507 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001508 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1509 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1510 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1511 }
Chris Lattner24943d22010-06-08 16:52:24 +00001512 return m_scratch_ast_context_ap.get();
1513}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001514
Sean Callanan4938bd62011-11-16 18:20:47 +00001515ClangASTImporter *
1516Target::GetClangASTImporter()
1517{
1518 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1519
1520 if (!ast_importer)
1521 {
1522 ast_importer = new ClangASTImporter();
1523 m_ast_importer_ap.reset(ast_importer);
1524 }
1525
1526 return ast_importer;
1527}
1528
Greg Clayton990de7b2010-11-18 23:32:35 +00001529void
Caroline Tice2a456812011-03-10 22:14:10 +00001530Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001531{
Greg Clayton334d33a2012-01-30 07:41:31 +00001532 UserSettingsController::InitializeSettingsController (GetSettingsController(),
Greg Clayton990de7b2010-11-18 23:32:35 +00001533 SettingsController::global_settings_table,
1534 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001535
1536 // Now call SettingsInitialize() on each 'child' setting of Target
1537 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001538}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001539
Greg Clayton990de7b2010-11-18 23:32:35 +00001540void
Caroline Tice2a456812011-03-10 22:14:10 +00001541Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001542{
Caroline Tice2a456812011-03-10 22:14:10 +00001543
1544 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1545
1546 Process::SettingsTerminate ();
1547
1548 // Now terminate Target Settings.
1549
Greg Clayton990de7b2010-11-18 23:32:35 +00001550 UserSettingsControllerSP &usc = GetSettingsController();
1551 UserSettingsController::FinalizeSettingsController (usc);
1552 usc.reset();
1553}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001554
Greg Clayton990de7b2010-11-18 23:32:35 +00001555UserSettingsControllerSP &
1556Target::GetSettingsController ()
1557{
Greg Clayton334d33a2012-01-30 07:41:31 +00001558 static UserSettingsControllerSP g_settings_controller_sp;
1559 if (!g_settings_controller_sp)
1560 {
1561 g_settings_controller_sp.reset (new Target::SettingsController);
1562 // The first shared pointer to Target::SettingsController in
1563 // g_settings_controller_sp must be fully created above so that
1564 // the TargetInstanceSettings can use a weak_ptr to refer back
1565 // to the master setttings controller
1566 InstanceSettingsSP default_instance_settings_sp (new TargetInstanceSettings (g_settings_controller_sp,
1567 false,
1568 InstanceSettings::GetDefaultName().AsCString()));
1569 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
1570 }
1571 return g_settings_controller_sp;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001572}
1573
Greg Clayton9ce95382012-02-13 23:10:39 +00001574FileSpecList
1575Target::GetDefaultExecutableSearchPaths ()
1576{
1577 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1578 if (settings_controller_sp)
1579 {
1580 lldb::InstanceSettingsSP instance_settings_sp (settings_controller_sp->GetDefaultInstanceSettings ());
1581 if (instance_settings_sp)
1582 return static_cast<TargetInstanceSettings *>(instance_settings_sp.get())->GetExecutableSearchPaths ();
1583 }
1584 return FileSpecList();
1585}
1586
1587
Caroline Tice5bc8c972010-09-20 20:44:43 +00001588ArchSpec
1589Target::GetDefaultArchitecture ()
1590{
Greg Clayton940b1032011-02-23 00:35:02 +00001591 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
Greg Clayton940b1032011-02-23 00:35:02 +00001592 if (settings_controller_sp)
Greg Clayton469e08d2012-05-15 02:44:13 +00001593 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1594 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001595}
1596
1597void
Greg Clayton940b1032011-02-23 00:35:02 +00001598Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001599{
Greg Clayton940b1032011-02-23 00:35:02 +00001600 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1601
1602 if (settings_controller_sp)
1603 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001604}
1605
Greg Claytona830adb2010-10-04 01:05:56 +00001606Target *
1607Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1608{
1609 // The target can either exist in the "process" of ExecutionContext, or in
1610 // the "target_sp" member of SymbolContext. This accessor helper function
1611 // will get the target from one of these locations.
1612
1613 Target *target = NULL;
1614 if (sc_ptr != NULL)
1615 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001616 if (target == NULL && exe_ctx_ptr)
1617 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001618 return target;
1619}
1620
1621
Caroline Tice1ebef442010-09-27 00:30:10 +00001622void
1623Target::UpdateInstanceName ()
1624{
1625 StreamString sstr;
1626
Greg Clayton5beb99d2011-08-11 02:48:45 +00001627 Module *exe_module = GetExecutableModulePointer();
1628 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001629 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001630 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001631 exe_module->GetFileSpec().GetFilename().AsCString(),
1632 exe_module->GetArchitecture().GetArchitectureName());
1633 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001634 }
1635}
1636
Sean Callanan77e93942010-10-29 00:29:03 +00001637const char *
1638Target::GetExpressionPrefixContentsAsCString ()
1639{
Sean Callanane0b7f942011-11-16 01:54:57 +00001640 if (!m_expr_prefix_contents.empty())
1641 return m_expr_prefix_contents.c_str();
Greg Claytonff44ab42011-04-23 02:04:55 +00001642 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001643}
1644
Greg Clayton427f2902010-12-14 02:59:59 +00001645ExecutionResults
1646Target::EvaluateExpression
1647(
1648 const char *expr_cstr,
1649 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001650 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001651 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001652 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001653 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001654 lldb::DynamicValueType use_dynamic,
Enrico Granata6cca9692012-07-16 23:10:35 +00001655 lldb::ValueObjectSP &result_valobj_sp,
1656 uint32_t single_thread_timeout_usec
Greg Clayton427f2902010-12-14 02:59:59 +00001657)
1658{
1659 ExecutionResults execution_results = eExecutionSetupError;
1660
1661 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001662
1663 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1664 return execution_results;
1665
Jim Ingham3613ae12011-05-12 02:06:14 +00001666 // We shouldn't run stop hooks in expressions.
1667 // Be sure to reset this if you return anywhere within this function.
1668 bool old_suppress_value = m_suppress_stop_hooks;
1669 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001670
1671 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001672
1673 const size_t expr_cstr_len = ::strlen (expr_cstr);
1674
Greg Clayton427f2902010-12-14 02:59:59 +00001675 if (frame)
1676 {
1677 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001678 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001679 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001680 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1681 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001682 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001683
1684 // Make sure we don't have any things that we know a variable expression
1685 // won't be able to deal with before calling into it
1686 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1687 {
1688 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1689 use_dynamic,
1690 expr_path_options,
1691 var_sp,
1692 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001693 // if this expression results in a bitfield, we give up and let the IR handle it
1694 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1695 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001696 }
Greg Clayton427f2902010-12-14 02:59:59 +00001697 }
1698 else if (m_process_sp)
1699 {
1700 m_process_sp->CalculateExecutionContext(exe_ctx);
1701 }
1702 else
1703 {
1704 CalculateExecutionContext(exe_ctx);
1705 }
1706
1707 if (result_valobj_sp)
1708 {
1709 execution_results = eExecutionCompleted;
1710 // We got a result from the frame variable expression path above...
1711 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1712
1713 lldb::ValueObjectSP const_valobj_sp;
1714
1715 // Check in case our value is already a constant value
1716 if (result_valobj_sp->GetIsConstant())
1717 {
1718 const_valobj_sp = result_valobj_sp;
1719 const_valobj_sp->SetName (persistent_variable_name);
1720 }
1721 else
Jim Inghame41494a2011-04-16 00:01:13 +00001722 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001723 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001724 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001725 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001726 if (dynamic_sp)
1727 result_valobj_sp = dynamic_sp;
1728 }
1729
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001730 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001731 }
Greg Clayton427f2902010-12-14 02:59:59 +00001732
Sean Callanan6a925532011-01-13 08:53:35 +00001733 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1734
Greg Clayton427f2902010-12-14 02:59:59 +00001735 result_valobj_sp = const_valobj_sp;
1736
Sean Callanan6a925532011-01-13 08:53:35 +00001737 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1738 assert (clang_expr_variable_sp.get());
1739
1740 // Set flags and live data as appropriate
1741
1742 const Value &result_value = live_valobj_sp->GetValue();
1743
1744 switch (result_value.GetValueType())
1745 {
1746 case Value::eValueTypeHostAddress:
1747 case Value::eValueTypeFileAddress:
1748 // we don't do anything with these for now
1749 break;
1750 case Value::eValueTypeScalar:
1751 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1752 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1753 break;
1754 case Value::eValueTypeLoadAddress:
1755 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1756 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1757 break;
1758 }
Greg Clayton427f2902010-12-14 02:59:59 +00001759 }
1760 else
1761 {
1762 // Make sure we aren't just trying to see the value of a persistent
1763 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001764 lldb::ClangExpressionVariableSP persistent_var_sp;
1765 // Only check for persistent variables the expression starts with a '$'
1766 if (expr_cstr[0] == '$')
1767 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1768
Greg Clayton427f2902010-12-14 02:59:59 +00001769 if (persistent_var_sp)
1770 {
1771 result_valobj_sp = persistent_var_sp->GetValueObject ();
1772 execution_results = eExecutionCompleted;
1773 }
1774 else
1775 {
1776 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001777
Greg Clayton427f2902010-12-14 02:59:59 +00001778 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001779 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001780 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001781 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001782 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001783 expr_cstr,
1784 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001785 result_valobj_sp,
1786 single_thread_timeout_usec);
Greg Clayton427f2902010-12-14 02:59:59 +00001787 }
1788 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001789
1790 m_suppress_stop_hooks = old_suppress_value;
1791
Greg Clayton427f2902010-12-14 02:59:59 +00001792 return execution_results;
1793}
1794
Greg Claytonc0fa5332011-05-22 22:46:53 +00001795lldb::addr_t
1796Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1797{
1798 addr_t code_addr = load_addr;
1799 switch (m_arch.GetMachine())
1800 {
1801 case llvm::Triple::arm:
1802 case llvm::Triple::thumb:
1803 switch (addr_class)
1804 {
1805 case eAddressClassData:
1806 case eAddressClassDebug:
1807 return LLDB_INVALID_ADDRESS;
1808
1809 case eAddressClassUnknown:
1810 case eAddressClassInvalid:
1811 case eAddressClassCode:
1812 case eAddressClassCodeAlternateISA:
1813 case eAddressClassRuntime:
1814 // Check if bit zero it no set?
1815 if ((code_addr & 1ull) == 0)
1816 {
1817 // Bit zero isn't set, check if the address is a multiple of 2?
1818 if (code_addr & 2ull)
1819 {
1820 // The address is a multiple of 2 so it must be thumb, set bit zero
1821 code_addr |= 1ull;
1822 }
1823 else if (addr_class == eAddressClassCodeAlternateISA)
1824 {
1825 // We checked the address and the address claims to be the alternate ISA
1826 // which means thumb, so set bit zero.
1827 code_addr |= 1ull;
1828 }
1829 }
1830 break;
1831 }
1832 break;
1833
1834 default:
1835 break;
1836 }
1837 return code_addr;
1838}
1839
1840lldb::addr_t
1841Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1842{
1843 addr_t opcode_addr = load_addr;
1844 switch (m_arch.GetMachine())
1845 {
1846 case llvm::Triple::arm:
1847 case llvm::Triple::thumb:
1848 switch (addr_class)
1849 {
1850 case eAddressClassData:
1851 case eAddressClassDebug:
1852 return LLDB_INVALID_ADDRESS;
1853
1854 case eAddressClassInvalid:
1855 case eAddressClassUnknown:
1856 case eAddressClassCode:
1857 case eAddressClassCodeAlternateISA:
1858 case eAddressClassRuntime:
1859 opcode_addr &= ~(1ull);
1860 break;
1861 }
1862 break;
1863
1864 default:
1865 break;
1866 }
1867 return opcode_addr;
1868}
1869
Jim Inghamd60d94a2011-03-11 03:53:59 +00001870lldb::user_id_t
1871Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1872{
1873 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001874 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001875 m_stop_hooks[new_uid] = new_hook_sp;
1876 return new_uid;
1877}
1878
1879bool
1880Target::RemoveStopHookByID (lldb::user_id_t user_id)
1881{
1882 size_t num_removed;
1883 num_removed = m_stop_hooks.erase (user_id);
1884 if (num_removed == 0)
1885 return false;
1886 else
1887 return true;
1888}
1889
1890void
1891Target::RemoveAllStopHooks ()
1892{
1893 m_stop_hooks.clear();
1894}
1895
1896Target::StopHookSP
1897Target::GetStopHookByID (lldb::user_id_t user_id)
1898{
1899 StopHookSP found_hook;
1900
1901 StopHookCollection::iterator specified_hook_iter;
1902 specified_hook_iter = m_stop_hooks.find (user_id);
1903 if (specified_hook_iter != m_stop_hooks.end())
1904 found_hook = (*specified_hook_iter).second;
1905 return found_hook;
1906}
1907
1908bool
1909Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1910{
1911 StopHookCollection::iterator specified_hook_iter;
1912 specified_hook_iter = m_stop_hooks.find (user_id);
1913 if (specified_hook_iter == m_stop_hooks.end())
1914 return false;
1915
1916 (*specified_hook_iter).second->SetIsActive (active_state);
1917 return true;
1918}
1919
1920void
1921Target::SetAllStopHooksActiveState (bool active_state)
1922{
1923 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1924 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1925 {
1926 (*pos).second->SetIsActive (active_state);
1927 }
1928}
1929
1930void
1931Target::RunStopHooks ()
1932{
Jim Ingham3613ae12011-05-12 02:06:14 +00001933 if (m_suppress_stop_hooks)
1934 return;
1935
Jim Inghamd60d94a2011-03-11 03:53:59 +00001936 if (!m_process_sp)
1937 return;
1938
1939 if (m_stop_hooks.empty())
1940 return;
1941
1942 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1943
1944 // If there aren't any active stop hooks, don't bother either:
1945 bool any_active_hooks = false;
1946 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1947 {
1948 if ((*pos).second->IsActive())
1949 {
1950 any_active_hooks = true;
1951 break;
1952 }
1953 }
1954 if (!any_active_hooks)
1955 return;
1956
1957 CommandReturnObject result;
1958
1959 std::vector<ExecutionContext> exc_ctx_with_reasons;
1960 std::vector<SymbolContext> sym_ctx_with_reasons;
1961
1962 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1963 size_t num_threads = cur_threadlist.GetSize();
1964 for (size_t i = 0; i < num_threads; i++)
1965 {
1966 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1967 if (cur_thread_sp->ThreadStoppedForAReason())
1968 {
1969 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1970 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1971 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1972 }
1973 }
1974
1975 // If no threads stopped for a reason, don't run the stop-hooks.
1976 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1977 if (num_exe_ctx == 0)
1978 return;
1979
Jim Inghame5ed8e92011-06-02 23:58:26 +00001980 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1981 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001982
1983 bool keep_going = true;
1984 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001985 bool print_hook_header;
1986 bool print_thread_header;
1987
1988 if (num_exe_ctx == 1)
1989 print_thread_header = false;
1990 else
1991 print_thread_header = true;
1992
1993 if (m_stop_hooks.size() == 1)
1994 print_hook_header = false;
1995 else
1996 print_hook_header = true;
1997
Jim Inghamd60d94a2011-03-11 03:53:59 +00001998 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1999 {
2000 // result.Clear();
2001 StopHookSP cur_hook_sp = (*pos).second;
2002 if (!cur_hook_sp->IsActive())
2003 continue;
2004
2005 bool any_thread_matched = false;
2006 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2007 {
2008 if ((cur_hook_sp->GetSpecifier () == NULL
2009 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2010 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00002011 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002012 {
2013 if (!hooks_ran)
2014 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00002015 hooks_ran = true;
2016 }
Jim Inghamc54840c2011-03-22 01:47:27 +00002017 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002018 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002019 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2020 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2021 NULL);
2022 if (cmd)
2023 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
2024 else
2025 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002026 any_thread_matched = true;
2027 }
2028
Jim Inghamc54840c2011-03-22 01:47:27 +00002029 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002030 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002031
2032 bool stop_on_continue = true;
2033 bool stop_on_error = true;
2034 bool echo_commands = false;
2035 bool print_results = true;
2036 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002037 &exc_ctx_with_reasons[i],
2038 stop_on_continue,
2039 stop_on_error,
2040 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002041 print_results,
2042 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002043 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002044
2045 // If the command started the target going again, we should bag out of
2046 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002047 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2048 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002049 {
Greg Clayton444e35b2011-10-19 18:09:39 +00002050 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002051 keep_going = false;
2052 }
2053 }
2054 }
2055 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002056
Caroline Tice4a348082011-05-02 20:41:46 +00002057 result.GetImmediateOutputStream()->Flush();
2058 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002059}
2060
Greg Claytonbbea1332011-07-08 00:48:09 +00002061
Jim Inghamd60d94a2011-03-11 03:53:59 +00002062//--------------------------------------------------------------
2063// class Target::StopHook
2064//--------------------------------------------------------------
2065
2066
2067Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2068 UserID (uid),
2069 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002070 m_commands (),
2071 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002072 m_thread_spec_ap(NULL),
2073 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002074{
2075}
2076
2077Target::StopHook::StopHook (const StopHook &rhs) :
2078 UserID (rhs.GetID()),
2079 m_target_sp (rhs.m_target_sp),
2080 m_commands (rhs.m_commands),
2081 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002082 m_thread_spec_ap (NULL),
2083 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002084{
2085 if (rhs.m_thread_spec_ap.get() != NULL)
2086 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2087}
2088
2089
2090Target::StopHook::~StopHook ()
2091{
2092}
2093
2094void
2095Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2096{
2097 m_thread_spec_ap.reset (specifier);
2098}
2099
2100
2101void
2102Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2103{
2104 int indent_level = s->GetIndentLevel();
2105
2106 s->SetIndentLevel(indent_level + 2);
2107
Greg Clayton444e35b2011-10-19 18:09:39 +00002108 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002109 if (m_active)
2110 s->Indent ("State: enabled\n");
2111 else
2112 s->Indent ("State: disabled\n");
2113
2114 if (m_specifier_sp)
2115 {
2116 s->Indent();
2117 s->PutCString ("Specifier:\n");
2118 s->SetIndentLevel (indent_level + 4);
2119 m_specifier_sp->GetDescription (s, level);
2120 s->SetIndentLevel (indent_level + 2);
2121 }
2122
2123 if (m_thread_spec_ap.get() != NULL)
2124 {
2125 StreamString tmp;
2126 s->Indent("Thread:\n");
2127 m_thread_spec_ap->GetDescription (&tmp, level);
2128 s->SetIndentLevel (indent_level + 4);
2129 s->Indent (tmp.GetData());
2130 s->PutCString ("\n");
2131 s->SetIndentLevel (indent_level + 2);
2132 }
2133
2134 s->Indent ("Commands: \n");
2135 s->SetIndentLevel (indent_level + 4);
2136 uint32_t num_commands = m_commands.GetSize();
2137 for (uint32_t i = 0; i < num_commands; i++)
2138 {
2139 s->Indent(m_commands.GetStringAtIndex(i));
2140 s->PutCString ("\n");
2141 }
2142 s->SetIndentLevel (indent_level);
2143}
2144
2145
Caroline Tice5bc8c972010-09-20 20:44:43 +00002146//--------------------------------------------------------------
2147// class Target::SettingsController
2148//--------------------------------------------------------------
2149
2150Target::SettingsController::SettingsController () :
2151 UserSettingsController ("target", Debugger::GetSettingsController()),
2152 m_default_architecture ()
2153{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002154}
2155
2156Target::SettingsController::~SettingsController ()
2157{
2158}
2159
2160lldb::InstanceSettingsSP
2161Target::SettingsController::CreateInstanceSettings (const char *instance_name)
2162{
Greg Clayton334d33a2012-01-30 07:41:31 +00002163 lldb::InstanceSettingsSP new_settings_sp (new TargetInstanceSettings (GetSettingsController(),
2164 false,
2165 instance_name));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002166 return new_settings_sp;
2167}
2168
Caroline Tice5bc8c972010-09-20 20:44:43 +00002169
Greg Claytonabb33022011-11-08 02:43:13 +00002170#define TSC_DEFAULT_ARCH "default-arch"
2171#define TSC_EXPR_PREFIX "expr-prefix"
2172#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Enrico Granatadba1de82012-03-27 02:35:13 +00002173#define TSC_ENABLE_SYNTHETIC "enable-synthetic-value"
Greg Claytonabb33022011-11-08 02:43:13 +00002174#define TSC_SKIP_PROLOGUE "skip-prologue"
2175#define TSC_SOURCE_MAP "source-map"
Greg Clayton9ce95382012-02-13 23:10:39 +00002176#define TSC_EXE_SEARCH_PATHS "exec-search-paths"
Greg Claytonabb33022011-11-08 02:43:13 +00002177#define TSC_MAX_CHILDREN "max-children-count"
2178#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
2179#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
2180#define TSC_RUN_ARGS "run-args"
2181#define TSC_ENV_VARS "env-vars"
2182#define TSC_INHERIT_ENV "inherit-env"
2183#define TSC_STDIN_PATH "input-path"
2184#define TSC_STDOUT_PATH "output-path"
2185#define TSC_STDERR_PATH "error-path"
2186#define TSC_DISABLE_ASLR "disable-aslr"
2187#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytond284b662011-02-18 01:44:25 +00002188
2189
2190static const ConstString &
2191GetSettingNameForDefaultArch ()
2192{
2193 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00002194 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002195}
2196
Greg Claytond284b662011-02-18 01:44:25 +00002197static const ConstString &
2198GetSettingNameForExpressionPrefix ()
2199{
2200 static ConstString g_const_string (TSC_EXPR_PREFIX);
2201 return g_const_string;
2202}
2203
2204static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00002205GetSettingNameForPreferDynamicValue ()
2206{
2207 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2208 return g_const_string;
2209}
2210
Greg Claytonff44ab42011-04-23 02:04:55 +00002211static const ConstString &
Enrico Granatadba1de82012-03-27 02:35:13 +00002212GetSettingNameForEnableSyntheticValue ()
2213{
2214 static ConstString g_const_string (TSC_ENABLE_SYNTHETIC);
2215 return g_const_string;
2216}
2217
2218static const ConstString &
Greg Claytonff44ab42011-04-23 02:04:55 +00002219GetSettingNameForSourcePathMap ()
2220{
2221 static ConstString g_const_string (TSC_SOURCE_MAP);
2222 return g_const_string;
2223}
Greg Claytond284b662011-02-18 01:44:25 +00002224
Greg Clayton17cd9952011-04-22 03:55:06 +00002225static const ConstString &
Greg Clayton9ce95382012-02-13 23:10:39 +00002226GetSettingNameForExecutableSearchPaths ()
2227{
2228 static ConstString g_const_string (TSC_EXE_SEARCH_PATHS);
2229 return g_const_string;
2230}
2231
2232static const ConstString &
Greg Clayton17cd9952011-04-22 03:55:06 +00002233GetSettingNameForSkipPrologue ()
2234{
2235 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2236 return g_const_string;
2237}
2238
Enrico Granata018921d2011-08-12 02:00:06 +00002239static const ConstString &
2240GetSettingNameForMaxChildren ()
2241{
2242 static ConstString g_const_string (TSC_MAX_CHILDREN);
2243 return g_const_string;
2244}
Greg Clayton17cd9952011-04-22 03:55:06 +00002245
Enrico Granata91544802011-09-06 19:20:51 +00002246static const ConstString &
2247GetSettingNameForMaxStringSummaryLength ()
2248{
2249 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2250 return g_const_string;
2251}
Greg Clayton17cd9952011-04-22 03:55:06 +00002252
Jim Ingham7089d8a2011-10-28 23:14:11 +00002253static const ConstString &
2254GetSettingNameForPlatformAvoid ()
2255{
2256 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2257 return g_const_string;
2258}
2259
Greg Claytonabb33022011-11-08 02:43:13 +00002260const ConstString &
2261GetSettingNameForRunArgs ()
2262{
2263 static ConstString g_const_string (TSC_RUN_ARGS);
2264 return g_const_string;
2265}
2266
2267const ConstString &
2268GetSettingNameForEnvVars ()
2269{
2270 static ConstString g_const_string (TSC_ENV_VARS);
2271 return g_const_string;
2272}
2273
2274const ConstString &
2275GetSettingNameForInheritHostEnv ()
2276{
2277 static ConstString g_const_string (TSC_INHERIT_ENV);
2278 return g_const_string;
2279}
2280
2281const ConstString &
2282GetSettingNameForInputPath ()
2283{
2284 static ConstString g_const_string (TSC_STDIN_PATH);
2285 return g_const_string;
2286}
2287
2288const ConstString &
2289GetSettingNameForOutputPath ()
2290{
2291 static ConstString g_const_string (TSC_STDOUT_PATH);
2292 return g_const_string;
2293}
2294
2295const ConstString &
2296GetSettingNameForErrorPath ()
2297{
2298 static ConstString g_const_string (TSC_STDERR_PATH);
2299 return g_const_string;
2300}
2301
2302const ConstString &
2303GetSettingNameForDisableASLR ()
2304{
2305 static ConstString g_const_string (TSC_DISABLE_ASLR);
2306 return g_const_string;
2307}
2308
2309const ConstString &
2310GetSettingNameForDisableSTDIO ()
2311{
2312 static ConstString g_const_string (TSC_DISABLE_STDIO);
2313 return g_const_string;
2314}
Jim Ingham7089d8a2011-10-28 23:14:11 +00002315
Caroline Tice5bc8c972010-09-20 20:44:43 +00002316bool
2317Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2318 const char *index_value,
2319 const char *value,
2320 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002321 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002322 Error&err)
2323{
Greg Claytond284b662011-02-18 01:44:25 +00002324 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002325 {
Greg Claytonb170aee2012-05-08 01:45:38 +00002326 m_default_architecture.SetTriple (value);
Greg Clayton940b1032011-02-23 00:35:02 +00002327 if (!m_default_architecture.IsValid())
2328 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002329 }
2330 return true;
2331}
2332
2333
2334bool
2335Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2336 StringList &value,
2337 Error &err)
2338{
Greg Claytond284b662011-02-18 01:44:25 +00002339 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002340 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002341 // If the arch is invalid (the default), don't show a string for it
2342 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002343 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002344 return true;
2345 }
2346 else
2347 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2348
2349 return false;
2350}
2351
2352//--------------------------------------------------------------
2353// class TargetInstanceSettings
2354//--------------------------------------------------------------
2355
Greg Clayton638351a2010-12-04 00:10:17 +00002356TargetInstanceSettings::TargetInstanceSettings
2357(
Greg Clayton334d33a2012-01-30 07:41:31 +00002358 const lldb::UserSettingsControllerSP &owner_sp,
Greg Clayton638351a2010-12-04 00:10:17 +00002359 bool live_instance,
2360 const char *name
2361) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002362 InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002363 m_expr_prefix_file (),
Sean Callanane0b7f942011-11-16 01:54:57 +00002364 m_expr_prefix_contents (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002365 m_prefer_dynamic_value (2),
Enrico Granatadba1de82012-03-27 02:35:13 +00002366 m_enable_synthetic_value(true, true),
Greg Claytonff44ab42011-04-23 02:04:55 +00002367 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002368 m_source_map (NULL, NULL),
Greg Clayton9ce95382012-02-13 23:10:39 +00002369 m_exe_search_paths (),
Enrico Granata91544802011-09-06 19:20:51 +00002370 m_max_children_display(256),
Jim Ingham7089d8a2011-10-28 23:14:11 +00002371 m_max_strlen_length(1024),
Greg Claytonabb33022011-11-08 02:43:13 +00002372 m_breakpoints_use_platform_avoid (true, true),
2373 m_run_args (),
2374 m_env_vars (),
2375 m_input_path (),
2376 m_output_path (),
2377 m_error_path (),
2378 m_disable_aslr (true),
2379 m_disable_stdio (false),
2380 m_inherit_host_env (true),
2381 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002382{
2383 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2384 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2385 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2386 // This is true for CreateInstanceName() too.
2387
2388 if (GetInstanceName () == InstanceSettings::InvalidName())
2389 {
2390 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Clayton334d33a2012-01-30 07:41:31 +00002391 owner_sp->RegisterInstanceSettings (this);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002392 }
2393
2394 if (live_instance)
2395 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002396 const lldb::InstanceSettingsSP &pending_settings = owner_sp->FindPendingSettings (m_instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002397 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002398 }
2399}
2400
2401TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002402 InstanceSettings (Target::GetSettingsController(), CreateInstanceName().AsCString()),
Greg Claytonff44ab42011-04-23 02:04:55 +00002403 m_expr_prefix_file (rhs.m_expr_prefix_file),
Sean Callanane0b7f942011-11-16 01:54:57 +00002404 m_expr_prefix_contents (rhs.m_expr_prefix_contents),
Greg Claytonff44ab42011-04-23 02:04:55 +00002405 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
Enrico Granatadba1de82012-03-27 02:35:13 +00002406 m_enable_synthetic_value(rhs.m_enable_synthetic_value),
Greg Claytonff44ab42011-04-23 02:04:55 +00002407 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002408 m_source_map (rhs.m_source_map),
Greg Clayton9ce95382012-02-13 23:10:39 +00002409 m_exe_search_paths (rhs.m_exe_search_paths),
Greg Claytonabb33022011-11-08 02:43:13 +00002410 m_max_children_display (rhs.m_max_children_display),
2411 m_max_strlen_length (rhs.m_max_strlen_length),
2412 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2413 m_run_args (rhs.m_run_args),
2414 m_env_vars (rhs.m_env_vars),
2415 m_input_path (rhs.m_input_path),
2416 m_output_path (rhs.m_output_path),
2417 m_error_path (rhs.m_error_path),
2418 m_disable_aslr (rhs.m_disable_aslr),
2419 m_disable_stdio (rhs.m_disable_stdio),
2420 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002421{
2422 if (m_instance_name != InstanceSettings::GetDefaultName())
2423 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002424 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
2425 if (owner_sp)
2426 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name),false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002427 }
2428}
2429
2430TargetInstanceSettings::~TargetInstanceSettings ()
2431{
2432}
2433
2434TargetInstanceSettings&
2435TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2436{
2437 if (this != &rhs)
2438 {
Greg Claytonabb33022011-11-08 02:43:13 +00002439 m_expr_prefix_file = rhs.m_expr_prefix_file;
Sean Callanane0b7f942011-11-16 01:54:57 +00002440 m_expr_prefix_contents = rhs.m_expr_prefix_contents;
Greg Claytonabb33022011-11-08 02:43:13 +00002441 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
Enrico Granatadba1de82012-03-27 02:35:13 +00002442 m_enable_synthetic_value = rhs.m_enable_synthetic_value;
Greg Claytonabb33022011-11-08 02:43:13 +00002443 m_skip_prologue = rhs.m_skip_prologue;
2444 m_source_map = rhs.m_source_map;
Greg Clayton9ce95382012-02-13 23:10:39 +00002445 m_exe_search_paths = rhs.m_exe_search_paths;
Greg Claytonabb33022011-11-08 02:43:13 +00002446 m_max_children_display = rhs.m_max_children_display;
2447 m_max_strlen_length = rhs.m_max_strlen_length;
2448 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2449 m_run_args = rhs.m_run_args;
2450 m_env_vars = rhs.m_env_vars;
2451 m_input_path = rhs.m_input_path;
2452 m_output_path = rhs.m_output_path;
2453 m_error_path = rhs.m_error_path;
2454 m_disable_aslr = rhs.m_disable_aslr;
2455 m_disable_stdio = rhs.m_disable_stdio;
2456 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002457 }
2458
2459 return *this;
2460}
2461
Caroline Tice5bc8c972010-09-20 20:44:43 +00002462void
2463TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2464 const char *index_value,
2465 const char *value,
2466 const ConstString &instance_name,
2467 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002468 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002469 Error &err,
2470 bool pending)
2471{
Greg Claytond284b662011-02-18 01:44:25 +00002472 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002473 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002474 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2475 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002476 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002477 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002478 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002479 default:
2480 break;
2481 case eVarSetOperationAssign:
2482 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002483 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002484 m_expr_prefix_contents.clear();
2485
Greg Claytonff44ab42011-04-23 02:04:55 +00002486 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2487 {
2488 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002489 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002490 return;
2491 }
2492
Greg Clayton4b23ab32012-01-06 02:01:06 +00002493 DataBufferSP file_data_sp (m_expr_prefix_file.GetCurrentValue().ReadFileContents(0, SIZE_MAX, &err));
Sean Callanane0b7f942011-11-16 01:54:57 +00002494
Greg Clayton4b23ab32012-01-06 02:01:06 +00002495 if (err.Success())
Greg Claytonff44ab42011-04-23 02:04:55 +00002496 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002497 if (file_data_sp && file_data_sp->GetByteSize() > 0)
2498 {
2499 m_expr_prefix_contents.assign((const char*)file_data_sp->GetBytes(), file_data_sp->GetByteSize());
2500 }
2501 else
2502 {
2503 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
2504 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002505 }
Sean Callanan77e93942010-10-29 00:29:03 +00002506 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002507 break;
2508 case eVarSetOperationClear:
Sean Callanane0b7f942011-11-16 01:54:57 +00002509 m_expr_prefix_contents.clear();
Sean Callanan77e93942010-10-29 00:29:03 +00002510 }
Sean Callanan77e93942010-10-29 00:29:03 +00002511 }
2512 }
Jim Inghame41494a2011-04-16 00:01:13 +00002513 else if (var_name == GetSettingNameForPreferDynamicValue())
2514 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002515 int new_value;
2516 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2517 if (err.Success())
2518 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002519 }
Enrico Granatadba1de82012-03-27 02:35:13 +00002520 else if (var_name == GetSettingNameForEnableSyntheticValue())
2521 {
2522 bool ok;
2523 bool new_value = Args::StringToBoolean(value, true, &ok);
2524 if (ok)
2525 m_enable_synthetic_value.SetCurrentValue(new_value);
2526 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002527 else if (var_name == GetSettingNameForSkipPrologue())
2528 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002529 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2530 }
Enrico Granata018921d2011-08-12 02:00:06 +00002531 else if (var_name == GetSettingNameForMaxChildren())
2532 {
2533 bool ok;
2534 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2535 if (ok)
2536 m_max_children_display = new_value;
2537 }
Enrico Granata91544802011-09-06 19:20:51 +00002538 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2539 {
2540 bool ok;
2541 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2542 if (ok)
2543 m_max_strlen_length = new_value;
2544 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002545 else if (var_name == GetSettingNameForExecutableSearchPaths())
2546 {
2547 switch (op)
2548 {
2549 case eVarSetOperationReplace:
2550 case eVarSetOperationInsertBefore:
2551 case eVarSetOperationInsertAfter:
2552 case eVarSetOperationRemove:
2553 default:
2554 break;
2555 case eVarSetOperationAssign:
2556 m_exe_search_paths.Clear();
2557 // Fall through to append....
2558 case eVarSetOperationAppend:
2559 {
2560 Args args(value);
2561 const uint32_t argc = args.GetArgumentCount();
2562 if (argc > 0)
2563 {
2564 const char *exe_search_path_dir;
2565 for (uint32_t idx = 0; (exe_search_path_dir = args.GetArgumentAtIndex(idx)) != NULL; ++idx)
2566 {
2567 FileSpec file_spec;
2568 file_spec.GetDirectory().SetCString(exe_search_path_dir);
2569 FileSpec::FileType file_type = file_spec.GetFileType();
2570 if (file_type == FileSpec::eFileTypeDirectory || file_type == FileSpec::eFileTypeInvalid)
2571 {
2572 m_exe_search_paths.Append(file_spec);
2573 }
2574 else
2575 {
2576 err.SetErrorStringWithFormat("executable search path '%s' exists, but it does not resolve to a directory", exe_search_path_dir);
2577 }
2578 }
2579 }
2580 }
2581 break;
2582
2583 case eVarSetOperationClear:
2584 m_exe_search_paths.Clear();
2585 break;
2586 }
2587 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002588 else if (var_name == GetSettingNameForSourcePathMap ())
2589 {
2590 switch (op)
2591 {
2592 case eVarSetOperationReplace:
2593 case eVarSetOperationInsertBefore:
2594 case eVarSetOperationInsertAfter:
2595 case eVarSetOperationRemove:
2596 default:
2597 break;
2598 case eVarSetOperationAssign:
2599 m_source_map.Clear(true);
2600 // Fall through to append....
2601 case eVarSetOperationAppend:
2602 {
2603 Args args(value);
2604 const uint32_t argc = args.GetArgumentCount();
2605 if (argc & 1 || argc == 0)
2606 {
2607 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2608 }
2609 else
2610 {
2611 char resolved_new_path[PATH_MAX];
2612 FileSpec file_spec;
2613 const char *old_path;
2614 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2615 {
2616 const char *new_path = args.GetArgumentAtIndex(idx+1);
2617 assert (new_path); // We have an even number of paths, this shouldn't happen!
2618
2619 file_spec.SetFile(new_path, true);
2620 if (file_spec.Exists())
2621 {
2622 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2623 {
2624 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2625 return;
2626 }
2627 }
2628 else
2629 {
2630 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2631 return;
2632 }
2633 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2634 }
2635 }
2636 }
2637 break;
2638
2639 case eVarSetOperationClear:
2640 m_source_map.Clear(true);
2641 break;
2642 }
Jim Inghame41494a2011-04-16 00:01:13 +00002643 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002644 else if (var_name == GetSettingNameForPlatformAvoid ())
2645 {
2646 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2647 }
Greg Claytonabb33022011-11-08 02:43:13 +00002648 else if (var_name == GetSettingNameForRunArgs())
2649 {
2650 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2651 }
2652 else if (var_name == GetSettingNameForEnvVars())
2653 {
2654 // This is nice for local debugging, but it is isn't correct for
2655 // remote debugging. We need to stop process.env-vars from being
2656 // populated with the host environment and add this as a launch option
2657 // and get the correct environment from the Target's platform.
2658 // GetHostEnvironmentIfNeeded ();
2659 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2660 }
2661 else if (var_name == GetSettingNameForInputPath())
2662 {
2663 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2664 }
2665 else if (var_name == GetSettingNameForOutputPath())
2666 {
2667 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2668 }
2669 else if (var_name == GetSettingNameForErrorPath())
2670 {
2671 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2672 }
2673 else if (var_name == GetSettingNameForDisableASLR())
2674 {
2675 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2676 }
2677 else if (var_name == GetSettingNameForDisableSTDIO ())
2678 {
2679 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2680 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002681}
2682
2683void
Greg Claytond284b662011-02-18 01:44:25 +00002684TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002685{
Sean Callanan77e93942010-10-29 00:29:03 +00002686 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2687
2688 if (!new_settings_ptr)
2689 return;
2690
Greg Claytonabb33022011-11-08 02:43:13 +00002691 *this = *new_settings_ptr;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002692}
2693
Caroline Ticebcb5b452010-09-20 21:37:42 +00002694bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002695TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2696 const ConstString &var_name,
2697 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002698 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002699{
Greg Claytond284b662011-02-18 01:44:25 +00002700 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002701 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002702 char path[PATH_MAX];
2703 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2704 if (path_len > 0)
2705 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002706 }
Jim Inghame41494a2011-04-16 00:01:13 +00002707 else if (var_name == GetSettingNameForPreferDynamicValue())
2708 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002709 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002710 }
Enrico Granatadba1de82012-03-27 02:35:13 +00002711 else if (var_name == GetSettingNameForEnableSyntheticValue())
2712 {
2713 if (m_skip_prologue)
2714 value.AppendString ("true");
2715 else
2716 value.AppendString ("false");
2717 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002718 else if (var_name == GetSettingNameForSkipPrologue())
2719 {
2720 if (m_skip_prologue)
2721 value.AppendString ("true");
2722 else
2723 value.AppendString ("false");
2724 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002725 else if (var_name == GetSettingNameForExecutableSearchPaths())
2726 {
2727 if (m_exe_search_paths.GetSize())
2728 {
2729 for (size_t i = 0, n = m_exe_search_paths.GetSize(); i < n; ++i)
2730 {
2731 value.AppendString(m_exe_search_paths.GetFileSpecAtIndex (i).GetDirectory().AsCString());
2732 }
2733 }
2734 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002735 else if (var_name == GetSettingNameForSourcePathMap ())
2736 {
Johnny Chen931449e2011-12-12 21:59:28 +00002737 if (m_source_map.GetSize())
2738 {
2739 size_t i;
2740 for (i = 0; i < m_source_map.GetSize(); ++i) {
2741 StreamString sstr;
2742 m_source_map.Dump(&sstr, i);
2743 value.AppendString(sstr.GetData());
2744 }
2745 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002746 }
Enrico Granata018921d2011-08-12 02:00:06 +00002747 else if (var_name == GetSettingNameForMaxChildren())
2748 {
2749 StreamString count_str;
2750 count_str.Printf ("%d", m_max_children_display);
2751 value.AppendString (count_str.GetData());
2752 }
Enrico Granata91544802011-09-06 19:20:51 +00002753 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2754 {
2755 StreamString count_str;
2756 count_str.Printf ("%d", m_max_strlen_length);
2757 value.AppendString (count_str.GetData());
2758 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002759 else if (var_name == GetSettingNameForPlatformAvoid())
2760 {
2761 if (m_breakpoints_use_platform_avoid)
2762 value.AppendString ("true");
2763 else
2764 value.AppendString ("false");
2765 }
Greg Claytonabb33022011-11-08 02:43:13 +00002766 else if (var_name == GetSettingNameForRunArgs())
2767 {
2768 if (m_run_args.GetArgumentCount() > 0)
2769 {
2770 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2771 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2772 }
2773 }
2774 else if (var_name == GetSettingNameForEnvVars())
2775 {
2776 GetHostEnvironmentIfNeeded ();
2777
2778 if (m_env_vars.size() > 0)
2779 {
2780 std::map<std::string, std::string>::iterator pos;
2781 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2782 {
2783 StreamString value_str;
2784 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2785 value.AppendString (value_str.GetData());
2786 }
2787 }
2788 }
2789 else if (var_name == GetSettingNameForInputPath())
2790 {
2791 value.AppendString (m_input_path.c_str());
2792 }
2793 else if (var_name == GetSettingNameForOutputPath())
2794 {
2795 value.AppendString (m_output_path.c_str());
2796 }
2797 else if (var_name == GetSettingNameForErrorPath())
2798 {
2799 value.AppendString (m_error_path.c_str());
2800 }
2801 else if (var_name == GetSettingNameForInheritHostEnv())
2802 {
2803 if (m_inherit_host_env)
2804 value.AppendString ("true");
2805 else
2806 value.AppendString ("false");
2807 }
2808 else if (var_name == GetSettingNameForDisableASLR())
2809 {
2810 if (m_disable_aslr)
2811 value.AppendString ("true");
2812 else
2813 value.AppendString ("false");
2814 }
2815 else if (var_name == GetSettingNameForDisableSTDIO())
2816 {
2817 if (m_disable_stdio)
2818 value.AppendString ("true");
2819 else
2820 value.AppendString ("false");
2821 }
Sean Callanan77e93942010-10-29 00:29:03 +00002822 else
2823 {
2824 if (err)
2825 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2826 return false;
2827 }
Sean Callanan77e93942010-10-29 00:29:03 +00002828 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002829}
2830
Greg Claytonabb33022011-11-08 02:43:13 +00002831void
2832Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2833{
2834 if (m_inherit_host_env && !m_got_host_env)
2835 {
2836 m_got_host_env = true;
2837 StringList host_env;
2838 const size_t host_env_count = Host::GetEnvironment (host_env);
2839 for (size_t idx=0; idx<host_env_count; idx++)
2840 {
2841 const char *env_entry = host_env.GetStringAtIndex (idx);
2842 if (env_entry)
2843 {
2844 const char *equal_pos = ::strchr(env_entry, '=');
2845 if (equal_pos)
2846 {
2847 std::string key (env_entry, equal_pos - env_entry);
2848 std::string value (equal_pos + 1);
2849 if (m_env_vars.find (key) == m_env_vars.end())
2850 m_env_vars[key] = value;
2851 }
2852 }
2853 }
2854 }
2855}
2856
2857
2858size_t
2859Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2860{
2861 GetHostEnvironmentIfNeeded ();
2862
2863 dictionary::const_iterator pos, end = m_env_vars.end();
2864 for (pos = m_env_vars.begin(); pos != end; ++pos)
2865 {
2866 std::string env_var_equal_value (pos->first);
2867 env_var_equal_value.append(1, '=');
2868 env_var_equal_value.append (pos->second);
2869 env.AppendArgument (env_var_equal_value.c_str());
2870 }
2871 return env.GetArgumentCount();
2872}
2873
2874
Caroline Tice5bc8c972010-09-20 20:44:43 +00002875const ConstString
2876TargetInstanceSettings::CreateInstanceName ()
2877{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002878 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002879 static int instance_count = 1;
2880
Caroline Tice5bc8c972010-09-20 20:44:43 +00002881 sstr.Printf ("target_%d", instance_count);
2882 ++instance_count;
2883
2884 const ConstString ret_val (sstr.GetData());
2885 return ret_val;
2886}
2887
2888//--------------------------------------------------
2889// Target::SettingsController Variable Tables
2890//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002891OptionEnumValueElement
2892TargetInstanceSettings::g_dynamic_value_types[] =
2893{
Greg Clayton577fbc32011-05-30 00:39:48 +00002894{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2895{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2896{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002897{ 0, NULL, NULL }
2898};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002899
2900SettingEntry
2901Target::SettingsController::global_settings_table[] =
2902{
Greg Claytond284b662011-02-18 01:44:25 +00002903 // var-name var-type default enum init'd hidden help-text
2904 // ================= ================== =========== ==== ====== ====== =========================================================================
2905 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2906 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2907};
2908
Caroline Tice5bc8c972010-09-20 20:44:43 +00002909SettingEntry
2910Target::SettingsController::instance_settings_table[] =
2911{
Enrico Granata91544802011-09-06 19:20:51 +00002912 // var-name var-type default enum init'd hidden help-text
2913 // ================= ================== =============== ======================= ====== ====== =========================================================================
2914 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2915 { 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 +00002916 { TSC_ENABLE_SYNTHETIC , eSetVarTypeBoolean, "true" , NULL, false, false, "Should synthetic values be used by default whenever available." },
Enrico Granata91544802011-09-06 19:20:51 +00002917 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
Jim Ingham3092c1e2012-07-18 18:42:57 +00002918 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings used to track the change of location between a source file when built, and "
2919 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2920 "some part (starting at the root) of the path to the file when it was built, "
2921 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2922 "Each element of the array is checked in order and the first one that results in a match wins." },
Greg Clayton9ce95382012-02-13 23:10:39 +00002923 { 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 +00002924 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2925 { 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 +00002926 { 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 +00002927 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2928 { 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." },
2929 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2930 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2931 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2932 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2933// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2934 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2935 { 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 +00002936 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002937};
Jim Ingham5a15e692012-02-16 06:50:00 +00002938
2939const ConstString &
2940Target::TargetEventData::GetFlavorString ()
2941{
2942 static ConstString g_flavor ("Target::TargetEventData");
2943 return g_flavor;
2944}
2945
2946const ConstString &
2947Target::TargetEventData::GetFlavor () const
2948{
2949 return TargetEventData::GetFlavorString ();
2950}
2951
2952Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2953 EventData(),
2954 m_target_sp (new_target_sp)
2955{
2956}
2957
2958Target::TargetEventData::~TargetEventData()
2959{
2960
2961}
2962
2963void
2964Target::TargetEventData::Dump (Stream *s) const
2965{
2966
2967}
2968
2969const TargetSP
2970Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2971{
2972 TargetSP target_sp;
2973
2974 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2975 if (data)
2976 target_sp = data->m_target_sp;
2977
2978 return target_sp;
2979}
2980
2981const Target::TargetEventData *
2982Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2983{
2984 if (event_ptr)
2985 {
2986 const EventData *event_data = event_ptr->GetData();
2987 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2988 return static_cast <const TargetEventData *> (event_ptr->GetData());
2989 }
2990 return NULL;
2991}
2992