blob: b9616039125ec085c1ad1d6685df04d5ca5d3868 [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"
Greg Clayton49ce8962012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000029#include "lldb/Core/Timer.h"
30#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000031#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000032#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000034#include "lldb/Interpreter/CommandInterpreter.h"
35#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chen3f883492012-06-04 23:19:54 +000036#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton73844aa2012-08-22 17:17:09 +000037#include "lldb/Interpreter/OptionValues.h"
38#include "lldb/Interpreter/Property.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039#include "lldb/lldb-private-log.h"
40#include "lldb/Symbol/ObjectFile.h"
41#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000042#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000043#include "lldb/Target/Thread.h"
44#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000045
46using namespace lldb;
47using namespace lldb_private;
48
Jim Ingham5a15e692012-02-16 06:50:00 +000049ConstString &
50Target::GetStaticBroadcasterClass ()
51{
52 static ConstString class_name ("lldb.target");
53 return class_name;
54}
55
Chris Lattner24943d22010-06-08 16:52:24 +000056//----------------------------------------------------------------------
57// Target constructor
58//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000059Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton73844aa2012-08-22 17:17:09 +000060 TargetProperties (this),
Jim Ingham94a5d0d2012-10-10 18:32:14 +000061 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton24bc5d92011-03-30 18:16:51 +000062 ExecutionContextScope (),
Greg Clayton63094e02010-06-23 01:19:29 +000063 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000064 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000065 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000066 m_arch (target_arch),
Enrico Granata146d9522012-11-08 02:22:02 +000067 m_images (this),
Greg Claytoneea26402010-09-14 23:36:40 +000068 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000069 m_breakpoint_list (false),
70 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000071 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000072 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000073 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000074 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000075 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000076 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000077 m_scratch_ast_source_ap (NULL),
78 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000079 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000080 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000081 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000082 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000083 m_suppress_stop_hooks (false),
84 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000085{
Greg Clayton49ce6822010-10-31 03:01:06 +000086 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
87 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
88 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000089
90 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000091
Greg Claytone005f2c2010-11-06 01:53:30 +000092 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000093 if (log)
94 log->Printf ("%p Target::Target()", this);
95}
96
97//----------------------------------------------------------------------
98// Destructor
99//----------------------------------------------------------------------
100Target::~Target()
101{
Greg Claytone005f2c2010-11-06 01:53:30 +0000102 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000103 if (log)
104 log->Printf ("%p Target::~Target()", this);
105 DeleteCurrentProcess ();
106}
107
108void
Caroline Tice7826c882010-10-26 03:11:13 +0000109Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000110{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000111// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000112 if (description_level != lldb::eDescriptionLevelBrief)
113 {
114 s->Indent();
115 s->PutCString("Target\n");
116 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000117 m_images.Dump(s);
118 m_breakpoint_list.Dump(s);
119 m_internal_breakpoint_list.Dump(s);
120 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000121 }
122 else
123 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000124 Module *exe_module = GetExecutableModulePointer();
125 if (exe_module)
126 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000127 else
128 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000129 }
Chris Lattner24943d22010-06-08 16:52:24 +0000130}
131
132void
133Target::DeleteCurrentProcess ()
134{
135 if (m_process_sp.get())
136 {
Greg Clayton49480b12010-09-14 23:52:43 +0000137 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000138 if (m_process_sp->IsAlive())
139 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000140
141 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000142
143 // Do any cleanup of the target we need to do between process instances.
144 // NB It is better to do this before destroying the process in case the
145 // clean up needs some help from the process.
146 m_breakpoint_list.ClearAllBreakpointSites();
147 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000148 // Disable watchpoints just on the debugger side.
Johnny Chen116a5cd2012-02-25 06:44:30 +0000149 Mutex::Locker locker;
150 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000151 DisableAllWatchpoints(false);
Johnny Chen116a5cd2012-02-25 06:44:30 +0000152 ClearAllWatchpointHitCounts();
Chris Lattner24943d22010-06-08 16:52:24 +0000153 m_process_sp.reset();
154 }
155}
156
157const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000158Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000159{
160 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000161 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000162 return m_process_sp;
163}
164
165const lldb::ProcessSP &
166Target::GetProcessSP () const
167{
168 return m_process_sp;
169}
170
Greg Clayton153ccd72011-08-10 02:10:13 +0000171void
172Target::Destroy()
173{
174 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000175 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000176 DeleteCurrentProcess ();
177 m_platform_sp.reset();
178 m_arch.Clear();
179 m_images.Clear();
180 m_section_load_list.Clear();
181 const bool notify = false;
182 m_breakpoint_list.RemoveAll(notify);
183 m_internal_breakpoint_list.RemoveAll(notify);
184 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000185 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000186 m_search_filter_sp.reset();
187 m_image_search_paths.Clear(notify);
188 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000189 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000190 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000191 m_persistent_variables.Clear();
192 m_stop_hooks.clear();
193 m_stop_hook_next_id = 0;
194 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000195 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000196}
197
198
Chris Lattner24943d22010-06-08 16:52:24 +0000199BreakpointList &
200Target::GetBreakpointList(bool internal)
201{
202 if (internal)
203 return m_internal_breakpoint_list;
204 else
205 return m_breakpoint_list;
206}
207
208const BreakpointList &
209Target::GetBreakpointList(bool internal) const
210{
211 if (internal)
212 return m_internal_breakpoint_list;
213 else
214 return m_breakpoint_list;
215}
216
217BreakpointSP
218Target::GetBreakpointByID (break_id_t break_id)
219{
220 BreakpointSP bp_sp;
221
222 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
223 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
224 else
225 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
226
227 return bp_sp;
228}
229
230BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000231Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000232 const FileSpecList *source_file_spec_list,
233 RegularExpression &source_regex,
234 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000235{
Jim Inghamd6d47972011-09-23 00:54:11 +0000236 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
237 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000238 return CreateBreakpoint (filter_sp, resolver_sp, internal);
239}
240
241
242BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000243Target::CreateBreakpoint (const FileSpecList *containingModules,
244 const FileSpec &file,
245 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000246 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000247 LazyBool skip_prologue,
248 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000249{
Greg Clayton49ce8962012-08-29 21:13:06 +0000250 if (check_inlines == eLazyBoolCalculate)
251 {
252 const InlineStrategy inline_strategy = GetInlineStrategy();
253 switch (inline_strategy)
254 {
255 case eInlineBreakpointsNever:
256 check_inlines = eLazyBoolNo;
257 break;
258
259 case eInlineBreakpointsHeaders:
260 if (file.IsSourceImplementationFile())
261 check_inlines = eLazyBoolNo;
262 else
263 check_inlines = eLazyBoolYes;
264 break;
265
266 case eInlineBreakpointsAlways:
267 check_inlines = eLazyBoolYes;
268 break;
269 }
270 }
Greg Clayton46365522012-09-07 23:48:57 +0000271 SearchFilterSP filter_sp;
272 if (check_inlines == eLazyBoolNo)
273 {
274 // Not checking for inlines, we are looking only for matching compile units
275 FileSpecList compile_unit_list;
276 compile_unit_list.Append (file);
277 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
278 }
279 else
280 {
281 filter_sp = GetSearchFilterForModuleList (containingModules);
282 }
Greg Clayton49ce8962012-08-29 21:13:06 +0000283 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
284 file,
285 line_no,
286 check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000287 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000288 return CreateBreakpoint (filter_sp, resolver_sp, internal);
289}
290
291
292BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000293Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000294{
Chris Lattner24943d22010-06-08 16:52:24 +0000295 Address so_addr;
296 // Attempt to resolve our load address if possible, though it is ok if
297 // it doesn't resolve to section/offset.
298
Greg Clayton33ed1702010-08-24 00:45:41 +0000299 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000300 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000301 if (!so_addr.IsValid())
302 {
303 // The address didn't resolve, so just set this as an absolute address
304 so_addr.SetOffset (addr);
305 }
306 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000307 return bp_sp;
308}
309
310BreakpointSP
311Target::CreateBreakpoint (Address &addr, bool internal)
312{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000313 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000314 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
315 return CreateBreakpoint (filter_sp, resolver_sp, internal);
316}
317
318BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000319Target::CreateBreakpoint (const FileSpecList *containingModules,
320 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000321 const char *func_name,
322 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000323 LazyBool skip_prologue,
324 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000325{
Greg Clayton12bec712010-06-28 21:30:43 +0000326 BreakpointSP bp_sp;
327 if (func_name)
328 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000329 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000330
331 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
332 func_name,
333 func_name_type_mask,
334 Breakpoint::Exact,
335 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000336 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
337 }
338 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000339}
340
Jim Ingham4722b102012-03-06 00:37:27 +0000341lldb::BreakpointSP
342Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000343 const FileSpecList *containingSourceFiles,
344 const std::vector<std::string> &func_names,
345 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000346 LazyBool skip_prologue,
347 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000348{
349 BreakpointSP bp_sp;
350 size_t num_names = func_names.size();
351 if (num_names > 0)
352 {
353 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
354
355 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
356 func_names,
357 func_name_type_mask,
358 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
359 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
360 }
361 return bp_sp;
362}
363
Jim Inghamc1053622012-03-03 02:05:11 +0000364BreakpointSP
365Target::CreateBreakpoint (const FileSpecList *containingModules,
366 const FileSpecList *containingSourceFiles,
367 const char *func_names[],
368 size_t num_names,
369 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000370 LazyBool skip_prologue,
371 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000372{
373 BreakpointSP bp_sp;
374 if (num_names > 0)
375 {
376 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
377
378 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
379 func_names,
380 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000381 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000382 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
383 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
384 }
385 return bp_sp;
386}
Chris Lattner24943d22010-06-08 16:52:24 +0000387
388SearchFilterSP
389Target::GetSearchFilterForModule (const FileSpec *containingModule)
390{
391 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000392 if (containingModule != NULL)
393 {
394 // TODO: We should look into sharing module based search filters
395 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000396 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000397 }
398 else
399 {
400 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000401 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000402 filter_sp = m_search_filter_sp;
403 }
404 return filter_sp;
405}
406
Jim Ingham03c8ee52011-09-21 01:17:13 +0000407SearchFilterSP
408Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
409{
410 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000411 if (containingModules && containingModules->GetSize() != 0)
412 {
413 // TODO: We should look into sharing module based search filters
414 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000415 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000416 }
417 else
418 {
419 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000420 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000421 filter_sp = m_search_filter_sp;
422 }
423 return filter_sp;
424}
425
Jim Inghamd6d47972011-09-23 00:54:11 +0000426SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000427Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
428 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000429{
430 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
431 return GetSearchFilterForModuleList(containingModules);
432
433 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000434 if (containingModules == NULL)
435 {
436 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
437 // but that will take a little reworking.
438
Greg Clayton13d24fb2012-01-29 20:56:30 +0000439 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000440 }
441 else
442 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000443 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000444 }
445 return filter_sp;
446}
447
Chris Lattner24943d22010-06-08 16:52:24 +0000448BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000449Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000450 const FileSpecList *containingSourceFiles,
451 RegularExpression &func_regex,
452 LazyBool skip_prologue,
453 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000454{
Jim Inghamd6d47972011-09-23 00:54:11 +0000455 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000456 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
457 func_regex,
458 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000459
460 return CreateBreakpoint (filter_sp, resolver_sp, internal);
461}
462
Jim Ingham3df164e2012-03-05 04:47:34 +0000463lldb::BreakpointSP
464Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
465{
466 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
467}
468
Chris Lattner24943d22010-06-08 16:52:24 +0000469BreakpointSP
470Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
471{
472 BreakpointSP bp_sp;
473 if (filter_sp && resolver_sp)
474 {
475 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
476 resolver_sp->SetBreakpoint (bp_sp.get());
477
478 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000479 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000480 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000481 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000482
Greg Claytone005f2c2010-11-06 01:53:30 +0000483 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000484 if (log)
485 {
486 StreamString s;
487 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
488 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
489 }
490
Chris Lattner24943d22010-06-08 16:52:24 +0000491 bp_sp->ResolveBreakpoint();
492 }
Jim Inghamd1686902010-10-14 23:45:03 +0000493
494 if (!internal && bp_sp)
495 {
496 m_last_created_breakpoint = bp_sp;
497 }
498
Chris Lattner24943d22010-06-08 16:52:24 +0000499 return bp_sp;
500}
501
Johnny Chenda5a8022011-09-20 23:28:55 +0000502bool
503Target::ProcessIsValid()
504{
505 return (m_process_sp && m_process_sp->IsAlive());
506}
507
Johnny Chen3f883492012-06-04 23:19:54 +0000508static bool
509CheckIfWatchpointsExhausted(Target *target, Error &error)
510{
511 uint32_t num_supported_hardware_watchpoints;
512 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
513 if (rc.Success())
514 {
515 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
516 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
517 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
518 num_supported_hardware_watchpoints);
519 }
520 return false;
521}
522
Johnny Chenecd4feb2011-10-14 00:42:25 +0000523// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000524// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000525WatchpointSP
Jim Ingham9e376622012-10-23 07:20:06 +0000526Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000527{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000528 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
529 if (log)
Greg Clayton851e30e2012-09-18 18:04:04 +0000530 log->Printf("Target::%s (addr = 0x%8.8llx size = %llu type = %u)\n",
Jim Ingham9e376622012-10-23 07:20:06 +0000531 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000532
Johnny Chenecd4feb2011-10-14 00:42:25 +0000533 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000534 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000535 {
536 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000537 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000538 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000539 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000540 {
541 if (size == 0)
542 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
543 else
544 error.SetErrorStringWithFormat("invalid watch address: %llu", addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000545 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000546 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000547
Johnny Chenecd4feb2011-10-14 00:42:25 +0000548 // Currently we only support one watchpoint per address, with total number
549 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000550
551 // Grab the list mutex while doing operations.
552 Mutex::Locker locker;
553 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000554 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000555 if (matched_sp)
556 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000557 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000558 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000559 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
560 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000561 // Return the existing watchpoint if both size and type match.
Jim Ingham9e376622012-10-23 07:20:06 +0000562 if (size == old_size && kind == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000563 wp_sp = matched_sp;
564 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000565 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000566 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000567 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000568 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000569 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000570 }
571
Johnny Chenecd4feb2011-10-14 00:42:25 +0000572 if (!wp_sp) {
Jim Ingham9e376622012-10-23 07:20:06 +0000573 Watchpoint *new_wp = new Watchpoint(*this, addr, size, type);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000574 if (!new_wp) {
575 printf("Watchpoint ctor failed, out of memory?\n");
576 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000577 }
Jim Ingham9e376622012-10-23 07:20:06 +0000578 new_wp->SetWatchpointType(kind);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000579 wp_sp.reset(new_wp);
580 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000581 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000582
Johnny Chen3f883492012-06-04 23:19:54 +0000583 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000584 if (log)
585 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
586 __FUNCTION__,
Johnny Chen3f883492012-06-04 23:19:54 +0000587 error.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000588 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000589
Johnny Chen3f883492012-06-04 23:19:54 +0000590 if (error.Fail()) {
Johnny Chen155599b2012-03-26 22:00:10 +0000591 // Enabling the watchpoint on the device side failed.
592 // Remove the said watchpoint from the list maintained by the target instance.
593 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000594 // See if we could provide more helpful error message.
595 if (!CheckIfWatchpointsExhausted(this, error))
596 {
597 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
598 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
599 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000600 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000601 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000602 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000603 m_last_created_watchpoint = wp_sp;
604 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000605}
606
Chris Lattner24943d22010-06-08 16:52:24 +0000607void
608Target::RemoveAllBreakpoints (bool internal_also)
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 (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
613
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000614 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000615 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000616 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000617
618 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000619}
620
621void
622Target::DisableAllBreakpoints (bool internal_also)
623{
Greg Claytone005f2c2010-11-06 01:53:30 +0000624 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000625 if (log)
626 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
627
628 m_breakpoint_list.SetEnabledAll (false);
629 if (internal_also)
630 m_internal_breakpoint_list.SetEnabledAll (false);
631}
632
633void
634Target::EnableAllBreakpoints (bool internal_also)
635{
Greg Claytone005f2c2010-11-06 01:53:30 +0000636 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000637 if (log)
638 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
639
640 m_breakpoint_list.SetEnabledAll (true);
641 if (internal_also)
642 m_internal_breakpoint_list.SetEnabledAll (true);
643}
644
645bool
646Target::RemoveBreakpointByID (break_id_t break_id)
647{
Greg Claytone005f2c2010-11-06 01:53:30 +0000648 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000649 if (log)
650 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
651
652 if (DisableBreakpointByID (break_id))
653 {
654 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000655 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000656 else
Jim Inghamd1686902010-10-14 23:45:03 +0000657 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000658 if (m_last_created_breakpoint)
659 {
660 if (m_last_created_breakpoint->GetID() == break_id)
661 m_last_created_breakpoint.reset();
662 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000663 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000664 }
Chris Lattner24943d22010-06-08 16:52:24 +0000665 return true;
666 }
667 return false;
668}
669
670bool
671Target::DisableBreakpointByID (break_id_t break_id)
672{
Greg Claytone005f2c2010-11-06 01:53:30 +0000673 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000674 if (log)
675 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
676
677 BreakpointSP bp_sp;
678
679 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
680 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
681 else
682 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
683 if (bp_sp)
684 {
685 bp_sp->SetEnabled (false);
686 return true;
687 }
688 return false;
689}
690
691bool
692Target::EnableBreakpointByID (break_id_t break_id)
693{
Greg Claytone005f2c2010-11-06 01:53:30 +0000694 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000695 if (log)
696 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
697 __FUNCTION__,
698 break_id,
699 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
700
701 BreakpointSP bp_sp;
702
703 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
704 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
705 else
706 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
707
708 if (bp_sp)
709 {
710 bp_sp->SetEnabled (true);
711 return true;
712 }
713 return false;
714}
715
Johnny Chenc86582f2011-09-23 21:21:43 +0000716// The flag 'end_to_end', default to true, signifies that the operation is
717// performed end to end, for both the debugger and the debuggee.
718
Johnny Chenecd4feb2011-10-14 00:42:25 +0000719// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
720// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000721bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000722Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000723{
724 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
725 if (log)
726 log->Printf ("Target::%s\n", __FUNCTION__);
727
Johnny Chenc86582f2011-09-23 21:21:43 +0000728 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000729 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000730 return true;
731 }
732
733 // Otherwise, it's an end to end operation.
734
Johnny Chenda5a8022011-09-20 23:28:55 +0000735 if (!ProcessIsValid())
736 return false;
737
Johnny Chenecd4feb2011-10-14 00:42:25 +0000738 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000739 for (size_t i = 0; i < num_watchpoints; ++i)
740 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000741 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
742 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000743 return false;
744
Johnny Chenecd4feb2011-10-14 00:42:25 +0000745 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000746 if (rc.Fail())
747 return false;
748 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000749 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000750 return true; // Success!
751}
752
Johnny Chenecd4feb2011-10-14 00:42:25 +0000753// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
754// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000755bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000756Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000757{
758 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
759 if (log)
760 log->Printf ("Target::%s\n", __FUNCTION__);
761
Johnny Chenc86582f2011-09-23 21:21:43 +0000762 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000763 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000764 return true;
765 }
766
767 // Otherwise, it's an end to end operation.
768
Johnny Chenda5a8022011-09-20 23:28:55 +0000769 if (!ProcessIsValid())
770 return false;
771
Johnny Chenecd4feb2011-10-14 00:42:25 +0000772 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000773 for (size_t i = 0; i < num_watchpoints; ++i)
774 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000775 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
776 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000777 return false;
778
Johnny Chenecd4feb2011-10-14 00:42:25 +0000779 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000780 if (rc.Fail())
781 return false;
782 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000783 return true; // Success!
784}
785
Johnny Chenecd4feb2011-10-14 00:42:25 +0000786// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
787// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000788bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000789Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000790{
791 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
792 if (log)
793 log->Printf ("Target::%s\n", __FUNCTION__);
794
Johnny Chenc86582f2011-09-23 21:21:43 +0000795 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000796 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000797 return true;
798 }
799
800 // Otherwise, it's an end to end operation.
801
Johnny Chenda5a8022011-09-20 23:28:55 +0000802 if (!ProcessIsValid())
803 return false;
804
Johnny Chenecd4feb2011-10-14 00:42:25 +0000805 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000806 for (size_t i = 0; i < num_watchpoints; ++i)
807 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000808 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
809 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000810 return false;
811
Johnny Chenecd4feb2011-10-14 00:42:25 +0000812 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000813 if (rc.Fail())
814 return false;
815 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000816 return true; // Success!
817}
818
Johnny Chen116a5cd2012-02-25 06:44:30 +0000819// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
820bool
821Target::ClearAllWatchpointHitCounts ()
822{
823 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
824 if (log)
825 log->Printf ("Target::%s\n", __FUNCTION__);
826
827 size_t num_watchpoints = m_watchpoint_list.GetSize();
828 for (size_t i = 0; i < num_watchpoints; ++i)
829 {
830 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
831 if (!wp_sp)
832 return false;
833
834 wp_sp->ResetHitCount();
835 }
836 return true; // Success!
837}
838
Johnny Chenecd4feb2011-10-14 00:42:25 +0000839// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000840// during these operations.
841bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000842Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000843{
844 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
845 if (log)
846 log->Printf ("Target::%s\n", __FUNCTION__);
847
848 if (!ProcessIsValid())
849 return false;
850
Johnny Chenecd4feb2011-10-14 00:42:25 +0000851 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000852 for (size_t i = 0; i < num_watchpoints; ++i)
853 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000854 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
855 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000856 return false;
857
Johnny Chenecd4feb2011-10-14 00:42:25 +0000858 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000859 }
860 return true; // Success!
861}
862
Johnny Chenecd4feb2011-10-14 00:42:25 +0000863// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000864bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000865Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000866{
867 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
868 if (log)
869 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
870
871 if (!ProcessIsValid())
872 return false;
873
Johnny Chenecd4feb2011-10-14 00:42:25 +0000874 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
875 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000876 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000877 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000878 if (rc.Success())
879 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000880
Johnny Chen01acfa72011-09-22 18:04:58 +0000881 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000882 }
883 return false;
884}
885
Johnny Chenecd4feb2011-10-14 00:42:25 +0000886// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000887bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000888Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000889{
890 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
891 if (log)
892 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
893
894 if (!ProcessIsValid())
895 return false;
896
Johnny Chenecd4feb2011-10-14 00:42:25 +0000897 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
898 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000899 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000900 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000901 if (rc.Success())
902 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000903
Johnny Chen01acfa72011-09-22 18:04:58 +0000904 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000905 }
906 return false;
907}
908
Johnny Chenecd4feb2011-10-14 00:42:25 +0000909// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000910bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000911Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000912{
913 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
914 if (log)
915 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
916
Johnny Chenecd4feb2011-10-14 00:42:25 +0000917 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000918 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000919 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000920 return true;
921 }
922 return false;
923}
924
Johnny Chenecd4feb2011-10-14 00:42:25 +0000925// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000926bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000927Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000928{
929 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
930 if (log)
931 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
932
933 if (!ProcessIsValid())
934 return false;
935
Johnny Chenecd4feb2011-10-14 00:42:25 +0000936 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
937 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000938 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000939 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000940 return true;
941 }
942 return false;
943}
944
Chris Lattner24943d22010-06-08 16:52:24 +0000945ModuleSP
946Target::GetExecutableModule ()
947{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000948 return m_images.GetModuleAtIndex(0);
949}
950
951Module*
952Target::GetExecutableModulePointer ()
953{
954 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000955}
956
Enrico Granata146d9522012-11-08 02:22:02 +0000957static void
958LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
959{
960 Error error;
961 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error))
962 {
963 target->GetDebugger().GetOutputStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
964 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
965 error.AsCString());
966 }
967}
968
Chris Lattner24943d22010-06-08 16:52:24 +0000969void
970Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
971{
972 m_images.Clear();
973 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000974 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000975 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000976
977 if (executable_sp.get())
978 {
979 Timer scoped_timer (__PRETTY_FUNCTION__,
980 "Target::SetExecutableModule (executable = '%s/%s')",
981 executable_sp->GetFileSpec().GetDirectory().AsCString(),
982 executable_sp->GetFileSpec().GetFilename().AsCString());
983
984 m_images.Append(executable_sp); // The first image is our exectuable file
985
Jim Ingham7508e732010-08-09 23:31:02 +0000986 // 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 +0000987 if (!m_arch.IsValid())
988 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000989
Chris Lattner24943d22010-06-08 16:52:24 +0000990 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000991 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000992
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000993 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000994 {
995 executable_objfile->GetDependentModules(dependent_files);
996 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
997 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000998 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
999 FileSpec platform_dependent_file_spec;
1000 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +00001001 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +00001002 else
1003 platform_dependent_file_spec = dependent_file_spec;
1004
Greg Clayton444fe992012-02-26 05:51:37 +00001005 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1006 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +00001007 if (image_module_sp.get())
1008 {
Chris Lattner24943d22010-06-08 16:52:24 +00001009 ObjectFile *objfile = image_module_sp->GetObjectFile();
1010 if (objfile)
1011 objfile->GetDependentModules(dependent_files);
1012 }
1013 }
1014 }
Chris Lattner24943d22010-06-08 16:52:24 +00001015 }
1016}
1017
1018
Jim Ingham7508e732010-08-09 23:31:02 +00001019bool
1020Target::SetArchitecture (const ArchSpec &arch_spec)
1021{
Greg Claytonb170aee2012-05-08 01:45:38 +00001022 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001023 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001024 // If we haven't got a valid arch spec, or the architectures are
1025 // compatible, so just update the architecture. Architectures can be
1026 // equal, yet the triple OS and vendor might change, so we need to do
1027 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001028 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001029 return true;
1030 }
1031 else
1032 {
1033 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +00001034 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001035 ModuleSP executable_sp = GetExecutableModule ();
1036 m_images.Clear();
1037 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001038 m_scratch_ast_source_ap.reset();
1039 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001040 // Need to do something about unsetting breakpoints.
1041
1042 if (executable_sp)
1043 {
Greg Clayton444fe992012-02-26 05:51:37 +00001044 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1045 Error error = ModuleList::GetSharedModule (module_spec,
1046 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001047 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001048 NULL,
1049 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001050
1051 if (!error.Fail() && executable_sp)
1052 {
1053 SetExecutableModule (executable_sp, true);
1054 return true;
1055 }
Jim Ingham7508e732010-08-09 23:31:02 +00001056 }
1057 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001058 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001059}
Chris Lattner24943d22010-06-08 16:52:24 +00001060
Chris Lattner24943d22010-06-08 16:52:24 +00001061void
Enrico Granata146d9522012-11-08 02:22:02 +00001062Target::WillClearList ()
1063{
1064}
1065
1066void
1067Target::ModuleAdded (const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001068{
1069 // A module is being added to this target for the first time
1070 ModuleList module_list;
1071 module_list.Append(module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +00001072 LoadScriptingResourceForModule(module_sp, this);
Chris Lattner24943d22010-06-08 16:52:24 +00001073 ModulesDidLoad (module_list);
1074}
1075
1076void
Enrico Granata146d9522012-11-08 02:22:02 +00001077Target::ModuleRemoved (const ModuleSP &module_sp)
1078{
1079 // A module is being added to this target for the first time
1080 ModuleList module_list;
1081 module_list.Append(module_sp);
1082 ModulesDidUnload (module_list);
1083}
1084
1085void
1086Target::ModuleUpdated (const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001087{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001088 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001089 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001090}
1091
1092void
1093Target::ModulesDidLoad (ModuleList &module_list)
1094{
Enrico Granata146d9522012-11-08 02:22:02 +00001095 if (module_list.GetSize())
1096 {
1097 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1098 // TODO: make event data that packages up the module_list
1099 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1100 }
Chris Lattner24943d22010-06-08 16:52:24 +00001101}
1102
1103void
1104Target::ModulesDidUnload (ModuleList &module_list)
1105{
Enrico Granata146d9522012-11-08 02:22:02 +00001106 if (module_list.GetSize())
1107 {
1108 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1109 // TODO: make event data that packages up the module_list
1110 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1111 }
Chris Lattner24943d22010-06-08 16:52:24 +00001112}
1113
Daniel Dunbar705a0982011-10-31 22:50:37 +00001114bool
Greg Clayton444fe992012-02-26 05:51:37 +00001115Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001116{
Greg Clayton73844aa2012-08-22 17:17:09 +00001117 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001118 {
1119 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001120 ModuleSpec module_spec (module_file_spec);
1121 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001122
1123 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1124 // black list.
1125 if (num_modules > 0)
1126 {
1127 for (int i = 0; i < num_modules; i++)
1128 {
1129 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1130 return false;
1131 }
1132 return true;
1133 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001134 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001135 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001136}
1137
Daniel Dunbar705a0982011-10-31 22:50:37 +00001138bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001139Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1140{
Greg Clayton73844aa2012-08-22 17:17:09 +00001141 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001142 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001143 if (m_platform_sp)
1144 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001145 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001146 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001147}
1148
Chris Lattner24943d22010-06-08 16:52:24 +00001149size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001150Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1151{
Greg Clayton3508c382012-02-24 01:59:29 +00001152 SectionSP section_sp (addr.GetSection());
1153 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001154 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001155 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1156 if (section_sp->IsEncrypted())
1157 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001158 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001159 return 0;
1160 }
Greg Clayton3508c382012-02-24 01:59:29 +00001161 ModuleSP module_sp (section_sp->GetModule());
1162 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001163 {
Greg Clayton3508c382012-02-24 01:59:29 +00001164 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1165 if (objfile)
1166 {
1167 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1168 addr.GetOffset(),
1169 dst,
1170 dst_len);
1171 if (bytes_read > 0)
1172 return bytes_read;
1173 else
1174 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1175 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001176 else
Greg Clayton3508c382012-02-24 01:59:29 +00001177 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001178 }
1179 else
Greg Clayton3508c382012-02-24 01:59:29 +00001180 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001181 }
1182 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001183 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001184
Greg Clayton26100dc2011-01-07 01:57:07 +00001185 return 0;
1186}
1187
1188size_t
Enrico Granata91544802011-09-06 19:20:51 +00001189Target::ReadMemory (const Address& addr,
1190 bool prefer_file_cache,
1191 void *dst,
1192 size_t dst_len,
1193 Error &error,
1194 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001195{
Chris Lattner24943d22010-06-08 16:52:24 +00001196 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001197
Enrico Granata91544802011-09-06 19:20:51 +00001198 // if we end up reading this from process memory, we will fill this
1199 // with the actual load address
1200 if (load_addr_ptr)
1201 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1202
Greg Clayton26100dc2011-01-07 01:57:07 +00001203 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001204
1205 addr_t load_addr = LLDB_INVALID_ADDRESS;
1206 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001207 Address resolved_addr;
1208 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001209 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001210 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001211 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001212 // No sections are loaded, so we must assume we are not running
1213 // yet and anything we are given is a file address.
1214 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1215 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001216 }
Greg Clayton70436352010-06-30 23:03:03 +00001217 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001218 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001219 // We have at least one section loaded. This can be becuase
1220 // we have manually loaded some sections with "target modules load ..."
1221 // or because we have have a live process that has sections loaded
1222 // through the dynamic loader
1223 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1224 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001225 }
Greg Clayton70436352010-06-30 23:03:03 +00001226 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001227 if (!resolved_addr.IsValid())
1228 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001229
Greg Clayton9b82f862011-07-11 05:12:02 +00001230
Greg Clayton26100dc2011-01-07 01:57:07 +00001231 if (prefer_file_cache)
1232 {
1233 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1234 if (bytes_read > 0)
1235 return bytes_read;
1236 }
Greg Clayton70436352010-06-30 23:03:03 +00001237
Johnny Chenda5a8022011-09-20 23:28:55 +00001238 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001239 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001240 if (load_addr == LLDB_INVALID_ADDRESS)
1241 load_addr = resolved_addr.GetLoadAddress (this);
1242
Greg Clayton70436352010-06-30 23:03:03 +00001243 if (load_addr == LLDB_INVALID_ADDRESS)
1244 {
Greg Clayton3508c382012-02-24 01:59:29 +00001245 ModuleSP addr_module_sp (resolved_addr.GetModule());
1246 if (addr_module_sp && addr_module_sp->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001247 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001248 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001249 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001250 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001251 else
Greg Clayton9c236732011-10-26 00:56:27 +00001252 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001253 }
1254 else
1255 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001256 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001257 if (bytes_read != dst_len)
1258 {
1259 if (error.Success())
1260 {
1261 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001262 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001263 else
Greg Clayton851e30e2012-09-18 18:04:04 +00001264 error.SetErrorStringWithFormat("only %llu of %llu bytes were read from memory at 0x%llx", (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001265 }
1266 }
Greg Clayton70436352010-06-30 23:03:03 +00001267 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001268 {
1269 if (load_addr_ptr)
1270 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001271 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001272 }
Greg Clayton70436352010-06-30 23:03:03 +00001273 // If the address is not section offset we have an address that
1274 // doesn't resolve to any address in any currently loaded shared
1275 // libaries and we failed to read memory so there isn't anything
1276 // more we can do. If it is section offset, we might be able to
1277 // read cached memory from the object file.
1278 if (!resolved_addr.IsSectionOffset())
1279 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001280 }
Chris Lattner24943d22010-06-08 16:52:24 +00001281 }
Greg Clayton70436352010-06-30 23:03:03 +00001282
Greg Clayton9b82f862011-07-11 05:12:02 +00001283 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001284 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001285 // If we didn't already try and read from the object file cache, then
1286 // try it after failing to read from the process.
1287 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001288 }
1289 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001290}
1291
Greg Clayton7dd98df2011-07-12 17:06:17 +00001292size_t
1293Target::ReadScalarIntegerFromMemory (const Address& addr,
1294 bool prefer_file_cache,
1295 uint32_t byte_size,
1296 bool is_signed,
1297 Scalar &scalar,
1298 Error &error)
1299{
1300 uint64_t uval;
1301
1302 if (byte_size <= sizeof(uval))
1303 {
1304 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1305 if (bytes_read == byte_size)
1306 {
1307 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1308 uint32_t offset = 0;
1309 if (byte_size <= 4)
1310 scalar = data.GetMaxU32 (&offset, byte_size);
1311 else
1312 scalar = data.GetMaxU64 (&offset, byte_size);
1313
1314 if (is_signed)
1315 scalar.SignExtend(byte_size * 8);
1316 return bytes_read;
1317 }
1318 }
1319 else
1320 {
1321 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1322 }
1323 return 0;
1324}
1325
1326uint64_t
1327Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1328 bool prefer_file_cache,
1329 size_t integer_byte_size,
1330 uint64_t fail_value,
1331 Error &error)
1332{
1333 Scalar scalar;
1334 if (ReadScalarIntegerFromMemory (addr,
1335 prefer_file_cache,
1336 integer_byte_size,
1337 false,
1338 scalar,
1339 error))
1340 return scalar.ULongLong(fail_value);
1341 return fail_value;
1342}
1343
1344bool
1345Target::ReadPointerFromMemory (const Address& addr,
1346 bool prefer_file_cache,
1347 Error &error,
1348 Address &pointer_addr)
1349{
1350 Scalar scalar;
1351 if (ReadScalarIntegerFromMemory (addr,
1352 prefer_file_cache,
1353 m_arch.GetAddressByteSize(),
1354 false,
1355 scalar,
1356 error))
1357 {
1358 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1359 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1360 {
1361 if (m_section_load_list.IsEmpty())
1362 {
1363 // No sections are loaded, so we must assume we are not running
1364 // yet and anything we are given is a file address.
1365 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1366 }
1367 else
1368 {
1369 // We have at least one section loaded. This can be becuase
1370 // we have manually loaded some sections with "target modules load ..."
1371 // or because we have have a live process that has sections loaded
1372 // through the dynamic loader
1373 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1374 }
1375 // We weren't able to resolve the pointer value, so just return
1376 // an address with no section
1377 if (!pointer_addr.IsValid())
1378 pointer_addr.SetOffset (pointer_vm_addr);
1379 return true;
1380
1381 }
1382 }
1383 return false;
1384}
Chris Lattner24943d22010-06-08 16:52:24 +00001385
1386ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001387Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001388{
Chris Lattner24943d22010-06-08 16:52:24 +00001389 ModuleSP module_sp;
1390
Chris Lattner24943d22010-06-08 16:52:24 +00001391 Error error;
1392
Jim Ingham03e5e512012-05-17 18:38:42 +00001393 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1394 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001395
Jim Ingham03e5e512012-05-17 18:38:42 +00001396 if (module_spec.GetUUID().IsValid())
1397 module_sp = m_images.FindFirstModule(module_spec);
1398
Greg Claytone1ef1e32012-04-27 00:58:27 +00001399 if (!module_sp)
1400 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001401 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1402 bool did_create_module = false;
1403
1404 // If there are image search path entries, try to use them first to acquire a suitable image.
1405 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001406 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001407 ModuleSpec transformed_spec (module_spec);
1408 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1409 {
1410 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1411 error = ModuleList::GetSharedModule (transformed_spec,
1412 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001413 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001414 &old_module_sp,
1415 &did_create_module);
1416 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001417 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001418
Greg Claytone1ef1e32012-04-27 00:58:27 +00001419 if (!module_sp)
1420 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001421 // If we have a UUID, we can check our global shared module list in case
1422 // we already have it. If we don't have a valid UUID, then we can't since
1423 // the path in "module_spec" will be a platform path, and we will need to
1424 // let the platform find that file. For example, we could be asking for
1425 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1426 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1427 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1428 // cache.
1429 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001430 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001431 // We have a UUID, it is OK to check the global module list...
1432 error = ModuleList::GetSharedModule (module_spec,
1433 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001434 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001435 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001436 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001437 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001438
1439 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001440 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001441 // The platform is responsible for finding and caching an appropriate
1442 // module in the shared module cache.
1443 if (m_platform_sp)
1444 {
1445 FileSpec platform_file_spec;
1446 error = m_platform_sp->GetSharedModule (module_spec,
1447 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001448 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001449 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001450 &did_create_module);
1451 }
1452 else
1453 {
1454 error.SetErrorString("no platform is currently set");
1455 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001456 }
1457 }
Chris Lattner24943d22010-06-08 16:52:24 +00001458
Jim Ingham03e5e512012-05-17 18:38:42 +00001459 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1460 // module in the list already, and if there was, let's remove it.
1461 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001462 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001463 // GetSharedModule is not guaranteed to find the old shared module, for instance
1464 // in the common case where you pass in the UUID, it is only going to find the one
1465 // module matching the UUID. In fact, it has no good way to know what the "old module"
1466 // relevant to this target is, since there might be many copies of a module with this file spec
1467 // in various running debug sessions, but only one of them will belong to this target.
1468 // So let's remove the UUID from the module list, and look in the target's module list.
1469 // Only do this if there is SOMETHING else in the module spec...
1470 if (!old_module_sp)
1471 {
1472 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
1473 {
1474 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1475 module_spec_copy.GetUUID().Clear();
1476
1477 ModuleList found_modules;
1478 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1479 if (num_found == 1)
1480 {
1481 old_module_sp = found_modules.GetModuleAtIndex(0);
1482 }
1483 }
1484 }
1485
Chris Lattner24943d22010-06-08 16:52:24 +00001486 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
Jim Ingham03e5e512012-05-17 18:38:42 +00001487 {
Enrico Granata146d9522012-11-08 02:22:02 +00001488 m_images.ReplaceModule(old_module_sp, module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001489 Module *old_module_ptr = old_module_sp.get();
1490 old_module_sp.reset();
1491 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1492 }
Chris Lattner24943d22010-06-08 16:52:24 +00001493 else
Enrico Granata146d9522012-11-08 02:22:02 +00001494 m_images.Append(module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001495 }
1496 }
1497 if (error_ptr)
1498 *error_ptr = error;
1499 return module_sp;
1500}
1501
1502
Greg Clayton289afcb2012-02-18 05:35:26 +00001503TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001504Target::CalculateTarget ()
1505{
Greg Clayton289afcb2012-02-18 05:35:26 +00001506 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001507}
1508
Greg Clayton289afcb2012-02-18 05:35:26 +00001509ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001510Target::CalculateProcess ()
1511{
Greg Clayton289afcb2012-02-18 05:35:26 +00001512 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001513}
1514
Greg Clayton289afcb2012-02-18 05:35:26 +00001515ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001516Target::CalculateThread ()
1517{
Greg Clayton289afcb2012-02-18 05:35:26 +00001518 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001519}
1520
Greg Clayton289afcb2012-02-18 05:35:26 +00001521StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001522Target::CalculateStackFrame ()
1523{
Greg Clayton289afcb2012-02-18 05:35:26 +00001524 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001525}
1526
1527void
Greg Claytona830adb2010-10-04 01:05:56 +00001528Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001529{
Greg Clayton567e7f32011-09-22 04:58:26 +00001530 exe_ctx.Clear();
1531 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001532}
1533
1534PathMappingList &
1535Target::GetImageSearchPathList ()
1536{
1537 return m_image_search_paths;
1538}
1539
1540void
1541Target::ImageSearchPathsChanged
1542(
1543 const PathMappingList &path_list,
1544 void *baton
1545)
1546{
1547 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001548 ModuleSP exe_module_sp (target->GetExecutableModule());
1549 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001550 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001551 target->m_images.Clear();
1552 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001553 }
1554}
1555
1556ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001557Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001558{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001559 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001560 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001561 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001562 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001563 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001564 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1565 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1566 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1567 }
Chris Lattner24943d22010-06-08 16:52:24 +00001568 return m_scratch_ast_context_ap.get();
1569}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001570
Sean Callanan4938bd62011-11-16 18:20:47 +00001571ClangASTImporter *
1572Target::GetClangASTImporter()
1573{
1574 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1575
1576 if (!ast_importer)
1577 {
1578 ast_importer = new ClangASTImporter();
1579 m_ast_importer_ap.reset(ast_importer);
1580 }
1581
1582 return ast_importer;
1583}
1584
Greg Clayton990de7b2010-11-18 23:32:35 +00001585void
Caroline Tice2a456812011-03-10 22:14:10 +00001586Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001587{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001588 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001589}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001590
Greg Clayton990de7b2010-11-18 23:32:35 +00001591void
Caroline Tice2a456812011-03-10 22:14:10 +00001592Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001593{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001594 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001595}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001596
Greg Clayton9ce95382012-02-13 23:10:39 +00001597FileSpecList
1598Target::GetDefaultExecutableSearchPaths ()
1599{
Greg Clayton73844aa2012-08-22 17:17:09 +00001600 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1601 if (properties_sp)
1602 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001603 return FileSpecList();
1604}
1605
Caroline Tice5bc8c972010-09-20 20:44:43 +00001606ArchSpec
1607Target::GetDefaultArchitecture ()
1608{
Greg Clayton73844aa2012-08-22 17:17:09 +00001609 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1610 if (properties_sp)
1611 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001612 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001613}
1614
1615void
Greg Clayton73844aa2012-08-22 17:17:09 +00001616Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001617{
Greg Clayton73844aa2012-08-22 17:17:09 +00001618 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1619 if (properties_sp)
1620 return properties_sp->SetDefaultArchitecture(arch);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001621}
1622
Greg Claytona830adb2010-10-04 01:05:56 +00001623Target *
1624Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1625{
1626 // The target can either exist in the "process" of ExecutionContext, or in
1627 // the "target_sp" member of SymbolContext. This accessor helper function
1628 // will get the target from one of these locations.
1629
1630 Target *target = NULL;
1631 if (sc_ptr != NULL)
1632 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001633 if (target == NULL && exe_ctx_ptr)
1634 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001635 return target;
1636}
1637
Greg Clayton427f2902010-12-14 02:59:59 +00001638ExecutionResults
1639Target::EvaluateExpression
1640(
1641 const char *expr_cstr,
1642 StackFrame *frame,
Enrico Granata6cca9692012-07-16 23:10:35 +00001643 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001644 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001645)
1646{
Enrico Granata3a08fd12012-09-18 17:43:16 +00001647 result_valobj_sp.reset();
1648
Greg Clayton427f2902010-12-14 02:59:59 +00001649 ExecutionResults execution_results = eExecutionSetupError;
1650
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001651 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1652 return execution_results;
1653
Jim Ingham3613ae12011-05-12 02:06:14 +00001654 // We shouldn't run stop hooks in expressions.
1655 // Be sure to reset this if you return anywhere within this function.
1656 bool old_suppress_value = m_suppress_stop_hooks;
1657 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001658
1659 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001660
1661 const size_t expr_cstr_len = ::strlen (expr_cstr);
1662
Greg Clayton427f2902010-12-14 02:59:59 +00001663 if (frame)
1664 {
1665 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001666 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001667 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001668 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1669 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001670 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001671
1672 // Make sure we don't have any things that we know a variable expression
1673 // won't be able to deal with before calling into it
1674 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1675 {
1676 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
Enrico Granatad27026e2012-09-05 20:41:26 +00001677 options.GetUseDynamic(),
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001678 expr_path_options,
1679 var_sp,
1680 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001681 // if this expression results in a bitfield, we give up and let the IR handle it
1682 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1683 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001684 }
Greg Clayton427f2902010-12-14 02:59:59 +00001685 }
1686 else if (m_process_sp)
1687 {
1688 m_process_sp->CalculateExecutionContext(exe_ctx);
1689 }
1690 else
1691 {
1692 CalculateExecutionContext(exe_ctx);
1693 }
1694
1695 if (result_valobj_sp)
1696 {
1697 execution_results = eExecutionCompleted;
1698 // We got a result from the frame variable expression path above...
1699 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1700
1701 lldb::ValueObjectSP const_valobj_sp;
1702
1703 // Check in case our value is already a constant value
1704 if (result_valobj_sp->GetIsConstant())
1705 {
1706 const_valobj_sp = result_valobj_sp;
1707 const_valobj_sp->SetName (persistent_variable_name);
1708 }
1709 else
Jim Inghame41494a2011-04-16 00:01:13 +00001710 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001711 if (options.GetUseDynamic() != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001712 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001713 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(options.GetUseDynamic());
Jim Inghame41494a2011-04-16 00:01:13 +00001714 if (dynamic_sp)
1715 result_valobj_sp = dynamic_sp;
1716 }
1717
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001718 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001719 }
Greg Clayton427f2902010-12-14 02:59:59 +00001720
Sean Callanan6a925532011-01-13 08:53:35 +00001721 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1722
Greg Clayton427f2902010-12-14 02:59:59 +00001723 result_valobj_sp = const_valobj_sp;
1724
Sean Callanan6a925532011-01-13 08:53:35 +00001725 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1726 assert (clang_expr_variable_sp.get());
1727
1728 // Set flags and live data as appropriate
1729
1730 const Value &result_value = live_valobj_sp->GetValue();
1731
1732 switch (result_value.GetValueType())
1733 {
1734 case Value::eValueTypeHostAddress:
1735 case Value::eValueTypeFileAddress:
1736 // we don't do anything with these for now
1737 break;
1738 case Value::eValueTypeScalar:
Greg Claytonf0fab4f2012-10-30 23:56:14 +00001739 case Value::eValueTypeVector:
Sean Callanan6a925532011-01-13 08:53:35 +00001740 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1741 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1742 break;
1743 case Value::eValueTypeLoadAddress:
1744 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1745 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1746 break;
1747 }
Greg Clayton427f2902010-12-14 02:59:59 +00001748 }
1749 else
1750 {
1751 // Make sure we aren't just trying to see the value of a persistent
1752 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001753 lldb::ClangExpressionVariableSP persistent_var_sp;
1754 // Only check for persistent variables the expression starts with a '$'
1755 if (expr_cstr[0] == '$')
1756 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1757
Greg Clayton427f2902010-12-14 02:59:59 +00001758 if (persistent_var_sp)
1759 {
1760 result_valobj_sp = persistent_var_sp->GetValueObject ();
1761 execution_results = eExecutionCompleted;
1762 }
1763 else
1764 {
1765 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001766
Greg Clayton427f2902010-12-14 02:59:59 +00001767 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Enrico Granatad27026e2012-09-05 20:41:26 +00001768 options.GetExecutionPolicy(),
Sean Callanan5b658cc2011-11-07 23:35:40 +00001769 lldb::eLanguageTypeUnknown,
Enrico Granatad27026e2012-09-05 20:41:26 +00001770 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1771 options.DoesUnwindOnError(),
Greg Clayton427f2902010-12-14 02:59:59 +00001772 expr_cstr,
1773 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001774 result_valobj_sp,
Jim Ingham47beabb2012-10-16 21:41:58 +00001775 options.GetRunOthers(),
1776 options.GetTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001777 }
1778 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001779
1780 m_suppress_stop_hooks = old_suppress_value;
1781
Greg Clayton427f2902010-12-14 02:59:59 +00001782 return execution_results;
1783}
1784
Greg Claytonc0fa5332011-05-22 22:46:53 +00001785lldb::addr_t
1786Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1787{
1788 addr_t code_addr = load_addr;
1789 switch (m_arch.GetMachine())
1790 {
1791 case llvm::Triple::arm:
1792 case llvm::Triple::thumb:
1793 switch (addr_class)
1794 {
1795 case eAddressClassData:
1796 case eAddressClassDebug:
1797 return LLDB_INVALID_ADDRESS;
1798
1799 case eAddressClassUnknown:
1800 case eAddressClassInvalid:
1801 case eAddressClassCode:
1802 case eAddressClassCodeAlternateISA:
1803 case eAddressClassRuntime:
1804 // Check if bit zero it no set?
1805 if ((code_addr & 1ull) == 0)
1806 {
1807 // Bit zero isn't set, check if the address is a multiple of 2?
1808 if (code_addr & 2ull)
1809 {
1810 // The address is a multiple of 2 so it must be thumb, set bit zero
1811 code_addr |= 1ull;
1812 }
1813 else if (addr_class == eAddressClassCodeAlternateISA)
1814 {
1815 // We checked the address and the address claims to be the alternate ISA
1816 // which means thumb, so set bit zero.
1817 code_addr |= 1ull;
1818 }
1819 }
1820 break;
1821 }
1822 break;
1823
1824 default:
1825 break;
1826 }
1827 return code_addr;
1828}
1829
1830lldb::addr_t
1831Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1832{
1833 addr_t opcode_addr = load_addr;
1834 switch (m_arch.GetMachine())
1835 {
1836 case llvm::Triple::arm:
1837 case llvm::Triple::thumb:
1838 switch (addr_class)
1839 {
1840 case eAddressClassData:
1841 case eAddressClassDebug:
1842 return LLDB_INVALID_ADDRESS;
1843
1844 case eAddressClassInvalid:
1845 case eAddressClassUnknown:
1846 case eAddressClassCode:
1847 case eAddressClassCodeAlternateISA:
1848 case eAddressClassRuntime:
1849 opcode_addr &= ~(1ull);
1850 break;
1851 }
1852 break;
1853
1854 default:
1855 break;
1856 }
1857 return opcode_addr;
1858}
1859
Jim Inghamd60d94a2011-03-11 03:53:59 +00001860lldb::user_id_t
1861Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1862{
1863 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001864 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001865 m_stop_hooks[new_uid] = new_hook_sp;
1866 return new_uid;
1867}
1868
1869bool
1870Target::RemoveStopHookByID (lldb::user_id_t user_id)
1871{
1872 size_t num_removed;
1873 num_removed = m_stop_hooks.erase (user_id);
1874 if (num_removed == 0)
1875 return false;
1876 else
1877 return true;
1878}
1879
1880void
1881Target::RemoveAllStopHooks ()
1882{
1883 m_stop_hooks.clear();
1884}
1885
1886Target::StopHookSP
1887Target::GetStopHookByID (lldb::user_id_t user_id)
1888{
1889 StopHookSP found_hook;
1890
1891 StopHookCollection::iterator specified_hook_iter;
1892 specified_hook_iter = m_stop_hooks.find (user_id);
1893 if (specified_hook_iter != m_stop_hooks.end())
1894 found_hook = (*specified_hook_iter).second;
1895 return found_hook;
1896}
1897
1898bool
1899Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
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 return false;
1905
1906 (*specified_hook_iter).second->SetIsActive (active_state);
1907 return true;
1908}
1909
1910void
1911Target::SetAllStopHooksActiveState (bool active_state)
1912{
1913 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1914 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1915 {
1916 (*pos).second->SetIsActive (active_state);
1917 }
1918}
1919
1920void
1921Target::RunStopHooks ()
1922{
Jim Ingham3613ae12011-05-12 02:06:14 +00001923 if (m_suppress_stop_hooks)
1924 return;
1925
Jim Inghamd60d94a2011-03-11 03:53:59 +00001926 if (!m_process_sp)
1927 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001928
1929 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1930 // since in that case we do not want to run the stop-hooks
1931 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1932 return;
1933
Jim Inghamd60d94a2011-03-11 03:53:59 +00001934 if (m_stop_hooks.empty())
1935 return;
1936
1937 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1938
1939 // If there aren't any active stop hooks, don't bother either:
1940 bool any_active_hooks = false;
1941 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1942 {
1943 if ((*pos).second->IsActive())
1944 {
1945 any_active_hooks = true;
1946 break;
1947 }
1948 }
1949 if (!any_active_hooks)
1950 return;
1951
1952 CommandReturnObject result;
1953
1954 std::vector<ExecutionContext> exc_ctx_with_reasons;
1955 std::vector<SymbolContext> sym_ctx_with_reasons;
1956
1957 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1958 size_t num_threads = cur_threadlist.GetSize();
1959 for (size_t i = 0; i < num_threads; i++)
1960 {
1961 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1962 if (cur_thread_sp->ThreadStoppedForAReason())
1963 {
1964 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1965 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1966 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1967 }
1968 }
1969
1970 // If no threads stopped for a reason, don't run the stop-hooks.
1971 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1972 if (num_exe_ctx == 0)
1973 return;
1974
Jim Inghame5ed8e92011-06-02 23:58:26 +00001975 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1976 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001977
1978 bool keep_going = true;
1979 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001980 bool print_hook_header;
1981 bool print_thread_header;
1982
1983 if (num_exe_ctx == 1)
1984 print_thread_header = false;
1985 else
1986 print_thread_header = true;
1987
1988 if (m_stop_hooks.size() == 1)
1989 print_hook_header = false;
1990 else
1991 print_hook_header = true;
1992
Jim Inghamd60d94a2011-03-11 03:53:59 +00001993 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1994 {
1995 // result.Clear();
1996 StopHookSP cur_hook_sp = (*pos).second;
1997 if (!cur_hook_sp->IsActive())
1998 continue;
1999
2000 bool any_thread_matched = false;
2001 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2002 {
2003 if ((cur_hook_sp->GetSpecifier () == NULL
2004 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2005 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00002006 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002007 {
2008 if (!hooks_ran)
2009 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00002010 hooks_ran = true;
2011 }
Jim Inghamc54840c2011-03-22 01:47:27 +00002012 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002013 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002014 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2015 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2016 NULL);
2017 if (cmd)
2018 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
2019 else
2020 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002021 any_thread_matched = true;
2022 }
2023
Jim Inghamc54840c2011-03-22 01:47:27 +00002024 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002025 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002026
2027 bool stop_on_continue = true;
2028 bool stop_on_error = true;
2029 bool echo_commands = false;
2030 bool print_results = true;
2031 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002032 &exc_ctx_with_reasons[i],
2033 stop_on_continue,
2034 stop_on_error,
2035 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002036 print_results,
2037 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002038 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002039
2040 // If the command started the target going again, we should bag out of
2041 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002042 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2043 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002044 {
Greg Clayton444e35b2011-10-19 18:09:39 +00002045 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002046 keep_going = false;
2047 }
2048 }
2049 }
2050 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002051
Caroline Tice4a348082011-05-02 20:41:46 +00002052 result.GetImmediateOutputStream()->Flush();
2053 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002054}
2055
Greg Claytonbbea1332011-07-08 00:48:09 +00002056
Jim Inghamd60d94a2011-03-11 03:53:59 +00002057//--------------------------------------------------------------
2058// class Target::StopHook
2059//--------------------------------------------------------------
2060
2061
2062Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2063 UserID (uid),
2064 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002065 m_commands (),
2066 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002067 m_thread_spec_ap(NULL),
2068 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002069{
2070}
2071
2072Target::StopHook::StopHook (const StopHook &rhs) :
2073 UserID (rhs.GetID()),
2074 m_target_sp (rhs.m_target_sp),
2075 m_commands (rhs.m_commands),
2076 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002077 m_thread_spec_ap (NULL),
2078 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002079{
2080 if (rhs.m_thread_spec_ap.get() != NULL)
2081 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2082}
2083
2084
2085Target::StopHook::~StopHook ()
2086{
2087}
2088
2089void
2090Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2091{
2092 m_thread_spec_ap.reset (specifier);
2093}
2094
2095
2096void
2097Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2098{
2099 int indent_level = s->GetIndentLevel();
2100
2101 s->SetIndentLevel(indent_level + 2);
2102
Greg Clayton444e35b2011-10-19 18:09:39 +00002103 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002104 if (m_active)
2105 s->Indent ("State: enabled\n");
2106 else
2107 s->Indent ("State: disabled\n");
2108
2109 if (m_specifier_sp)
2110 {
2111 s->Indent();
2112 s->PutCString ("Specifier:\n");
2113 s->SetIndentLevel (indent_level + 4);
2114 m_specifier_sp->GetDescription (s, level);
2115 s->SetIndentLevel (indent_level + 2);
2116 }
2117
2118 if (m_thread_spec_ap.get() != NULL)
2119 {
2120 StreamString tmp;
2121 s->Indent("Thread:\n");
2122 m_thread_spec_ap->GetDescription (&tmp, level);
2123 s->SetIndentLevel (indent_level + 4);
2124 s->Indent (tmp.GetData());
2125 s->PutCString ("\n");
2126 s->SetIndentLevel (indent_level + 2);
2127 }
2128
2129 s->Indent ("Commands: \n");
2130 s->SetIndentLevel (indent_level + 4);
2131 uint32_t num_commands = m_commands.GetSize();
2132 for (uint32_t i = 0; i < num_commands; i++)
2133 {
2134 s->Indent(m_commands.GetStringAtIndex(i));
2135 s->PutCString ("\n");
2136 }
2137 s->SetIndentLevel (indent_level);
2138}
2139
Greg Clayton73844aa2012-08-22 17:17:09 +00002140//--------------------------------------------------------------
2141// class TargetProperties
2142//--------------------------------------------------------------
2143
2144OptionEnumValueElement
2145lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002146{
Greg Clayton73844aa2012-08-22 17:17:09 +00002147 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2148 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2149 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2150 { 0, NULL, NULL }
2151};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002152
Greg Clayton49ce8962012-08-29 21:13:06 +00002153static OptionEnumValueElement
2154g_inline_breakpoint_enums[] =
2155{
2156 { eInlineBreakpointsNever, "never", "Never look for inline breakpoint locations (fastest). This setting should only be used if you know that no inlining occurs in your programs."},
2157 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2158 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2159 { 0, NULL, NULL }
2160};
2161
Greg Clayton73844aa2012-08-22 17:17:09 +00002162static PropertyDefinition
2163g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002164{
Greg Clayton73844aa2012-08-22 17:17:09 +00002165 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2166 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2167 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2168 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2169 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2170 { "source-map" , OptionValue::eTypePathMap , false, 0 , NULL, NULL, "Source path remappings used to track the change of location between a source file when built, and "
2171 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2172 "some part (starting at the root) of the path to the file when it was built, "
2173 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2174 "Each element of the array is checked in order and the first one that results in a match wins." },
2175 { "exec-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "Executable search paths to use when locating executable files whose paths don't match the local file system." },
2176 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2177 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2178 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
Greg Clayton0c8446c2012-10-17 22:57:12 +00002179 { "arg0" , OptionValue::eTypeString , false, 0 , NULL, NULL, "The first argument passed to the program in the argument array which can be different from the executable itself." },
2180 { "run-args" , OptionValue::eTypeArgs , false, 0 , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run. Note that this does NOT include the argv[0] which is in target.arg0." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002181 { "env-vars" , OptionValue::eTypeDictionary, false, OptionValue::eTypeString , NULL, NULL, "A list of all the environment variables to be passed to the executable's environment, and their values." },
2182 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2183 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2184 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2185 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2186 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2187 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Disable stdin/stdout for process (e.g. for a GUI application)" },
Greg Clayton49ce8962012-08-29 21:13:06 +00002188 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2189 "Breakpoint locations can end up being inlined by the compiler, so that a compile unit 'a.c' might contain an inlined function from another source file. "
2190 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2191 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2192 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2193 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2194 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002195 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2196};
2197enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002198{
Greg Clayton73844aa2012-08-22 17:17:09 +00002199 ePropertyDefaultArch,
2200 ePropertyExprPrefix,
2201 ePropertyPreferDynamic,
2202 ePropertyEnableSynthetic,
2203 ePropertySkipPrologue,
2204 ePropertySourceMap,
2205 ePropertyExecutableSearchPaths,
2206 ePropertyMaxChildrenCount,
2207 ePropertyMaxSummaryLength,
2208 ePropertyBreakpointUseAvoidList,
Greg Clayton0c8446c2012-10-17 22:57:12 +00002209 ePropertyArg0,
Greg Clayton73844aa2012-08-22 17:17:09 +00002210 ePropertyRunArgs,
2211 ePropertyEnvVars,
2212 ePropertyInheritEnv,
2213 ePropertyInputPath,
2214 ePropertyOutputPath,
2215 ePropertyErrorPath,
2216 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002217 ePropertyDisableSTDIO,
Greg Clayton87e9d322012-10-19 18:02:49 +00002218 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002219};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002220
Caroline Tice5bc8c972010-09-20 20:44:43 +00002221
Greg Clayton73844aa2012-08-22 17:17:09 +00002222class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002223{
Greg Clayton73844aa2012-08-22 17:17:09 +00002224public:
2225 TargetOptionValueProperties (const ConstString &name) :
2226 OptionValueProperties (name),
2227 m_target (NULL),
2228 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002229 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002230 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002231
Greg Clayton73844aa2012-08-22 17:17:09 +00002232 // This constructor is used when creating TargetOptionValueProperties when it
2233 // is part of a new lldb_private::Target instance. It will copy all current
2234 // global property values as needed
2235 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2236 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2237 m_target (target),
2238 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002239 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002240 }
2241
2242 virtual const Property *
2243 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2244 {
2245 // When gettings the value for a key from the target options, we will always
2246 // try and grab the setting from the current target if there is one. Else we just
2247 // use the one from this instance.
2248 if (idx == ePropertyEnvVars)
2249 GetHostEnvironmentIfNeeded ();
2250
2251 if (exe_ctx)
2252 {
2253 Target *target = exe_ctx->GetTargetPtr();
2254 if (target)
2255 {
2256 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2257 if (this != target_properties)
2258 return target_properties->ProtectedGetPropertyAtIndex (idx);
2259 }
2260 }
2261 return ProtectedGetPropertyAtIndex (idx);
2262 }
2263protected:
2264
2265 void
2266 GetHostEnvironmentIfNeeded () const
2267 {
2268 if (!m_got_host_env)
2269 {
2270 if (m_target)
2271 {
2272 m_got_host_env = true;
2273 const uint32_t idx = ePropertyInheritEnv;
2274 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2275 {
2276 PlatformSP platform_sp (m_target->GetPlatform());
2277 if (platform_sp)
2278 {
2279 StringList env;
2280 if (platform_sp->GetEnvironment(env))
2281 {
2282 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2283 if (env_dict)
2284 {
2285 const bool can_replace = false;
2286 const size_t envc = env.GetSize();
2287 for (size_t idx=0; idx<envc; idx++)
2288 {
2289 const char *env_entry = env.GetStringAtIndex (idx);
2290 if (env_entry)
2291 {
2292 const char *equal_pos = ::strchr(env_entry, '=');
2293 ConstString key;
2294 // It is ok to have environment variables with no values
2295 const char *value = NULL;
2296 if (equal_pos)
2297 {
2298 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2299 if (equal_pos[1])
2300 value = equal_pos + 1;
2301 }
2302 else
2303 {
2304 key.SetCString(env_entry);
2305 }
2306 // Don't allow existing keys to be replaced with ones we get from the platform environment
2307 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2308 }
2309 }
2310 }
2311 }
2312 }
2313 }
2314 }
2315 }
2316 }
2317 Target *m_target;
2318 mutable bool m_got_host_env;
2319};
2320
2321TargetProperties::TargetProperties (Target *target) :
2322 Properties ()
2323{
2324 if (target)
2325 {
2326 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002327 }
2328 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002329 {
2330 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2331 m_collection_sp->Initialize(g_properties);
2332 m_collection_sp->AppendProperty(ConstString("process"),
2333 ConstString("Settings specify to processes."),
2334 true,
2335 Process::GetGlobalProperties()->GetValueProperties());
2336 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002337}
2338
Greg Clayton73844aa2012-08-22 17:17:09 +00002339TargetProperties::~TargetProperties ()
2340{
2341}
2342ArchSpec
2343TargetProperties::GetDefaultArchitecture () const
2344{
2345 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2346 if (value)
2347 return value->GetCurrentValue();
2348 return ArchSpec();
2349}
2350
2351void
2352TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2353{
2354 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2355 if (value)
2356 return value->SetCurrentValue(arch, true);
2357}
2358
2359lldb::DynamicValueType
2360TargetProperties::GetPreferDynamicValue() const
2361{
2362 const uint32_t idx = ePropertyPreferDynamic;
2363 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2364}
2365
2366bool
2367TargetProperties::GetDisableASLR () const
2368{
2369 const uint32_t idx = ePropertyDisableASLR;
2370 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2371}
2372
2373void
2374TargetProperties::SetDisableASLR (bool b)
2375{
2376 const uint32_t idx = ePropertyDisableASLR;
2377 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2378}
2379
2380bool
2381TargetProperties::GetDisableSTDIO () const
2382{
2383 const uint32_t idx = ePropertyDisableSTDIO;
2384 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2385}
2386
2387void
2388TargetProperties::SetDisableSTDIO (bool b)
2389{
2390 const uint32_t idx = ePropertyDisableSTDIO;
2391 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2392}
2393
Greg Clayton49ce8962012-08-29 21:13:06 +00002394InlineStrategy
2395TargetProperties::GetInlineStrategy () const
2396{
2397 const uint32_t idx = ePropertyInlineStrategy;
2398 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2399}
2400
Greg Clayton0c8446c2012-10-17 22:57:12 +00002401const char *
2402TargetProperties::GetArg0 () const
2403{
2404 const uint32_t idx = ePropertyArg0;
2405 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2406}
2407
2408void
2409TargetProperties::SetArg0 (const char *arg)
2410{
2411 const uint32_t idx = ePropertyArg0;
2412 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2413}
2414
Greg Clayton73844aa2012-08-22 17:17:09 +00002415bool
2416TargetProperties::GetRunArguments (Args &args) const
2417{
2418 const uint32_t idx = ePropertyRunArgs;
2419 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2420}
2421
2422void
2423TargetProperties::SetRunArguments (const Args &args)
2424{
2425 const uint32_t idx = ePropertyRunArgs;
2426 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2427}
2428
2429size_t
2430TargetProperties::GetEnvironmentAsArgs (Args &env) const
2431{
2432 const uint32_t idx = ePropertyEnvVars;
2433 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2434}
2435
2436bool
2437TargetProperties::GetSkipPrologue() const
2438{
2439 const uint32_t idx = ePropertySkipPrologue;
2440 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2441}
2442
2443PathMappingList &
2444TargetProperties::GetSourcePathMap () const
2445{
2446 const uint32_t idx = ePropertySourceMap;
2447 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2448 assert(option_value);
2449 return option_value->GetCurrentValue();
2450}
2451
2452FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002453TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002454{
2455 const uint32_t idx = ePropertyExecutableSearchPaths;
2456 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2457 assert(option_value);
2458 return option_value->GetCurrentValue();
2459}
2460
2461bool
2462TargetProperties::GetEnableSyntheticValue () const
2463{
2464 const uint32_t idx = ePropertyEnableSynthetic;
2465 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2466}
2467
2468uint32_t
2469TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2470{
2471 const uint32_t idx = ePropertyMaxChildrenCount;
2472 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2473}
2474
2475uint32_t
2476TargetProperties::GetMaximumSizeOfStringSummary() const
2477{
2478 const uint32_t idx = ePropertyMaxSummaryLength;
2479 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2480}
2481
2482FileSpec
2483TargetProperties::GetStandardInputPath () const
2484{
2485 const uint32_t idx = ePropertyInputPath;
2486 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2487}
2488
2489void
2490TargetProperties::SetStandardInputPath (const char *p)
2491{
2492 const uint32_t idx = ePropertyInputPath;
2493 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2494}
2495
2496FileSpec
2497TargetProperties::GetStandardOutputPath () const
2498{
2499 const uint32_t idx = ePropertyOutputPath;
2500 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2501}
2502
2503void
2504TargetProperties::SetStandardOutputPath (const char *p)
2505{
2506 const uint32_t idx = ePropertyOutputPath;
2507 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2508}
2509
2510FileSpec
2511TargetProperties::GetStandardErrorPath () const
2512{
2513 const uint32_t idx = ePropertyErrorPath;
2514 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2515}
2516
Greg Claytonc6e82e42012-08-22 18:39:03 +00002517const char *
2518TargetProperties::GetExpressionPrefixContentsAsCString ()
2519{
2520 const uint32_t idx = ePropertyExprPrefix;
2521 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2522 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002523 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002524 const bool null_terminate = true;
2525 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002526 if (data_sp)
2527 return (const char *) data_sp->GetBytes();
2528 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002529 return NULL;
2530}
2531
Greg Clayton73844aa2012-08-22 17:17:09 +00002532void
2533TargetProperties::SetStandardErrorPath (const char *p)
2534{
2535 const uint32_t idx = ePropertyErrorPath;
2536 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2537}
2538
2539bool
2540TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2541{
2542 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2543 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2544}
2545
2546const TargetPropertiesSP &
2547Target::GetGlobalProperties()
2548{
2549 static TargetPropertiesSP g_settings_sp;
2550 if (!g_settings_sp)
2551 {
2552 g_settings_sp.reset (new TargetProperties (NULL));
2553 }
2554 return g_settings_sp;
2555}
2556
Jim Ingham5a15e692012-02-16 06:50:00 +00002557const ConstString &
2558Target::TargetEventData::GetFlavorString ()
2559{
2560 static ConstString g_flavor ("Target::TargetEventData");
2561 return g_flavor;
2562}
2563
2564const ConstString &
2565Target::TargetEventData::GetFlavor () const
2566{
2567 return TargetEventData::GetFlavorString ();
2568}
2569
2570Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2571 EventData(),
2572 m_target_sp (new_target_sp)
2573{
2574}
2575
2576Target::TargetEventData::~TargetEventData()
2577{
2578
2579}
2580
2581void
2582Target::TargetEventData::Dump (Stream *s) const
2583{
2584
2585}
2586
2587const TargetSP
2588Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2589{
2590 TargetSP target_sp;
2591
2592 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2593 if (data)
2594 target_sp = data->m_target_sp;
2595
2596 return target_sp;
2597}
2598
2599const Target::TargetEventData *
2600Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2601{
2602 if (event_ptr)
2603 {
2604 const EventData *event_data = event_ptr->GetData();
2605 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2606 return static_cast <const TargetEventData *> (event_ptr->GetData());
2607 }
2608 return NULL;
2609}
2610