blob: 888545a0f1aa240bb0e3a337d86d95f080f5936d [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 Ingham5a15e692012-02-16 06:50:00 +0000104
105 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +0000106
Greg Claytone005f2c2010-11-06 01:53:30 +0000107 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000108 if (log)
109 log->Printf ("%p Target::Target()", this);
Jason Molenda8c6cf432012-12-05 00:25:49 +0000110 if (m_arch.IsValid())
111 {
Jason Molenda332dc002012-12-12 02:23:56 +0000112 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 +0000113 }
Chris Lattner24943d22010-06-08 16:52:24 +0000114}
115
116//----------------------------------------------------------------------
117// Destructor
118//----------------------------------------------------------------------
119Target::~Target()
120{
Greg Claytone005f2c2010-11-06 01:53:30 +0000121 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000122 if (log)
123 log->Printf ("%p Target::~Target()", this);
124 DeleteCurrentProcess ();
125}
126
127void
Caroline Tice7826c882010-10-26 03:11:13 +0000128Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000129{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000130// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000131 if (description_level != lldb::eDescriptionLevelBrief)
132 {
133 s->Indent();
134 s->PutCString("Target\n");
135 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000136 m_images.Dump(s);
137 m_breakpoint_list.Dump(s);
138 m_internal_breakpoint_list.Dump(s);
139 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000140 }
141 else
142 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000143 Module *exe_module = GetExecutableModulePointer();
144 if (exe_module)
145 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000146 else
147 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000148 }
Chris Lattner24943d22010-06-08 16:52:24 +0000149}
150
151void
Greg Clayton0bce9a22012-12-05 00:16:59 +0000152Target::CleanupProcess ()
153{
154 // Do any cleanup of the target we need to do between process instances.
155 // NB It is better to do this before destroying the process in case the
156 // clean up needs some help from the process.
157 m_breakpoint_list.ClearAllBreakpointSites();
158 m_internal_breakpoint_list.ClearAllBreakpointSites();
159 // Disable watchpoints just on the debugger side.
160 Mutex::Locker locker;
161 this->GetWatchpointList().GetListMutex(locker);
162 DisableAllWatchpoints(false);
163 ClearAllWatchpointHitCounts();
164}
165
166void
Chris Lattner24943d22010-06-08 16:52:24 +0000167Target::DeleteCurrentProcess ()
168{
169 if (m_process_sp.get())
170 {
Greg Clayton49480b12010-09-14 23:52:43 +0000171 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000172 if (m_process_sp->IsAlive())
173 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000174
175 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000176
Greg Clayton0bce9a22012-12-05 00:16:59 +0000177 CleanupProcess ();
178
Chris Lattner24943d22010-06-08 16:52:24 +0000179 m_process_sp.reset();
180 }
181}
182
183const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000184Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000185{
186 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000187 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000188 return m_process_sp;
189}
190
191const lldb::ProcessSP &
192Target::GetProcessSP () const
193{
194 return m_process_sp;
195}
196
Greg Clayton153ccd72011-08-10 02:10:13 +0000197void
198Target::Destroy()
199{
200 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000201 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000202 DeleteCurrentProcess ();
203 m_platform_sp.reset();
204 m_arch.Clear();
205 m_images.Clear();
206 m_section_load_list.Clear();
207 const bool notify = false;
208 m_breakpoint_list.RemoveAll(notify);
209 m_internal_breakpoint_list.RemoveAll(notify);
210 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000211 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000212 m_search_filter_sp.reset();
213 m_image_search_paths.Clear(notify);
214 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000215 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000216 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000217 m_persistent_variables.Clear();
218 m_stop_hooks.clear();
219 m_stop_hook_next_id = 0;
220 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000221 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000222}
223
224
Chris Lattner24943d22010-06-08 16:52:24 +0000225BreakpointList &
226Target::GetBreakpointList(bool internal)
227{
228 if (internal)
229 return m_internal_breakpoint_list;
230 else
231 return m_breakpoint_list;
232}
233
234const BreakpointList &
235Target::GetBreakpointList(bool internal) const
236{
237 if (internal)
238 return m_internal_breakpoint_list;
239 else
240 return m_breakpoint_list;
241}
242
243BreakpointSP
244Target::GetBreakpointByID (break_id_t break_id)
245{
246 BreakpointSP bp_sp;
247
248 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
249 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
250 else
251 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
252
253 return bp_sp;
254}
255
256BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000257Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000258 const FileSpecList *source_file_spec_list,
259 RegularExpression &source_regex,
260 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000261{
Jim Inghamd6d47972011-09-23 00:54:11 +0000262 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
263 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000264 return CreateBreakpoint (filter_sp, resolver_sp, internal);
265}
266
267
268BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000269Target::CreateBreakpoint (const FileSpecList *containingModules,
270 const FileSpec &file,
271 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000272 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000273 LazyBool skip_prologue,
274 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000275{
Greg Clayton49ce8962012-08-29 21:13:06 +0000276 if (check_inlines == eLazyBoolCalculate)
277 {
278 const InlineStrategy inline_strategy = GetInlineStrategy();
279 switch (inline_strategy)
280 {
281 case eInlineBreakpointsNever:
282 check_inlines = eLazyBoolNo;
283 break;
284
285 case eInlineBreakpointsHeaders:
286 if (file.IsSourceImplementationFile())
287 check_inlines = eLazyBoolNo;
288 else
289 check_inlines = eLazyBoolYes;
290 break;
291
292 case eInlineBreakpointsAlways:
293 check_inlines = eLazyBoolYes;
294 break;
295 }
296 }
Greg Clayton46365522012-09-07 23:48:57 +0000297 SearchFilterSP filter_sp;
298 if (check_inlines == eLazyBoolNo)
299 {
300 // Not checking for inlines, we are looking only for matching compile units
301 FileSpecList compile_unit_list;
302 compile_unit_list.Append (file);
303 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
304 }
305 else
306 {
307 filter_sp = GetSearchFilterForModuleList (containingModules);
308 }
Greg Clayton49ce8962012-08-29 21:13:06 +0000309 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
310 file,
311 line_no,
312 check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000313 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000314 return CreateBreakpoint (filter_sp, resolver_sp, internal);
315}
316
317
318BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000319Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000320{
Chris Lattner24943d22010-06-08 16:52:24 +0000321 Address so_addr;
322 // Attempt to resolve our load address if possible, though it is ok if
323 // it doesn't resolve to section/offset.
324
Greg Clayton33ed1702010-08-24 00:45:41 +0000325 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000326 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000327 if (!so_addr.IsValid())
328 {
329 // The address didn't resolve, so just set this as an absolute address
330 so_addr.SetOffset (addr);
331 }
332 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000333 return bp_sp;
334}
335
336BreakpointSP
337Target::CreateBreakpoint (Address &addr, bool internal)
338{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000339 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000340 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
341 return CreateBreakpoint (filter_sp, resolver_sp, internal);
342}
343
344BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000345Target::CreateBreakpoint (const FileSpecList *containingModules,
346 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000347 const char *func_name,
348 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000349 LazyBool skip_prologue,
350 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000351{
Greg Clayton12bec712010-06-28 21:30:43 +0000352 BreakpointSP bp_sp;
353 if (func_name)
354 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000355 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000356
357 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
358 func_name,
359 func_name_type_mask,
360 Breakpoint::Exact,
361 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000362 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
363 }
364 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000365}
366
Jim Ingham4722b102012-03-06 00:37:27 +0000367lldb::BreakpointSP
368Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000369 const FileSpecList *containingSourceFiles,
370 const std::vector<std::string> &func_names,
371 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000372 LazyBool skip_prologue,
373 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000374{
375 BreakpointSP bp_sp;
376 size_t num_names = func_names.size();
377 if (num_names > 0)
378 {
379 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
380
381 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
382 func_names,
383 func_name_type_mask,
384 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
385 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
386 }
387 return bp_sp;
388}
389
Jim Inghamc1053622012-03-03 02:05:11 +0000390BreakpointSP
391Target::CreateBreakpoint (const FileSpecList *containingModules,
392 const FileSpecList *containingSourceFiles,
393 const char *func_names[],
394 size_t num_names,
395 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000396 LazyBool skip_prologue,
397 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000398{
399 BreakpointSP bp_sp;
400 if (num_names > 0)
401 {
402 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
403
404 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
405 func_names,
406 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000407 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000408 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
409 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
410 }
411 return bp_sp;
412}
Chris Lattner24943d22010-06-08 16:52:24 +0000413
414SearchFilterSP
415Target::GetSearchFilterForModule (const FileSpec *containingModule)
416{
417 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000418 if (containingModule != NULL)
419 {
420 // TODO: We should look into sharing module based search filters
421 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000422 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000423 }
424 else
425 {
426 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000427 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000428 filter_sp = m_search_filter_sp;
429 }
430 return filter_sp;
431}
432
Jim Ingham03c8ee52011-09-21 01:17:13 +0000433SearchFilterSP
434Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
435{
436 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000437 if (containingModules && containingModules->GetSize() != 0)
438 {
439 // TODO: We should look into sharing module based search filters
440 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000441 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000442 }
443 else
444 {
445 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000446 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000447 filter_sp = m_search_filter_sp;
448 }
449 return filter_sp;
450}
451
Jim Inghamd6d47972011-09-23 00:54:11 +0000452SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000453Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
454 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000455{
456 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
457 return GetSearchFilterForModuleList(containingModules);
458
459 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000460 if (containingModules == NULL)
461 {
462 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
463 // but that will take a little reworking.
464
Greg Clayton13d24fb2012-01-29 20:56:30 +0000465 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000466 }
467 else
468 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000469 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000470 }
471 return filter_sp;
472}
473
Chris Lattner24943d22010-06-08 16:52:24 +0000474BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000475Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000476 const FileSpecList *containingSourceFiles,
477 RegularExpression &func_regex,
478 LazyBool skip_prologue,
479 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000480{
Jim Inghamd6d47972011-09-23 00:54:11 +0000481 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000482 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
483 func_regex,
484 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000485
486 return CreateBreakpoint (filter_sp, resolver_sp, internal);
487}
488
Jim Ingham3df164e2012-03-05 04:47:34 +0000489lldb::BreakpointSP
490Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
491{
492 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
493}
494
Chris Lattner24943d22010-06-08 16:52:24 +0000495BreakpointSP
496Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
497{
498 BreakpointSP bp_sp;
499 if (filter_sp && resolver_sp)
500 {
501 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
502 resolver_sp->SetBreakpoint (bp_sp.get());
503
504 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000505 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000506 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000507 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000508
Greg Claytone005f2c2010-11-06 01:53:30 +0000509 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000510 if (log)
511 {
512 StreamString s;
513 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
514 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
515 }
516
Chris Lattner24943d22010-06-08 16:52:24 +0000517 bp_sp->ResolveBreakpoint();
518 }
Jim Inghamd1686902010-10-14 23:45:03 +0000519
520 if (!internal && bp_sp)
521 {
522 m_last_created_breakpoint = bp_sp;
523 }
524
Chris Lattner24943d22010-06-08 16:52:24 +0000525 return bp_sp;
526}
527
Johnny Chenda5a8022011-09-20 23:28:55 +0000528bool
529Target::ProcessIsValid()
530{
531 return (m_process_sp && m_process_sp->IsAlive());
532}
533
Johnny Chen3f883492012-06-04 23:19:54 +0000534static bool
535CheckIfWatchpointsExhausted(Target *target, Error &error)
536{
537 uint32_t num_supported_hardware_watchpoints;
538 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
539 if (rc.Success())
540 {
541 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
542 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
543 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
544 num_supported_hardware_watchpoints);
545 }
546 return false;
547}
548
Johnny Chenecd4feb2011-10-14 00:42:25 +0000549// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000550// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000551WatchpointSP
Jim Ingham9e376622012-10-23 07:20:06 +0000552Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000553{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000554 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
555 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000556 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Ingham9e376622012-10-23 07:20:06 +0000557 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000558
Johnny Chenecd4feb2011-10-14 00:42:25 +0000559 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000560 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000561 {
562 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000563 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000564 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000565 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000566 {
567 if (size == 0)
568 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
569 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000570 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000571 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000572 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000573
Johnny Chenecd4feb2011-10-14 00:42:25 +0000574 // Currently we only support one watchpoint per address, with total number
575 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000576
577 // Grab the list mutex while doing operations.
578 Mutex::Locker locker;
579 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000580 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000581 if (matched_sp)
582 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000583 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000584 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000585 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
586 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000587 // Return the existing watchpoint if both size and type match.
Jim Ingham9e376622012-10-23 07:20:06 +0000588 if (size == old_size && kind == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000589 wp_sp = matched_sp;
590 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000591 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000592 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000593 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000594 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000595 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000596 }
597
Jason Molendac4d49fd2012-12-05 23:07:34 +0000598 if (!wp_sp)
599 {
Jim Ingham9e376622012-10-23 07:20:06 +0000600 Watchpoint *new_wp = new Watchpoint(*this, addr, size, type);
Jason Molendac4d49fd2012-12-05 23:07:34 +0000601 if (!new_wp)
602 {
603 error.SetErrorString("Watchpoint ctor failed, out of memory?");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000604 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000605 }
Jim Ingham9e376622012-10-23 07:20:06 +0000606 new_wp->SetWatchpointType(kind);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000607 wp_sp.reset(new_wp);
608 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000609 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000610
Johnny Chen3f883492012-06-04 23:19:54 +0000611 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000612 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +0000613 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
614 __FUNCTION__,
615 error.Success() ? "succeeded" : "failed",
616 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000617
Jason Molendac4d49fd2012-12-05 23:07:34 +0000618 if (error.Fail())
619 {
Johnny Chen155599b2012-03-26 22:00:10 +0000620 // Enabling the watchpoint on the device side failed.
621 // Remove the said watchpoint from the list maintained by the target instance.
622 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000623 // See if we could provide more helpful error message.
624 if (!CheckIfWatchpointsExhausted(this, error))
625 {
626 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
627 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
628 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000629 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000630 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000631 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000632 m_last_created_watchpoint = wp_sp;
633 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000634}
635
Chris Lattner24943d22010-06-08 16:52:24 +0000636void
637Target::RemoveAllBreakpoints (bool internal_also)
638{
Greg Claytone005f2c2010-11-06 01:53:30 +0000639 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000640 if (log)
641 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
642
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000643 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000644 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000645 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000646
647 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000648}
649
650void
651Target::DisableAllBreakpoints (bool internal_also)
652{
Greg Claytone005f2c2010-11-06 01:53:30 +0000653 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000654 if (log)
655 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
656
657 m_breakpoint_list.SetEnabledAll (false);
658 if (internal_also)
659 m_internal_breakpoint_list.SetEnabledAll (false);
660}
661
662void
663Target::EnableAllBreakpoints (bool internal_also)
664{
Greg Claytone005f2c2010-11-06 01:53:30 +0000665 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000666 if (log)
667 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
668
669 m_breakpoint_list.SetEnabledAll (true);
670 if (internal_also)
671 m_internal_breakpoint_list.SetEnabledAll (true);
672}
673
674bool
675Target::RemoveBreakpointByID (break_id_t break_id)
676{
Greg Claytone005f2c2010-11-06 01:53:30 +0000677 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000678 if (log)
679 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
680
681 if (DisableBreakpointByID (break_id))
682 {
683 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000684 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000685 else
Jim Inghamd1686902010-10-14 23:45:03 +0000686 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000687 if (m_last_created_breakpoint)
688 {
689 if (m_last_created_breakpoint->GetID() == break_id)
690 m_last_created_breakpoint.reset();
691 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000692 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000693 }
Chris Lattner24943d22010-06-08 16:52:24 +0000694 return true;
695 }
696 return false;
697}
698
699bool
700Target::DisableBreakpointByID (break_id_t break_id)
701{
Greg Claytone005f2c2010-11-06 01:53:30 +0000702 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000703 if (log)
704 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
705
706 BreakpointSP bp_sp;
707
708 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
709 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
710 else
711 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
712 if (bp_sp)
713 {
714 bp_sp->SetEnabled (false);
715 return true;
716 }
717 return false;
718}
719
720bool
721Target::EnableBreakpointByID (break_id_t break_id)
722{
Greg Claytone005f2c2010-11-06 01:53:30 +0000723 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000724 if (log)
725 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
726 __FUNCTION__,
727 break_id,
728 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
729
730 BreakpointSP bp_sp;
731
732 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
733 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
734 else
735 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
736
737 if (bp_sp)
738 {
739 bp_sp->SetEnabled (true);
740 return true;
741 }
742 return false;
743}
744
Johnny Chenc86582f2011-09-23 21:21:43 +0000745// The flag 'end_to_end', default to true, signifies that the operation is
746// performed end to end, for both the debugger and the debuggee.
747
Johnny Chenecd4feb2011-10-14 00:42:25 +0000748// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
749// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000750bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000751Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000752{
753 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
754 if (log)
755 log->Printf ("Target::%s\n", __FUNCTION__);
756
Johnny Chenc86582f2011-09-23 21:21:43 +0000757 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000758 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000759 return true;
760 }
761
762 // Otherwise, it's an end to end operation.
763
Johnny Chenda5a8022011-09-20 23:28:55 +0000764 if (!ProcessIsValid())
765 return false;
766
Johnny Chenecd4feb2011-10-14 00:42:25 +0000767 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000768 for (size_t i = 0; i < num_watchpoints; ++i)
769 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000770 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
771 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000772 return false;
773
Johnny Chenecd4feb2011-10-14 00:42:25 +0000774 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000775 if (rc.Fail())
776 return false;
777 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000778 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000779 return true; // Success!
780}
781
Johnny Chenecd4feb2011-10-14 00:42:25 +0000782// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
783// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000784bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000785Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000786{
787 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
788 if (log)
789 log->Printf ("Target::%s\n", __FUNCTION__);
790
Johnny Chenc86582f2011-09-23 21:21:43 +0000791 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000792 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000793 return true;
794 }
795
796 // Otherwise, it's an end to end operation.
797
Johnny Chenda5a8022011-09-20 23:28:55 +0000798 if (!ProcessIsValid())
799 return false;
800
Johnny Chenecd4feb2011-10-14 00:42:25 +0000801 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000802 for (size_t i = 0; i < num_watchpoints; ++i)
803 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000804 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
805 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000806 return false;
807
Johnny Chenecd4feb2011-10-14 00:42:25 +0000808 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000809 if (rc.Fail())
810 return false;
811 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000812 return true; // Success!
813}
814
Johnny Chenecd4feb2011-10-14 00:42:25 +0000815// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
816// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000817bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000818Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000819{
820 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
821 if (log)
822 log->Printf ("Target::%s\n", __FUNCTION__);
823
Johnny Chenc86582f2011-09-23 21:21:43 +0000824 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000825 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000826 return true;
827 }
828
829 // Otherwise, it's an end to end operation.
830
Johnny Chenda5a8022011-09-20 23:28:55 +0000831 if (!ProcessIsValid())
832 return false;
833
Johnny Chenecd4feb2011-10-14 00:42:25 +0000834 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000835 for (size_t i = 0; i < num_watchpoints; ++i)
836 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000837 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
838 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000839 return false;
840
Johnny Chenecd4feb2011-10-14 00:42:25 +0000841 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000842 if (rc.Fail())
843 return false;
844 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000845 return true; // Success!
846}
847
Johnny Chen116a5cd2012-02-25 06:44:30 +0000848// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
849bool
850Target::ClearAllWatchpointHitCounts ()
851{
852 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
853 if (log)
854 log->Printf ("Target::%s\n", __FUNCTION__);
855
856 size_t num_watchpoints = m_watchpoint_list.GetSize();
857 for (size_t i = 0; i < num_watchpoints; ++i)
858 {
859 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
860 if (!wp_sp)
861 return false;
862
863 wp_sp->ResetHitCount();
864 }
865 return true; // Success!
866}
867
Johnny Chenecd4feb2011-10-14 00:42:25 +0000868// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000869// during these operations.
870bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000871Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000872{
873 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
874 if (log)
875 log->Printf ("Target::%s\n", __FUNCTION__);
876
877 if (!ProcessIsValid())
878 return false;
879
Johnny Chenecd4feb2011-10-14 00:42:25 +0000880 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000881 for (size_t i = 0; i < num_watchpoints; ++i)
882 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000883 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
884 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000885 return false;
886
Johnny Chenecd4feb2011-10-14 00:42:25 +0000887 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000888 }
889 return true; // Success!
890}
891
Johnny Chenecd4feb2011-10-14 00:42:25 +0000892// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000893bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000894Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000895{
896 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
897 if (log)
898 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
899
900 if (!ProcessIsValid())
901 return false;
902
Johnny Chenecd4feb2011-10-14 00:42:25 +0000903 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
904 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000905 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000906 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000907 if (rc.Success())
908 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000909
Johnny Chen01acfa72011-09-22 18:04:58 +0000910 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000911 }
912 return false;
913}
914
Johnny Chenecd4feb2011-10-14 00:42:25 +0000915// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000916bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000917Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000918{
919 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
920 if (log)
921 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
922
923 if (!ProcessIsValid())
924 return false;
925
Johnny Chenecd4feb2011-10-14 00:42:25 +0000926 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
927 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000928 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000929 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000930 if (rc.Success())
931 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000932
Johnny Chen01acfa72011-09-22 18:04:58 +0000933 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000934 }
935 return false;
936}
937
Johnny Chenecd4feb2011-10-14 00:42:25 +0000938// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000939bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000940Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000941{
942 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
943 if (log)
944 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
945
Johnny Chenecd4feb2011-10-14 00:42:25 +0000946 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000947 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000948 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000949 return true;
950 }
951 return false;
952}
953
Johnny Chenecd4feb2011-10-14 00:42:25 +0000954// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000955bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000956Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000957{
958 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
959 if (log)
960 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
961
962 if (!ProcessIsValid())
963 return false;
964
Johnny Chenecd4feb2011-10-14 00:42:25 +0000965 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
966 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000967 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000968 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000969 return true;
970 }
971 return false;
972}
973
Chris Lattner24943d22010-06-08 16:52:24 +0000974ModuleSP
975Target::GetExecutableModule ()
976{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000977 return m_images.GetModuleAtIndex(0);
978}
979
980Module*
981Target::GetExecutableModulePointer ()
982{
983 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000984}
985
Enrico Granata146d9522012-11-08 02:22:02 +0000986static void
987LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
988{
989 Error error;
990 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error))
991 {
992 target->GetDebugger().GetOutputStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
993 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
994 error.AsCString());
995 }
996}
997
Chris Lattner24943d22010-06-08 16:52:24 +0000998void
999Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1000{
Jason Molenda8c6cf432012-12-05 00:25:49 +00001001 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Chris Lattner24943d22010-06-08 16:52:24 +00001002 m_images.Clear();
1003 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +00001004 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001005 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001006
1007 if (executable_sp.get())
1008 {
1009 Timer scoped_timer (__PRETTY_FUNCTION__,
1010 "Target::SetExecutableModule (executable = '%s/%s')",
1011 executable_sp->GetFileSpec().GetDirectory().AsCString(),
1012 executable_sp->GetFileSpec().GetFilename().AsCString());
1013
1014 m_images.Append(executable_sp); // The first image is our exectuable file
1015
Jim Ingham7508e732010-08-09 23:31:02 +00001016 // 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 +00001017 if (!m_arch.IsValid())
Jason Molenda8c6cf432012-12-05 00:25:49 +00001018 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001019 m_arch = executable_sp->GetArchitecture();
Jason Molenda8c6cf432012-12-05 00:25:49 +00001020 if (log)
1021 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1022 }
1023
Chris Lattner24943d22010-06-08 16:52:24 +00001024 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001025 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +00001026
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001027 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +00001028 {
1029 executable_objfile->GetDependentModules(dependent_files);
1030 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1031 {
Greg Claytonb1888f22011-03-19 01:12:21 +00001032 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1033 FileSpec platform_dependent_file_spec;
1034 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +00001035 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +00001036 else
1037 platform_dependent_file_spec = dependent_file_spec;
1038
Greg Clayton444fe992012-02-26 05:51:37 +00001039 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1040 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +00001041 if (image_module_sp.get())
1042 {
Chris Lattner24943d22010-06-08 16:52:24 +00001043 ObjectFile *objfile = image_module_sp->GetObjectFile();
1044 if (objfile)
1045 objfile->GetDependentModules(dependent_files);
1046 }
1047 }
1048 }
Chris Lattner24943d22010-06-08 16:52:24 +00001049 }
1050}
1051
1052
Jim Ingham7508e732010-08-09 23:31:02 +00001053bool
1054Target::SetArchitecture (const ArchSpec &arch_spec)
1055{
Jason Molenda8c6cf432012-12-05 00:25:49 +00001056 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb170aee2012-05-08 01:45:38 +00001057 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001058 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001059 // If we haven't got a valid arch spec, or the architectures are
1060 // compatible, so just update the architecture. Architectures can be
1061 // equal, yet the triple OS and vendor might change, so we need to do
1062 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001063 m_arch = arch_spec;
Jason Molenda8c6cf432012-12-05 00:25:49 +00001064 if (log)
1065 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 +00001066 return true;
1067 }
1068 else
1069 {
1070 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molenda8c6cf432012-12-05 00:25:49 +00001071 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +00001072 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 +00001073 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001074 ModuleSP executable_sp = GetExecutableModule ();
1075 m_images.Clear();
1076 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001077 m_scratch_ast_source_ap.reset();
1078 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001079 // Need to do something about unsetting breakpoints.
1080
1081 if (executable_sp)
1082 {
Jason Molenda8c6cf432012-12-05 00:25:49 +00001083 if (log)
1084 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 +00001085 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1086 Error error = ModuleList::GetSharedModule (module_spec,
1087 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001088 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001089 NULL,
1090 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001091
1092 if (!error.Fail() && executable_sp)
1093 {
1094 SetExecutableModule (executable_sp, true);
1095 return true;
1096 }
Jim Ingham7508e732010-08-09 23:31:02 +00001097 }
1098 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001099 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001100}
Chris Lattner24943d22010-06-08 16:52:24 +00001101
Chris Lattner24943d22010-06-08 16:52:24 +00001102void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001103Target::WillClearList (const ModuleList& module_list)
Enrico Granata146d9522012-11-08 02:22:02 +00001104{
1105}
1106
1107void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001108Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001109{
1110 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001111 ModuleList my_module_list;
1112 my_module_list.Append(module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +00001113 LoadScriptingResourceForModule(module_sp, this);
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001114 ModulesDidLoad (my_module_list);
Chris Lattner24943d22010-06-08 16:52:24 +00001115}
1116
1117void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001118Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata146d9522012-11-08 02:22:02 +00001119{
1120 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001121 ModuleList my_module_list;
1122 my_module_list.Append(module_sp);
1123 ModulesDidUnload (my_module_list);
Enrico Granata146d9522012-11-08 02:22:02 +00001124}
1125
1126void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001127Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001128{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001129 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001130 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001131}
1132
1133void
1134Target::ModulesDidLoad (ModuleList &module_list)
1135{
Enrico Granata146d9522012-11-08 02:22:02 +00001136 if (module_list.GetSize())
1137 {
1138 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1139 // TODO: make event data that packages up the module_list
1140 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1141 }
Chris Lattner24943d22010-06-08 16:52:24 +00001142}
1143
1144void
1145Target::ModulesDidUnload (ModuleList &module_list)
1146{
Enrico Granata146d9522012-11-08 02:22:02 +00001147 if (module_list.GetSize())
1148 {
1149 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1150 // TODO: make event data that packages up the module_list
1151 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1152 }
Chris Lattner24943d22010-06-08 16:52:24 +00001153}
1154
Daniel Dunbar705a0982011-10-31 22:50:37 +00001155bool
Greg Clayton444fe992012-02-26 05:51:37 +00001156Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001157{
Greg Clayton73844aa2012-08-22 17:17:09 +00001158 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001159 {
1160 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001161 ModuleSpec module_spec (module_file_spec);
1162 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001163
1164 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1165 // black list.
1166 if (num_modules > 0)
1167 {
1168 for (int i = 0; i < num_modules; i++)
1169 {
1170 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1171 return false;
1172 }
1173 return true;
1174 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001175 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001176 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001177}
1178
Daniel Dunbar705a0982011-10-31 22:50:37 +00001179bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001180Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1181{
Greg Clayton73844aa2012-08-22 17:17:09 +00001182 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001183 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001184 if (m_platform_sp)
1185 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001186 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001187 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001188}
1189
Chris Lattner24943d22010-06-08 16:52:24 +00001190size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001191Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1192{
Greg Clayton3508c382012-02-24 01:59:29 +00001193 SectionSP section_sp (addr.GetSection());
1194 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001195 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001196 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1197 if (section_sp->IsEncrypted())
1198 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001199 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001200 return 0;
1201 }
Greg Clayton3508c382012-02-24 01:59:29 +00001202 ModuleSP module_sp (section_sp->GetModule());
1203 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001204 {
Greg Clayton3508c382012-02-24 01:59:29 +00001205 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1206 if (objfile)
1207 {
1208 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1209 addr.GetOffset(),
1210 dst,
1211 dst_len);
1212 if (bytes_read > 0)
1213 return bytes_read;
1214 else
1215 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1216 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001217 else
Greg Clayton3508c382012-02-24 01:59:29 +00001218 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001219 }
1220 else
Greg Clayton3508c382012-02-24 01:59:29 +00001221 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001222 }
1223 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001224 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001225
Greg Clayton26100dc2011-01-07 01:57:07 +00001226 return 0;
1227}
1228
1229size_t
Enrico Granata91544802011-09-06 19:20:51 +00001230Target::ReadMemory (const Address& addr,
1231 bool prefer_file_cache,
1232 void *dst,
1233 size_t dst_len,
1234 Error &error,
1235 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001236{
Chris Lattner24943d22010-06-08 16:52:24 +00001237 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001238
Enrico Granata91544802011-09-06 19:20:51 +00001239 // if we end up reading this from process memory, we will fill this
1240 // with the actual load address
1241 if (load_addr_ptr)
1242 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1243
Greg Clayton26100dc2011-01-07 01:57:07 +00001244 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001245
1246 addr_t load_addr = LLDB_INVALID_ADDRESS;
1247 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001248 Address resolved_addr;
1249 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001250 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001251 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001252 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001253 // No sections are loaded, so we must assume we are not running
1254 // yet and anything we are given is a file address.
1255 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1256 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001257 }
Greg Clayton70436352010-06-30 23:03:03 +00001258 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001259 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001260 // We have at least one section loaded. This can be becuase
1261 // we have manually loaded some sections with "target modules load ..."
1262 // or because we have have a live process that has sections loaded
1263 // through the dynamic loader
1264 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1265 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001266 }
Greg Clayton70436352010-06-30 23:03:03 +00001267 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001268 if (!resolved_addr.IsValid())
1269 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001270
Greg Clayton9b82f862011-07-11 05:12:02 +00001271
Greg Clayton26100dc2011-01-07 01:57:07 +00001272 if (prefer_file_cache)
1273 {
1274 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1275 if (bytes_read > 0)
1276 return bytes_read;
1277 }
Greg Clayton70436352010-06-30 23:03:03 +00001278
Johnny Chenda5a8022011-09-20 23:28:55 +00001279 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001280 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001281 if (load_addr == LLDB_INVALID_ADDRESS)
1282 load_addr = resolved_addr.GetLoadAddress (this);
1283
Greg Clayton70436352010-06-30 23:03:03 +00001284 if (load_addr == LLDB_INVALID_ADDRESS)
1285 {
Greg Clayton3508c382012-02-24 01:59:29 +00001286 ModuleSP addr_module_sp (resolved_addr.GetModule());
1287 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001288 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001289 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001290 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001291 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001292 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001293 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001294 }
1295 else
1296 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001297 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001298 if (bytes_read != dst_len)
1299 {
1300 if (error.Success())
1301 {
1302 if (bytes_read == 0)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001303 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001304 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001305 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 +00001306 }
1307 }
Greg Clayton70436352010-06-30 23:03:03 +00001308 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001309 {
1310 if (load_addr_ptr)
1311 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001312 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001313 }
Greg Clayton70436352010-06-30 23:03:03 +00001314 // If the address is not section offset we have an address that
1315 // doesn't resolve to any address in any currently loaded shared
1316 // libaries and we failed to read memory so there isn't anything
1317 // more we can do. If it is section offset, we might be able to
1318 // read cached memory from the object file.
1319 if (!resolved_addr.IsSectionOffset())
1320 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001321 }
Chris Lattner24943d22010-06-08 16:52:24 +00001322 }
Greg Clayton70436352010-06-30 23:03:03 +00001323
Greg Clayton9b82f862011-07-11 05:12:02 +00001324 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001325 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001326 // If we didn't already try and read from the object file cache, then
1327 // try it after failing to read from the process.
1328 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001329 }
1330 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001331}
1332
Greg Clayton7dd98df2011-07-12 17:06:17 +00001333size_t
1334Target::ReadScalarIntegerFromMemory (const Address& addr,
1335 bool prefer_file_cache,
1336 uint32_t byte_size,
1337 bool is_signed,
1338 Scalar &scalar,
1339 Error &error)
1340{
1341 uint64_t uval;
1342
1343 if (byte_size <= sizeof(uval))
1344 {
1345 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1346 if (bytes_read == byte_size)
1347 {
1348 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1349 uint32_t offset = 0;
1350 if (byte_size <= 4)
1351 scalar = data.GetMaxU32 (&offset, byte_size);
1352 else
1353 scalar = data.GetMaxU64 (&offset, byte_size);
1354
1355 if (is_signed)
1356 scalar.SignExtend(byte_size * 8);
1357 return bytes_read;
1358 }
1359 }
1360 else
1361 {
1362 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1363 }
1364 return 0;
1365}
1366
1367uint64_t
1368Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1369 bool prefer_file_cache,
1370 size_t integer_byte_size,
1371 uint64_t fail_value,
1372 Error &error)
1373{
1374 Scalar scalar;
1375 if (ReadScalarIntegerFromMemory (addr,
1376 prefer_file_cache,
1377 integer_byte_size,
1378 false,
1379 scalar,
1380 error))
1381 return scalar.ULongLong(fail_value);
1382 return fail_value;
1383}
1384
1385bool
1386Target::ReadPointerFromMemory (const Address& addr,
1387 bool prefer_file_cache,
1388 Error &error,
1389 Address &pointer_addr)
1390{
1391 Scalar scalar;
1392 if (ReadScalarIntegerFromMemory (addr,
1393 prefer_file_cache,
1394 m_arch.GetAddressByteSize(),
1395 false,
1396 scalar,
1397 error))
1398 {
1399 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1400 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1401 {
1402 if (m_section_load_list.IsEmpty())
1403 {
1404 // No sections are loaded, so we must assume we are not running
1405 // yet and anything we are given is a file address.
1406 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1407 }
1408 else
1409 {
1410 // We have at least one section loaded. This can be becuase
1411 // we have manually loaded some sections with "target modules load ..."
1412 // or because we have have a live process that has sections loaded
1413 // through the dynamic loader
1414 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1415 }
1416 // We weren't able to resolve the pointer value, so just return
1417 // an address with no section
1418 if (!pointer_addr.IsValid())
1419 pointer_addr.SetOffset (pointer_vm_addr);
1420 return true;
1421
1422 }
1423 }
1424 return false;
1425}
Chris Lattner24943d22010-06-08 16:52:24 +00001426
1427ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001428Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001429{
Chris Lattner24943d22010-06-08 16:52:24 +00001430 ModuleSP module_sp;
1431
Chris Lattner24943d22010-06-08 16:52:24 +00001432 Error error;
1433
Jim Ingham03e5e512012-05-17 18:38:42 +00001434 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1435 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001436
Jim Ingham03e5e512012-05-17 18:38:42 +00001437 if (module_spec.GetUUID().IsValid())
1438 module_sp = m_images.FindFirstModule(module_spec);
1439
Greg Claytone1ef1e32012-04-27 00:58:27 +00001440 if (!module_sp)
1441 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001442 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1443 bool did_create_module = false;
1444
1445 // If there are image search path entries, try to use them first to acquire a suitable image.
1446 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001447 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001448 ModuleSpec transformed_spec (module_spec);
1449 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1450 {
1451 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1452 error = ModuleList::GetSharedModule (transformed_spec,
1453 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001454 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001455 &old_module_sp,
1456 &did_create_module);
1457 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001458 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001459
Greg Claytone1ef1e32012-04-27 00:58:27 +00001460 if (!module_sp)
1461 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001462 // If we have a UUID, we can check our global shared module list in case
1463 // we already have it. If we don't have a valid UUID, then we can't since
1464 // the path in "module_spec" will be a platform path, and we will need to
1465 // let the platform find that file. For example, we could be asking for
1466 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1467 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1468 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1469 // cache.
1470 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001471 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001472 // We have a UUID, it is OK to check the global module list...
1473 error = ModuleList::GetSharedModule (module_spec,
1474 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001475 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001476 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001477 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001478 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001479
1480 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001481 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001482 // The platform is responsible for finding and caching an appropriate
1483 // module in the shared module cache.
1484 if (m_platform_sp)
1485 {
1486 FileSpec platform_file_spec;
1487 error = m_platform_sp->GetSharedModule (module_spec,
1488 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001489 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001490 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001491 &did_create_module);
1492 }
1493 else
1494 {
1495 error.SetErrorString("no platform is currently set");
1496 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001497 }
1498 }
Chris Lattner24943d22010-06-08 16:52:24 +00001499
Jim Ingham03e5e512012-05-17 18:38:42 +00001500 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1501 // module in the list already, and if there was, let's remove it.
1502 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001503 {
Greg Clayton1649a722012-11-29 22:16:27 +00001504 ObjectFile *objfile = module_sp->GetObjectFile();
1505 if (objfile)
Jim Ingham03e5e512012-05-17 18:38:42 +00001506 {
Greg Clayton1649a722012-11-29 22:16:27 +00001507 switch (objfile->GetType())
Jim Ingham03e5e512012-05-17 18:38:42 +00001508 {
Greg Clayton1649a722012-11-29 22:16:27 +00001509 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1510 case ObjectFile::eTypeExecutable: /// A normal executable
1511 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1512 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1513 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1514 break;
1515 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1516 if (error_ptr)
1517 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1518 return ModuleSP();
1519 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1520 if (error_ptr)
1521 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1522 return ModuleSP();
1523 default:
1524 if (error_ptr)
1525 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1526 return ModuleSP();
1527 }
1528 // GetSharedModule is not guaranteed to find the old shared module, for instance
1529 // in the common case where you pass in the UUID, it is only going to find the one
1530 // module matching the UUID. In fact, it has no good way to know what the "old module"
1531 // relevant to this target is, since there might be many copies of a module with this file spec
1532 // in various running debug sessions, but only one of them will belong to this target.
1533 // So let's remove the UUID from the module list, and look in the target's module list.
1534 // Only do this if there is SOMETHING else in the module spec...
1535 if (!old_module_sp)
1536 {
1537 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham03e5e512012-05-17 18:38:42 +00001538 {
Greg Clayton1649a722012-11-29 22:16:27 +00001539 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1540 module_spec_copy.GetUUID().Clear();
1541
1542 ModuleList found_modules;
1543 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1544 if (num_found == 1)
1545 {
1546 old_module_sp = found_modules.GetModuleAtIndex(0);
1547 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001548 }
1549 }
Greg Clayton1649a722012-11-29 22:16:27 +00001550
1551 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1552 {
1553 m_images.ReplaceModule(old_module_sp, module_sp);
1554 Module *old_module_ptr = old_module_sp.get();
1555 old_module_sp.reset();
1556 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1557 }
1558 else
1559 m_images.Append(module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001560 }
Chris Lattner24943d22010-06-08 16:52:24 +00001561 }
1562 }
1563 if (error_ptr)
1564 *error_ptr = error;
1565 return module_sp;
1566}
1567
1568
Greg Clayton289afcb2012-02-18 05:35:26 +00001569TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001570Target::CalculateTarget ()
1571{
Greg Clayton289afcb2012-02-18 05:35:26 +00001572 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001573}
1574
Greg Clayton289afcb2012-02-18 05:35:26 +00001575ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001576Target::CalculateProcess ()
1577{
Greg Clayton289afcb2012-02-18 05:35:26 +00001578 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001579}
1580
Greg Clayton289afcb2012-02-18 05:35:26 +00001581ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001582Target::CalculateThread ()
1583{
Greg Clayton289afcb2012-02-18 05:35:26 +00001584 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001585}
1586
Greg Clayton289afcb2012-02-18 05:35:26 +00001587StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001588Target::CalculateStackFrame ()
1589{
Greg Clayton289afcb2012-02-18 05:35:26 +00001590 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001591}
1592
1593void
Greg Claytona830adb2010-10-04 01:05:56 +00001594Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001595{
Greg Clayton567e7f32011-09-22 04:58:26 +00001596 exe_ctx.Clear();
1597 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001598}
1599
1600PathMappingList &
1601Target::GetImageSearchPathList ()
1602{
1603 return m_image_search_paths;
1604}
1605
1606void
1607Target::ImageSearchPathsChanged
1608(
1609 const PathMappingList &path_list,
1610 void *baton
1611)
1612{
1613 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001614 ModuleSP exe_module_sp (target->GetExecutableModule());
1615 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001616 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001617 target->m_images.Clear();
1618 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001619 }
1620}
1621
1622ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001623Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001624{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001625 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001626 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001627 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001628 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001629 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001630 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1631 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1632 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1633 }
Chris Lattner24943d22010-06-08 16:52:24 +00001634 return m_scratch_ast_context_ap.get();
1635}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001636
Sean Callanan4938bd62011-11-16 18:20:47 +00001637ClangASTImporter *
1638Target::GetClangASTImporter()
1639{
1640 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1641
1642 if (!ast_importer)
1643 {
1644 ast_importer = new ClangASTImporter();
1645 m_ast_importer_ap.reset(ast_importer);
1646 }
1647
1648 return ast_importer;
1649}
1650
Greg Clayton990de7b2010-11-18 23:32:35 +00001651void
Caroline Tice2a456812011-03-10 22:14:10 +00001652Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001653{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001654 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001655}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001656
Greg Clayton990de7b2010-11-18 23:32:35 +00001657void
Caroline Tice2a456812011-03-10 22:14:10 +00001658Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001659{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001660 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001661}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001662
Greg Clayton9ce95382012-02-13 23:10:39 +00001663FileSpecList
1664Target::GetDefaultExecutableSearchPaths ()
1665{
Greg Clayton73844aa2012-08-22 17:17:09 +00001666 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1667 if (properties_sp)
1668 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001669 return FileSpecList();
1670}
1671
Caroline Tice5bc8c972010-09-20 20:44:43 +00001672ArchSpec
1673Target::GetDefaultArchitecture ()
1674{
Greg Clayton73844aa2012-08-22 17:17:09 +00001675 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1676 if (properties_sp)
1677 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001678 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001679}
1680
1681void
Greg Clayton73844aa2012-08-22 17:17:09 +00001682Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001683{
Greg Clayton73844aa2012-08-22 17:17:09 +00001684 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1685 if (properties_sp)
Jason Molenda332dc002012-12-12 02:23:56 +00001686 {
1687 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 +00001688 return properties_sp->SetDefaultArchitecture(arch);
Jason Molenda332dc002012-12-12 02:23:56 +00001689 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001690}
1691
Greg Claytona830adb2010-10-04 01:05:56 +00001692Target *
1693Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1694{
1695 // The target can either exist in the "process" of ExecutionContext, or in
1696 // the "target_sp" member of SymbolContext. This accessor helper function
1697 // will get the target from one of these locations.
1698
1699 Target *target = NULL;
1700 if (sc_ptr != NULL)
1701 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001702 if (target == NULL && exe_ctx_ptr)
1703 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001704 return target;
1705}
1706
Greg Clayton427f2902010-12-14 02:59:59 +00001707ExecutionResults
1708Target::EvaluateExpression
1709(
1710 const char *expr_cstr,
1711 StackFrame *frame,
Enrico Granata6cca9692012-07-16 23:10:35 +00001712 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001713 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001714)
1715{
Enrico Granata3a08fd12012-09-18 17:43:16 +00001716 result_valobj_sp.reset();
1717
Greg Clayton427f2902010-12-14 02:59:59 +00001718 ExecutionResults execution_results = eExecutionSetupError;
1719
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001720 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1721 return execution_results;
1722
Jim Ingham3613ae12011-05-12 02:06:14 +00001723 // We shouldn't run stop hooks in expressions.
1724 // Be sure to reset this if you return anywhere within this function.
1725 bool old_suppress_value = m_suppress_stop_hooks;
1726 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001727
1728 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001729
1730 const size_t expr_cstr_len = ::strlen (expr_cstr);
1731
Greg Clayton427f2902010-12-14 02:59:59 +00001732 if (frame)
1733 {
1734 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001735 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001736 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001737 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1738 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001739 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001740
1741 // Make sure we don't have any things that we know a variable expression
1742 // won't be able to deal with before calling into it
1743 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1744 {
1745 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
Enrico Granatad27026e2012-09-05 20:41:26 +00001746 options.GetUseDynamic(),
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001747 expr_path_options,
1748 var_sp,
1749 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001750 // if this expression results in a bitfield, we give up and let the IR handle it
1751 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1752 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001753 }
Greg Clayton427f2902010-12-14 02:59:59 +00001754 }
1755 else if (m_process_sp)
1756 {
1757 m_process_sp->CalculateExecutionContext(exe_ctx);
1758 }
1759 else
1760 {
1761 CalculateExecutionContext(exe_ctx);
1762 }
1763
1764 if (result_valobj_sp)
1765 {
1766 execution_results = eExecutionCompleted;
1767 // We got a result from the frame variable expression path above...
1768 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1769
1770 lldb::ValueObjectSP const_valobj_sp;
1771
1772 // Check in case our value is already a constant value
1773 if (result_valobj_sp->GetIsConstant())
1774 {
1775 const_valobj_sp = result_valobj_sp;
1776 const_valobj_sp->SetName (persistent_variable_name);
1777 }
1778 else
Jim Inghame41494a2011-04-16 00:01:13 +00001779 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001780 if (options.GetUseDynamic() != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001781 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001782 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(options.GetUseDynamic());
Jim Inghame41494a2011-04-16 00:01:13 +00001783 if (dynamic_sp)
1784 result_valobj_sp = dynamic_sp;
1785 }
1786
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001787 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001788 }
Greg Clayton427f2902010-12-14 02:59:59 +00001789
Sean Callanan6a925532011-01-13 08:53:35 +00001790 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1791
Greg Clayton427f2902010-12-14 02:59:59 +00001792 result_valobj_sp = const_valobj_sp;
1793
Sean Callanan6a925532011-01-13 08:53:35 +00001794 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1795 assert (clang_expr_variable_sp.get());
1796
1797 // Set flags and live data as appropriate
1798
1799 const Value &result_value = live_valobj_sp->GetValue();
1800
1801 switch (result_value.GetValueType())
1802 {
1803 case Value::eValueTypeHostAddress:
1804 case Value::eValueTypeFileAddress:
1805 // we don't do anything with these for now
1806 break;
1807 case Value::eValueTypeScalar:
Greg Claytonf0fab4f2012-10-30 23:56:14 +00001808 case Value::eValueTypeVector:
Sean Callanan6a925532011-01-13 08:53:35 +00001809 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1810 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1811 break;
1812 case Value::eValueTypeLoadAddress:
1813 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1814 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1815 break;
1816 }
Greg Clayton427f2902010-12-14 02:59:59 +00001817 }
1818 else
1819 {
1820 // Make sure we aren't just trying to see the value of a persistent
1821 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001822 lldb::ClangExpressionVariableSP persistent_var_sp;
1823 // Only check for persistent variables the expression starts with a '$'
1824 if (expr_cstr[0] == '$')
1825 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1826
Greg Clayton427f2902010-12-14 02:59:59 +00001827 if (persistent_var_sp)
1828 {
1829 result_valobj_sp = persistent_var_sp->GetValueObject ();
1830 execution_results = eExecutionCompleted;
1831 }
1832 else
1833 {
1834 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001835
Greg Clayton427f2902010-12-14 02:59:59 +00001836 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Enrico Granatad27026e2012-09-05 20:41:26 +00001837 options.GetExecutionPolicy(),
Sean Callanan5b658cc2011-11-07 23:35:40 +00001838 lldb::eLanguageTypeUnknown,
Enrico Granatad27026e2012-09-05 20:41:26 +00001839 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1840 options.DoesUnwindOnError(),
Greg Clayton427f2902010-12-14 02:59:59 +00001841 expr_cstr,
1842 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001843 result_valobj_sp,
Jim Ingham47beabb2012-10-16 21:41:58 +00001844 options.GetRunOthers(),
1845 options.GetTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001846 }
1847 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001848
1849 m_suppress_stop_hooks = old_suppress_value;
1850
Greg Clayton427f2902010-12-14 02:59:59 +00001851 return execution_results;
1852}
1853
Greg Claytonc0fa5332011-05-22 22:46:53 +00001854lldb::addr_t
1855Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1856{
1857 addr_t code_addr = load_addr;
1858 switch (m_arch.GetMachine())
1859 {
1860 case llvm::Triple::arm:
1861 case llvm::Triple::thumb:
1862 switch (addr_class)
1863 {
1864 case eAddressClassData:
1865 case eAddressClassDebug:
1866 return LLDB_INVALID_ADDRESS;
1867
1868 case eAddressClassUnknown:
1869 case eAddressClassInvalid:
1870 case eAddressClassCode:
1871 case eAddressClassCodeAlternateISA:
1872 case eAddressClassRuntime:
1873 // Check if bit zero it no set?
1874 if ((code_addr & 1ull) == 0)
1875 {
1876 // Bit zero isn't set, check if the address is a multiple of 2?
1877 if (code_addr & 2ull)
1878 {
1879 // The address is a multiple of 2 so it must be thumb, set bit zero
1880 code_addr |= 1ull;
1881 }
1882 else if (addr_class == eAddressClassCodeAlternateISA)
1883 {
1884 // We checked the address and the address claims to be the alternate ISA
1885 // which means thumb, so set bit zero.
1886 code_addr |= 1ull;
1887 }
1888 }
1889 break;
1890 }
1891 break;
1892
1893 default:
1894 break;
1895 }
1896 return code_addr;
1897}
1898
1899lldb::addr_t
1900Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1901{
1902 addr_t opcode_addr = load_addr;
1903 switch (m_arch.GetMachine())
1904 {
1905 case llvm::Triple::arm:
1906 case llvm::Triple::thumb:
1907 switch (addr_class)
1908 {
1909 case eAddressClassData:
1910 case eAddressClassDebug:
1911 return LLDB_INVALID_ADDRESS;
1912
1913 case eAddressClassInvalid:
1914 case eAddressClassUnknown:
1915 case eAddressClassCode:
1916 case eAddressClassCodeAlternateISA:
1917 case eAddressClassRuntime:
1918 opcode_addr &= ~(1ull);
1919 break;
1920 }
1921 break;
1922
1923 default:
1924 break;
1925 }
1926 return opcode_addr;
1927}
1928
Jim Inghamd60d94a2011-03-11 03:53:59 +00001929lldb::user_id_t
1930Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1931{
1932 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001933 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001934 m_stop_hooks[new_uid] = new_hook_sp;
1935 return new_uid;
1936}
1937
1938bool
1939Target::RemoveStopHookByID (lldb::user_id_t user_id)
1940{
1941 size_t num_removed;
1942 num_removed = m_stop_hooks.erase (user_id);
1943 if (num_removed == 0)
1944 return false;
1945 else
1946 return true;
1947}
1948
1949void
1950Target::RemoveAllStopHooks ()
1951{
1952 m_stop_hooks.clear();
1953}
1954
1955Target::StopHookSP
1956Target::GetStopHookByID (lldb::user_id_t user_id)
1957{
1958 StopHookSP found_hook;
1959
1960 StopHookCollection::iterator specified_hook_iter;
1961 specified_hook_iter = m_stop_hooks.find (user_id);
1962 if (specified_hook_iter != m_stop_hooks.end())
1963 found_hook = (*specified_hook_iter).second;
1964 return found_hook;
1965}
1966
1967bool
1968Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1969{
1970 StopHookCollection::iterator specified_hook_iter;
1971 specified_hook_iter = m_stop_hooks.find (user_id);
1972 if (specified_hook_iter == m_stop_hooks.end())
1973 return false;
1974
1975 (*specified_hook_iter).second->SetIsActive (active_state);
1976 return true;
1977}
1978
1979void
1980Target::SetAllStopHooksActiveState (bool active_state)
1981{
1982 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1983 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1984 {
1985 (*pos).second->SetIsActive (active_state);
1986 }
1987}
1988
1989void
1990Target::RunStopHooks ()
1991{
Jim Ingham3613ae12011-05-12 02:06:14 +00001992 if (m_suppress_stop_hooks)
1993 return;
1994
Jim Inghamd60d94a2011-03-11 03:53:59 +00001995 if (!m_process_sp)
1996 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001997
1998 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1999 // since in that case we do not want to run the stop-hooks
2000 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2001 return;
2002
Jim Inghamd60d94a2011-03-11 03:53:59 +00002003 if (m_stop_hooks.empty())
2004 return;
2005
2006 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2007
2008 // If there aren't any active stop hooks, don't bother either:
2009 bool any_active_hooks = false;
2010 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2011 {
2012 if ((*pos).second->IsActive())
2013 {
2014 any_active_hooks = true;
2015 break;
2016 }
2017 }
2018 if (!any_active_hooks)
2019 return;
2020
2021 CommandReturnObject result;
2022
2023 std::vector<ExecutionContext> exc_ctx_with_reasons;
2024 std::vector<SymbolContext> sym_ctx_with_reasons;
2025
2026 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2027 size_t num_threads = cur_threadlist.GetSize();
2028 for (size_t i = 0; i < num_threads; i++)
2029 {
2030 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2031 if (cur_thread_sp->ThreadStoppedForAReason())
2032 {
2033 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2034 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2035 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2036 }
2037 }
2038
2039 // If no threads stopped for a reason, don't run the stop-hooks.
2040 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2041 if (num_exe_ctx == 0)
2042 return;
2043
Jim Inghame5ed8e92011-06-02 23:58:26 +00002044 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2045 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002046
2047 bool keep_going = true;
2048 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00002049 bool print_hook_header;
2050 bool print_thread_header;
2051
2052 if (num_exe_ctx == 1)
2053 print_thread_header = false;
2054 else
2055 print_thread_header = true;
2056
2057 if (m_stop_hooks.size() == 1)
2058 print_hook_header = false;
2059 else
2060 print_hook_header = true;
2061
Jim Inghamd60d94a2011-03-11 03:53:59 +00002062 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2063 {
2064 // result.Clear();
2065 StopHookSP cur_hook_sp = (*pos).second;
2066 if (!cur_hook_sp->IsActive())
2067 continue;
2068
2069 bool any_thread_matched = false;
2070 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2071 {
2072 if ((cur_hook_sp->GetSpecifier () == NULL
2073 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2074 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00002075 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002076 {
2077 if (!hooks_ran)
2078 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00002079 hooks_ran = true;
2080 }
Jim Inghamc54840c2011-03-22 01:47:27 +00002081 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002082 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002083 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2084 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2085 NULL);
2086 if (cmd)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002087 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chen4d96a742011-10-24 23:01:06 +00002088 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002089 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002090 any_thread_matched = true;
2091 }
2092
Jim Inghamc54840c2011-03-22 01:47:27 +00002093 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002094 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002095
2096 bool stop_on_continue = true;
2097 bool stop_on_error = true;
2098 bool echo_commands = false;
2099 bool print_results = true;
2100 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002101 &exc_ctx_with_reasons[i],
2102 stop_on_continue,
2103 stop_on_error,
2104 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002105 print_results,
2106 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002107 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002108
2109 // If the command started the target going again, we should bag out of
2110 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002111 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2112 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002113 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002114 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002115 keep_going = false;
2116 }
2117 }
2118 }
2119 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002120
Caroline Tice4a348082011-05-02 20:41:46 +00002121 result.GetImmediateOutputStream()->Flush();
2122 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002123}
2124
Greg Claytonbbea1332011-07-08 00:48:09 +00002125
Jim Inghamd60d94a2011-03-11 03:53:59 +00002126//--------------------------------------------------------------
2127// class Target::StopHook
2128//--------------------------------------------------------------
2129
2130
2131Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2132 UserID (uid),
2133 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002134 m_commands (),
2135 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002136 m_thread_spec_ap(NULL),
2137 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002138{
2139}
2140
2141Target::StopHook::StopHook (const StopHook &rhs) :
2142 UserID (rhs.GetID()),
2143 m_target_sp (rhs.m_target_sp),
2144 m_commands (rhs.m_commands),
2145 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002146 m_thread_spec_ap (NULL),
2147 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002148{
2149 if (rhs.m_thread_spec_ap.get() != NULL)
2150 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2151}
2152
2153
2154Target::StopHook::~StopHook ()
2155{
2156}
2157
2158void
2159Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2160{
2161 m_thread_spec_ap.reset (specifier);
2162}
2163
2164
2165void
2166Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2167{
2168 int indent_level = s->GetIndentLevel();
2169
2170 s->SetIndentLevel(indent_level + 2);
2171
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002172 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002173 if (m_active)
2174 s->Indent ("State: enabled\n");
2175 else
2176 s->Indent ("State: disabled\n");
2177
2178 if (m_specifier_sp)
2179 {
2180 s->Indent();
2181 s->PutCString ("Specifier:\n");
2182 s->SetIndentLevel (indent_level + 4);
2183 m_specifier_sp->GetDescription (s, level);
2184 s->SetIndentLevel (indent_level + 2);
2185 }
2186
2187 if (m_thread_spec_ap.get() != NULL)
2188 {
2189 StreamString tmp;
2190 s->Indent("Thread:\n");
2191 m_thread_spec_ap->GetDescription (&tmp, level);
2192 s->SetIndentLevel (indent_level + 4);
2193 s->Indent (tmp.GetData());
2194 s->PutCString ("\n");
2195 s->SetIndentLevel (indent_level + 2);
2196 }
2197
2198 s->Indent ("Commands: \n");
2199 s->SetIndentLevel (indent_level + 4);
2200 uint32_t num_commands = m_commands.GetSize();
2201 for (uint32_t i = 0; i < num_commands; i++)
2202 {
2203 s->Indent(m_commands.GetStringAtIndex(i));
2204 s->PutCString ("\n");
2205 }
2206 s->SetIndentLevel (indent_level);
2207}
2208
Greg Clayton73844aa2012-08-22 17:17:09 +00002209//--------------------------------------------------------------
2210// class TargetProperties
2211//--------------------------------------------------------------
2212
2213OptionEnumValueElement
2214lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002215{
Greg Clayton73844aa2012-08-22 17:17:09 +00002216 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2217 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2218 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2219 { 0, NULL, NULL }
2220};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002221
Greg Clayton49ce8962012-08-29 21:13:06 +00002222static OptionEnumValueElement
2223g_inline_breakpoint_enums[] =
2224{
2225 { 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."},
2226 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2227 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2228 { 0, NULL, NULL }
2229};
2230
Greg Clayton73844aa2012-08-22 17:17:09 +00002231static PropertyDefinition
2232g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002233{
Greg Clayton73844aa2012-08-22 17:17:09 +00002234 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2235 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2236 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2237 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2238 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2239 { "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 "
2240 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2241 "some part (starting at the root) of the path to the file when it was built, "
2242 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2243 "Each element of the array is checked in order and the first one that results in a match wins." },
2244 { "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." },
2245 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2246 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2247 { "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 +00002248 { "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." },
2249 { "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 +00002250 { "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." },
2251 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2252 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2253 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2254 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2255 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2256 { "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 +00002257 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2258 "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. "
2259 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2260 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2261 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2262 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2263 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002264 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2265};
2266enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002267{
Greg Clayton73844aa2012-08-22 17:17:09 +00002268 ePropertyDefaultArch,
2269 ePropertyExprPrefix,
2270 ePropertyPreferDynamic,
2271 ePropertyEnableSynthetic,
2272 ePropertySkipPrologue,
2273 ePropertySourceMap,
2274 ePropertyExecutableSearchPaths,
2275 ePropertyMaxChildrenCount,
2276 ePropertyMaxSummaryLength,
2277 ePropertyBreakpointUseAvoidList,
Greg Clayton0c8446c2012-10-17 22:57:12 +00002278 ePropertyArg0,
Greg Clayton73844aa2012-08-22 17:17:09 +00002279 ePropertyRunArgs,
2280 ePropertyEnvVars,
2281 ePropertyInheritEnv,
2282 ePropertyInputPath,
2283 ePropertyOutputPath,
2284 ePropertyErrorPath,
2285 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002286 ePropertyDisableSTDIO,
Greg Clayton87e9d322012-10-19 18:02:49 +00002287 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002288};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002289
Caroline Tice5bc8c972010-09-20 20:44:43 +00002290
Greg Clayton73844aa2012-08-22 17:17:09 +00002291class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002292{
Greg Clayton73844aa2012-08-22 17:17:09 +00002293public:
2294 TargetOptionValueProperties (const ConstString &name) :
2295 OptionValueProperties (name),
2296 m_target (NULL),
2297 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002298 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002299 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002300
Greg Clayton73844aa2012-08-22 17:17:09 +00002301 // This constructor is used when creating TargetOptionValueProperties when it
2302 // is part of a new lldb_private::Target instance. It will copy all current
2303 // global property values as needed
2304 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2305 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2306 m_target (target),
2307 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002308 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002309 }
2310
2311 virtual const Property *
2312 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2313 {
2314 // When gettings the value for a key from the target options, we will always
2315 // try and grab the setting from the current target if there is one. Else we just
2316 // use the one from this instance.
2317 if (idx == ePropertyEnvVars)
2318 GetHostEnvironmentIfNeeded ();
2319
2320 if (exe_ctx)
2321 {
2322 Target *target = exe_ctx->GetTargetPtr();
2323 if (target)
2324 {
2325 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2326 if (this != target_properties)
2327 return target_properties->ProtectedGetPropertyAtIndex (idx);
2328 }
2329 }
2330 return ProtectedGetPropertyAtIndex (idx);
2331 }
2332protected:
2333
2334 void
2335 GetHostEnvironmentIfNeeded () const
2336 {
2337 if (!m_got_host_env)
2338 {
2339 if (m_target)
2340 {
2341 m_got_host_env = true;
2342 const uint32_t idx = ePropertyInheritEnv;
2343 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2344 {
2345 PlatformSP platform_sp (m_target->GetPlatform());
2346 if (platform_sp)
2347 {
2348 StringList env;
2349 if (platform_sp->GetEnvironment(env))
2350 {
2351 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2352 if (env_dict)
2353 {
2354 const bool can_replace = false;
2355 const size_t envc = env.GetSize();
2356 for (size_t idx=0; idx<envc; idx++)
2357 {
2358 const char *env_entry = env.GetStringAtIndex (idx);
2359 if (env_entry)
2360 {
2361 const char *equal_pos = ::strchr(env_entry, '=');
2362 ConstString key;
2363 // It is ok to have environment variables with no values
2364 const char *value = NULL;
2365 if (equal_pos)
2366 {
2367 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2368 if (equal_pos[1])
2369 value = equal_pos + 1;
2370 }
2371 else
2372 {
2373 key.SetCString(env_entry);
2374 }
2375 // Don't allow existing keys to be replaced with ones we get from the platform environment
2376 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2377 }
2378 }
2379 }
2380 }
2381 }
2382 }
2383 }
2384 }
2385 }
2386 Target *m_target;
2387 mutable bool m_got_host_env;
2388};
2389
2390TargetProperties::TargetProperties (Target *target) :
2391 Properties ()
2392{
2393 if (target)
2394 {
2395 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002396 }
2397 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002398 {
2399 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2400 m_collection_sp->Initialize(g_properties);
2401 m_collection_sp->AppendProperty(ConstString("process"),
2402 ConstString("Settings specify to processes."),
2403 true,
2404 Process::GetGlobalProperties()->GetValueProperties());
2405 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002406}
2407
Greg Clayton73844aa2012-08-22 17:17:09 +00002408TargetProperties::~TargetProperties ()
2409{
2410}
2411ArchSpec
2412TargetProperties::GetDefaultArchitecture () const
2413{
2414 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2415 if (value)
2416 return value->GetCurrentValue();
2417 return ArchSpec();
2418}
2419
2420void
2421TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2422{
2423 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2424 if (value)
2425 return value->SetCurrentValue(arch, true);
2426}
2427
2428lldb::DynamicValueType
2429TargetProperties::GetPreferDynamicValue() const
2430{
2431 const uint32_t idx = ePropertyPreferDynamic;
2432 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2433}
2434
2435bool
2436TargetProperties::GetDisableASLR () const
2437{
2438 const uint32_t idx = ePropertyDisableASLR;
2439 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2440}
2441
2442void
2443TargetProperties::SetDisableASLR (bool b)
2444{
2445 const uint32_t idx = ePropertyDisableASLR;
2446 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2447}
2448
2449bool
2450TargetProperties::GetDisableSTDIO () const
2451{
2452 const uint32_t idx = ePropertyDisableSTDIO;
2453 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2454}
2455
2456void
2457TargetProperties::SetDisableSTDIO (bool b)
2458{
2459 const uint32_t idx = ePropertyDisableSTDIO;
2460 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2461}
2462
Greg Clayton49ce8962012-08-29 21:13:06 +00002463InlineStrategy
2464TargetProperties::GetInlineStrategy () const
2465{
2466 const uint32_t idx = ePropertyInlineStrategy;
2467 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2468}
2469
Greg Clayton0c8446c2012-10-17 22:57:12 +00002470const char *
2471TargetProperties::GetArg0 () const
2472{
2473 const uint32_t idx = ePropertyArg0;
2474 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2475}
2476
2477void
2478TargetProperties::SetArg0 (const char *arg)
2479{
2480 const uint32_t idx = ePropertyArg0;
2481 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2482}
2483
Greg Clayton73844aa2012-08-22 17:17:09 +00002484bool
2485TargetProperties::GetRunArguments (Args &args) const
2486{
2487 const uint32_t idx = ePropertyRunArgs;
2488 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2489}
2490
2491void
2492TargetProperties::SetRunArguments (const Args &args)
2493{
2494 const uint32_t idx = ePropertyRunArgs;
2495 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2496}
2497
2498size_t
2499TargetProperties::GetEnvironmentAsArgs (Args &env) const
2500{
2501 const uint32_t idx = ePropertyEnvVars;
2502 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2503}
2504
2505bool
2506TargetProperties::GetSkipPrologue() const
2507{
2508 const uint32_t idx = ePropertySkipPrologue;
2509 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2510}
2511
2512PathMappingList &
2513TargetProperties::GetSourcePathMap () const
2514{
2515 const uint32_t idx = ePropertySourceMap;
2516 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2517 assert(option_value);
2518 return option_value->GetCurrentValue();
2519}
2520
2521FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002522TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002523{
2524 const uint32_t idx = ePropertyExecutableSearchPaths;
2525 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2526 assert(option_value);
2527 return option_value->GetCurrentValue();
2528}
2529
2530bool
2531TargetProperties::GetEnableSyntheticValue () const
2532{
2533 const uint32_t idx = ePropertyEnableSynthetic;
2534 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2535}
2536
2537uint32_t
2538TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2539{
2540 const uint32_t idx = ePropertyMaxChildrenCount;
2541 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2542}
2543
2544uint32_t
2545TargetProperties::GetMaximumSizeOfStringSummary() const
2546{
2547 const uint32_t idx = ePropertyMaxSummaryLength;
2548 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2549}
2550
2551FileSpec
2552TargetProperties::GetStandardInputPath () const
2553{
2554 const uint32_t idx = ePropertyInputPath;
2555 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2556}
2557
2558void
2559TargetProperties::SetStandardInputPath (const char *p)
2560{
2561 const uint32_t idx = ePropertyInputPath;
2562 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2563}
2564
2565FileSpec
2566TargetProperties::GetStandardOutputPath () const
2567{
2568 const uint32_t idx = ePropertyOutputPath;
2569 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2570}
2571
2572void
2573TargetProperties::SetStandardOutputPath (const char *p)
2574{
2575 const uint32_t idx = ePropertyOutputPath;
2576 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2577}
2578
2579FileSpec
2580TargetProperties::GetStandardErrorPath () const
2581{
2582 const uint32_t idx = ePropertyErrorPath;
2583 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2584}
2585
Greg Claytonc6e82e42012-08-22 18:39:03 +00002586const char *
2587TargetProperties::GetExpressionPrefixContentsAsCString ()
2588{
2589 const uint32_t idx = ePropertyExprPrefix;
2590 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2591 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002592 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002593 const bool null_terminate = true;
2594 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002595 if (data_sp)
2596 return (const char *) data_sp->GetBytes();
2597 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002598 return NULL;
2599}
2600
Greg Clayton73844aa2012-08-22 17:17:09 +00002601void
2602TargetProperties::SetStandardErrorPath (const char *p)
2603{
2604 const uint32_t idx = ePropertyErrorPath;
2605 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2606}
2607
2608bool
2609TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2610{
2611 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2612 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2613}
2614
2615const TargetPropertiesSP &
2616Target::GetGlobalProperties()
2617{
2618 static TargetPropertiesSP g_settings_sp;
2619 if (!g_settings_sp)
2620 {
2621 g_settings_sp.reset (new TargetProperties (NULL));
2622 }
2623 return g_settings_sp;
2624}
2625
Jim Ingham5a15e692012-02-16 06:50:00 +00002626const ConstString &
2627Target::TargetEventData::GetFlavorString ()
2628{
2629 static ConstString g_flavor ("Target::TargetEventData");
2630 return g_flavor;
2631}
2632
2633const ConstString &
2634Target::TargetEventData::GetFlavor () const
2635{
2636 return TargetEventData::GetFlavorString ();
2637}
2638
2639Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2640 EventData(),
2641 m_target_sp (new_target_sp)
2642{
2643}
2644
2645Target::TargetEventData::~TargetEventData()
2646{
2647
2648}
2649
2650void
2651Target::TargetEventData::Dump (Stream *s) const
2652{
2653
2654}
2655
2656const TargetSP
2657Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2658{
2659 TargetSP target_sp;
2660
2661 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2662 if (data)
2663 target_sp = data->m_target_sp;
2664
2665 return target_sp;
2666}
2667
2668const Target::TargetEventData *
2669Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2670{
2671 if (event_ptr)
2672 {
2673 const EventData *event_data = event_ptr->GetData();
2674 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2675 return static_cast <const TargetEventData *> (event_ptr->GetData());
2676 }
2677 return NULL;
2678}
2679