blob: 932ef72ae439c99cdcf0ae7dac550185015d9d44 [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),
Enrico Granata146d9522012-11-08 02:22:02 +000069 m_images (this),
Greg Claytoneea26402010-09-14 23:36:40 +000070 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000071 m_breakpoint_list (false),
72 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000073 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000074 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000075 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000076 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000077 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000078 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000079 m_scratch_ast_source_ap (NULL),
80 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000081 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000082 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000083 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000084 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000085 m_suppress_stop_hooks (false),
86 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000087{
Greg Clayton49ce6822010-10-31 03:01:06 +000088 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
89 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
90 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000091
92 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000093
Greg Claytone005f2c2010-11-06 01:53:30 +000094 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000095 if (log)
96 log->Printf ("%p Target::Target()", this);
Jason Molenda8c6cf432012-12-05 00:25:49 +000097 if (m_arch.IsValid())
98 {
99 LogSP log_target(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Clayton48eca2f2012-12-05 18:23:55 +0000100 if (log_target)
101 log_target->Printf("Target::Target created with architecture %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jason Molenda8c6cf432012-12-05 00:25:49 +0000102 }
Chris Lattner24943d22010-06-08 16:52:24 +0000103}
104
105//----------------------------------------------------------------------
106// Destructor
107//----------------------------------------------------------------------
108Target::~Target()
109{
Greg Claytone005f2c2010-11-06 01:53:30 +0000110 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000111 if (log)
112 log->Printf ("%p Target::~Target()", this);
113 DeleteCurrentProcess ();
114}
115
116void
Caroline Tice7826c882010-10-26 03:11:13 +0000117Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000118{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000119// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000120 if (description_level != lldb::eDescriptionLevelBrief)
121 {
122 s->Indent();
123 s->PutCString("Target\n");
124 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000125 m_images.Dump(s);
126 m_breakpoint_list.Dump(s);
127 m_internal_breakpoint_list.Dump(s);
128 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000129 }
130 else
131 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000132 Module *exe_module = GetExecutableModulePointer();
133 if (exe_module)
134 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000135 else
136 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000137 }
Chris Lattner24943d22010-06-08 16:52:24 +0000138}
139
140void
Greg Clayton0bce9a22012-12-05 00:16:59 +0000141Target::CleanupProcess ()
142{
143 // Do any cleanup of the target we need to do between process instances.
144 // NB It is better to do this before destroying the process in case the
145 // clean up needs some help from the process.
146 m_breakpoint_list.ClearAllBreakpointSites();
147 m_internal_breakpoint_list.ClearAllBreakpointSites();
148 // Disable watchpoints just on the debugger side.
149 Mutex::Locker locker;
150 this->GetWatchpointList().GetListMutex(locker);
151 DisableAllWatchpoints(false);
152 ClearAllWatchpointHitCounts();
153}
154
155void
Chris Lattner24943d22010-06-08 16:52:24 +0000156Target::DeleteCurrentProcess ()
157{
158 if (m_process_sp.get())
159 {
Greg Clayton49480b12010-09-14 23:52:43 +0000160 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000161 if (m_process_sp->IsAlive())
162 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000163
164 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000165
Greg Clayton0bce9a22012-12-05 00:16:59 +0000166 CleanupProcess ();
167
Chris Lattner24943d22010-06-08 16:52:24 +0000168 m_process_sp.reset();
169 }
170}
171
172const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000173Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000174{
175 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000176 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000177 return m_process_sp;
178}
179
180const lldb::ProcessSP &
181Target::GetProcessSP () const
182{
183 return m_process_sp;
184}
185
Greg Clayton153ccd72011-08-10 02:10:13 +0000186void
187Target::Destroy()
188{
189 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000190 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000191 DeleteCurrentProcess ();
192 m_platform_sp.reset();
193 m_arch.Clear();
194 m_images.Clear();
195 m_section_load_list.Clear();
196 const bool notify = false;
197 m_breakpoint_list.RemoveAll(notify);
198 m_internal_breakpoint_list.RemoveAll(notify);
199 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000200 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000201 m_search_filter_sp.reset();
202 m_image_search_paths.Clear(notify);
203 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000204 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000205 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000206 m_persistent_variables.Clear();
207 m_stop_hooks.clear();
208 m_stop_hook_next_id = 0;
209 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000210 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000211}
212
213
Chris Lattner24943d22010-06-08 16:52:24 +0000214BreakpointList &
215Target::GetBreakpointList(bool internal)
216{
217 if (internal)
218 return m_internal_breakpoint_list;
219 else
220 return m_breakpoint_list;
221}
222
223const BreakpointList &
224Target::GetBreakpointList(bool internal) const
225{
226 if (internal)
227 return m_internal_breakpoint_list;
228 else
229 return m_breakpoint_list;
230}
231
232BreakpointSP
233Target::GetBreakpointByID (break_id_t break_id)
234{
235 BreakpointSP bp_sp;
236
237 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
238 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
239 else
240 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
241
242 return bp_sp;
243}
244
245BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000246Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000247 const FileSpecList *source_file_spec_list,
248 RegularExpression &source_regex,
249 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000250{
Jim Inghamd6d47972011-09-23 00:54:11 +0000251 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
252 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000253 return CreateBreakpoint (filter_sp, resolver_sp, internal);
254}
255
256
257BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000258Target::CreateBreakpoint (const FileSpecList *containingModules,
259 const FileSpec &file,
260 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000261 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000262 LazyBool skip_prologue,
263 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000264{
Greg Clayton49ce8962012-08-29 21:13:06 +0000265 if (check_inlines == eLazyBoolCalculate)
266 {
267 const InlineStrategy inline_strategy = GetInlineStrategy();
268 switch (inline_strategy)
269 {
270 case eInlineBreakpointsNever:
271 check_inlines = eLazyBoolNo;
272 break;
273
274 case eInlineBreakpointsHeaders:
275 if (file.IsSourceImplementationFile())
276 check_inlines = eLazyBoolNo;
277 else
278 check_inlines = eLazyBoolYes;
279 break;
280
281 case eInlineBreakpointsAlways:
282 check_inlines = eLazyBoolYes;
283 break;
284 }
285 }
Greg Clayton46365522012-09-07 23:48:57 +0000286 SearchFilterSP filter_sp;
287 if (check_inlines == eLazyBoolNo)
288 {
289 // Not checking for inlines, we are looking only for matching compile units
290 FileSpecList compile_unit_list;
291 compile_unit_list.Append (file);
292 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
293 }
294 else
295 {
296 filter_sp = GetSearchFilterForModuleList (containingModules);
297 }
Greg Clayton49ce8962012-08-29 21:13:06 +0000298 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
299 file,
300 line_no,
301 check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000302 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000303 return CreateBreakpoint (filter_sp, resolver_sp, internal);
304}
305
306
307BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000308Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000309{
Chris Lattner24943d22010-06-08 16:52:24 +0000310 Address so_addr;
311 // Attempt to resolve our load address if possible, though it is ok if
312 // it doesn't resolve to section/offset.
313
Greg Clayton33ed1702010-08-24 00:45:41 +0000314 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000315 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000316 if (!so_addr.IsValid())
317 {
318 // The address didn't resolve, so just set this as an absolute address
319 so_addr.SetOffset (addr);
320 }
321 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000322 return bp_sp;
323}
324
325BreakpointSP
326Target::CreateBreakpoint (Address &addr, bool internal)
327{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000328 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000329 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
330 return CreateBreakpoint (filter_sp, resolver_sp, internal);
331}
332
333BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000334Target::CreateBreakpoint (const FileSpecList *containingModules,
335 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000336 const char *func_name,
337 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000338 LazyBool skip_prologue,
339 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000340{
Greg Clayton12bec712010-06-28 21:30:43 +0000341 BreakpointSP bp_sp;
342 if (func_name)
343 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000344 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000345
346 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
347 func_name,
348 func_name_type_mask,
349 Breakpoint::Exact,
350 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000351 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
352 }
353 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000354}
355
Jim Ingham4722b102012-03-06 00:37:27 +0000356lldb::BreakpointSP
357Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000358 const FileSpecList *containingSourceFiles,
359 const std::vector<std::string> &func_names,
360 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000361 LazyBool skip_prologue,
362 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000363{
364 BreakpointSP bp_sp;
365 size_t num_names = func_names.size();
366 if (num_names > 0)
367 {
368 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
369
370 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
371 func_names,
372 func_name_type_mask,
373 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
374 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
375 }
376 return bp_sp;
377}
378
Jim Inghamc1053622012-03-03 02:05:11 +0000379BreakpointSP
380Target::CreateBreakpoint (const FileSpecList *containingModules,
381 const FileSpecList *containingSourceFiles,
382 const char *func_names[],
383 size_t num_names,
384 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000385 LazyBool skip_prologue,
386 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000387{
388 BreakpointSP bp_sp;
389 if (num_names > 0)
390 {
391 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
392
393 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
394 func_names,
395 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000396 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000397 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
398 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
399 }
400 return bp_sp;
401}
Chris Lattner24943d22010-06-08 16:52:24 +0000402
403SearchFilterSP
404Target::GetSearchFilterForModule (const FileSpec *containingModule)
405{
406 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000407 if (containingModule != NULL)
408 {
409 // TODO: We should look into sharing module based search filters
410 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000411 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000412 }
413 else
414 {
415 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000416 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000417 filter_sp = m_search_filter_sp;
418 }
419 return filter_sp;
420}
421
Jim Ingham03c8ee52011-09-21 01:17:13 +0000422SearchFilterSP
423Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
424{
425 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000426 if (containingModules && containingModules->GetSize() != 0)
427 {
428 // TODO: We should look into sharing module based search filters
429 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000430 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000431 }
432 else
433 {
434 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000435 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000436 filter_sp = m_search_filter_sp;
437 }
438 return filter_sp;
439}
440
Jim Inghamd6d47972011-09-23 00:54:11 +0000441SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000442Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
443 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000444{
445 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
446 return GetSearchFilterForModuleList(containingModules);
447
448 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000449 if (containingModules == NULL)
450 {
451 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
452 // but that will take a little reworking.
453
Greg Clayton13d24fb2012-01-29 20:56:30 +0000454 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000455 }
456 else
457 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000458 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000459 }
460 return filter_sp;
461}
462
Chris Lattner24943d22010-06-08 16:52:24 +0000463BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000464Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000465 const FileSpecList *containingSourceFiles,
466 RegularExpression &func_regex,
467 LazyBool skip_prologue,
468 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000469{
Jim Inghamd6d47972011-09-23 00:54:11 +0000470 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000471 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
472 func_regex,
473 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000474
475 return CreateBreakpoint (filter_sp, resolver_sp, internal);
476}
477
Jim Ingham3df164e2012-03-05 04:47:34 +0000478lldb::BreakpointSP
479Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
480{
481 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
482}
483
Chris Lattner24943d22010-06-08 16:52:24 +0000484BreakpointSP
485Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
486{
487 BreakpointSP bp_sp;
488 if (filter_sp && resolver_sp)
489 {
490 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
491 resolver_sp->SetBreakpoint (bp_sp.get());
492
493 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000494 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000495 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000496 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000497
Greg Claytone005f2c2010-11-06 01:53:30 +0000498 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000499 if (log)
500 {
501 StreamString s;
502 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
503 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
504 }
505
Chris Lattner24943d22010-06-08 16:52:24 +0000506 bp_sp->ResolveBreakpoint();
507 }
Jim Inghamd1686902010-10-14 23:45:03 +0000508
509 if (!internal && bp_sp)
510 {
511 m_last_created_breakpoint = bp_sp;
512 }
513
Chris Lattner24943d22010-06-08 16:52:24 +0000514 return bp_sp;
515}
516
Johnny Chenda5a8022011-09-20 23:28:55 +0000517bool
518Target::ProcessIsValid()
519{
520 return (m_process_sp && m_process_sp->IsAlive());
521}
522
Johnny Chen3f883492012-06-04 23:19:54 +0000523static bool
524CheckIfWatchpointsExhausted(Target *target, Error &error)
525{
526 uint32_t num_supported_hardware_watchpoints;
527 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
528 if (rc.Success())
529 {
530 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
531 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
532 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
533 num_supported_hardware_watchpoints);
534 }
535 return false;
536}
537
Johnny Chenecd4feb2011-10-14 00:42:25 +0000538// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000539// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000540WatchpointSP
Jim Ingham9e376622012-10-23 07:20:06 +0000541Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000542{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000543 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
544 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000545 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Ingham9e376622012-10-23 07:20:06 +0000546 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000547
Johnny Chenecd4feb2011-10-14 00:42:25 +0000548 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000549 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000550 {
551 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000552 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000553 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000554 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000555 {
556 if (size == 0)
557 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
558 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000559 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000560 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000561 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000562
Johnny Chenecd4feb2011-10-14 00:42:25 +0000563 // Currently we only support one watchpoint per address, with total number
564 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000565
566 // Grab the list mutex while doing operations.
567 Mutex::Locker locker;
568 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000569 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000570 if (matched_sp)
571 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000572 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000573 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000574 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
575 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000576 // Return the existing watchpoint if both size and type match.
Jim Ingham9e376622012-10-23 07:20:06 +0000577 if (size == old_size && kind == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000578 wp_sp = matched_sp;
579 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000580 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000581 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000582 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000583 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000584 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000585 }
586
Jason Molendac4d49fd2012-12-05 23:07:34 +0000587 if (!wp_sp)
588 {
Jim Ingham9e376622012-10-23 07:20:06 +0000589 Watchpoint *new_wp = new Watchpoint(*this, addr, size, type);
Jason Molendac4d49fd2012-12-05 23:07:34 +0000590 if (!new_wp)
591 {
592 error.SetErrorString("Watchpoint ctor failed, out of memory?");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000593 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000594 }
Jim Ingham9e376622012-10-23 07:20:06 +0000595 new_wp->SetWatchpointType(kind);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000596 wp_sp.reset(new_wp);
597 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000598 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000599
Johnny Chen3f883492012-06-04 23:19:54 +0000600 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000601 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +0000602 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
603 __FUNCTION__,
604 error.Success() ? "succeeded" : "failed",
605 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000606
Jason Molendac4d49fd2012-12-05 23:07:34 +0000607 if (error.Fail())
608 {
Johnny Chen155599b2012-03-26 22:00:10 +0000609 // Enabling the watchpoint on the device side failed.
610 // Remove the said watchpoint from the list maintained by the target instance.
611 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000612 // See if we could provide more helpful error message.
613 if (!CheckIfWatchpointsExhausted(this, error))
614 {
615 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
616 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
617 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000618 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000619 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000620 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000621 m_last_created_watchpoint = wp_sp;
622 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000623}
624
Chris Lattner24943d22010-06-08 16:52:24 +0000625void
626Target::RemoveAllBreakpoints (bool internal_also)
627{
Greg Claytone005f2c2010-11-06 01:53:30 +0000628 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000629 if (log)
630 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
631
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000632 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000633 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000634 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000635
636 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000637}
638
639void
640Target::DisableAllBreakpoints (bool internal_also)
641{
Greg Claytone005f2c2010-11-06 01:53:30 +0000642 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000643 if (log)
644 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
645
646 m_breakpoint_list.SetEnabledAll (false);
647 if (internal_also)
648 m_internal_breakpoint_list.SetEnabledAll (false);
649}
650
651void
652Target::EnableAllBreakpoints (bool internal_also)
653{
Greg Claytone005f2c2010-11-06 01:53:30 +0000654 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000655 if (log)
656 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
657
658 m_breakpoint_list.SetEnabledAll (true);
659 if (internal_also)
660 m_internal_breakpoint_list.SetEnabledAll (true);
661}
662
663bool
664Target::RemoveBreakpointByID (break_id_t break_id)
665{
Greg Claytone005f2c2010-11-06 01:53:30 +0000666 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000667 if (log)
668 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
669
670 if (DisableBreakpointByID (break_id))
671 {
672 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000673 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000674 else
Jim Inghamd1686902010-10-14 23:45:03 +0000675 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000676 if (m_last_created_breakpoint)
677 {
678 if (m_last_created_breakpoint->GetID() == break_id)
679 m_last_created_breakpoint.reset();
680 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000681 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000682 }
Chris Lattner24943d22010-06-08 16:52:24 +0000683 return true;
684 }
685 return false;
686}
687
688bool
689Target::DisableBreakpointByID (break_id_t break_id)
690{
Greg Claytone005f2c2010-11-06 01:53:30 +0000691 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000692 if (log)
693 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
694
695 BreakpointSP bp_sp;
696
697 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
698 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
699 else
700 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
701 if (bp_sp)
702 {
703 bp_sp->SetEnabled (false);
704 return true;
705 }
706 return false;
707}
708
709bool
710Target::EnableBreakpointByID (break_id_t break_id)
711{
Greg Claytone005f2c2010-11-06 01:53:30 +0000712 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000713 if (log)
714 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
715 __FUNCTION__,
716 break_id,
717 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
718
719 BreakpointSP bp_sp;
720
721 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
722 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
723 else
724 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
725
726 if (bp_sp)
727 {
728 bp_sp->SetEnabled (true);
729 return true;
730 }
731 return false;
732}
733
Johnny Chenc86582f2011-09-23 21:21:43 +0000734// The flag 'end_to_end', default to true, signifies that the operation is
735// performed end to end, for both the debugger and the debuggee.
736
Johnny Chenecd4feb2011-10-14 00:42:25 +0000737// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
738// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000739bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000740Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000741{
742 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
743 if (log)
744 log->Printf ("Target::%s\n", __FUNCTION__);
745
Johnny Chenc86582f2011-09-23 21:21:43 +0000746 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000747 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000748 return true;
749 }
750
751 // Otherwise, it's an end to end operation.
752
Johnny Chenda5a8022011-09-20 23:28:55 +0000753 if (!ProcessIsValid())
754 return false;
755
Johnny Chenecd4feb2011-10-14 00:42:25 +0000756 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000757 for (size_t i = 0; i < num_watchpoints; ++i)
758 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000759 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
760 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000761 return false;
762
Johnny Chenecd4feb2011-10-14 00:42:25 +0000763 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000764 if (rc.Fail())
765 return false;
766 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000767 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000768 return true; // Success!
769}
770
Johnny Chenecd4feb2011-10-14 00:42:25 +0000771// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
772// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000773bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000774Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000775{
776 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
777 if (log)
778 log->Printf ("Target::%s\n", __FUNCTION__);
779
Johnny Chenc86582f2011-09-23 21:21:43 +0000780 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000781 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000782 return true;
783 }
784
785 // Otherwise, it's an end to end operation.
786
Johnny Chenda5a8022011-09-20 23:28:55 +0000787 if (!ProcessIsValid())
788 return false;
789
Johnny Chenecd4feb2011-10-14 00:42:25 +0000790 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000791 for (size_t i = 0; i < num_watchpoints; ++i)
792 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000793 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
794 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000795 return false;
796
Johnny Chenecd4feb2011-10-14 00:42:25 +0000797 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000798 if (rc.Fail())
799 return false;
800 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000801 return true; // Success!
802}
803
Johnny Chenecd4feb2011-10-14 00:42:25 +0000804// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
805// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000806bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000807Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000808{
809 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
810 if (log)
811 log->Printf ("Target::%s\n", __FUNCTION__);
812
Johnny Chenc86582f2011-09-23 21:21:43 +0000813 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000814 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000815 return true;
816 }
817
818 // Otherwise, it's an end to end operation.
819
Johnny Chenda5a8022011-09-20 23:28:55 +0000820 if (!ProcessIsValid())
821 return false;
822
Johnny Chenecd4feb2011-10-14 00:42:25 +0000823 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000824 for (size_t i = 0; i < num_watchpoints; ++i)
825 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000826 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
827 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000828 return false;
829
Johnny Chenecd4feb2011-10-14 00:42:25 +0000830 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000831 if (rc.Fail())
832 return false;
833 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000834 return true; // Success!
835}
836
Johnny Chen116a5cd2012-02-25 06:44:30 +0000837// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
838bool
839Target::ClearAllWatchpointHitCounts ()
840{
841 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
842 if (log)
843 log->Printf ("Target::%s\n", __FUNCTION__);
844
845 size_t num_watchpoints = m_watchpoint_list.GetSize();
846 for (size_t i = 0; i < num_watchpoints; ++i)
847 {
848 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
849 if (!wp_sp)
850 return false;
851
852 wp_sp->ResetHitCount();
853 }
854 return true; // Success!
855}
856
Johnny Chenecd4feb2011-10-14 00:42:25 +0000857// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000858// during these operations.
859bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000860Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000861{
862 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
863 if (log)
864 log->Printf ("Target::%s\n", __FUNCTION__);
865
866 if (!ProcessIsValid())
867 return false;
868
Johnny Chenecd4feb2011-10-14 00:42:25 +0000869 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000870 for (size_t i = 0; i < num_watchpoints; ++i)
871 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000872 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
873 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000874 return false;
875
Johnny Chenecd4feb2011-10-14 00:42:25 +0000876 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000877 }
878 return true; // Success!
879}
880
Johnny Chenecd4feb2011-10-14 00:42:25 +0000881// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000882bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000883Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000884{
885 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
886 if (log)
887 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
888
889 if (!ProcessIsValid())
890 return false;
891
Johnny Chenecd4feb2011-10-14 00:42:25 +0000892 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
893 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000894 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000895 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000896 if (rc.Success())
897 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000898
Johnny Chen01acfa72011-09-22 18:04:58 +0000899 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000900 }
901 return false;
902}
903
Johnny Chenecd4feb2011-10-14 00:42:25 +0000904// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000905bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000906Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000907{
908 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
909 if (log)
910 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
911
912 if (!ProcessIsValid())
913 return false;
914
Johnny Chenecd4feb2011-10-14 00:42:25 +0000915 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
916 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000917 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000918 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000919 if (rc.Success())
920 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000921
Johnny Chen01acfa72011-09-22 18:04:58 +0000922 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000923 }
924 return false;
925}
926
Johnny Chenecd4feb2011-10-14 00:42:25 +0000927// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000928bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000929Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000930{
931 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
932 if (log)
933 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
934
Johnny Chenecd4feb2011-10-14 00:42:25 +0000935 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000936 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000937 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000938 return true;
939 }
940 return false;
941}
942
Johnny Chenecd4feb2011-10-14 00:42:25 +0000943// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000944bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000945Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000946{
947 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
948 if (log)
949 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
950
951 if (!ProcessIsValid())
952 return false;
953
Johnny Chenecd4feb2011-10-14 00:42:25 +0000954 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
955 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000956 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000957 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000958 return true;
959 }
960 return false;
961}
962
Chris Lattner24943d22010-06-08 16:52:24 +0000963ModuleSP
964Target::GetExecutableModule ()
965{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000966 return m_images.GetModuleAtIndex(0);
967}
968
969Module*
970Target::GetExecutableModulePointer ()
971{
972 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000973}
974
Enrico Granata146d9522012-11-08 02:22:02 +0000975static void
976LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
977{
978 Error error;
979 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error))
980 {
981 target->GetDebugger().GetOutputStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
982 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
983 error.AsCString());
984 }
985}
986
Chris Lattner24943d22010-06-08 16:52:24 +0000987void
988Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
989{
Jason Molenda8c6cf432012-12-05 00:25:49 +0000990 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Chris Lattner24943d22010-06-08 16:52:24 +0000991 m_images.Clear();
992 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000993 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000994 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000995
996 if (executable_sp.get())
997 {
998 Timer scoped_timer (__PRETTY_FUNCTION__,
999 "Target::SetExecutableModule (executable = '%s/%s')",
1000 executable_sp->GetFileSpec().GetDirectory().AsCString(),
1001 executable_sp->GetFileSpec().GetFilename().AsCString());
1002
1003 m_images.Append(executable_sp); // The first image is our exectuable file
1004
Jim Ingham7508e732010-08-09 23:31:02 +00001005 // 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 +00001006 if (!m_arch.IsValid())
Jason Molenda8c6cf432012-12-05 00:25:49 +00001007 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001008 m_arch = executable_sp->GetArchitecture();
Jason Molenda8c6cf432012-12-05 00:25:49 +00001009 if (log)
1010 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1011 }
1012
Chris Lattner24943d22010-06-08 16:52:24 +00001013 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001014 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +00001015
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001016 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +00001017 {
1018 executable_objfile->GetDependentModules(dependent_files);
1019 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1020 {
Greg Claytonb1888f22011-03-19 01:12:21 +00001021 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1022 FileSpec platform_dependent_file_spec;
1023 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +00001024 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +00001025 else
1026 platform_dependent_file_spec = dependent_file_spec;
1027
Greg Clayton444fe992012-02-26 05:51:37 +00001028 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1029 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +00001030 if (image_module_sp.get())
1031 {
Chris Lattner24943d22010-06-08 16:52:24 +00001032 ObjectFile *objfile = image_module_sp->GetObjectFile();
1033 if (objfile)
1034 objfile->GetDependentModules(dependent_files);
1035 }
1036 }
1037 }
Chris Lattner24943d22010-06-08 16:52:24 +00001038 }
1039}
1040
1041
Jim Ingham7508e732010-08-09 23:31:02 +00001042bool
1043Target::SetArchitecture (const ArchSpec &arch_spec)
1044{
Jason Molenda8c6cf432012-12-05 00:25:49 +00001045 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb170aee2012-05-08 01:45:38 +00001046 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001047 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001048 // If we haven't got a valid arch spec, or the architectures are
1049 // compatible, so just update the architecture. Architectures can be
1050 // equal, yet the triple OS and vendor might change, so we need to do
1051 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001052 m_arch = arch_spec;
Jason Molenda8c6cf432012-12-05 00:25:49 +00001053 if (log)
1054 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 +00001055 return true;
1056 }
1057 else
1058 {
1059 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molenda8c6cf432012-12-05 00:25:49 +00001060 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +00001061 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 +00001062 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001063 ModuleSP executable_sp = GetExecutableModule ();
1064 m_images.Clear();
1065 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001066 m_scratch_ast_source_ap.reset();
1067 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001068 // Need to do something about unsetting breakpoints.
1069
1070 if (executable_sp)
1071 {
Jason Molenda8c6cf432012-12-05 00:25:49 +00001072 if (log)
1073 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 +00001074 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1075 Error error = ModuleList::GetSharedModule (module_spec,
1076 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001077 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001078 NULL,
1079 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001080
1081 if (!error.Fail() && executable_sp)
1082 {
1083 SetExecutableModule (executable_sp, true);
1084 return true;
1085 }
Jim Ingham7508e732010-08-09 23:31:02 +00001086 }
1087 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001088 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001089}
Chris Lattner24943d22010-06-08 16:52:24 +00001090
Chris Lattner24943d22010-06-08 16:52:24 +00001091void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001092Target::WillClearList (const ModuleList& module_list)
Enrico Granata146d9522012-11-08 02:22:02 +00001093{
1094}
1095
1096void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001097Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001098{
1099 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001100 ModuleList my_module_list;
1101 my_module_list.Append(module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +00001102 LoadScriptingResourceForModule(module_sp, this);
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001103 ModulesDidLoad (my_module_list);
Chris Lattner24943d22010-06-08 16:52:24 +00001104}
1105
1106void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001107Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata146d9522012-11-08 02:22:02 +00001108{
1109 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001110 ModuleList my_module_list;
1111 my_module_list.Append(module_sp);
1112 ModulesDidUnload (my_module_list);
Enrico Granata146d9522012-11-08 02:22:02 +00001113}
1114
1115void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001116Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001117{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001118 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001119 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001120}
1121
1122void
1123Target::ModulesDidLoad (ModuleList &module_list)
1124{
Enrico Granata146d9522012-11-08 02:22:02 +00001125 if (module_list.GetSize())
1126 {
1127 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1128 // TODO: make event data that packages up the module_list
1129 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1130 }
Chris Lattner24943d22010-06-08 16:52:24 +00001131}
1132
1133void
1134Target::ModulesDidUnload (ModuleList &module_list)
1135{
Enrico Granata146d9522012-11-08 02:22:02 +00001136 if (module_list.GetSize())
1137 {
1138 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1139 // TODO: make event data that packages up the module_list
1140 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1141 }
Chris Lattner24943d22010-06-08 16:52:24 +00001142}
1143
Daniel Dunbar705a0982011-10-31 22:50:37 +00001144bool
Greg Clayton444fe992012-02-26 05:51:37 +00001145Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001146{
Greg Clayton73844aa2012-08-22 17:17:09 +00001147 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001148 {
1149 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001150 ModuleSpec module_spec (module_file_spec);
1151 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001152
1153 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1154 // black list.
1155 if (num_modules > 0)
1156 {
1157 for (int i = 0; i < num_modules; i++)
1158 {
1159 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1160 return false;
1161 }
1162 return true;
1163 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001164 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001165 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001166}
1167
Daniel Dunbar705a0982011-10-31 22:50:37 +00001168bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001169Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1170{
Greg Clayton73844aa2012-08-22 17:17:09 +00001171 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001172 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001173 if (m_platform_sp)
1174 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
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
Chris Lattner24943d22010-06-08 16:52:24 +00001179size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001180Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1181{
Greg Clayton3508c382012-02-24 01:59:29 +00001182 SectionSP section_sp (addr.GetSection());
1183 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001184 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001185 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1186 if (section_sp->IsEncrypted())
1187 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001188 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001189 return 0;
1190 }
Greg Clayton3508c382012-02-24 01:59:29 +00001191 ModuleSP module_sp (section_sp->GetModule());
1192 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001193 {
Greg Clayton3508c382012-02-24 01:59:29 +00001194 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1195 if (objfile)
1196 {
1197 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1198 addr.GetOffset(),
1199 dst,
1200 dst_len);
1201 if (bytes_read > 0)
1202 return bytes_read;
1203 else
1204 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1205 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001206 else
Greg Clayton3508c382012-02-24 01:59:29 +00001207 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001208 }
1209 else
Greg Clayton3508c382012-02-24 01:59:29 +00001210 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001211 }
1212 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001213 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001214
Greg Clayton26100dc2011-01-07 01:57:07 +00001215 return 0;
1216}
1217
1218size_t
Enrico Granata91544802011-09-06 19:20:51 +00001219Target::ReadMemory (const Address& addr,
1220 bool prefer_file_cache,
1221 void *dst,
1222 size_t dst_len,
1223 Error &error,
1224 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001225{
Chris Lattner24943d22010-06-08 16:52:24 +00001226 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001227
Enrico Granata91544802011-09-06 19:20:51 +00001228 // if we end up reading this from process memory, we will fill this
1229 // with the actual load address
1230 if (load_addr_ptr)
1231 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1232
Greg Clayton26100dc2011-01-07 01:57:07 +00001233 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001234
1235 addr_t load_addr = LLDB_INVALID_ADDRESS;
1236 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001237 Address resolved_addr;
1238 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001239 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001240 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001241 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001242 // No sections are loaded, so we must assume we are not running
1243 // yet and anything we are given is a file address.
1244 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1245 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001246 }
Greg Clayton70436352010-06-30 23:03:03 +00001247 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001248 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001249 // We have at least one section loaded. This can be becuase
1250 // we have manually loaded some sections with "target modules load ..."
1251 // or because we have have a live process that has sections loaded
1252 // through the dynamic loader
1253 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1254 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001255 }
Greg Clayton70436352010-06-30 23:03:03 +00001256 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001257 if (!resolved_addr.IsValid())
1258 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001259
Greg Clayton9b82f862011-07-11 05:12:02 +00001260
Greg Clayton26100dc2011-01-07 01:57:07 +00001261 if (prefer_file_cache)
1262 {
1263 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1264 if (bytes_read > 0)
1265 return bytes_read;
1266 }
Greg Clayton70436352010-06-30 23:03:03 +00001267
Johnny Chenda5a8022011-09-20 23:28:55 +00001268 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001269 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001270 if (load_addr == LLDB_INVALID_ADDRESS)
1271 load_addr = resolved_addr.GetLoadAddress (this);
1272
Greg Clayton70436352010-06-30 23:03:03 +00001273 if (load_addr == LLDB_INVALID_ADDRESS)
1274 {
Greg Clayton3508c382012-02-24 01:59:29 +00001275 ModuleSP addr_module_sp (resolved_addr.GetModule());
1276 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001277 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001278 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001279 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001280 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001281 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001282 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001283 }
1284 else
1285 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001286 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001287 if (bytes_read != dst_len)
1288 {
1289 if (error.Success())
1290 {
1291 if (bytes_read == 0)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001292 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001293 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001294 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 +00001295 }
1296 }
Greg Clayton70436352010-06-30 23:03:03 +00001297 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001298 {
1299 if (load_addr_ptr)
1300 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001301 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001302 }
Greg Clayton70436352010-06-30 23:03:03 +00001303 // If the address is not section offset we have an address that
1304 // doesn't resolve to any address in any currently loaded shared
1305 // libaries and we failed to read memory so there isn't anything
1306 // more we can do. If it is section offset, we might be able to
1307 // read cached memory from the object file.
1308 if (!resolved_addr.IsSectionOffset())
1309 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001310 }
Chris Lattner24943d22010-06-08 16:52:24 +00001311 }
Greg Clayton70436352010-06-30 23:03:03 +00001312
Greg Clayton9b82f862011-07-11 05:12:02 +00001313 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001314 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001315 // If we didn't already try and read from the object file cache, then
1316 // try it after failing to read from the process.
1317 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001318 }
1319 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001320}
1321
Greg Clayton7dd98df2011-07-12 17:06:17 +00001322size_t
1323Target::ReadScalarIntegerFromMemory (const Address& addr,
1324 bool prefer_file_cache,
1325 uint32_t byte_size,
1326 bool is_signed,
1327 Scalar &scalar,
1328 Error &error)
1329{
1330 uint64_t uval;
1331
1332 if (byte_size <= sizeof(uval))
1333 {
1334 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1335 if (bytes_read == byte_size)
1336 {
1337 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1338 uint32_t offset = 0;
1339 if (byte_size <= 4)
1340 scalar = data.GetMaxU32 (&offset, byte_size);
1341 else
1342 scalar = data.GetMaxU64 (&offset, byte_size);
1343
1344 if (is_signed)
1345 scalar.SignExtend(byte_size * 8);
1346 return bytes_read;
1347 }
1348 }
1349 else
1350 {
1351 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1352 }
1353 return 0;
1354}
1355
1356uint64_t
1357Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1358 bool prefer_file_cache,
1359 size_t integer_byte_size,
1360 uint64_t fail_value,
1361 Error &error)
1362{
1363 Scalar scalar;
1364 if (ReadScalarIntegerFromMemory (addr,
1365 prefer_file_cache,
1366 integer_byte_size,
1367 false,
1368 scalar,
1369 error))
1370 return scalar.ULongLong(fail_value);
1371 return fail_value;
1372}
1373
1374bool
1375Target::ReadPointerFromMemory (const Address& addr,
1376 bool prefer_file_cache,
1377 Error &error,
1378 Address &pointer_addr)
1379{
1380 Scalar scalar;
1381 if (ReadScalarIntegerFromMemory (addr,
1382 prefer_file_cache,
1383 m_arch.GetAddressByteSize(),
1384 false,
1385 scalar,
1386 error))
1387 {
1388 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1389 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1390 {
1391 if (m_section_load_list.IsEmpty())
1392 {
1393 // No sections are loaded, so we must assume we are not running
1394 // yet and anything we are given is a file address.
1395 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1396 }
1397 else
1398 {
1399 // We have at least one section loaded. This can be becuase
1400 // we have manually loaded some sections with "target modules load ..."
1401 // or because we have have a live process that has sections loaded
1402 // through the dynamic loader
1403 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1404 }
1405 // We weren't able to resolve the pointer value, so just return
1406 // an address with no section
1407 if (!pointer_addr.IsValid())
1408 pointer_addr.SetOffset (pointer_vm_addr);
1409 return true;
1410
1411 }
1412 }
1413 return false;
1414}
Chris Lattner24943d22010-06-08 16:52:24 +00001415
1416ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001417Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001418{
Chris Lattner24943d22010-06-08 16:52:24 +00001419 ModuleSP module_sp;
1420
Chris Lattner24943d22010-06-08 16:52:24 +00001421 Error error;
1422
Jim Ingham03e5e512012-05-17 18:38:42 +00001423 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1424 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001425
Jim Ingham03e5e512012-05-17 18:38:42 +00001426 if (module_spec.GetUUID().IsValid())
1427 module_sp = m_images.FindFirstModule(module_spec);
1428
Greg Claytone1ef1e32012-04-27 00:58:27 +00001429 if (!module_sp)
1430 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001431 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1432 bool did_create_module = false;
1433
1434 // If there are image search path entries, try to use them first to acquire a suitable image.
1435 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001436 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001437 ModuleSpec transformed_spec (module_spec);
1438 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1439 {
1440 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1441 error = ModuleList::GetSharedModule (transformed_spec,
1442 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001443 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001444 &old_module_sp,
1445 &did_create_module);
1446 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001447 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001448
Greg Claytone1ef1e32012-04-27 00:58:27 +00001449 if (!module_sp)
1450 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001451 // If we have a UUID, we can check our global shared module list in case
1452 // we already have it. If we don't have a valid UUID, then we can't since
1453 // the path in "module_spec" will be a platform path, and we will need to
1454 // let the platform find that file. For example, we could be asking for
1455 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1456 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1457 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1458 // cache.
1459 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001460 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001461 // We have a UUID, it is OK to check the global module list...
1462 error = ModuleList::GetSharedModule (module_spec,
1463 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001464 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001465 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001466 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001467 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001468
1469 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001470 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001471 // The platform is responsible for finding and caching an appropriate
1472 // module in the shared module cache.
1473 if (m_platform_sp)
1474 {
1475 FileSpec platform_file_spec;
1476 error = m_platform_sp->GetSharedModule (module_spec,
1477 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001478 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001479 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001480 &did_create_module);
1481 }
1482 else
1483 {
1484 error.SetErrorString("no platform is currently set");
1485 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001486 }
1487 }
Chris Lattner24943d22010-06-08 16:52:24 +00001488
Jim Ingham03e5e512012-05-17 18:38:42 +00001489 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1490 // module in the list already, and if there was, let's remove it.
1491 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001492 {
Greg Clayton1649a722012-11-29 22:16:27 +00001493 ObjectFile *objfile = module_sp->GetObjectFile();
1494 if (objfile)
Jim Ingham03e5e512012-05-17 18:38:42 +00001495 {
Greg Clayton1649a722012-11-29 22:16:27 +00001496 switch (objfile->GetType())
Jim Ingham03e5e512012-05-17 18:38:42 +00001497 {
Greg Clayton1649a722012-11-29 22:16:27 +00001498 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1499 case ObjectFile::eTypeExecutable: /// A normal executable
1500 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1501 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1502 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1503 break;
1504 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1505 if (error_ptr)
1506 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1507 return ModuleSP();
1508 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1509 if (error_ptr)
1510 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1511 return ModuleSP();
1512 default:
1513 if (error_ptr)
1514 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1515 return ModuleSP();
1516 }
1517 // GetSharedModule is not guaranteed to find the old shared module, for instance
1518 // in the common case where you pass in the UUID, it is only going to find the one
1519 // module matching the UUID. In fact, it has no good way to know what the "old module"
1520 // relevant to this target is, since there might be many copies of a module with this file spec
1521 // in various running debug sessions, but only one of them will belong to this target.
1522 // So let's remove the UUID from the module list, and look in the target's module list.
1523 // Only do this if there is SOMETHING else in the module spec...
1524 if (!old_module_sp)
1525 {
1526 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham03e5e512012-05-17 18:38:42 +00001527 {
Greg Clayton1649a722012-11-29 22:16:27 +00001528 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1529 module_spec_copy.GetUUID().Clear();
1530
1531 ModuleList found_modules;
1532 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1533 if (num_found == 1)
1534 {
1535 old_module_sp = found_modules.GetModuleAtIndex(0);
1536 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001537 }
1538 }
Greg Clayton1649a722012-11-29 22:16:27 +00001539
1540 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1541 {
1542 m_images.ReplaceModule(old_module_sp, module_sp);
1543 Module *old_module_ptr = old_module_sp.get();
1544 old_module_sp.reset();
1545 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1546 }
1547 else
1548 m_images.Append(module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001549 }
Chris Lattner24943d22010-06-08 16:52:24 +00001550 }
1551 }
1552 if (error_ptr)
1553 *error_ptr = error;
1554 return module_sp;
1555}
1556
1557
Greg Clayton289afcb2012-02-18 05:35:26 +00001558TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001559Target::CalculateTarget ()
1560{
Greg Clayton289afcb2012-02-18 05:35:26 +00001561 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001562}
1563
Greg Clayton289afcb2012-02-18 05:35:26 +00001564ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001565Target::CalculateProcess ()
1566{
Greg Clayton289afcb2012-02-18 05:35:26 +00001567 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001568}
1569
Greg Clayton289afcb2012-02-18 05:35:26 +00001570ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001571Target::CalculateThread ()
1572{
Greg Clayton289afcb2012-02-18 05:35:26 +00001573 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001574}
1575
Greg Clayton289afcb2012-02-18 05:35:26 +00001576StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001577Target::CalculateStackFrame ()
1578{
Greg Clayton289afcb2012-02-18 05:35:26 +00001579 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001580}
1581
1582void
Greg Claytona830adb2010-10-04 01:05:56 +00001583Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001584{
Greg Clayton567e7f32011-09-22 04:58:26 +00001585 exe_ctx.Clear();
1586 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001587}
1588
1589PathMappingList &
1590Target::GetImageSearchPathList ()
1591{
1592 return m_image_search_paths;
1593}
1594
1595void
1596Target::ImageSearchPathsChanged
1597(
1598 const PathMappingList &path_list,
1599 void *baton
1600)
1601{
1602 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001603 ModuleSP exe_module_sp (target->GetExecutableModule());
1604 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001605 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001606 target->m_images.Clear();
1607 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001608 }
1609}
1610
1611ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001612Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001613{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001614 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001615 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001616 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001617 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001618 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001619 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1620 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1621 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1622 }
Chris Lattner24943d22010-06-08 16:52:24 +00001623 return m_scratch_ast_context_ap.get();
1624}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001625
Sean Callanan4938bd62011-11-16 18:20:47 +00001626ClangASTImporter *
1627Target::GetClangASTImporter()
1628{
1629 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1630
1631 if (!ast_importer)
1632 {
1633 ast_importer = new ClangASTImporter();
1634 m_ast_importer_ap.reset(ast_importer);
1635 }
1636
1637 return ast_importer;
1638}
1639
Greg Clayton990de7b2010-11-18 23:32:35 +00001640void
Caroline Tice2a456812011-03-10 22:14:10 +00001641Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001642{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001643 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001644}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001645
Greg Clayton990de7b2010-11-18 23:32:35 +00001646void
Caroline Tice2a456812011-03-10 22:14:10 +00001647Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001648{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001649 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001650}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001651
Greg Clayton9ce95382012-02-13 23:10:39 +00001652FileSpecList
1653Target::GetDefaultExecutableSearchPaths ()
1654{
Greg Clayton73844aa2012-08-22 17:17:09 +00001655 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1656 if (properties_sp)
1657 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001658 return FileSpecList();
1659}
1660
Caroline Tice5bc8c972010-09-20 20:44:43 +00001661ArchSpec
1662Target::GetDefaultArchitecture ()
1663{
Greg Clayton73844aa2012-08-22 17:17:09 +00001664 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1665 if (properties_sp)
1666 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001667 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001668}
1669
1670void
Greg Clayton73844aa2012-08-22 17:17:09 +00001671Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001672{
Greg Clayton73844aa2012-08-22 17:17:09 +00001673 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1674 if (properties_sp)
1675 return properties_sp->SetDefaultArchitecture(arch);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001676}
1677
Greg Claytona830adb2010-10-04 01:05:56 +00001678Target *
1679Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1680{
1681 // The target can either exist in the "process" of ExecutionContext, or in
1682 // the "target_sp" member of SymbolContext. This accessor helper function
1683 // will get the target from one of these locations.
1684
1685 Target *target = NULL;
1686 if (sc_ptr != NULL)
1687 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001688 if (target == NULL && exe_ctx_ptr)
1689 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001690 return target;
1691}
1692
Greg Clayton427f2902010-12-14 02:59:59 +00001693ExecutionResults
1694Target::EvaluateExpression
1695(
1696 const char *expr_cstr,
1697 StackFrame *frame,
Enrico Granata6cca9692012-07-16 23:10:35 +00001698 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001699 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001700)
1701{
Enrico Granata3a08fd12012-09-18 17:43:16 +00001702 result_valobj_sp.reset();
1703
Greg Clayton427f2902010-12-14 02:59:59 +00001704 ExecutionResults execution_results = eExecutionSetupError;
1705
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001706 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1707 return execution_results;
1708
Jim Ingham3613ae12011-05-12 02:06:14 +00001709 // We shouldn't run stop hooks in expressions.
1710 // Be sure to reset this if you return anywhere within this function.
1711 bool old_suppress_value = m_suppress_stop_hooks;
1712 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001713
1714 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001715
1716 const size_t expr_cstr_len = ::strlen (expr_cstr);
1717
Greg Clayton427f2902010-12-14 02:59:59 +00001718 if (frame)
1719 {
1720 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001721 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001722 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001723 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1724 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001725 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001726
1727 // Make sure we don't have any things that we know a variable expression
1728 // won't be able to deal with before calling into it
1729 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1730 {
1731 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
Enrico Granatad27026e2012-09-05 20:41:26 +00001732 options.GetUseDynamic(),
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001733 expr_path_options,
1734 var_sp,
1735 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001736 // if this expression results in a bitfield, we give up and let the IR handle it
1737 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1738 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001739 }
Greg Clayton427f2902010-12-14 02:59:59 +00001740 }
1741 else if (m_process_sp)
1742 {
1743 m_process_sp->CalculateExecutionContext(exe_ctx);
1744 }
1745 else
1746 {
1747 CalculateExecutionContext(exe_ctx);
1748 }
1749
1750 if (result_valobj_sp)
1751 {
1752 execution_results = eExecutionCompleted;
1753 // We got a result from the frame variable expression path above...
1754 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1755
1756 lldb::ValueObjectSP const_valobj_sp;
1757
1758 // Check in case our value is already a constant value
1759 if (result_valobj_sp->GetIsConstant())
1760 {
1761 const_valobj_sp = result_valobj_sp;
1762 const_valobj_sp->SetName (persistent_variable_name);
1763 }
1764 else
Jim Inghame41494a2011-04-16 00:01:13 +00001765 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001766 if (options.GetUseDynamic() != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001767 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001768 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(options.GetUseDynamic());
Jim Inghame41494a2011-04-16 00:01:13 +00001769 if (dynamic_sp)
1770 result_valobj_sp = dynamic_sp;
1771 }
1772
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001773 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001774 }
Greg Clayton427f2902010-12-14 02:59:59 +00001775
Sean Callanan6a925532011-01-13 08:53:35 +00001776 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1777
Greg Clayton427f2902010-12-14 02:59:59 +00001778 result_valobj_sp = const_valobj_sp;
1779
Sean Callanan6a925532011-01-13 08:53:35 +00001780 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1781 assert (clang_expr_variable_sp.get());
1782
1783 // Set flags and live data as appropriate
1784
1785 const Value &result_value = live_valobj_sp->GetValue();
1786
1787 switch (result_value.GetValueType())
1788 {
1789 case Value::eValueTypeHostAddress:
1790 case Value::eValueTypeFileAddress:
1791 // we don't do anything with these for now
1792 break;
1793 case Value::eValueTypeScalar:
Greg Claytonf0fab4f2012-10-30 23:56:14 +00001794 case Value::eValueTypeVector:
Sean Callanan6a925532011-01-13 08:53:35 +00001795 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1796 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1797 break;
1798 case Value::eValueTypeLoadAddress:
1799 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1800 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1801 break;
1802 }
Greg Clayton427f2902010-12-14 02:59:59 +00001803 }
1804 else
1805 {
1806 // Make sure we aren't just trying to see the value of a persistent
1807 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001808 lldb::ClangExpressionVariableSP persistent_var_sp;
1809 // Only check for persistent variables the expression starts with a '$'
1810 if (expr_cstr[0] == '$')
1811 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1812
Greg Clayton427f2902010-12-14 02:59:59 +00001813 if (persistent_var_sp)
1814 {
1815 result_valobj_sp = persistent_var_sp->GetValueObject ();
1816 execution_results = eExecutionCompleted;
1817 }
1818 else
1819 {
1820 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001821
Greg Clayton427f2902010-12-14 02:59:59 +00001822 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Enrico Granatad27026e2012-09-05 20:41:26 +00001823 options.GetExecutionPolicy(),
Sean Callanan5b658cc2011-11-07 23:35:40 +00001824 lldb::eLanguageTypeUnknown,
Enrico Granatad27026e2012-09-05 20:41:26 +00001825 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1826 options.DoesUnwindOnError(),
Greg Clayton427f2902010-12-14 02:59:59 +00001827 expr_cstr,
1828 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001829 result_valobj_sp,
Jim Ingham47beabb2012-10-16 21:41:58 +00001830 options.GetRunOthers(),
1831 options.GetTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001832 }
1833 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001834
1835 m_suppress_stop_hooks = old_suppress_value;
1836
Greg Clayton427f2902010-12-14 02:59:59 +00001837 return execution_results;
1838}
1839
Greg Claytonc0fa5332011-05-22 22:46:53 +00001840lldb::addr_t
1841Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1842{
1843 addr_t code_addr = load_addr;
1844 switch (m_arch.GetMachine())
1845 {
1846 case llvm::Triple::arm:
1847 case llvm::Triple::thumb:
1848 switch (addr_class)
1849 {
1850 case eAddressClassData:
1851 case eAddressClassDebug:
1852 return LLDB_INVALID_ADDRESS;
1853
1854 case eAddressClassUnknown:
1855 case eAddressClassInvalid:
1856 case eAddressClassCode:
1857 case eAddressClassCodeAlternateISA:
1858 case eAddressClassRuntime:
1859 // Check if bit zero it no set?
1860 if ((code_addr & 1ull) == 0)
1861 {
1862 // Bit zero isn't set, check if the address is a multiple of 2?
1863 if (code_addr & 2ull)
1864 {
1865 // The address is a multiple of 2 so it must be thumb, set bit zero
1866 code_addr |= 1ull;
1867 }
1868 else if (addr_class == eAddressClassCodeAlternateISA)
1869 {
1870 // We checked the address and the address claims to be the alternate ISA
1871 // which means thumb, so set bit zero.
1872 code_addr |= 1ull;
1873 }
1874 }
1875 break;
1876 }
1877 break;
1878
1879 default:
1880 break;
1881 }
1882 return code_addr;
1883}
1884
1885lldb::addr_t
1886Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1887{
1888 addr_t opcode_addr = load_addr;
1889 switch (m_arch.GetMachine())
1890 {
1891 case llvm::Triple::arm:
1892 case llvm::Triple::thumb:
1893 switch (addr_class)
1894 {
1895 case eAddressClassData:
1896 case eAddressClassDebug:
1897 return LLDB_INVALID_ADDRESS;
1898
1899 case eAddressClassInvalid:
1900 case eAddressClassUnknown:
1901 case eAddressClassCode:
1902 case eAddressClassCodeAlternateISA:
1903 case eAddressClassRuntime:
1904 opcode_addr &= ~(1ull);
1905 break;
1906 }
1907 break;
1908
1909 default:
1910 break;
1911 }
1912 return opcode_addr;
1913}
1914
Jim Inghamd60d94a2011-03-11 03:53:59 +00001915lldb::user_id_t
1916Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1917{
1918 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001919 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001920 m_stop_hooks[new_uid] = new_hook_sp;
1921 return new_uid;
1922}
1923
1924bool
1925Target::RemoveStopHookByID (lldb::user_id_t user_id)
1926{
1927 size_t num_removed;
1928 num_removed = m_stop_hooks.erase (user_id);
1929 if (num_removed == 0)
1930 return false;
1931 else
1932 return true;
1933}
1934
1935void
1936Target::RemoveAllStopHooks ()
1937{
1938 m_stop_hooks.clear();
1939}
1940
1941Target::StopHookSP
1942Target::GetStopHookByID (lldb::user_id_t user_id)
1943{
1944 StopHookSP found_hook;
1945
1946 StopHookCollection::iterator specified_hook_iter;
1947 specified_hook_iter = m_stop_hooks.find (user_id);
1948 if (specified_hook_iter != m_stop_hooks.end())
1949 found_hook = (*specified_hook_iter).second;
1950 return found_hook;
1951}
1952
1953bool
1954Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1955{
1956 StopHookCollection::iterator specified_hook_iter;
1957 specified_hook_iter = m_stop_hooks.find (user_id);
1958 if (specified_hook_iter == m_stop_hooks.end())
1959 return false;
1960
1961 (*specified_hook_iter).second->SetIsActive (active_state);
1962 return true;
1963}
1964
1965void
1966Target::SetAllStopHooksActiveState (bool active_state)
1967{
1968 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1969 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1970 {
1971 (*pos).second->SetIsActive (active_state);
1972 }
1973}
1974
1975void
1976Target::RunStopHooks ()
1977{
Jim Ingham3613ae12011-05-12 02:06:14 +00001978 if (m_suppress_stop_hooks)
1979 return;
1980
Jim Inghamd60d94a2011-03-11 03:53:59 +00001981 if (!m_process_sp)
1982 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001983
1984 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1985 // since in that case we do not want to run the stop-hooks
1986 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1987 return;
1988
Jim Inghamd60d94a2011-03-11 03:53:59 +00001989 if (m_stop_hooks.empty())
1990 return;
1991
1992 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1993
1994 // If there aren't any active stop hooks, don't bother either:
1995 bool any_active_hooks = false;
1996 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1997 {
1998 if ((*pos).second->IsActive())
1999 {
2000 any_active_hooks = true;
2001 break;
2002 }
2003 }
2004 if (!any_active_hooks)
2005 return;
2006
2007 CommandReturnObject result;
2008
2009 std::vector<ExecutionContext> exc_ctx_with_reasons;
2010 std::vector<SymbolContext> sym_ctx_with_reasons;
2011
2012 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2013 size_t num_threads = cur_threadlist.GetSize();
2014 for (size_t i = 0; i < num_threads; i++)
2015 {
2016 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2017 if (cur_thread_sp->ThreadStoppedForAReason())
2018 {
2019 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2020 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2021 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2022 }
2023 }
2024
2025 // If no threads stopped for a reason, don't run the stop-hooks.
2026 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2027 if (num_exe_ctx == 0)
2028 return;
2029
Jim Inghame5ed8e92011-06-02 23:58:26 +00002030 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2031 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002032
2033 bool keep_going = true;
2034 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00002035 bool print_hook_header;
2036 bool print_thread_header;
2037
2038 if (num_exe_ctx == 1)
2039 print_thread_header = false;
2040 else
2041 print_thread_header = true;
2042
2043 if (m_stop_hooks.size() == 1)
2044 print_hook_header = false;
2045 else
2046 print_hook_header = true;
2047
Jim Inghamd60d94a2011-03-11 03:53:59 +00002048 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2049 {
2050 // result.Clear();
2051 StopHookSP cur_hook_sp = (*pos).second;
2052 if (!cur_hook_sp->IsActive())
2053 continue;
2054
2055 bool any_thread_matched = false;
2056 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2057 {
2058 if ((cur_hook_sp->GetSpecifier () == NULL
2059 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2060 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00002061 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002062 {
2063 if (!hooks_ran)
2064 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00002065 hooks_ran = true;
2066 }
Jim Inghamc54840c2011-03-22 01:47:27 +00002067 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002068 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002069 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2070 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2071 NULL);
2072 if (cmd)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002073 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chen4d96a742011-10-24 23:01:06 +00002074 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002075 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002076 any_thread_matched = true;
2077 }
2078
Jim Inghamc54840c2011-03-22 01:47:27 +00002079 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002080 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002081
2082 bool stop_on_continue = true;
2083 bool stop_on_error = true;
2084 bool echo_commands = false;
2085 bool print_results = true;
2086 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002087 &exc_ctx_with_reasons[i],
2088 stop_on_continue,
2089 stop_on_error,
2090 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002091 print_results,
2092 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002093 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002094
2095 // If the command started the target going again, we should bag out of
2096 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002097 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2098 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002099 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002100 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002101 keep_going = false;
2102 }
2103 }
2104 }
2105 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002106
Caroline Tice4a348082011-05-02 20:41:46 +00002107 result.GetImmediateOutputStream()->Flush();
2108 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002109}
2110
Greg Claytonbbea1332011-07-08 00:48:09 +00002111
Jim Inghamd60d94a2011-03-11 03:53:59 +00002112//--------------------------------------------------------------
2113// class Target::StopHook
2114//--------------------------------------------------------------
2115
2116
2117Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2118 UserID (uid),
2119 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002120 m_commands (),
2121 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002122 m_thread_spec_ap(NULL),
2123 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002124{
2125}
2126
2127Target::StopHook::StopHook (const StopHook &rhs) :
2128 UserID (rhs.GetID()),
2129 m_target_sp (rhs.m_target_sp),
2130 m_commands (rhs.m_commands),
2131 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002132 m_thread_spec_ap (NULL),
2133 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002134{
2135 if (rhs.m_thread_spec_ap.get() != NULL)
2136 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2137}
2138
2139
2140Target::StopHook::~StopHook ()
2141{
2142}
2143
2144void
2145Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2146{
2147 m_thread_spec_ap.reset (specifier);
2148}
2149
2150
2151void
2152Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2153{
2154 int indent_level = s->GetIndentLevel();
2155
2156 s->SetIndentLevel(indent_level + 2);
2157
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002158 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002159 if (m_active)
2160 s->Indent ("State: enabled\n");
2161 else
2162 s->Indent ("State: disabled\n");
2163
2164 if (m_specifier_sp)
2165 {
2166 s->Indent();
2167 s->PutCString ("Specifier:\n");
2168 s->SetIndentLevel (indent_level + 4);
2169 m_specifier_sp->GetDescription (s, level);
2170 s->SetIndentLevel (indent_level + 2);
2171 }
2172
2173 if (m_thread_spec_ap.get() != NULL)
2174 {
2175 StreamString tmp;
2176 s->Indent("Thread:\n");
2177 m_thread_spec_ap->GetDescription (&tmp, level);
2178 s->SetIndentLevel (indent_level + 4);
2179 s->Indent (tmp.GetData());
2180 s->PutCString ("\n");
2181 s->SetIndentLevel (indent_level + 2);
2182 }
2183
2184 s->Indent ("Commands: \n");
2185 s->SetIndentLevel (indent_level + 4);
2186 uint32_t num_commands = m_commands.GetSize();
2187 for (uint32_t i = 0; i < num_commands; i++)
2188 {
2189 s->Indent(m_commands.GetStringAtIndex(i));
2190 s->PutCString ("\n");
2191 }
2192 s->SetIndentLevel (indent_level);
2193}
2194
Greg Clayton73844aa2012-08-22 17:17:09 +00002195//--------------------------------------------------------------
2196// class TargetProperties
2197//--------------------------------------------------------------
2198
2199OptionEnumValueElement
2200lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002201{
Greg Clayton73844aa2012-08-22 17:17:09 +00002202 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2203 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2204 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2205 { 0, NULL, NULL }
2206};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002207
Greg Clayton49ce8962012-08-29 21:13:06 +00002208static OptionEnumValueElement
2209g_inline_breakpoint_enums[] =
2210{
2211 { 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."},
2212 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2213 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2214 { 0, NULL, NULL }
2215};
2216
Greg Clayton73844aa2012-08-22 17:17:09 +00002217static PropertyDefinition
2218g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002219{
Greg Clayton73844aa2012-08-22 17:17:09 +00002220 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2221 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2222 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2223 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2224 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2225 { "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 "
2226 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2227 "some part (starting at the root) of the path to the file when it was built, "
2228 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2229 "Each element of the array is checked in order and the first one that results in a match wins." },
2230 { "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." },
2231 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2232 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2233 { "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 +00002234 { "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." },
2235 { "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 +00002236 { "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." },
2237 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2238 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2239 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2240 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2241 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2242 { "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 +00002243 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2244 "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. "
2245 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2246 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2247 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2248 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2249 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002250 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2251};
2252enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002253{
Greg Clayton73844aa2012-08-22 17:17:09 +00002254 ePropertyDefaultArch,
2255 ePropertyExprPrefix,
2256 ePropertyPreferDynamic,
2257 ePropertyEnableSynthetic,
2258 ePropertySkipPrologue,
2259 ePropertySourceMap,
2260 ePropertyExecutableSearchPaths,
2261 ePropertyMaxChildrenCount,
2262 ePropertyMaxSummaryLength,
2263 ePropertyBreakpointUseAvoidList,
Greg Clayton0c8446c2012-10-17 22:57:12 +00002264 ePropertyArg0,
Greg Clayton73844aa2012-08-22 17:17:09 +00002265 ePropertyRunArgs,
2266 ePropertyEnvVars,
2267 ePropertyInheritEnv,
2268 ePropertyInputPath,
2269 ePropertyOutputPath,
2270 ePropertyErrorPath,
2271 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002272 ePropertyDisableSTDIO,
Greg Clayton87e9d322012-10-19 18:02:49 +00002273 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002274};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002275
Caroline Tice5bc8c972010-09-20 20:44:43 +00002276
Greg Clayton73844aa2012-08-22 17:17:09 +00002277class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002278{
Greg Clayton73844aa2012-08-22 17:17:09 +00002279public:
2280 TargetOptionValueProperties (const ConstString &name) :
2281 OptionValueProperties (name),
2282 m_target (NULL),
2283 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002284 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002285 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002286
Greg Clayton73844aa2012-08-22 17:17:09 +00002287 // This constructor is used when creating TargetOptionValueProperties when it
2288 // is part of a new lldb_private::Target instance. It will copy all current
2289 // global property values as needed
2290 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2291 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2292 m_target (target),
2293 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002294 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002295 }
2296
2297 virtual const Property *
2298 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2299 {
2300 // When gettings the value for a key from the target options, we will always
2301 // try and grab the setting from the current target if there is one. Else we just
2302 // use the one from this instance.
2303 if (idx == ePropertyEnvVars)
2304 GetHostEnvironmentIfNeeded ();
2305
2306 if (exe_ctx)
2307 {
2308 Target *target = exe_ctx->GetTargetPtr();
2309 if (target)
2310 {
2311 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2312 if (this != target_properties)
2313 return target_properties->ProtectedGetPropertyAtIndex (idx);
2314 }
2315 }
2316 return ProtectedGetPropertyAtIndex (idx);
2317 }
2318protected:
2319
2320 void
2321 GetHostEnvironmentIfNeeded () const
2322 {
2323 if (!m_got_host_env)
2324 {
2325 if (m_target)
2326 {
2327 m_got_host_env = true;
2328 const uint32_t idx = ePropertyInheritEnv;
2329 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2330 {
2331 PlatformSP platform_sp (m_target->GetPlatform());
2332 if (platform_sp)
2333 {
2334 StringList env;
2335 if (platform_sp->GetEnvironment(env))
2336 {
2337 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2338 if (env_dict)
2339 {
2340 const bool can_replace = false;
2341 const size_t envc = env.GetSize();
2342 for (size_t idx=0; idx<envc; idx++)
2343 {
2344 const char *env_entry = env.GetStringAtIndex (idx);
2345 if (env_entry)
2346 {
2347 const char *equal_pos = ::strchr(env_entry, '=');
2348 ConstString key;
2349 // It is ok to have environment variables with no values
2350 const char *value = NULL;
2351 if (equal_pos)
2352 {
2353 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2354 if (equal_pos[1])
2355 value = equal_pos + 1;
2356 }
2357 else
2358 {
2359 key.SetCString(env_entry);
2360 }
2361 // Don't allow existing keys to be replaced with ones we get from the platform environment
2362 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2363 }
2364 }
2365 }
2366 }
2367 }
2368 }
2369 }
2370 }
2371 }
2372 Target *m_target;
2373 mutable bool m_got_host_env;
2374};
2375
2376TargetProperties::TargetProperties (Target *target) :
2377 Properties ()
2378{
2379 if (target)
2380 {
2381 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002382 }
2383 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002384 {
2385 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2386 m_collection_sp->Initialize(g_properties);
2387 m_collection_sp->AppendProperty(ConstString("process"),
2388 ConstString("Settings specify to processes."),
2389 true,
2390 Process::GetGlobalProperties()->GetValueProperties());
2391 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002392}
2393
Greg Clayton73844aa2012-08-22 17:17:09 +00002394TargetProperties::~TargetProperties ()
2395{
2396}
2397ArchSpec
2398TargetProperties::GetDefaultArchitecture () const
2399{
2400 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2401 if (value)
2402 return value->GetCurrentValue();
2403 return ArchSpec();
2404}
2405
2406void
2407TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2408{
2409 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2410 if (value)
2411 return value->SetCurrentValue(arch, true);
2412}
2413
2414lldb::DynamicValueType
2415TargetProperties::GetPreferDynamicValue() const
2416{
2417 const uint32_t idx = ePropertyPreferDynamic;
2418 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2419}
2420
2421bool
2422TargetProperties::GetDisableASLR () const
2423{
2424 const uint32_t idx = ePropertyDisableASLR;
2425 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2426}
2427
2428void
2429TargetProperties::SetDisableASLR (bool b)
2430{
2431 const uint32_t idx = ePropertyDisableASLR;
2432 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2433}
2434
2435bool
2436TargetProperties::GetDisableSTDIO () const
2437{
2438 const uint32_t idx = ePropertyDisableSTDIO;
2439 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2440}
2441
2442void
2443TargetProperties::SetDisableSTDIO (bool b)
2444{
2445 const uint32_t idx = ePropertyDisableSTDIO;
2446 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2447}
2448
Greg Clayton49ce8962012-08-29 21:13:06 +00002449InlineStrategy
2450TargetProperties::GetInlineStrategy () const
2451{
2452 const uint32_t idx = ePropertyInlineStrategy;
2453 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2454}
2455
Greg Clayton0c8446c2012-10-17 22:57:12 +00002456const char *
2457TargetProperties::GetArg0 () const
2458{
2459 const uint32_t idx = ePropertyArg0;
2460 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2461}
2462
2463void
2464TargetProperties::SetArg0 (const char *arg)
2465{
2466 const uint32_t idx = ePropertyArg0;
2467 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2468}
2469
Greg Clayton73844aa2012-08-22 17:17:09 +00002470bool
2471TargetProperties::GetRunArguments (Args &args) const
2472{
2473 const uint32_t idx = ePropertyRunArgs;
2474 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2475}
2476
2477void
2478TargetProperties::SetRunArguments (const Args &args)
2479{
2480 const uint32_t idx = ePropertyRunArgs;
2481 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2482}
2483
2484size_t
2485TargetProperties::GetEnvironmentAsArgs (Args &env) const
2486{
2487 const uint32_t idx = ePropertyEnvVars;
2488 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2489}
2490
2491bool
2492TargetProperties::GetSkipPrologue() const
2493{
2494 const uint32_t idx = ePropertySkipPrologue;
2495 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2496}
2497
2498PathMappingList &
2499TargetProperties::GetSourcePathMap () const
2500{
2501 const uint32_t idx = ePropertySourceMap;
2502 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2503 assert(option_value);
2504 return option_value->GetCurrentValue();
2505}
2506
2507FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002508TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002509{
2510 const uint32_t idx = ePropertyExecutableSearchPaths;
2511 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2512 assert(option_value);
2513 return option_value->GetCurrentValue();
2514}
2515
2516bool
2517TargetProperties::GetEnableSyntheticValue () const
2518{
2519 const uint32_t idx = ePropertyEnableSynthetic;
2520 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2521}
2522
2523uint32_t
2524TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2525{
2526 const uint32_t idx = ePropertyMaxChildrenCount;
2527 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2528}
2529
2530uint32_t
2531TargetProperties::GetMaximumSizeOfStringSummary() const
2532{
2533 const uint32_t idx = ePropertyMaxSummaryLength;
2534 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2535}
2536
2537FileSpec
2538TargetProperties::GetStandardInputPath () const
2539{
2540 const uint32_t idx = ePropertyInputPath;
2541 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2542}
2543
2544void
2545TargetProperties::SetStandardInputPath (const char *p)
2546{
2547 const uint32_t idx = ePropertyInputPath;
2548 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2549}
2550
2551FileSpec
2552TargetProperties::GetStandardOutputPath () const
2553{
2554 const uint32_t idx = ePropertyOutputPath;
2555 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2556}
2557
2558void
2559TargetProperties::SetStandardOutputPath (const char *p)
2560{
2561 const uint32_t idx = ePropertyOutputPath;
2562 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2563}
2564
2565FileSpec
2566TargetProperties::GetStandardErrorPath () const
2567{
2568 const uint32_t idx = ePropertyErrorPath;
2569 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2570}
2571
Greg Claytonc6e82e42012-08-22 18:39:03 +00002572const char *
2573TargetProperties::GetExpressionPrefixContentsAsCString ()
2574{
2575 const uint32_t idx = ePropertyExprPrefix;
2576 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2577 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002578 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002579 const bool null_terminate = true;
2580 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002581 if (data_sp)
2582 return (const char *) data_sp->GetBytes();
2583 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002584 return NULL;
2585}
2586
Greg Clayton73844aa2012-08-22 17:17:09 +00002587void
2588TargetProperties::SetStandardErrorPath (const char *p)
2589{
2590 const uint32_t idx = ePropertyErrorPath;
2591 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2592}
2593
2594bool
2595TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2596{
2597 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2598 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2599}
2600
2601const TargetPropertiesSP &
2602Target::GetGlobalProperties()
2603{
2604 static TargetPropertiesSP g_settings_sp;
2605 if (!g_settings_sp)
2606 {
2607 g_settings_sp.reset (new TargetProperties (NULL));
2608 }
2609 return g_settings_sp;
2610}
2611
Jim Ingham5a15e692012-02-16 06:50:00 +00002612const ConstString &
2613Target::TargetEventData::GetFlavorString ()
2614{
2615 static ConstString g_flavor ("Target::TargetEventData");
2616 return g_flavor;
2617}
2618
2619const ConstString &
2620Target::TargetEventData::GetFlavor () const
2621{
2622 return TargetEventData::GetFlavorString ();
2623}
2624
2625Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2626 EventData(),
2627 m_target_sp (new_target_sp)
2628{
2629}
2630
2631Target::TargetEventData::~TargetEventData()
2632{
2633
2634}
2635
2636void
2637Target::TargetEventData::Dump (Stream *s) const
2638{
2639
2640}
2641
2642const TargetSP
2643Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2644{
2645 TargetSP target_sp;
2646
2647 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2648 if (data)
2649 target_sp = data->m_target_sp;
2650
2651 return target_sp;
2652}
2653
2654const Target::TargetEventData *
2655Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2656{
2657 if (event_ptr)
2658 {
2659 const EventData *event_data = event_ptr->GetData();
2660 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2661 return static_cast <const TargetEventData *> (event_ptr->GetData());
2662 }
2663 return NULL;
2664}
2665