blob: 219453b21fc25e53c7b399f17200f2455095c758 [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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham03c8ee52011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton427f2902010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000031#include "lldb/Core/Timer.h"
32#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000033#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000034#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000036#include "lldb/Interpreter/CommandInterpreter.h"
37#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chen3f883492012-06-04 23:19:54 +000038#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton73844aa2012-08-22 17:17:09 +000039#include "lldb/Interpreter/OptionValues.h"
40#include "lldb/Interpreter/Property.h"
Chris Lattner24943d22010-06-08 16:52:24 +000041#include "lldb/lldb-private-log.h"
42#include "lldb/Symbol/ObjectFile.h"
43#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000044#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000045#include "lldb/Target/Thread.h"
46#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000047
48using namespace lldb;
49using namespace lldb_private;
50
Jim Ingham5a15e692012-02-16 06:50:00 +000051ConstString &
52Target::GetStaticBroadcasterClass ()
53{
54 static ConstString class_name ("lldb.target");
55 return class_name;
56}
57
Chris Lattner24943d22010-06-08 16:52:24 +000058//----------------------------------------------------------------------
59// Target constructor
60//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000061Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton73844aa2012-08-22 17:17:09 +000062 TargetProperties (this),
Jim Ingham94a5d0d2012-10-10 18:32:14 +000063 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton24bc5d92011-03-30 18:16:51 +000064 ExecutionContextScope (),
Greg Clayton63094e02010-06-23 01:19:29 +000065 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000066 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000067 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000068 m_arch (target_arch),
Jason Molendaaa9b36f2012-12-13 01:54:18 +000069#ifndef LLDB_DISABLE_PYTHON
Enrico Granata146d9522012-11-08 02:22:02 +000070 m_images (this),
Jason Molendaaa9b36f2012-12-13 01:54:18 +000071#else
72 // FIXME: The module added notification needed for python scripting support
73 // causes a problem with in-memory-only Modules at startup if we have
74 // a breakpoint (the ObjectFile is parsed before we've set the Section load
75 // addresses leading to an invalid __LINKEDIT section addr on Mac OS X and
76 // all the problems that will happen from that).
77 // As a temporary solution for iOS debugging (where all the modules are in-memory-only),
78 // disable this notification system there. The problem could still happen on
79 // an x86 system but it is much less common.
80 // <rdar://problem/12831670> describes the failure mode for on-iOS debugging.
81 m_images (NULL),
82#endif
Greg Claytoneea26402010-09-14 23:36:40 +000083 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000084 m_breakpoint_list (false),
85 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000086 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000087 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000088 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000089 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000090 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000091 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000092 m_scratch_ast_source_ap (NULL),
93 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000094 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000095 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000096 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000097 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000098 m_suppress_stop_hooks (false),
99 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000100{
Greg Clayton49ce6822010-10-31 03:01:06 +0000101 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
102 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
103 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham9c970a32012-12-18 02:03:49 +0000104 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Jim Ingham5a15e692012-02-16 06:50:00 +0000105
106 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +0000107
Greg Claytone005f2c2010-11-06 01:53:30 +0000108 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000109 if (log)
110 log->Printf ("%p Target::Target()", this);
Jason Molenda8c6cf432012-12-05 00:25:49 +0000111 if (m_arch.IsValid())
112 {
Jason Molenda332dc002012-12-12 02:23:56 +0000113 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::Target created with architecture %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jason Molenda8c6cf432012-12-05 00:25:49 +0000114 }
Chris Lattner24943d22010-06-08 16:52:24 +0000115}
116
117//----------------------------------------------------------------------
118// Destructor
119//----------------------------------------------------------------------
120Target::~Target()
121{
Greg Claytone005f2c2010-11-06 01:53:30 +0000122 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000123 if (log)
124 log->Printf ("%p Target::~Target()", this);
125 DeleteCurrentProcess ();
126}
127
128void
Caroline Tice7826c882010-10-26 03:11:13 +0000129Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000130{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000131// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000132 if (description_level != lldb::eDescriptionLevelBrief)
133 {
134 s->Indent();
135 s->PutCString("Target\n");
136 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000137 m_images.Dump(s);
138 m_breakpoint_list.Dump(s);
139 m_internal_breakpoint_list.Dump(s);
140 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000141 }
142 else
143 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000144 Module *exe_module = GetExecutableModulePointer();
145 if (exe_module)
146 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000147 else
148 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000149 }
Chris Lattner24943d22010-06-08 16:52:24 +0000150}
151
152void
Greg Clayton0bce9a22012-12-05 00:16:59 +0000153Target::CleanupProcess ()
154{
155 // Do any cleanup of the target we need to do between process instances.
156 // NB It is better to do this before destroying the process in case the
157 // clean up needs some help from the process.
158 m_breakpoint_list.ClearAllBreakpointSites();
159 m_internal_breakpoint_list.ClearAllBreakpointSites();
160 // Disable watchpoints just on the debugger side.
161 Mutex::Locker locker;
162 this->GetWatchpointList().GetListMutex(locker);
163 DisableAllWatchpoints(false);
164 ClearAllWatchpointHitCounts();
165}
166
167void
Chris Lattner24943d22010-06-08 16:52:24 +0000168Target::DeleteCurrentProcess ()
169{
170 if (m_process_sp.get())
171 {
Greg Clayton49480b12010-09-14 23:52:43 +0000172 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000173 if (m_process_sp->IsAlive())
174 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000175
176 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000177
Greg Clayton0bce9a22012-12-05 00:16:59 +0000178 CleanupProcess ();
179
Chris Lattner24943d22010-06-08 16:52:24 +0000180 m_process_sp.reset();
181 }
182}
183
184const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000185Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000186{
187 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000188 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000189 return m_process_sp;
190}
191
192const lldb::ProcessSP &
193Target::GetProcessSP () const
194{
195 return m_process_sp;
196}
197
Greg Clayton153ccd72011-08-10 02:10:13 +0000198void
199Target::Destroy()
200{
201 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000202 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000203 DeleteCurrentProcess ();
204 m_platform_sp.reset();
205 m_arch.Clear();
206 m_images.Clear();
207 m_section_load_list.Clear();
208 const bool notify = false;
209 m_breakpoint_list.RemoveAll(notify);
210 m_internal_breakpoint_list.RemoveAll(notify);
211 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000212 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000213 m_search_filter_sp.reset();
214 m_image_search_paths.Clear(notify);
215 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000216 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000217 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000218 m_persistent_variables.Clear();
219 m_stop_hooks.clear();
220 m_stop_hook_next_id = 0;
221 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000222 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000223}
224
225
Chris Lattner24943d22010-06-08 16:52:24 +0000226BreakpointList &
227Target::GetBreakpointList(bool internal)
228{
229 if (internal)
230 return m_internal_breakpoint_list;
231 else
232 return m_breakpoint_list;
233}
234
235const BreakpointList &
236Target::GetBreakpointList(bool internal) const
237{
238 if (internal)
239 return m_internal_breakpoint_list;
240 else
241 return m_breakpoint_list;
242}
243
244BreakpointSP
245Target::GetBreakpointByID (break_id_t break_id)
246{
247 BreakpointSP bp_sp;
248
249 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
250 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
251 else
252 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
253
254 return bp_sp;
255}
256
257BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000258Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000259 const FileSpecList *source_file_spec_list,
260 RegularExpression &source_regex,
261 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000262{
Jim Inghamd6d47972011-09-23 00:54:11 +0000263 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
264 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000265 return CreateBreakpoint (filter_sp, resolver_sp, internal);
266}
267
268
269BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000270Target::CreateBreakpoint (const FileSpecList *containingModules,
271 const FileSpec &file,
272 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000273 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000274 LazyBool skip_prologue,
275 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000276{
Greg Clayton49ce8962012-08-29 21:13:06 +0000277 if (check_inlines == eLazyBoolCalculate)
278 {
279 const InlineStrategy inline_strategy = GetInlineStrategy();
280 switch (inline_strategy)
281 {
282 case eInlineBreakpointsNever:
283 check_inlines = eLazyBoolNo;
284 break;
285
286 case eInlineBreakpointsHeaders:
287 if (file.IsSourceImplementationFile())
288 check_inlines = eLazyBoolNo;
289 else
290 check_inlines = eLazyBoolYes;
291 break;
292
293 case eInlineBreakpointsAlways:
294 check_inlines = eLazyBoolYes;
295 break;
296 }
297 }
Greg Clayton46365522012-09-07 23:48:57 +0000298 SearchFilterSP filter_sp;
299 if (check_inlines == eLazyBoolNo)
300 {
301 // Not checking for inlines, we are looking only for matching compile units
302 FileSpecList compile_unit_list;
303 compile_unit_list.Append (file);
304 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
305 }
306 else
307 {
308 filter_sp = GetSearchFilterForModuleList (containingModules);
309 }
Greg Clayton49ce8962012-08-29 21:13:06 +0000310 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
311 file,
312 line_no,
313 check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000314 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000315 return CreateBreakpoint (filter_sp, resolver_sp, internal);
316}
317
318
319BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000320Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000321{
Chris Lattner24943d22010-06-08 16:52:24 +0000322 Address so_addr;
323 // Attempt to resolve our load address if possible, though it is ok if
324 // it doesn't resolve to section/offset.
325
Greg Clayton33ed1702010-08-24 00:45:41 +0000326 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000327 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000328 if (!so_addr.IsValid())
329 {
330 // The address didn't resolve, so just set this as an absolute address
331 so_addr.SetOffset (addr);
332 }
333 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000334 return bp_sp;
335}
336
337BreakpointSP
338Target::CreateBreakpoint (Address &addr, bool internal)
339{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000340 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000341 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
342 return CreateBreakpoint (filter_sp, resolver_sp, internal);
343}
344
345BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000346Target::CreateBreakpoint (const FileSpecList *containingModules,
347 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000348 const char *func_name,
349 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000350 LazyBool skip_prologue,
351 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000352{
Greg Clayton12bec712010-06-28 21:30:43 +0000353 BreakpointSP bp_sp;
354 if (func_name)
355 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000356 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000357
358 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
359 func_name,
360 func_name_type_mask,
361 Breakpoint::Exact,
362 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000363 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
364 }
365 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000366}
367
Jim Ingham4722b102012-03-06 00:37:27 +0000368lldb::BreakpointSP
369Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000370 const FileSpecList *containingSourceFiles,
371 const std::vector<std::string> &func_names,
372 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000373 LazyBool skip_prologue,
374 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000375{
376 BreakpointSP bp_sp;
377 size_t num_names = func_names.size();
378 if (num_names > 0)
379 {
380 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
381
382 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
383 func_names,
384 func_name_type_mask,
385 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
386 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
387 }
388 return bp_sp;
389}
390
Jim Inghamc1053622012-03-03 02:05:11 +0000391BreakpointSP
392Target::CreateBreakpoint (const FileSpecList *containingModules,
393 const FileSpecList *containingSourceFiles,
394 const char *func_names[],
395 size_t num_names,
396 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000397 LazyBool skip_prologue,
398 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000399{
400 BreakpointSP bp_sp;
401 if (num_names > 0)
402 {
403 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
404
405 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
406 func_names,
407 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000408 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000409 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
410 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
411 }
412 return bp_sp;
413}
Chris Lattner24943d22010-06-08 16:52:24 +0000414
415SearchFilterSP
416Target::GetSearchFilterForModule (const FileSpec *containingModule)
417{
418 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000419 if (containingModule != NULL)
420 {
421 // TODO: We should look into sharing module based search filters
422 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000423 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000424 }
425 else
426 {
427 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000428 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000429 filter_sp = m_search_filter_sp;
430 }
431 return filter_sp;
432}
433
Jim Ingham03c8ee52011-09-21 01:17:13 +0000434SearchFilterSP
435Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
436{
437 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000438 if (containingModules && containingModules->GetSize() != 0)
439 {
440 // TODO: We should look into sharing module based search filters
441 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000442 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000443 }
444 else
445 {
446 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000447 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000448 filter_sp = m_search_filter_sp;
449 }
450 return filter_sp;
451}
452
Jim Inghamd6d47972011-09-23 00:54:11 +0000453SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000454Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
455 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000456{
457 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
458 return GetSearchFilterForModuleList(containingModules);
459
460 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000461 if (containingModules == NULL)
462 {
463 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
464 // but that will take a little reworking.
465
Greg Clayton13d24fb2012-01-29 20:56:30 +0000466 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000467 }
468 else
469 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000470 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000471 }
472 return filter_sp;
473}
474
Chris Lattner24943d22010-06-08 16:52:24 +0000475BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000476Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000477 const FileSpecList *containingSourceFiles,
478 RegularExpression &func_regex,
479 LazyBool skip_prologue,
480 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000481{
Jim Inghamd6d47972011-09-23 00:54:11 +0000482 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000483 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
484 func_regex,
485 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000486
487 return CreateBreakpoint (filter_sp, resolver_sp, internal);
488}
489
Jim Ingham3df164e2012-03-05 04:47:34 +0000490lldb::BreakpointSP
491Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
492{
493 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
494}
495
Chris Lattner24943d22010-06-08 16:52:24 +0000496BreakpointSP
497Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
498{
499 BreakpointSP bp_sp;
500 if (filter_sp && resolver_sp)
501 {
502 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
503 resolver_sp->SetBreakpoint (bp_sp.get());
504
505 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000506 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000507 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000508 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000509
Greg Claytone005f2c2010-11-06 01:53:30 +0000510 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000511 if (log)
512 {
513 StreamString s;
514 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
515 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
516 }
517
Chris Lattner24943d22010-06-08 16:52:24 +0000518 bp_sp->ResolveBreakpoint();
519 }
Jim Inghamd1686902010-10-14 23:45:03 +0000520
521 if (!internal && bp_sp)
522 {
523 m_last_created_breakpoint = bp_sp;
524 }
525
Chris Lattner24943d22010-06-08 16:52:24 +0000526 return bp_sp;
527}
528
Johnny Chenda5a8022011-09-20 23:28:55 +0000529bool
530Target::ProcessIsValid()
531{
532 return (m_process_sp && m_process_sp->IsAlive());
533}
534
Johnny Chen3f883492012-06-04 23:19:54 +0000535static bool
536CheckIfWatchpointsExhausted(Target *target, Error &error)
537{
538 uint32_t num_supported_hardware_watchpoints;
539 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
540 if (rc.Success())
541 {
542 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
543 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
544 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
545 num_supported_hardware_watchpoints);
546 }
547 return false;
548}
549
Johnny Chenecd4feb2011-10-14 00:42:25 +0000550// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000551// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000552WatchpointSP
Jim Ingham9e376622012-10-23 07:20:06 +0000553Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000554{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000555 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
556 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000557 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Ingham9e376622012-10-23 07:20:06 +0000558 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000559
Johnny Chenecd4feb2011-10-14 00:42:25 +0000560 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000561 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000562 {
563 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000564 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000565 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000566 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000567 {
568 if (size == 0)
569 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
570 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000571 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000572 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000573 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000574
Johnny Chenecd4feb2011-10-14 00:42:25 +0000575 // Currently we only support one watchpoint per address, with total number
576 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000577
578 // Grab the list mutex while doing operations.
Jim Ingham9c970a32012-12-18 02:03:49 +0000579 const bool notify = false; // Don't notify about all the state changes we do on creating the watchpoint.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000580 Mutex::Locker locker;
581 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000582 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000583 if (matched_sp)
584 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000585 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000586 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000587 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
588 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000589 // Return the existing watchpoint if both size and type match.
Jim Ingham9e376622012-10-23 07:20:06 +0000590 if (size == old_size && kind == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000591 wp_sp = matched_sp;
Jim Ingham9c970a32012-12-18 02:03:49 +0000592 wp_sp->SetEnabled(false, notify);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000593 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000594 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham9c970a32012-12-18 02:03:49 +0000595 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
596 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000597 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000598 }
599
Jason Molendac4d49fd2012-12-05 23:07:34 +0000600 if (!wp_sp)
601 {
Jim Ingham9c970a32012-12-18 02:03:49 +0000602 wp_sp.reset(new Watchpoint(*this, addr, size, type));
603 wp_sp->SetWatchpointType(kind, notify);
604 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000605 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000606
Jim Ingham9c970a32012-12-18 02:03:49 +0000607 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000608 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +0000609 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
610 __FUNCTION__,
611 error.Success() ? "succeeded" : "failed",
612 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000613
Jason Molendac4d49fd2012-12-05 23:07:34 +0000614 if (error.Fail())
615 {
Johnny Chen155599b2012-03-26 22:00:10 +0000616 // Enabling the watchpoint on the device side failed.
617 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham9c970a32012-12-18 02:03:49 +0000618 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chen3f883492012-06-04 23:19:54 +0000619 // See if we could provide more helpful error message.
620 if (!CheckIfWatchpointsExhausted(this, error))
621 {
622 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
623 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
624 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000625 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000626 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000627 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000628 m_last_created_watchpoint = wp_sp;
629 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000630}
631
Chris Lattner24943d22010-06-08 16:52:24 +0000632void
633Target::RemoveAllBreakpoints (bool internal_also)
634{
Greg Claytone005f2c2010-11-06 01:53:30 +0000635 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000636 if (log)
637 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
638
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000639 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000640 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000641 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000642
643 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000644}
645
646void
647Target::DisableAllBreakpoints (bool internal_also)
648{
Greg Claytone005f2c2010-11-06 01:53:30 +0000649 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000650 if (log)
651 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
652
653 m_breakpoint_list.SetEnabledAll (false);
654 if (internal_also)
655 m_internal_breakpoint_list.SetEnabledAll (false);
656}
657
658void
659Target::EnableAllBreakpoints (bool internal_also)
660{
Greg Claytone005f2c2010-11-06 01:53:30 +0000661 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000662 if (log)
663 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
664
665 m_breakpoint_list.SetEnabledAll (true);
666 if (internal_also)
667 m_internal_breakpoint_list.SetEnabledAll (true);
668}
669
670bool
671Target::RemoveBreakpointByID (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 if (DisableBreakpointByID (break_id))
678 {
679 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000680 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000681 else
Jim Inghamd1686902010-10-14 23:45:03 +0000682 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000683 if (m_last_created_breakpoint)
684 {
685 if (m_last_created_breakpoint->GetID() == break_id)
686 m_last_created_breakpoint.reset();
687 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000688 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000689 }
Chris Lattner24943d22010-06-08 16:52:24 +0000690 return true;
691 }
692 return false;
693}
694
695bool
696Target::DisableBreakpointByID (break_id_t break_id)
697{
Greg Claytone005f2c2010-11-06 01:53:30 +0000698 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000699 if (log)
700 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
701
702 BreakpointSP bp_sp;
703
704 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
705 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
706 else
707 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
708 if (bp_sp)
709 {
710 bp_sp->SetEnabled (false);
711 return true;
712 }
713 return false;
714}
715
716bool
717Target::EnableBreakpointByID (break_id_t break_id)
718{
Greg Claytone005f2c2010-11-06 01:53:30 +0000719 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000720 if (log)
721 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
722 __FUNCTION__,
723 break_id,
724 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
725
726 BreakpointSP bp_sp;
727
728 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
729 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
730 else
731 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
732
733 if (bp_sp)
734 {
735 bp_sp->SetEnabled (true);
736 return true;
737 }
738 return false;
739}
740
Johnny Chenc86582f2011-09-23 21:21:43 +0000741// The flag 'end_to_end', default to true, signifies that the operation is
742// performed end to end, for both the debugger and the debuggee.
743
Johnny Chenecd4feb2011-10-14 00:42:25 +0000744// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
745// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000746bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000747Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000748{
749 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
750 if (log)
751 log->Printf ("Target::%s\n", __FUNCTION__);
752
Johnny Chenc86582f2011-09-23 21:21:43 +0000753 if (!end_to_end) {
Jim Ingham9c970a32012-12-18 02:03:49 +0000754 m_watchpoint_list.RemoveAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000755 return true;
756 }
757
758 // Otherwise, it's an end to end operation.
759
Johnny Chenda5a8022011-09-20 23:28:55 +0000760 if (!ProcessIsValid())
761 return false;
762
Johnny Chenecd4feb2011-10-14 00:42:25 +0000763 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000764 for (size_t i = 0; i < num_watchpoints; ++i)
765 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000766 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
767 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000768 return false;
769
Johnny Chenecd4feb2011-10-14 00:42:25 +0000770 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000771 if (rc.Fail())
772 return false;
773 }
Jim Ingham9c970a32012-12-18 02:03:49 +0000774 m_watchpoint_list.RemoveAll (true);
Johnny Chenda5a8022011-09-20 23:28:55 +0000775 return true; // Success!
776}
777
Johnny Chenecd4feb2011-10-14 00:42:25 +0000778// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
779// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000780bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000781Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000782{
783 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
784 if (log)
785 log->Printf ("Target::%s\n", __FUNCTION__);
786
Johnny Chenc86582f2011-09-23 21:21:43 +0000787 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000788 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000789 return true;
790 }
791
792 // Otherwise, it's an end to end operation.
793
Johnny Chenda5a8022011-09-20 23:28:55 +0000794 if (!ProcessIsValid())
795 return false;
796
Johnny Chenecd4feb2011-10-14 00:42:25 +0000797 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000798 for (size_t i = 0; i < num_watchpoints; ++i)
799 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000800 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
801 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000802 return false;
803
Johnny Chenecd4feb2011-10-14 00:42:25 +0000804 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000805 if (rc.Fail())
806 return false;
807 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000808 return true; // Success!
809}
810
Johnny Chenecd4feb2011-10-14 00:42:25 +0000811// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
812// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000813bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000814Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000815{
816 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
817 if (log)
818 log->Printf ("Target::%s\n", __FUNCTION__);
819
Johnny Chenc86582f2011-09-23 21:21:43 +0000820 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000821 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000822 return true;
823 }
824
825 // Otherwise, it's an end to end operation.
826
Johnny Chenda5a8022011-09-20 23:28:55 +0000827 if (!ProcessIsValid())
828 return false;
829
Johnny Chenecd4feb2011-10-14 00:42:25 +0000830 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000831 for (size_t i = 0; i < num_watchpoints; ++i)
832 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000833 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
834 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000835 return false;
836
Johnny Chenecd4feb2011-10-14 00:42:25 +0000837 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000838 if (rc.Fail())
839 return false;
840 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000841 return true; // Success!
842}
843
Johnny Chen116a5cd2012-02-25 06:44:30 +0000844// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
845bool
846Target::ClearAllWatchpointHitCounts ()
847{
848 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
849 if (log)
850 log->Printf ("Target::%s\n", __FUNCTION__);
851
852 size_t num_watchpoints = m_watchpoint_list.GetSize();
853 for (size_t i = 0; i < num_watchpoints; ++i)
854 {
855 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
856 if (!wp_sp)
857 return false;
858
859 wp_sp->ResetHitCount();
860 }
861 return true; // Success!
862}
863
Johnny Chenecd4feb2011-10-14 00:42:25 +0000864// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000865// during these operations.
866bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000867Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000868{
869 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
870 if (log)
871 log->Printf ("Target::%s\n", __FUNCTION__);
872
873 if (!ProcessIsValid())
874 return false;
875
Johnny Chenecd4feb2011-10-14 00:42:25 +0000876 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000877 for (size_t i = 0; i < num_watchpoints; ++i)
878 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000879 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
880 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000881 return false;
882
Johnny Chenecd4feb2011-10-14 00:42:25 +0000883 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000884 }
885 return true; // Success!
886}
887
Johnny Chenecd4feb2011-10-14 00:42:25 +0000888// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000889bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000890Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000891{
892 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
893 if (log)
894 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
895
896 if (!ProcessIsValid())
897 return false;
898
Johnny Chenecd4feb2011-10-14 00:42:25 +0000899 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
900 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000901 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000902 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000903 if (rc.Success())
904 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000905
Johnny Chen01acfa72011-09-22 18:04:58 +0000906 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000907 }
908 return false;
909}
910
Johnny Chenecd4feb2011-10-14 00:42:25 +0000911// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000912bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000913Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000914{
915 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
916 if (log)
917 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
918
919 if (!ProcessIsValid())
920 return false;
921
Johnny Chenecd4feb2011-10-14 00:42:25 +0000922 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
923 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000924 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000925 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000926 if (rc.Success())
927 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000928
Johnny Chen01acfa72011-09-22 18:04:58 +0000929 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000930 }
931 return false;
932}
933
Johnny Chenecd4feb2011-10-14 00:42:25 +0000934// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000935bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000936Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000937{
938 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
939 if (log)
940 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
941
Johnny Chenecd4feb2011-10-14 00:42:25 +0000942 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000943 {
Jim Ingham9c970a32012-12-18 02:03:49 +0000944 m_watchpoint_list.Remove(watch_id, true);
Johnny Chenda5a8022011-09-20 23:28:55 +0000945 return true;
946 }
947 return false;
948}
949
Johnny Chenecd4feb2011-10-14 00:42:25 +0000950// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000951bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000952Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000953{
954 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
955 if (log)
956 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
957
958 if (!ProcessIsValid())
959 return false;
960
Johnny Chenecd4feb2011-10-14 00:42:25 +0000961 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
962 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000963 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000964 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000965 return true;
966 }
967 return false;
968}
969
Chris Lattner24943d22010-06-08 16:52:24 +0000970ModuleSP
971Target::GetExecutableModule ()
972{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000973 return m_images.GetModuleAtIndex(0);
974}
975
976Module*
977Target::GetExecutableModulePointer ()
978{
979 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000980}
981
Enrico Granata146d9522012-11-08 02:22:02 +0000982static void
983LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
984{
985 Error error;
986 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error))
987 {
988 target->GetDebugger().GetOutputStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
989 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
990 error.AsCString());
991 }
992}
993
Chris Lattner24943d22010-06-08 16:52:24 +0000994void
995Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
996{
Jason Molenda8c6cf432012-12-05 00:25:49 +0000997 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Chris Lattner24943d22010-06-08 16:52:24 +0000998 m_images.Clear();
999 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +00001000 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001001 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001002
1003 if (executable_sp.get())
1004 {
1005 Timer scoped_timer (__PRETTY_FUNCTION__,
1006 "Target::SetExecutableModule (executable = '%s/%s')",
1007 executable_sp->GetFileSpec().GetDirectory().AsCString(),
1008 executable_sp->GetFileSpec().GetFilename().AsCString());
1009
1010 m_images.Append(executable_sp); // The first image is our exectuable file
1011
Jim Ingham7508e732010-08-09 23:31:02 +00001012 // 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 +00001013 if (!m_arch.IsValid())
Jason Molenda8c6cf432012-12-05 00:25:49 +00001014 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001015 m_arch = executable_sp->GetArchitecture();
Jason Molenda8c6cf432012-12-05 00:25:49 +00001016 if (log)
1017 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1018 }
1019
Chris Lattner24943d22010-06-08 16:52:24 +00001020 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001021 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +00001022
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001023 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +00001024 {
1025 executable_objfile->GetDependentModules(dependent_files);
1026 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1027 {
Greg Claytonb1888f22011-03-19 01:12:21 +00001028 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1029 FileSpec platform_dependent_file_spec;
1030 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +00001031 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +00001032 else
1033 platform_dependent_file_spec = dependent_file_spec;
1034
Greg Clayton444fe992012-02-26 05:51:37 +00001035 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1036 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +00001037 if (image_module_sp.get())
1038 {
Chris Lattner24943d22010-06-08 16:52:24 +00001039 ObjectFile *objfile = image_module_sp->GetObjectFile();
1040 if (objfile)
1041 objfile->GetDependentModules(dependent_files);
1042 }
1043 }
1044 }
Chris Lattner24943d22010-06-08 16:52:24 +00001045 }
1046}
1047
1048
Jim Ingham7508e732010-08-09 23:31:02 +00001049bool
1050Target::SetArchitecture (const ArchSpec &arch_spec)
1051{
Jason Molenda8c6cf432012-12-05 00:25:49 +00001052 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callanan40e278c2012-12-13 22:07:14 +00001053 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001054 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001055 // If we haven't got a valid arch spec, or the architectures are
1056 // compatible, so just update the architecture. Architectures can be
1057 // equal, yet the triple OS and vendor might change, so we need to do
1058 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001059 m_arch = arch_spec;
Jason Molenda8c6cf432012-12-05 00:25:49 +00001060 if (log)
1061 log->Printf ("Target::SetArchitecture setting architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +00001062 return true;
1063 }
1064 else
1065 {
1066 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molenda8c6cf432012-12-05 00:25:49 +00001067 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +00001068 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001069 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001070 ModuleSP executable_sp = GetExecutableModule ();
1071 m_images.Clear();
1072 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001073 m_scratch_ast_source_ap.reset();
1074 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001075 // Need to do something about unsetting breakpoints.
1076
1077 if (executable_sp)
1078 {
Jason Molenda8c6cf432012-12-05 00:25:49 +00001079 if (log)
1080 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Greg Clayton444fe992012-02-26 05:51:37 +00001081 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1082 Error error = ModuleList::GetSharedModule (module_spec,
1083 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001084 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001085 NULL,
1086 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001087
1088 if (!error.Fail() && executable_sp)
1089 {
1090 SetExecutableModule (executable_sp, true);
1091 return true;
1092 }
Jim Ingham7508e732010-08-09 23:31:02 +00001093 }
1094 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001095 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001096}
Chris Lattner24943d22010-06-08 16:52:24 +00001097
Chris Lattner24943d22010-06-08 16:52:24 +00001098void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001099Target::WillClearList (const ModuleList& module_list)
Enrico Granata146d9522012-11-08 02:22:02 +00001100{
1101}
1102
1103void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001104Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001105{
1106 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001107 ModuleList my_module_list;
1108 my_module_list.Append(module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +00001109 LoadScriptingResourceForModule(module_sp, this);
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001110 ModulesDidLoad (my_module_list);
Chris Lattner24943d22010-06-08 16:52:24 +00001111}
1112
1113void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001114Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata146d9522012-11-08 02:22:02 +00001115{
1116 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001117 ModuleList my_module_list;
1118 my_module_list.Append(module_sp);
1119 ModulesDidUnload (my_module_list);
Enrico Granata146d9522012-11-08 02:22:02 +00001120}
1121
1122void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001123Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001124{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001125 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001126 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001127}
1128
1129void
1130Target::ModulesDidLoad (ModuleList &module_list)
1131{
Enrico Granata146d9522012-11-08 02:22:02 +00001132 if (module_list.GetSize())
1133 {
1134 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1135 // TODO: make event data that packages up the module_list
1136 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1137 }
Chris Lattner24943d22010-06-08 16:52:24 +00001138}
1139
1140void
1141Target::ModulesDidUnload (ModuleList &module_list)
1142{
Enrico Granata146d9522012-11-08 02:22:02 +00001143 if (module_list.GetSize())
1144 {
1145 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1146 // TODO: make event data that packages up the module_list
1147 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1148 }
Chris Lattner24943d22010-06-08 16:52:24 +00001149}
1150
Daniel Dunbar705a0982011-10-31 22:50:37 +00001151bool
Greg Clayton444fe992012-02-26 05:51:37 +00001152Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001153{
Greg Clayton73844aa2012-08-22 17:17:09 +00001154 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001155 {
1156 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001157 ModuleSpec module_spec (module_file_spec);
1158 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001159
1160 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1161 // black list.
1162 if (num_modules > 0)
1163 {
1164 for (int i = 0; i < num_modules; i++)
1165 {
1166 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1167 return false;
1168 }
1169 return true;
1170 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001171 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001172 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001173}
1174
Daniel Dunbar705a0982011-10-31 22:50:37 +00001175bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001176Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1177{
Greg Clayton73844aa2012-08-22 17:17:09 +00001178 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001179 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001180 if (m_platform_sp)
1181 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001182 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001183 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001184}
1185
Chris Lattner24943d22010-06-08 16:52:24 +00001186size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001187Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1188{
Greg Clayton3508c382012-02-24 01:59:29 +00001189 SectionSP section_sp (addr.GetSection());
1190 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001191 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001192 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1193 if (section_sp->IsEncrypted())
1194 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001195 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001196 return 0;
1197 }
Greg Clayton3508c382012-02-24 01:59:29 +00001198 ModuleSP module_sp (section_sp->GetModule());
1199 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001200 {
Greg Clayton3508c382012-02-24 01:59:29 +00001201 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1202 if (objfile)
1203 {
1204 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1205 addr.GetOffset(),
1206 dst,
1207 dst_len);
1208 if (bytes_read > 0)
1209 return bytes_read;
1210 else
1211 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1212 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001213 else
Greg Clayton3508c382012-02-24 01:59:29 +00001214 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001215 }
1216 else
Greg Clayton3508c382012-02-24 01:59:29 +00001217 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001218 }
1219 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001220 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001221
Greg Clayton26100dc2011-01-07 01:57:07 +00001222 return 0;
1223}
1224
1225size_t
Enrico Granata91544802011-09-06 19:20:51 +00001226Target::ReadMemory (const Address& addr,
1227 bool prefer_file_cache,
1228 void *dst,
1229 size_t dst_len,
1230 Error &error,
1231 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001232{
Chris Lattner24943d22010-06-08 16:52:24 +00001233 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001234
Enrico Granata91544802011-09-06 19:20:51 +00001235 // if we end up reading this from process memory, we will fill this
1236 // with the actual load address
1237 if (load_addr_ptr)
1238 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1239
Greg Clayton26100dc2011-01-07 01:57:07 +00001240 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001241
1242 addr_t load_addr = LLDB_INVALID_ADDRESS;
1243 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001244 Address resolved_addr;
1245 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001246 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001247 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001248 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001249 // No sections are loaded, so we must assume we are not running
1250 // yet and anything we are given is a file address.
1251 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1252 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001253 }
Greg Clayton70436352010-06-30 23:03:03 +00001254 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001255 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001256 // We have at least one section loaded. This can be becuase
1257 // we have manually loaded some sections with "target modules load ..."
1258 // or because we have have a live process that has sections loaded
1259 // through the dynamic loader
1260 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1261 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001262 }
Greg Clayton70436352010-06-30 23:03:03 +00001263 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001264 if (!resolved_addr.IsValid())
1265 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001266
Greg Clayton9b82f862011-07-11 05:12:02 +00001267
Greg Clayton26100dc2011-01-07 01:57:07 +00001268 if (prefer_file_cache)
1269 {
1270 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1271 if (bytes_read > 0)
1272 return bytes_read;
1273 }
Greg Clayton70436352010-06-30 23:03:03 +00001274
Johnny Chenda5a8022011-09-20 23:28:55 +00001275 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001276 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001277 if (load_addr == LLDB_INVALID_ADDRESS)
1278 load_addr = resolved_addr.GetLoadAddress (this);
1279
Greg Clayton70436352010-06-30 23:03:03 +00001280 if (load_addr == LLDB_INVALID_ADDRESS)
1281 {
Greg Clayton3508c382012-02-24 01:59:29 +00001282 ModuleSP addr_module_sp (resolved_addr.GetModule());
1283 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001284 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001285 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001286 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001287 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001288 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001289 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001290 }
1291 else
1292 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001293 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001294 if (bytes_read != dst_len)
1295 {
1296 if (error.Success())
1297 {
1298 if (bytes_read == 0)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001299 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001300 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001301 error.SetErrorStringWithFormat("only %" PRIu64 " of %" PRIu64 " bytes were read from memory at 0x%" PRIx64, (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001302 }
1303 }
Greg Clayton70436352010-06-30 23:03:03 +00001304 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001305 {
1306 if (load_addr_ptr)
1307 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001308 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001309 }
Greg Clayton70436352010-06-30 23:03:03 +00001310 // If the address is not section offset we have an address that
1311 // doesn't resolve to any address in any currently loaded shared
1312 // libaries and we failed to read memory so there isn't anything
1313 // more we can do. If it is section offset, we might be able to
1314 // read cached memory from the object file.
1315 if (!resolved_addr.IsSectionOffset())
1316 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001317 }
Chris Lattner24943d22010-06-08 16:52:24 +00001318 }
Greg Clayton70436352010-06-30 23:03:03 +00001319
Greg Clayton9b82f862011-07-11 05:12:02 +00001320 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001321 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001322 // If we didn't already try and read from the object file cache, then
1323 // try it after failing to read from the process.
1324 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001325 }
1326 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001327}
1328
Greg Clayton7dd98df2011-07-12 17:06:17 +00001329size_t
1330Target::ReadScalarIntegerFromMemory (const Address& addr,
1331 bool prefer_file_cache,
1332 uint32_t byte_size,
1333 bool is_signed,
1334 Scalar &scalar,
1335 Error &error)
1336{
1337 uint64_t uval;
1338
1339 if (byte_size <= sizeof(uval))
1340 {
1341 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1342 if (bytes_read == byte_size)
1343 {
1344 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1345 uint32_t offset = 0;
1346 if (byte_size <= 4)
1347 scalar = data.GetMaxU32 (&offset, byte_size);
1348 else
1349 scalar = data.GetMaxU64 (&offset, byte_size);
1350
1351 if (is_signed)
1352 scalar.SignExtend(byte_size * 8);
1353 return bytes_read;
1354 }
1355 }
1356 else
1357 {
1358 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1359 }
1360 return 0;
1361}
1362
1363uint64_t
1364Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1365 bool prefer_file_cache,
1366 size_t integer_byte_size,
1367 uint64_t fail_value,
1368 Error &error)
1369{
1370 Scalar scalar;
1371 if (ReadScalarIntegerFromMemory (addr,
1372 prefer_file_cache,
1373 integer_byte_size,
1374 false,
1375 scalar,
1376 error))
1377 return scalar.ULongLong(fail_value);
1378 return fail_value;
1379}
1380
1381bool
1382Target::ReadPointerFromMemory (const Address& addr,
1383 bool prefer_file_cache,
1384 Error &error,
1385 Address &pointer_addr)
1386{
1387 Scalar scalar;
1388 if (ReadScalarIntegerFromMemory (addr,
1389 prefer_file_cache,
1390 m_arch.GetAddressByteSize(),
1391 false,
1392 scalar,
1393 error))
1394 {
1395 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1396 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1397 {
1398 if (m_section_load_list.IsEmpty())
1399 {
1400 // No sections are loaded, so we must assume we are not running
1401 // yet and anything we are given is a file address.
1402 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1403 }
1404 else
1405 {
1406 // We have at least one section loaded. This can be becuase
1407 // we have manually loaded some sections with "target modules load ..."
1408 // or because we have have a live process that has sections loaded
1409 // through the dynamic loader
1410 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1411 }
1412 // We weren't able to resolve the pointer value, so just return
1413 // an address with no section
1414 if (!pointer_addr.IsValid())
1415 pointer_addr.SetOffset (pointer_vm_addr);
1416 return true;
1417
1418 }
1419 }
1420 return false;
1421}
Chris Lattner24943d22010-06-08 16:52:24 +00001422
1423ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001424Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001425{
Chris Lattner24943d22010-06-08 16:52:24 +00001426 ModuleSP module_sp;
1427
Chris Lattner24943d22010-06-08 16:52:24 +00001428 Error error;
1429
Jim Ingham03e5e512012-05-17 18:38:42 +00001430 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1431 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001432
Jim Ingham03e5e512012-05-17 18:38:42 +00001433 if (module_spec.GetUUID().IsValid())
1434 module_sp = m_images.FindFirstModule(module_spec);
1435
Greg Claytone1ef1e32012-04-27 00:58:27 +00001436 if (!module_sp)
1437 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001438 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1439 bool did_create_module = false;
1440
1441 // If there are image search path entries, try to use them first to acquire a suitable image.
1442 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001443 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001444 ModuleSpec transformed_spec (module_spec);
1445 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1446 {
1447 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1448 error = ModuleList::GetSharedModule (transformed_spec,
1449 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001450 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001451 &old_module_sp,
1452 &did_create_module);
1453 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001454 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001455
Greg Claytone1ef1e32012-04-27 00:58:27 +00001456 if (!module_sp)
1457 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001458 // If we have a UUID, we can check our global shared module list in case
1459 // we already have it. If we don't have a valid UUID, then we can't since
1460 // the path in "module_spec" will be a platform path, and we will need to
1461 // let the platform find that file. For example, we could be asking for
1462 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1463 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1464 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1465 // cache.
1466 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001467 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001468 // We have a UUID, it is OK to check the global module list...
1469 error = ModuleList::GetSharedModule (module_spec,
1470 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001471 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001472 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001473 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001474 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001475
1476 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001477 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001478 // The platform is responsible for finding and caching an appropriate
1479 // module in the shared module cache.
1480 if (m_platform_sp)
1481 {
1482 FileSpec platform_file_spec;
1483 error = m_platform_sp->GetSharedModule (module_spec,
1484 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001485 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001486 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001487 &did_create_module);
1488 }
1489 else
1490 {
1491 error.SetErrorString("no platform is currently set");
1492 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001493 }
1494 }
Chris Lattner24943d22010-06-08 16:52:24 +00001495
Jim Ingham03e5e512012-05-17 18:38:42 +00001496 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1497 // module in the list already, and if there was, let's remove it.
1498 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001499 {
Greg Clayton1649a722012-11-29 22:16:27 +00001500 ObjectFile *objfile = module_sp->GetObjectFile();
1501 if (objfile)
Jim Ingham03e5e512012-05-17 18:38:42 +00001502 {
Greg Clayton1649a722012-11-29 22:16:27 +00001503 switch (objfile->GetType())
Jim Ingham03e5e512012-05-17 18:38:42 +00001504 {
Greg Clayton1649a722012-11-29 22:16:27 +00001505 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1506 case ObjectFile::eTypeExecutable: /// A normal executable
1507 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1508 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1509 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1510 break;
1511 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1512 if (error_ptr)
1513 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1514 return ModuleSP();
1515 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1516 if (error_ptr)
1517 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1518 return ModuleSP();
1519 default:
1520 if (error_ptr)
1521 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1522 return ModuleSP();
1523 }
1524 // GetSharedModule is not guaranteed to find the old shared module, for instance
1525 // in the common case where you pass in the UUID, it is only going to find the one
1526 // module matching the UUID. In fact, it has no good way to know what the "old module"
1527 // relevant to this target is, since there might be many copies of a module with this file spec
1528 // in various running debug sessions, but only one of them will belong to this target.
1529 // So let's remove the UUID from the module list, and look in the target's module list.
1530 // Only do this if there is SOMETHING else in the module spec...
1531 if (!old_module_sp)
1532 {
1533 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham03e5e512012-05-17 18:38:42 +00001534 {
Greg Clayton1649a722012-11-29 22:16:27 +00001535 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1536 module_spec_copy.GetUUID().Clear();
1537
1538 ModuleList found_modules;
1539 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1540 if (num_found == 1)
1541 {
1542 old_module_sp = found_modules.GetModuleAtIndex(0);
1543 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001544 }
1545 }
Greg Clayton1649a722012-11-29 22:16:27 +00001546
1547 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1548 {
1549 m_images.ReplaceModule(old_module_sp, module_sp);
1550 Module *old_module_ptr = old_module_sp.get();
1551 old_module_sp.reset();
1552 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1553 }
1554 else
1555 m_images.Append(module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001556 }
Chris Lattner24943d22010-06-08 16:52:24 +00001557 }
1558 }
1559 if (error_ptr)
1560 *error_ptr = error;
1561 return module_sp;
1562}
1563
1564
Greg Clayton289afcb2012-02-18 05:35:26 +00001565TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001566Target::CalculateTarget ()
1567{
Greg Clayton289afcb2012-02-18 05:35:26 +00001568 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001569}
1570
Greg Clayton289afcb2012-02-18 05:35:26 +00001571ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001572Target::CalculateProcess ()
1573{
Greg Clayton289afcb2012-02-18 05:35:26 +00001574 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001575}
1576
Greg Clayton289afcb2012-02-18 05:35:26 +00001577ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001578Target::CalculateThread ()
1579{
Greg Clayton289afcb2012-02-18 05:35:26 +00001580 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001581}
1582
Greg Clayton289afcb2012-02-18 05:35:26 +00001583StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001584Target::CalculateStackFrame ()
1585{
Greg Clayton289afcb2012-02-18 05:35:26 +00001586 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001587}
1588
1589void
Greg Claytona830adb2010-10-04 01:05:56 +00001590Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001591{
Greg Clayton567e7f32011-09-22 04:58:26 +00001592 exe_ctx.Clear();
1593 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001594}
1595
1596PathMappingList &
1597Target::GetImageSearchPathList ()
1598{
1599 return m_image_search_paths;
1600}
1601
1602void
1603Target::ImageSearchPathsChanged
1604(
1605 const PathMappingList &path_list,
1606 void *baton
1607)
1608{
1609 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001610 ModuleSP exe_module_sp (target->GetExecutableModule());
1611 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001612 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001613 target->m_images.Clear();
1614 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001615 }
1616}
1617
1618ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001619Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001620{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001621 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001622 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001623 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001624 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001625 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001626 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1627 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1628 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1629 }
Chris Lattner24943d22010-06-08 16:52:24 +00001630 return m_scratch_ast_context_ap.get();
1631}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001632
Sean Callanan4938bd62011-11-16 18:20:47 +00001633ClangASTImporter *
1634Target::GetClangASTImporter()
1635{
1636 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1637
1638 if (!ast_importer)
1639 {
1640 ast_importer = new ClangASTImporter();
1641 m_ast_importer_ap.reset(ast_importer);
1642 }
1643
1644 return ast_importer;
1645}
1646
Greg Clayton990de7b2010-11-18 23:32:35 +00001647void
Caroline Tice2a456812011-03-10 22:14:10 +00001648Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001649{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001650 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001651}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001652
Greg Clayton990de7b2010-11-18 23:32:35 +00001653void
Caroline Tice2a456812011-03-10 22:14:10 +00001654Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001655{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001656 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001657}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001658
Greg Clayton9ce95382012-02-13 23:10:39 +00001659FileSpecList
1660Target::GetDefaultExecutableSearchPaths ()
1661{
Greg Clayton73844aa2012-08-22 17:17:09 +00001662 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1663 if (properties_sp)
1664 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001665 return FileSpecList();
1666}
1667
Caroline Tice5bc8c972010-09-20 20:44:43 +00001668ArchSpec
1669Target::GetDefaultArchitecture ()
1670{
Greg Clayton73844aa2012-08-22 17:17:09 +00001671 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1672 if (properties_sp)
1673 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001674 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001675}
1676
1677void
Greg Clayton73844aa2012-08-22 17:17:09 +00001678Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001679{
Greg Clayton73844aa2012-08-22 17:17:09 +00001680 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1681 if (properties_sp)
Jason Molenda332dc002012-12-12 02:23:56 +00001682 {
1683 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::SetDefaultArchitecture setting target's default architecture to %s (%s)", arch.GetArchitectureName(), arch.GetTriple().getTriple().c_str());
Greg Clayton73844aa2012-08-22 17:17:09 +00001684 return properties_sp->SetDefaultArchitecture(arch);
Jason Molenda332dc002012-12-12 02:23:56 +00001685 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001686}
1687
Greg Claytona830adb2010-10-04 01:05:56 +00001688Target *
1689Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1690{
1691 // The target can either exist in the "process" of ExecutionContext, or in
1692 // the "target_sp" member of SymbolContext. This accessor helper function
1693 // will get the target from one of these locations.
1694
1695 Target *target = NULL;
1696 if (sc_ptr != NULL)
1697 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001698 if (target == NULL && exe_ctx_ptr)
1699 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001700 return target;
1701}
1702
Greg Clayton427f2902010-12-14 02:59:59 +00001703ExecutionResults
1704Target::EvaluateExpression
1705(
1706 const char *expr_cstr,
1707 StackFrame *frame,
Enrico Granata6cca9692012-07-16 23:10:35 +00001708 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001709 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001710)
1711{
Enrico Granata3a08fd12012-09-18 17:43:16 +00001712 result_valobj_sp.reset();
1713
Greg Clayton427f2902010-12-14 02:59:59 +00001714 ExecutionResults execution_results = eExecutionSetupError;
1715
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001716 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1717 return execution_results;
1718
Jim Ingham3613ae12011-05-12 02:06:14 +00001719 // We shouldn't run stop hooks in expressions.
1720 // Be sure to reset this if you return anywhere within this function.
1721 bool old_suppress_value = m_suppress_stop_hooks;
1722 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001723
1724 ExecutionContext exe_ctx;
Sean Callanan4ab92782013-01-12 02:04:23 +00001725
Greg Clayton427f2902010-12-14 02:59:59 +00001726 if (frame)
1727 {
1728 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton427f2902010-12-14 02:59:59 +00001729 }
1730 else if (m_process_sp)
1731 {
1732 m_process_sp->CalculateExecutionContext(exe_ctx);
1733 }
1734 else
1735 {
1736 CalculateExecutionContext(exe_ctx);
1737 }
1738
Sean Callanan4ab92782013-01-12 02:04:23 +00001739 // Make sure we aren't just trying to see the value of a persistent
1740 // variable (something like "$0")
1741 lldb::ClangExpressionVariableSP persistent_var_sp;
1742 // Only check for persistent variables the expression starts with a '$'
1743 if (expr_cstr[0] == '$')
1744 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1745
1746 if (persistent_var_sp)
Greg Clayton427f2902010-12-14 02:59:59 +00001747 {
Sean Callanan4ab92782013-01-12 02:04:23 +00001748 result_valobj_sp = persistent_var_sp->GetValueObject ();
Greg Clayton427f2902010-12-14 02:59:59 +00001749 execution_results = eExecutionCompleted;
Greg Clayton427f2902010-12-14 02:59:59 +00001750 }
1751 else
1752 {
Sean Callanan4ab92782013-01-12 02:04:23 +00001753 const char *prefix = GetExpressionPrefixContentsAsCString();
1754
1755 execution_results = ClangUserExpression::Evaluate (exe_ctx,
1756 options.GetExecutionPolicy(),
1757 lldb::eLanguageTypeUnknown,
1758 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1759 options.DoesUnwindOnError(),
1760 expr_cstr,
1761 prefix,
1762 result_valobj_sp,
1763 options.GetRunOthers(),
1764 options.GetTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001765 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001766
1767 m_suppress_stop_hooks = old_suppress_value;
1768
Greg Clayton427f2902010-12-14 02:59:59 +00001769 return execution_results;
1770}
1771
Greg Claytonc0fa5332011-05-22 22:46:53 +00001772lldb::addr_t
1773Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1774{
1775 addr_t code_addr = load_addr;
1776 switch (m_arch.GetMachine())
1777 {
1778 case llvm::Triple::arm:
1779 case llvm::Triple::thumb:
1780 switch (addr_class)
1781 {
1782 case eAddressClassData:
1783 case eAddressClassDebug:
1784 return LLDB_INVALID_ADDRESS;
1785
1786 case eAddressClassUnknown:
1787 case eAddressClassInvalid:
1788 case eAddressClassCode:
1789 case eAddressClassCodeAlternateISA:
1790 case eAddressClassRuntime:
1791 // Check if bit zero it no set?
1792 if ((code_addr & 1ull) == 0)
1793 {
1794 // Bit zero isn't set, check if the address is a multiple of 2?
1795 if (code_addr & 2ull)
1796 {
1797 // The address is a multiple of 2 so it must be thumb, set bit zero
1798 code_addr |= 1ull;
1799 }
1800 else if (addr_class == eAddressClassCodeAlternateISA)
1801 {
1802 // We checked the address and the address claims to be the alternate ISA
1803 // which means thumb, so set bit zero.
1804 code_addr |= 1ull;
1805 }
1806 }
1807 break;
1808 }
1809 break;
1810
1811 default:
1812 break;
1813 }
1814 return code_addr;
1815}
1816
1817lldb::addr_t
1818Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1819{
1820 addr_t opcode_addr = load_addr;
1821 switch (m_arch.GetMachine())
1822 {
1823 case llvm::Triple::arm:
1824 case llvm::Triple::thumb:
1825 switch (addr_class)
1826 {
1827 case eAddressClassData:
1828 case eAddressClassDebug:
1829 return LLDB_INVALID_ADDRESS;
1830
1831 case eAddressClassInvalid:
1832 case eAddressClassUnknown:
1833 case eAddressClassCode:
1834 case eAddressClassCodeAlternateISA:
1835 case eAddressClassRuntime:
1836 opcode_addr &= ~(1ull);
1837 break;
1838 }
1839 break;
1840
1841 default:
1842 break;
1843 }
1844 return opcode_addr;
1845}
1846
Jim Inghamd60d94a2011-03-11 03:53:59 +00001847lldb::user_id_t
1848Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1849{
1850 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001851 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001852 m_stop_hooks[new_uid] = new_hook_sp;
1853 return new_uid;
1854}
1855
1856bool
1857Target::RemoveStopHookByID (lldb::user_id_t user_id)
1858{
1859 size_t num_removed;
1860 num_removed = m_stop_hooks.erase (user_id);
1861 if (num_removed == 0)
1862 return false;
1863 else
1864 return true;
1865}
1866
1867void
1868Target::RemoveAllStopHooks ()
1869{
1870 m_stop_hooks.clear();
1871}
1872
1873Target::StopHookSP
1874Target::GetStopHookByID (lldb::user_id_t user_id)
1875{
1876 StopHookSP found_hook;
1877
1878 StopHookCollection::iterator specified_hook_iter;
1879 specified_hook_iter = m_stop_hooks.find (user_id);
1880 if (specified_hook_iter != m_stop_hooks.end())
1881 found_hook = (*specified_hook_iter).second;
1882 return found_hook;
1883}
1884
1885bool
1886Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1887{
1888 StopHookCollection::iterator specified_hook_iter;
1889 specified_hook_iter = m_stop_hooks.find (user_id);
1890 if (specified_hook_iter == m_stop_hooks.end())
1891 return false;
1892
1893 (*specified_hook_iter).second->SetIsActive (active_state);
1894 return true;
1895}
1896
1897void
1898Target::SetAllStopHooksActiveState (bool active_state)
1899{
1900 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1901 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1902 {
1903 (*pos).second->SetIsActive (active_state);
1904 }
1905}
1906
1907void
1908Target::RunStopHooks ()
1909{
Jim Ingham3613ae12011-05-12 02:06:14 +00001910 if (m_suppress_stop_hooks)
1911 return;
1912
Jim Inghamd60d94a2011-03-11 03:53:59 +00001913 if (!m_process_sp)
1914 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001915
1916 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1917 // since in that case we do not want to run the stop-hooks
1918 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1919 return;
1920
Jim Inghamd60d94a2011-03-11 03:53:59 +00001921 if (m_stop_hooks.empty())
1922 return;
1923
1924 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1925
1926 // If there aren't any active stop hooks, don't bother either:
1927 bool any_active_hooks = false;
1928 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1929 {
1930 if ((*pos).second->IsActive())
1931 {
1932 any_active_hooks = true;
1933 break;
1934 }
1935 }
1936 if (!any_active_hooks)
1937 return;
1938
1939 CommandReturnObject result;
1940
1941 std::vector<ExecutionContext> exc_ctx_with_reasons;
1942 std::vector<SymbolContext> sym_ctx_with_reasons;
1943
1944 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1945 size_t num_threads = cur_threadlist.GetSize();
1946 for (size_t i = 0; i < num_threads; i++)
1947 {
1948 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1949 if (cur_thread_sp->ThreadStoppedForAReason())
1950 {
1951 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1952 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1953 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1954 }
1955 }
1956
1957 // If no threads stopped for a reason, don't run the stop-hooks.
1958 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1959 if (num_exe_ctx == 0)
1960 return;
1961
Jim Inghame5ed8e92011-06-02 23:58:26 +00001962 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1963 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001964
1965 bool keep_going = true;
1966 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001967 bool print_hook_header;
1968 bool print_thread_header;
1969
1970 if (num_exe_ctx == 1)
1971 print_thread_header = false;
1972 else
1973 print_thread_header = true;
1974
1975 if (m_stop_hooks.size() == 1)
1976 print_hook_header = false;
1977 else
1978 print_hook_header = true;
1979
Jim Inghamd60d94a2011-03-11 03:53:59 +00001980 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1981 {
1982 // result.Clear();
1983 StopHookSP cur_hook_sp = (*pos).second;
1984 if (!cur_hook_sp->IsActive())
1985 continue;
1986
1987 bool any_thread_matched = false;
1988 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1989 {
1990 if ((cur_hook_sp->GetSpecifier () == NULL
1991 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1992 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00001993 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001994 {
1995 if (!hooks_ran)
1996 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001997 hooks_ran = true;
1998 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001999 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002000 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002001 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2002 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2003 NULL);
2004 if (cmd)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002005 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chen4d96a742011-10-24 23:01:06 +00002006 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002007 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002008 any_thread_matched = true;
2009 }
2010
Jim Inghamc54840c2011-03-22 01:47:27 +00002011 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002012 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002013
2014 bool stop_on_continue = true;
2015 bool stop_on_error = true;
2016 bool echo_commands = false;
2017 bool print_results = true;
2018 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002019 &exc_ctx_with_reasons[i],
2020 stop_on_continue,
2021 stop_on_error,
2022 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002023 print_results,
2024 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002025 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002026
2027 // If the command started the target going again, we should bag out of
2028 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002029 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2030 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002031 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002032 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002033 keep_going = false;
2034 }
2035 }
2036 }
2037 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002038
Caroline Tice4a348082011-05-02 20:41:46 +00002039 result.GetImmediateOutputStream()->Flush();
2040 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002041}
2042
Greg Claytonbbea1332011-07-08 00:48:09 +00002043
Jim Inghamd60d94a2011-03-11 03:53:59 +00002044//--------------------------------------------------------------
2045// class Target::StopHook
2046//--------------------------------------------------------------
2047
2048
2049Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2050 UserID (uid),
2051 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002052 m_commands (),
2053 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002054 m_thread_spec_ap(NULL),
2055 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002056{
2057}
2058
2059Target::StopHook::StopHook (const StopHook &rhs) :
2060 UserID (rhs.GetID()),
2061 m_target_sp (rhs.m_target_sp),
2062 m_commands (rhs.m_commands),
2063 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002064 m_thread_spec_ap (NULL),
2065 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002066{
2067 if (rhs.m_thread_spec_ap.get() != NULL)
2068 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2069}
2070
2071
2072Target::StopHook::~StopHook ()
2073{
2074}
2075
2076void
2077Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2078{
2079 m_thread_spec_ap.reset (specifier);
2080}
2081
2082
2083void
2084Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2085{
2086 int indent_level = s->GetIndentLevel();
2087
2088 s->SetIndentLevel(indent_level + 2);
2089
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002090 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002091 if (m_active)
2092 s->Indent ("State: enabled\n");
2093 else
2094 s->Indent ("State: disabled\n");
2095
2096 if (m_specifier_sp)
2097 {
2098 s->Indent();
2099 s->PutCString ("Specifier:\n");
2100 s->SetIndentLevel (indent_level + 4);
2101 m_specifier_sp->GetDescription (s, level);
2102 s->SetIndentLevel (indent_level + 2);
2103 }
2104
2105 if (m_thread_spec_ap.get() != NULL)
2106 {
2107 StreamString tmp;
2108 s->Indent("Thread:\n");
2109 m_thread_spec_ap->GetDescription (&tmp, level);
2110 s->SetIndentLevel (indent_level + 4);
2111 s->Indent (tmp.GetData());
2112 s->PutCString ("\n");
2113 s->SetIndentLevel (indent_level + 2);
2114 }
2115
2116 s->Indent ("Commands: \n");
2117 s->SetIndentLevel (indent_level + 4);
2118 uint32_t num_commands = m_commands.GetSize();
2119 for (uint32_t i = 0; i < num_commands; i++)
2120 {
2121 s->Indent(m_commands.GetStringAtIndex(i));
2122 s->PutCString ("\n");
2123 }
2124 s->SetIndentLevel (indent_level);
2125}
2126
Greg Clayton73844aa2012-08-22 17:17:09 +00002127//--------------------------------------------------------------
2128// class TargetProperties
2129//--------------------------------------------------------------
2130
2131OptionEnumValueElement
2132lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002133{
Greg Clayton73844aa2012-08-22 17:17:09 +00002134 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2135 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2136 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2137 { 0, NULL, NULL }
2138};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002139
Greg Clayton49ce8962012-08-29 21:13:06 +00002140static OptionEnumValueElement
2141g_inline_breakpoint_enums[] =
2142{
2143 { 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."},
2144 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2145 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2146 { 0, NULL, NULL }
2147};
2148
Greg Clayton73844aa2012-08-22 17:17:09 +00002149static PropertyDefinition
2150g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002151{
Greg Clayton73844aa2012-08-22 17:17:09 +00002152 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2153 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2154 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2155 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2156 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2157 { "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 "
2158 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2159 "some part (starting at the root) of the path to the file when it was built, "
2160 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2161 "Each element of the array is checked in order and the first one that results in a match wins." },
2162 { "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." },
2163 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2164 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2165 { "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 +00002166 { "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." },
2167 { "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 +00002168 { "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." },
2169 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2170 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2171 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2172 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2173 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2174 { "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 +00002175 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2176 "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. "
2177 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2178 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2179 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2180 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2181 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002182 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2183};
2184enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002185{
Greg Clayton73844aa2012-08-22 17:17:09 +00002186 ePropertyDefaultArch,
2187 ePropertyExprPrefix,
2188 ePropertyPreferDynamic,
2189 ePropertyEnableSynthetic,
2190 ePropertySkipPrologue,
2191 ePropertySourceMap,
2192 ePropertyExecutableSearchPaths,
2193 ePropertyMaxChildrenCount,
2194 ePropertyMaxSummaryLength,
2195 ePropertyBreakpointUseAvoidList,
Greg Clayton0c8446c2012-10-17 22:57:12 +00002196 ePropertyArg0,
Greg Clayton73844aa2012-08-22 17:17:09 +00002197 ePropertyRunArgs,
2198 ePropertyEnvVars,
2199 ePropertyInheritEnv,
2200 ePropertyInputPath,
2201 ePropertyOutputPath,
2202 ePropertyErrorPath,
2203 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002204 ePropertyDisableSTDIO,
Greg Clayton87e9d322012-10-19 18:02:49 +00002205 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002206};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002207
Caroline Tice5bc8c972010-09-20 20:44:43 +00002208
Greg Clayton73844aa2012-08-22 17:17:09 +00002209class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002210{
Greg Clayton73844aa2012-08-22 17:17:09 +00002211public:
2212 TargetOptionValueProperties (const ConstString &name) :
2213 OptionValueProperties (name),
2214 m_target (NULL),
2215 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002216 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002217 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002218
Greg Clayton73844aa2012-08-22 17:17:09 +00002219 // This constructor is used when creating TargetOptionValueProperties when it
2220 // is part of a new lldb_private::Target instance. It will copy all current
2221 // global property values as needed
2222 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2223 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2224 m_target (target),
2225 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002226 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002227 }
2228
2229 virtual const Property *
2230 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2231 {
2232 // When gettings the value for a key from the target options, we will always
2233 // try and grab the setting from the current target if there is one. Else we just
2234 // use the one from this instance.
2235 if (idx == ePropertyEnvVars)
2236 GetHostEnvironmentIfNeeded ();
2237
2238 if (exe_ctx)
2239 {
2240 Target *target = exe_ctx->GetTargetPtr();
2241 if (target)
2242 {
2243 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2244 if (this != target_properties)
2245 return target_properties->ProtectedGetPropertyAtIndex (idx);
2246 }
2247 }
2248 return ProtectedGetPropertyAtIndex (idx);
2249 }
2250protected:
2251
2252 void
2253 GetHostEnvironmentIfNeeded () const
2254 {
2255 if (!m_got_host_env)
2256 {
2257 if (m_target)
2258 {
2259 m_got_host_env = true;
2260 const uint32_t idx = ePropertyInheritEnv;
2261 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2262 {
2263 PlatformSP platform_sp (m_target->GetPlatform());
2264 if (platform_sp)
2265 {
2266 StringList env;
2267 if (platform_sp->GetEnvironment(env))
2268 {
2269 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2270 if (env_dict)
2271 {
2272 const bool can_replace = false;
2273 const size_t envc = env.GetSize();
2274 for (size_t idx=0; idx<envc; idx++)
2275 {
2276 const char *env_entry = env.GetStringAtIndex (idx);
2277 if (env_entry)
2278 {
2279 const char *equal_pos = ::strchr(env_entry, '=');
2280 ConstString key;
2281 // It is ok to have environment variables with no values
2282 const char *value = NULL;
2283 if (equal_pos)
2284 {
2285 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2286 if (equal_pos[1])
2287 value = equal_pos + 1;
2288 }
2289 else
2290 {
2291 key.SetCString(env_entry);
2292 }
2293 // Don't allow existing keys to be replaced with ones we get from the platform environment
2294 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2295 }
2296 }
2297 }
2298 }
2299 }
2300 }
2301 }
2302 }
2303 }
2304 Target *m_target;
2305 mutable bool m_got_host_env;
2306};
2307
2308TargetProperties::TargetProperties (Target *target) :
2309 Properties ()
2310{
2311 if (target)
2312 {
2313 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002314 }
2315 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002316 {
2317 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2318 m_collection_sp->Initialize(g_properties);
2319 m_collection_sp->AppendProperty(ConstString("process"),
2320 ConstString("Settings specify to processes."),
2321 true,
2322 Process::GetGlobalProperties()->GetValueProperties());
2323 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002324}
2325
Greg Clayton73844aa2012-08-22 17:17:09 +00002326TargetProperties::~TargetProperties ()
2327{
2328}
2329ArchSpec
2330TargetProperties::GetDefaultArchitecture () const
2331{
2332 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2333 if (value)
2334 return value->GetCurrentValue();
2335 return ArchSpec();
2336}
2337
2338void
2339TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2340{
2341 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2342 if (value)
2343 return value->SetCurrentValue(arch, true);
2344}
2345
2346lldb::DynamicValueType
2347TargetProperties::GetPreferDynamicValue() const
2348{
2349 const uint32_t idx = ePropertyPreferDynamic;
2350 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2351}
2352
2353bool
2354TargetProperties::GetDisableASLR () const
2355{
2356 const uint32_t idx = ePropertyDisableASLR;
2357 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2358}
2359
2360void
2361TargetProperties::SetDisableASLR (bool b)
2362{
2363 const uint32_t idx = ePropertyDisableASLR;
2364 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2365}
2366
2367bool
2368TargetProperties::GetDisableSTDIO () const
2369{
2370 const uint32_t idx = ePropertyDisableSTDIO;
2371 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2372}
2373
2374void
2375TargetProperties::SetDisableSTDIO (bool b)
2376{
2377 const uint32_t idx = ePropertyDisableSTDIO;
2378 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2379}
2380
Greg Clayton49ce8962012-08-29 21:13:06 +00002381InlineStrategy
2382TargetProperties::GetInlineStrategy () const
2383{
2384 const uint32_t idx = ePropertyInlineStrategy;
2385 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2386}
2387
Greg Clayton0c8446c2012-10-17 22:57:12 +00002388const char *
2389TargetProperties::GetArg0 () const
2390{
2391 const uint32_t idx = ePropertyArg0;
2392 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2393}
2394
2395void
2396TargetProperties::SetArg0 (const char *arg)
2397{
2398 const uint32_t idx = ePropertyArg0;
2399 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2400}
2401
Greg Clayton73844aa2012-08-22 17:17:09 +00002402bool
2403TargetProperties::GetRunArguments (Args &args) const
2404{
2405 const uint32_t idx = ePropertyRunArgs;
2406 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2407}
2408
2409void
2410TargetProperties::SetRunArguments (const Args &args)
2411{
2412 const uint32_t idx = ePropertyRunArgs;
2413 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2414}
2415
2416size_t
2417TargetProperties::GetEnvironmentAsArgs (Args &env) const
2418{
2419 const uint32_t idx = ePropertyEnvVars;
2420 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2421}
2422
2423bool
2424TargetProperties::GetSkipPrologue() const
2425{
2426 const uint32_t idx = ePropertySkipPrologue;
2427 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2428}
2429
2430PathMappingList &
2431TargetProperties::GetSourcePathMap () const
2432{
2433 const uint32_t idx = ePropertySourceMap;
2434 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2435 assert(option_value);
2436 return option_value->GetCurrentValue();
2437}
2438
2439FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002440TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002441{
2442 const uint32_t idx = ePropertyExecutableSearchPaths;
2443 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2444 assert(option_value);
2445 return option_value->GetCurrentValue();
2446}
2447
2448bool
2449TargetProperties::GetEnableSyntheticValue () const
2450{
2451 const uint32_t idx = ePropertyEnableSynthetic;
2452 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2453}
2454
2455uint32_t
2456TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2457{
2458 const uint32_t idx = ePropertyMaxChildrenCount;
2459 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2460}
2461
2462uint32_t
2463TargetProperties::GetMaximumSizeOfStringSummary() const
2464{
2465 const uint32_t idx = ePropertyMaxSummaryLength;
2466 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2467}
2468
2469FileSpec
2470TargetProperties::GetStandardInputPath () const
2471{
2472 const uint32_t idx = ePropertyInputPath;
2473 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2474}
2475
2476void
2477TargetProperties::SetStandardInputPath (const char *p)
2478{
2479 const uint32_t idx = ePropertyInputPath;
2480 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2481}
2482
2483FileSpec
2484TargetProperties::GetStandardOutputPath () const
2485{
2486 const uint32_t idx = ePropertyOutputPath;
2487 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2488}
2489
2490void
2491TargetProperties::SetStandardOutputPath (const char *p)
2492{
2493 const uint32_t idx = ePropertyOutputPath;
2494 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2495}
2496
2497FileSpec
2498TargetProperties::GetStandardErrorPath () const
2499{
2500 const uint32_t idx = ePropertyErrorPath;
2501 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2502}
2503
Greg Claytonc6e82e42012-08-22 18:39:03 +00002504const char *
2505TargetProperties::GetExpressionPrefixContentsAsCString ()
2506{
2507 const uint32_t idx = ePropertyExprPrefix;
2508 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2509 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002510 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002511 const bool null_terminate = true;
2512 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002513 if (data_sp)
2514 return (const char *) data_sp->GetBytes();
2515 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002516 return NULL;
2517}
2518
Greg Clayton73844aa2012-08-22 17:17:09 +00002519void
2520TargetProperties::SetStandardErrorPath (const char *p)
2521{
2522 const uint32_t idx = ePropertyErrorPath;
2523 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2524}
2525
2526bool
2527TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2528{
2529 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2530 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2531}
2532
2533const TargetPropertiesSP &
2534Target::GetGlobalProperties()
2535{
2536 static TargetPropertiesSP g_settings_sp;
2537 if (!g_settings_sp)
2538 {
2539 g_settings_sp.reset (new TargetProperties (NULL));
2540 }
2541 return g_settings_sp;
2542}
2543
Jim Ingham5a15e692012-02-16 06:50:00 +00002544const ConstString &
2545Target::TargetEventData::GetFlavorString ()
2546{
2547 static ConstString g_flavor ("Target::TargetEventData");
2548 return g_flavor;
2549}
2550
2551const ConstString &
2552Target::TargetEventData::GetFlavor () const
2553{
2554 return TargetEventData::GetFlavorString ();
2555}
2556
2557Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2558 EventData(),
2559 m_target_sp (new_target_sp)
2560{
2561}
2562
2563Target::TargetEventData::~TargetEventData()
2564{
2565
2566}
2567
2568void
2569Target::TargetEventData::Dump (Stream *s) const
2570{
2571
2572}
2573
2574const TargetSP
2575Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2576{
2577 TargetSP target_sp;
2578
2579 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2580 if (data)
2581 target_sp = data->m_target_sp;
2582
2583 return target_sp;
2584}
2585
2586const Target::TargetEventData *
2587Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2588{
2589 if (event_ptr)
2590 {
2591 const EventData *event_data = event_ptr->GetData();
2592 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2593 return static_cast <const TargetEventData *> (event_ptr->GetData());
2594 }
2595 return NULL;
2596}
2597