blob: a8cd131580bb8aa0164b8f900c5b994565ac0a0e [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 {
Jason Molenda332dc002012-12-12 02:23:56 +000099 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::Target created with architecture %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jason Molenda8c6cf432012-12-05 00:25:49 +0000100 }
Chris Lattner24943d22010-06-08 16:52:24 +0000101}
102
103//----------------------------------------------------------------------
104// Destructor
105//----------------------------------------------------------------------
106Target::~Target()
107{
Greg Claytone005f2c2010-11-06 01:53:30 +0000108 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000109 if (log)
110 log->Printf ("%p Target::~Target()", this);
111 DeleteCurrentProcess ();
112}
113
114void
Caroline Tice7826c882010-10-26 03:11:13 +0000115Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000116{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000117// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000118 if (description_level != lldb::eDescriptionLevelBrief)
119 {
120 s->Indent();
121 s->PutCString("Target\n");
122 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000123 m_images.Dump(s);
124 m_breakpoint_list.Dump(s);
125 m_internal_breakpoint_list.Dump(s);
126 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000127 }
128 else
129 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000130 Module *exe_module = GetExecutableModulePointer();
131 if (exe_module)
132 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000133 else
134 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000135 }
Chris Lattner24943d22010-06-08 16:52:24 +0000136}
137
138void
Greg Clayton0bce9a22012-12-05 00:16:59 +0000139Target::CleanupProcess ()
140{
141 // Do any cleanup of the target we need to do between process instances.
142 // NB It is better to do this before destroying the process in case the
143 // clean up needs some help from the process.
144 m_breakpoint_list.ClearAllBreakpointSites();
145 m_internal_breakpoint_list.ClearAllBreakpointSites();
146 // Disable watchpoints just on the debugger side.
147 Mutex::Locker locker;
148 this->GetWatchpointList().GetListMutex(locker);
149 DisableAllWatchpoints(false);
150 ClearAllWatchpointHitCounts();
151}
152
153void
Chris Lattner24943d22010-06-08 16:52:24 +0000154Target::DeleteCurrentProcess ()
155{
156 if (m_process_sp.get())
157 {
Greg Clayton49480b12010-09-14 23:52:43 +0000158 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000159 if (m_process_sp->IsAlive())
160 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000161
162 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000163
Greg Clayton0bce9a22012-12-05 00:16:59 +0000164 CleanupProcess ();
165
Chris Lattner24943d22010-06-08 16:52:24 +0000166 m_process_sp.reset();
167 }
168}
169
170const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000171Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000172{
173 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000174 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000175 return m_process_sp;
176}
177
178const lldb::ProcessSP &
179Target::GetProcessSP () const
180{
181 return m_process_sp;
182}
183
Greg Clayton153ccd72011-08-10 02:10:13 +0000184void
185Target::Destroy()
186{
187 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000188 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000189 DeleteCurrentProcess ();
190 m_platform_sp.reset();
191 m_arch.Clear();
192 m_images.Clear();
193 m_section_load_list.Clear();
194 const bool notify = false;
195 m_breakpoint_list.RemoveAll(notify);
196 m_internal_breakpoint_list.RemoveAll(notify);
197 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000198 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000199 m_search_filter_sp.reset();
200 m_image_search_paths.Clear(notify);
201 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000202 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000203 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000204 m_persistent_variables.Clear();
205 m_stop_hooks.clear();
206 m_stop_hook_next_id = 0;
207 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000208 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000209}
210
211
Chris Lattner24943d22010-06-08 16:52:24 +0000212BreakpointList &
213Target::GetBreakpointList(bool internal)
214{
215 if (internal)
216 return m_internal_breakpoint_list;
217 else
218 return m_breakpoint_list;
219}
220
221const BreakpointList &
222Target::GetBreakpointList(bool internal) const
223{
224 if (internal)
225 return m_internal_breakpoint_list;
226 else
227 return m_breakpoint_list;
228}
229
230BreakpointSP
231Target::GetBreakpointByID (break_id_t break_id)
232{
233 BreakpointSP bp_sp;
234
235 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
236 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
237 else
238 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
239
240 return bp_sp;
241}
242
243BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000244Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000245 const FileSpecList *source_file_spec_list,
246 RegularExpression &source_regex,
247 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000248{
Jim Inghamd6d47972011-09-23 00:54:11 +0000249 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
250 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000251 return CreateBreakpoint (filter_sp, resolver_sp, internal);
252}
253
254
255BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000256Target::CreateBreakpoint (const FileSpecList *containingModules,
257 const FileSpec &file,
258 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000259 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000260 LazyBool skip_prologue,
261 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000262{
Greg Clayton49ce8962012-08-29 21:13:06 +0000263 if (check_inlines == eLazyBoolCalculate)
264 {
265 const InlineStrategy inline_strategy = GetInlineStrategy();
266 switch (inline_strategy)
267 {
268 case eInlineBreakpointsNever:
269 check_inlines = eLazyBoolNo;
270 break;
271
272 case eInlineBreakpointsHeaders:
273 if (file.IsSourceImplementationFile())
274 check_inlines = eLazyBoolNo;
275 else
276 check_inlines = eLazyBoolYes;
277 break;
278
279 case eInlineBreakpointsAlways:
280 check_inlines = eLazyBoolYes;
281 break;
282 }
283 }
Greg Clayton46365522012-09-07 23:48:57 +0000284 SearchFilterSP filter_sp;
285 if (check_inlines == eLazyBoolNo)
286 {
287 // Not checking for inlines, we are looking only for matching compile units
288 FileSpecList compile_unit_list;
289 compile_unit_list.Append (file);
290 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
291 }
292 else
293 {
294 filter_sp = GetSearchFilterForModuleList (containingModules);
295 }
Greg Clayton49ce8962012-08-29 21:13:06 +0000296 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
297 file,
298 line_no,
299 check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000300 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000301 return CreateBreakpoint (filter_sp, resolver_sp, internal);
302}
303
304
305BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000306Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000307{
Chris Lattner24943d22010-06-08 16:52:24 +0000308 Address so_addr;
309 // Attempt to resolve our load address if possible, though it is ok if
310 // it doesn't resolve to section/offset.
311
Greg Clayton33ed1702010-08-24 00:45:41 +0000312 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000313 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000314 if (!so_addr.IsValid())
315 {
316 // The address didn't resolve, so just set this as an absolute address
317 so_addr.SetOffset (addr);
318 }
319 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000320 return bp_sp;
321}
322
323BreakpointSP
324Target::CreateBreakpoint (Address &addr, bool internal)
325{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000326 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000327 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
328 return CreateBreakpoint (filter_sp, resolver_sp, internal);
329}
330
331BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000332Target::CreateBreakpoint (const FileSpecList *containingModules,
333 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000334 const char *func_name,
335 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000336 LazyBool skip_prologue,
337 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000338{
Greg Clayton12bec712010-06-28 21:30:43 +0000339 BreakpointSP bp_sp;
340 if (func_name)
341 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000342 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000343
344 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
345 func_name,
346 func_name_type_mask,
347 Breakpoint::Exact,
348 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000349 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
350 }
351 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000352}
353
Jim Ingham4722b102012-03-06 00:37:27 +0000354lldb::BreakpointSP
355Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000356 const FileSpecList *containingSourceFiles,
357 const std::vector<std::string> &func_names,
358 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000359 LazyBool skip_prologue,
360 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000361{
362 BreakpointSP bp_sp;
363 size_t num_names = func_names.size();
364 if (num_names > 0)
365 {
366 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
367
368 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
369 func_names,
370 func_name_type_mask,
371 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
372 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
373 }
374 return bp_sp;
375}
376
Jim Inghamc1053622012-03-03 02:05:11 +0000377BreakpointSP
378Target::CreateBreakpoint (const FileSpecList *containingModules,
379 const FileSpecList *containingSourceFiles,
380 const char *func_names[],
381 size_t num_names,
382 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000383 LazyBool skip_prologue,
384 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000385{
386 BreakpointSP bp_sp;
387 if (num_names > 0)
388 {
389 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
390
391 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
392 func_names,
393 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000394 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000395 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
396 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
397 }
398 return bp_sp;
399}
Chris Lattner24943d22010-06-08 16:52:24 +0000400
401SearchFilterSP
402Target::GetSearchFilterForModule (const FileSpec *containingModule)
403{
404 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000405 if (containingModule != NULL)
406 {
407 // TODO: We should look into sharing module based search filters
408 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000409 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000410 }
411 else
412 {
413 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000414 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000415 filter_sp = m_search_filter_sp;
416 }
417 return filter_sp;
418}
419
Jim Ingham03c8ee52011-09-21 01:17:13 +0000420SearchFilterSP
421Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
422{
423 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000424 if (containingModules && containingModules->GetSize() != 0)
425 {
426 // TODO: We should look into sharing module based search filters
427 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000428 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000429 }
430 else
431 {
432 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000433 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000434 filter_sp = m_search_filter_sp;
435 }
436 return filter_sp;
437}
438
Jim Inghamd6d47972011-09-23 00:54:11 +0000439SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000440Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
441 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000442{
443 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
444 return GetSearchFilterForModuleList(containingModules);
445
446 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000447 if (containingModules == NULL)
448 {
449 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
450 // but that will take a little reworking.
451
Greg Clayton13d24fb2012-01-29 20:56:30 +0000452 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000453 }
454 else
455 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000456 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000457 }
458 return filter_sp;
459}
460
Chris Lattner24943d22010-06-08 16:52:24 +0000461BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000462Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000463 const FileSpecList *containingSourceFiles,
464 RegularExpression &func_regex,
465 LazyBool skip_prologue,
466 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000467{
Jim Inghamd6d47972011-09-23 00:54:11 +0000468 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000469 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
470 func_regex,
471 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000472
473 return CreateBreakpoint (filter_sp, resolver_sp, internal);
474}
475
Jim Ingham3df164e2012-03-05 04:47:34 +0000476lldb::BreakpointSP
477Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
478{
479 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
480}
481
Chris Lattner24943d22010-06-08 16:52:24 +0000482BreakpointSP
483Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
484{
485 BreakpointSP bp_sp;
486 if (filter_sp && resolver_sp)
487 {
488 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
489 resolver_sp->SetBreakpoint (bp_sp.get());
490
491 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000492 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000493 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000494 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000495
Greg Claytone005f2c2010-11-06 01:53:30 +0000496 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000497 if (log)
498 {
499 StreamString s;
500 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
501 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
502 }
503
Chris Lattner24943d22010-06-08 16:52:24 +0000504 bp_sp->ResolveBreakpoint();
505 }
Jim Inghamd1686902010-10-14 23:45:03 +0000506
507 if (!internal && bp_sp)
508 {
509 m_last_created_breakpoint = bp_sp;
510 }
511
Chris Lattner24943d22010-06-08 16:52:24 +0000512 return bp_sp;
513}
514
Johnny Chenda5a8022011-09-20 23:28:55 +0000515bool
516Target::ProcessIsValid()
517{
518 return (m_process_sp && m_process_sp->IsAlive());
519}
520
Johnny Chen3f883492012-06-04 23:19:54 +0000521static bool
522CheckIfWatchpointsExhausted(Target *target, Error &error)
523{
524 uint32_t num_supported_hardware_watchpoints;
525 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
526 if (rc.Success())
527 {
528 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
529 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
530 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
531 num_supported_hardware_watchpoints);
532 }
533 return false;
534}
535
Johnny Chenecd4feb2011-10-14 00:42:25 +0000536// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000537// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000538WatchpointSP
Jim Ingham9e376622012-10-23 07:20:06 +0000539Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000540{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000541 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
542 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000543 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Ingham9e376622012-10-23 07:20:06 +0000544 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000545
Johnny Chenecd4feb2011-10-14 00:42:25 +0000546 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000547 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000548 {
549 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000550 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000551 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000552 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000553 {
554 if (size == 0)
555 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
556 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000557 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000558 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000559 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000560
Johnny Chenecd4feb2011-10-14 00:42:25 +0000561 // Currently we only support one watchpoint per address, with total number
562 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000563
564 // Grab the list mutex while doing operations.
565 Mutex::Locker locker;
566 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000567 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000568 if (matched_sp)
569 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000570 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000571 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000572 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
573 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000574 // Return the existing watchpoint if both size and type match.
Jim Ingham9e376622012-10-23 07:20:06 +0000575 if (size == old_size && kind == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000576 wp_sp = matched_sp;
577 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000578 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000579 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000580 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000581 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000582 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000583 }
584
Jason Molendac4d49fd2012-12-05 23:07:34 +0000585 if (!wp_sp)
586 {
Jim Ingham9e376622012-10-23 07:20:06 +0000587 Watchpoint *new_wp = new Watchpoint(*this, addr, size, type);
Jason Molendac4d49fd2012-12-05 23:07:34 +0000588 if (!new_wp)
589 {
590 error.SetErrorString("Watchpoint ctor failed, out of memory?");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000591 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000592 }
Jim Ingham9e376622012-10-23 07:20:06 +0000593 new_wp->SetWatchpointType(kind);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000594 wp_sp.reset(new_wp);
595 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000596 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000597
Johnny Chen3f883492012-06-04 23:19:54 +0000598 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000599 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +0000600 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
601 __FUNCTION__,
602 error.Success() ? "succeeded" : "failed",
603 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000604
Jason Molendac4d49fd2012-12-05 23:07:34 +0000605 if (error.Fail())
606 {
Johnny Chen155599b2012-03-26 22:00:10 +0000607 // Enabling the watchpoint on the device side failed.
608 // Remove the said watchpoint from the list maintained by the target instance.
609 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000610 // See if we could provide more helpful error message.
611 if (!CheckIfWatchpointsExhausted(this, error))
612 {
613 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
614 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
615 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000616 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000617 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000618 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000619 m_last_created_watchpoint = wp_sp;
620 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000621}
622
Chris Lattner24943d22010-06-08 16:52:24 +0000623void
624Target::RemoveAllBreakpoints (bool internal_also)
625{
Greg Claytone005f2c2010-11-06 01:53:30 +0000626 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000627 if (log)
628 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
629
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000630 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000631 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000632 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000633
634 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000635}
636
637void
638Target::DisableAllBreakpoints (bool internal_also)
639{
Greg Claytone005f2c2010-11-06 01:53:30 +0000640 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000641 if (log)
642 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
643
644 m_breakpoint_list.SetEnabledAll (false);
645 if (internal_also)
646 m_internal_breakpoint_list.SetEnabledAll (false);
647}
648
649void
650Target::EnableAllBreakpoints (bool internal_also)
651{
Greg Claytone005f2c2010-11-06 01:53:30 +0000652 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000653 if (log)
654 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
655
656 m_breakpoint_list.SetEnabledAll (true);
657 if (internal_also)
658 m_internal_breakpoint_list.SetEnabledAll (true);
659}
660
661bool
662Target::RemoveBreakpointByID (break_id_t break_id)
663{
Greg Claytone005f2c2010-11-06 01:53:30 +0000664 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000665 if (log)
666 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
667
668 if (DisableBreakpointByID (break_id))
669 {
670 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000671 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000672 else
Jim Inghamd1686902010-10-14 23:45:03 +0000673 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000674 if (m_last_created_breakpoint)
675 {
676 if (m_last_created_breakpoint->GetID() == break_id)
677 m_last_created_breakpoint.reset();
678 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000679 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000680 }
Chris Lattner24943d22010-06-08 16:52:24 +0000681 return true;
682 }
683 return false;
684}
685
686bool
687Target::DisableBreakpointByID (break_id_t break_id)
688{
Greg Claytone005f2c2010-11-06 01:53:30 +0000689 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000690 if (log)
691 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
692
693 BreakpointSP bp_sp;
694
695 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
696 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
697 else
698 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
699 if (bp_sp)
700 {
701 bp_sp->SetEnabled (false);
702 return true;
703 }
704 return false;
705}
706
707bool
708Target::EnableBreakpointByID (break_id_t break_id)
709{
Greg Claytone005f2c2010-11-06 01:53:30 +0000710 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000711 if (log)
712 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
713 __FUNCTION__,
714 break_id,
715 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
716
717 BreakpointSP bp_sp;
718
719 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
720 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
721 else
722 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
723
724 if (bp_sp)
725 {
726 bp_sp->SetEnabled (true);
727 return true;
728 }
729 return false;
730}
731
Johnny Chenc86582f2011-09-23 21:21:43 +0000732// The flag 'end_to_end', default to true, signifies that the operation is
733// performed end to end, for both the debugger and the debuggee.
734
Johnny Chenecd4feb2011-10-14 00:42:25 +0000735// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
736// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000737bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000738Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000739{
740 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
741 if (log)
742 log->Printf ("Target::%s\n", __FUNCTION__);
743
Johnny Chenc86582f2011-09-23 21:21:43 +0000744 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000745 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000746 return true;
747 }
748
749 // Otherwise, it's an end to end operation.
750
Johnny Chenda5a8022011-09-20 23:28:55 +0000751 if (!ProcessIsValid())
752 return false;
753
Johnny Chenecd4feb2011-10-14 00:42:25 +0000754 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000755 for (size_t i = 0; i < num_watchpoints; ++i)
756 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000757 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
758 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000759 return false;
760
Johnny Chenecd4feb2011-10-14 00:42:25 +0000761 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000762 if (rc.Fail())
763 return false;
764 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000765 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000766 return true; // Success!
767}
768
Johnny Chenecd4feb2011-10-14 00:42:25 +0000769// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
770// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000771bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000772Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000773{
774 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
775 if (log)
776 log->Printf ("Target::%s\n", __FUNCTION__);
777
Johnny Chenc86582f2011-09-23 21:21:43 +0000778 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000779 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000780 return true;
781 }
782
783 // Otherwise, it's an end to end operation.
784
Johnny Chenda5a8022011-09-20 23:28:55 +0000785 if (!ProcessIsValid())
786 return false;
787
Johnny Chenecd4feb2011-10-14 00:42:25 +0000788 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000789 for (size_t i = 0; i < num_watchpoints; ++i)
790 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000791 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
792 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000793 return false;
794
Johnny Chenecd4feb2011-10-14 00:42:25 +0000795 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000796 if (rc.Fail())
797 return false;
798 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000799 return true; // Success!
800}
801
Johnny Chenecd4feb2011-10-14 00:42:25 +0000802// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
803// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000804bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000805Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000806{
807 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
808 if (log)
809 log->Printf ("Target::%s\n", __FUNCTION__);
810
Johnny Chenc86582f2011-09-23 21:21:43 +0000811 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000812 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000813 return true;
814 }
815
816 // Otherwise, it's an end to end operation.
817
Johnny Chenda5a8022011-09-20 23:28:55 +0000818 if (!ProcessIsValid())
819 return false;
820
Johnny Chenecd4feb2011-10-14 00:42:25 +0000821 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000822 for (size_t i = 0; i < num_watchpoints; ++i)
823 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000824 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
825 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000826 return false;
827
Johnny Chenecd4feb2011-10-14 00:42:25 +0000828 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000829 if (rc.Fail())
830 return false;
831 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000832 return true; // Success!
833}
834
Johnny Chen116a5cd2012-02-25 06:44:30 +0000835// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
836bool
837Target::ClearAllWatchpointHitCounts ()
838{
839 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
840 if (log)
841 log->Printf ("Target::%s\n", __FUNCTION__);
842
843 size_t num_watchpoints = m_watchpoint_list.GetSize();
844 for (size_t i = 0; i < num_watchpoints; ++i)
845 {
846 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
847 if (!wp_sp)
848 return false;
849
850 wp_sp->ResetHitCount();
851 }
852 return true; // Success!
853}
854
Johnny Chenecd4feb2011-10-14 00:42:25 +0000855// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000856// during these operations.
857bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000858Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000859{
860 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
861 if (log)
862 log->Printf ("Target::%s\n", __FUNCTION__);
863
864 if (!ProcessIsValid())
865 return false;
866
Johnny Chenecd4feb2011-10-14 00:42:25 +0000867 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000868 for (size_t i = 0; i < num_watchpoints; ++i)
869 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000870 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
871 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000872 return false;
873
Johnny Chenecd4feb2011-10-14 00:42:25 +0000874 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000875 }
876 return true; // Success!
877}
878
Johnny Chenecd4feb2011-10-14 00:42:25 +0000879// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000880bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000881Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000882{
883 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
884 if (log)
885 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
886
887 if (!ProcessIsValid())
888 return false;
889
Johnny Chenecd4feb2011-10-14 00:42:25 +0000890 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
891 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000892 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000893 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000894 if (rc.Success())
895 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000896
Johnny Chen01acfa72011-09-22 18:04:58 +0000897 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000898 }
899 return false;
900}
901
Johnny Chenecd4feb2011-10-14 00:42:25 +0000902// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000903bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000904Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000905{
906 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
907 if (log)
908 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
909
910 if (!ProcessIsValid())
911 return false;
912
Johnny Chenecd4feb2011-10-14 00:42:25 +0000913 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
914 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000915 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000916 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000917 if (rc.Success())
918 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000919
Johnny Chen01acfa72011-09-22 18:04:58 +0000920 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000921 }
922 return false;
923}
924
Johnny Chenecd4feb2011-10-14 00:42:25 +0000925// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000926bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000927Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000928{
929 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
930 if (log)
931 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
932
Johnny Chenecd4feb2011-10-14 00:42:25 +0000933 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000934 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000935 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000936 return true;
937 }
938 return false;
939}
940
Johnny Chenecd4feb2011-10-14 00:42:25 +0000941// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000942bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000943Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000944{
945 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
946 if (log)
947 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
948
949 if (!ProcessIsValid())
950 return false;
951
Johnny Chenecd4feb2011-10-14 00:42:25 +0000952 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
953 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000954 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000955 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000956 return true;
957 }
958 return false;
959}
960
Chris Lattner24943d22010-06-08 16:52:24 +0000961ModuleSP
962Target::GetExecutableModule ()
963{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000964 return m_images.GetModuleAtIndex(0);
965}
966
967Module*
968Target::GetExecutableModulePointer ()
969{
970 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000971}
972
Enrico Granata146d9522012-11-08 02:22:02 +0000973static void
974LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
975{
976 Error error;
977 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error))
978 {
979 target->GetDebugger().GetOutputStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
980 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
981 error.AsCString());
982 }
983}
984
Chris Lattner24943d22010-06-08 16:52:24 +0000985void
986Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
987{
Jason Molenda8c6cf432012-12-05 00:25:49 +0000988 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Chris Lattner24943d22010-06-08 16:52:24 +0000989 m_images.Clear();
990 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000991 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000992 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000993
994 if (executable_sp.get())
995 {
996 Timer scoped_timer (__PRETTY_FUNCTION__,
997 "Target::SetExecutableModule (executable = '%s/%s')",
998 executable_sp->GetFileSpec().GetDirectory().AsCString(),
999 executable_sp->GetFileSpec().GetFilename().AsCString());
1000
1001 m_images.Append(executable_sp); // The first image is our exectuable file
1002
Jim Ingham7508e732010-08-09 23:31:02 +00001003 // 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 +00001004 if (!m_arch.IsValid())
Jason Molenda8c6cf432012-12-05 00:25:49 +00001005 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001006 m_arch = executable_sp->GetArchitecture();
Jason Molenda8c6cf432012-12-05 00:25:49 +00001007 if (log)
1008 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1009 }
1010
Chris Lattner24943d22010-06-08 16:52:24 +00001011 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001012 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +00001013
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001014 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +00001015 {
1016 executable_objfile->GetDependentModules(dependent_files);
1017 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1018 {
Greg Claytonb1888f22011-03-19 01:12:21 +00001019 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1020 FileSpec platform_dependent_file_spec;
1021 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +00001022 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +00001023 else
1024 platform_dependent_file_spec = dependent_file_spec;
1025
Greg Clayton444fe992012-02-26 05:51:37 +00001026 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1027 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +00001028 if (image_module_sp.get())
1029 {
Chris Lattner24943d22010-06-08 16:52:24 +00001030 ObjectFile *objfile = image_module_sp->GetObjectFile();
1031 if (objfile)
1032 objfile->GetDependentModules(dependent_files);
1033 }
1034 }
1035 }
Chris Lattner24943d22010-06-08 16:52:24 +00001036 }
1037}
1038
1039
Jim Ingham7508e732010-08-09 23:31:02 +00001040bool
1041Target::SetArchitecture (const ArchSpec &arch_spec)
1042{
Jason Molenda8c6cf432012-12-05 00:25:49 +00001043 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb170aee2012-05-08 01:45:38 +00001044 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001045 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001046 // If we haven't got a valid arch spec, or the architectures are
1047 // compatible, so just update the architecture. Architectures can be
1048 // equal, yet the triple OS and vendor might change, so we need to do
1049 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001050 m_arch = arch_spec;
Jason Molenda8c6cf432012-12-05 00:25:49 +00001051 if (log)
1052 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 +00001053 return true;
1054 }
1055 else
1056 {
1057 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molenda8c6cf432012-12-05 00:25:49 +00001058 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +00001059 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 +00001060 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001061 ModuleSP executable_sp = GetExecutableModule ();
1062 m_images.Clear();
1063 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001064 m_scratch_ast_source_ap.reset();
1065 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001066 // Need to do something about unsetting breakpoints.
1067
1068 if (executable_sp)
1069 {
Jason Molenda8c6cf432012-12-05 00:25:49 +00001070 if (log)
1071 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 +00001072 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1073 Error error = ModuleList::GetSharedModule (module_spec,
1074 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001075 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001076 NULL,
1077 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001078
1079 if (!error.Fail() && executable_sp)
1080 {
1081 SetExecutableModule (executable_sp, true);
1082 return true;
1083 }
Jim Ingham7508e732010-08-09 23:31:02 +00001084 }
1085 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001086 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001087}
Chris Lattner24943d22010-06-08 16:52:24 +00001088
Chris Lattner24943d22010-06-08 16:52:24 +00001089void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001090Target::WillClearList (const ModuleList& module_list)
Enrico Granata146d9522012-11-08 02:22:02 +00001091{
1092}
1093
1094void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001095Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001096{
1097 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001098 ModuleList my_module_list;
1099 my_module_list.Append(module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +00001100 LoadScriptingResourceForModule(module_sp, this);
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001101 ModulesDidLoad (my_module_list);
Chris Lattner24943d22010-06-08 16:52:24 +00001102}
1103
1104void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001105Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata146d9522012-11-08 02:22:02 +00001106{
1107 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001108 ModuleList my_module_list;
1109 my_module_list.Append(module_sp);
1110 ModulesDidUnload (my_module_list);
Enrico Granata146d9522012-11-08 02:22:02 +00001111}
1112
1113void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001114Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001115{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001116 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001117 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001118}
1119
1120void
1121Target::ModulesDidLoad (ModuleList &module_list)
1122{
Enrico Granata146d9522012-11-08 02:22:02 +00001123 if (module_list.GetSize())
1124 {
1125 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1126 // TODO: make event data that packages up the module_list
1127 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1128 }
Chris Lattner24943d22010-06-08 16:52:24 +00001129}
1130
1131void
1132Target::ModulesDidUnload (ModuleList &module_list)
1133{
Enrico Granata146d9522012-11-08 02:22:02 +00001134 if (module_list.GetSize())
1135 {
1136 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1137 // TODO: make event data that packages up the module_list
1138 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1139 }
Chris Lattner24943d22010-06-08 16:52:24 +00001140}
1141
Daniel Dunbar705a0982011-10-31 22:50:37 +00001142bool
Greg Clayton444fe992012-02-26 05:51:37 +00001143Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001144{
Greg Clayton73844aa2012-08-22 17:17:09 +00001145 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001146 {
1147 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001148 ModuleSpec module_spec (module_file_spec);
1149 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001150
1151 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1152 // black list.
1153 if (num_modules > 0)
1154 {
1155 for (int i = 0; i < num_modules; i++)
1156 {
1157 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1158 return false;
1159 }
1160 return true;
1161 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001162 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001163 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001164}
1165
Daniel Dunbar705a0982011-10-31 22:50:37 +00001166bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001167Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1168{
Greg Clayton73844aa2012-08-22 17:17:09 +00001169 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001170 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001171 if (m_platform_sp)
1172 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001173 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001174 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001175}
1176
Chris Lattner24943d22010-06-08 16:52:24 +00001177size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001178Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1179{
Greg Clayton3508c382012-02-24 01:59:29 +00001180 SectionSP section_sp (addr.GetSection());
1181 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001182 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001183 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1184 if (section_sp->IsEncrypted())
1185 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001186 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001187 return 0;
1188 }
Greg Clayton3508c382012-02-24 01:59:29 +00001189 ModuleSP module_sp (section_sp->GetModule());
1190 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001191 {
Greg Clayton3508c382012-02-24 01:59:29 +00001192 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1193 if (objfile)
1194 {
1195 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1196 addr.GetOffset(),
1197 dst,
1198 dst_len);
1199 if (bytes_read > 0)
1200 return bytes_read;
1201 else
1202 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1203 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001204 else
Greg Clayton3508c382012-02-24 01:59:29 +00001205 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001206 }
1207 else
Greg Clayton3508c382012-02-24 01:59:29 +00001208 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001209 }
1210 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001211 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001212
Greg Clayton26100dc2011-01-07 01:57:07 +00001213 return 0;
1214}
1215
1216size_t
Enrico Granata91544802011-09-06 19:20:51 +00001217Target::ReadMemory (const Address& addr,
1218 bool prefer_file_cache,
1219 void *dst,
1220 size_t dst_len,
1221 Error &error,
1222 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001223{
Chris Lattner24943d22010-06-08 16:52:24 +00001224 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001225
Enrico Granata91544802011-09-06 19:20:51 +00001226 // if we end up reading this from process memory, we will fill this
1227 // with the actual load address
1228 if (load_addr_ptr)
1229 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1230
Greg Clayton26100dc2011-01-07 01:57:07 +00001231 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001232
1233 addr_t load_addr = LLDB_INVALID_ADDRESS;
1234 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001235 Address resolved_addr;
1236 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001237 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001238 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001239 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001240 // No sections are loaded, so we must assume we are not running
1241 // yet and anything we are given is a file address.
1242 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1243 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001244 }
Greg Clayton70436352010-06-30 23:03:03 +00001245 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001246 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001247 // We have at least one section loaded. This can be becuase
1248 // we have manually loaded some sections with "target modules load ..."
1249 // or because we have have a live process that has sections loaded
1250 // through the dynamic loader
1251 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1252 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001253 }
Greg Clayton70436352010-06-30 23:03:03 +00001254 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001255 if (!resolved_addr.IsValid())
1256 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001257
Greg Clayton9b82f862011-07-11 05:12:02 +00001258
Greg Clayton26100dc2011-01-07 01:57:07 +00001259 if (prefer_file_cache)
1260 {
1261 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1262 if (bytes_read > 0)
1263 return bytes_read;
1264 }
Greg Clayton70436352010-06-30 23:03:03 +00001265
Johnny Chenda5a8022011-09-20 23:28:55 +00001266 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001267 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001268 if (load_addr == LLDB_INVALID_ADDRESS)
1269 load_addr = resolved_addr.GetLoadAddress (this);
1270
Greg Clayton70436352010-06-30 23:03:03 +00001271 if (load_addr == LLDB_INVALID_ADDRESS)
1272 {
Greg Clayton3508c382012-02-24 01:59:29 +00001273 ModuleSP addr_module_sp (resolved_addr.GetModule());
1274 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001275 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001276 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001277 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001278 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001279 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001280 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001281 }
1282 else
1283 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001284 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001285 if (bytes_read != dst_len)
1286 {
1287 if (error.Success())
1288 {
1289 if (bytes_read == 0)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001290 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001291 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001292 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 +00001293 }
1294 }
Greg Clayton70436352010-06-30 23:03:03 +00001295 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001296 {
1297 if (load_addr_ptr)
1298 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001299 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001300 }
Greg Clayton70436352010-06-30 23:03:03 +00001301 // If the address is not section offset we have an address that
1302 // doesn't resolve to any address in any currently loaded shared
1303 // libaries and we failed to read memory so there isn't anything
1304 // more we can do. If it is section offset, we might be able to
1305 // read cached memory from the object file.
1306 if (!resolved_addr.IsSectionOffset())
1307 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001308 }
Chris Lattner24943d22010-06-08 16:52:24 +00001309 }
Greg Clayton70436352010-06-30 23:03:03 +00001310
Greg Clayton9b82f862011-07-11 05:12:02 +00001311 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001312 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001313 // If we didn't already try and read from the object file cache, then
1314 // try it after failing to read from the process.
1315 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001316 }
1317 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001318}
1319
Greg Clayton7dd98df2011-07-12 17:06:17 +00001320size_t
1321Target::ReadScalarIntegerFromMemory (const Address& addr,
1322 bool prefer_file_cache,
1323 uint32_t byte_size,
1324 bool is_signed,
1325 Scalar &scalar,
1326 Error &error)
1327{
1328 uint64_t uval;
1329
1330 if (byte_size <= sizeof(uval))
1331 {
1332 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1333 if (bytes_read == byte_size)
1334 {
1335 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1336 uint32_t offset = 0;
1337 if (byte_size <= 4)
1338 scalar = data.GetMaxU32 (&offset, byte_size);
1339 else
1340 scalar = data.GetMaxU64 (&offset, byte_size);
1341
1342 if (is_signed)
1343 scalar.SignExtend(byte_size * 8);
1344 return bytes_read;
1345 }
1346 }
1347 else
1348 {
1349 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1350 }
1351 return 0;
1352}
1353
1354uint64_t
1355Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1356 bool prefer_file_cache,
1357 size_t integer_byte_size,
1358 uint64_t fail_value,
1359 Error &error)
1360{
1361 Scalar scalar;
1362 if (ReadScalarIntegerFromMemory (addr,
1363 prefer_file_cache,
1364 integer_byte_size,
1365 false,
1366 scalar,
1367 error))
1368 return scalar.ULongLong(fail_value);
1369 return fail_value;
1370}
1371
1372bool
1373Target::ReadPointerFromMemory (const Address& addr,
1374 bool prefer_file_cache,
1375 Error &error,
1376 Address &pointer_addr)
1377{
1378 Scalar scalar;
1379 if (ReadScalarIntegerFromMemory (addr,
1380 prefer_file_cache,
1381 m_arch.GetAddressByteSize(),
1382 false,
1383 scalar,
1384 error))
1385 {
1386 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1387 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1388 {
1389 if (m_section_load_list.IsEmpty())
1390 {
1391 // No sections are loaded, so we must assume we are not running
1392 // yet and anything we are given is a file address.
1393 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1394 }
1395 else
1396 {
1397 // We have at least one section loaded. This can be becuase
1398 // we have manually loaded some sections with "target modules load ..."
1399 // or because we have have a live process that has sections loaded
1400 // through the dynamic loader
1401 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1402 }
1403 // We weren't able to resolve the pointer value, so just return
1404 // an address with no section
1405 if (!pointer_addr.IsValid())
1406 pointer_addr.SetOffset (pointer_vm_addr);
1407 return true;
1408
1409 }
1410 }
1411 return false;
1412}
Chris Lattner24943d22010-06-08 16:52:24 +00001413
1414ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001415Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001416{
Chris Lattner24943d22010-06-08 16:52:24 +00001417 ModuleSP module_sp;
1418
Chris Lattner24943d22010-06-08 16:52:24 +00001419 Error error;
1420
Jim Ingham03e5e512012-05-17 18:38:42 +00001421 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1422 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001423
Jim Ingham03e5e512012-05-17 18:38:42 +00001424 if (module_spec.GetUUID().IsValid())
1425 module_sp = m_images.FindFirstModule(module_spec);
1426
Greg Claytone1ef1e32012-04-27 00:58:27 +00001427 if (!module_sp)
1428 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001429 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1430 bool did_create_module = false;
1431
1432 // If there are image search path entries, try to use them first to acquire a suitable image.
1433 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001434 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001435 ModuleSpec transformed_spec (module_spec);
1436 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1437 {
1438 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1439 error = ModuleList::GetSharedModule (transformed_spec,
1440 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001441 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001442 &old_module_sp,
1443 &did_create_module);
1444 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001445 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001446
Greg Claytone1ef1e32012-04-27 00:58:27 +00001447 if (!module_sp)
1448 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001449 // If we have a UUID, we can check our global shared module list in case
1450 // we already have it. If we don't have a valid UUID, then we can't since
1451 // the path in "module_spec" will be a platform path, and we will need to
1452 // let the platform find that file. For example, we could be asking for
1453 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1454 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1455 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1456 // cache.
1457 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001458 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001459 // We have a UUID, it is OK to check the global module list...
1460 error = ModuleList::GetSharedModule (module_spec,
1461 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001462 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001463 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001464 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001465 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001466
1467 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001468 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001469 // The platform is responsible for finding and caching an appropriate
1470 // module in the shared module cache.
1471 if (m_platform_sp)
1472 {
1473 FileSpec platform_file_spec;
1474 error = m_platform_sp->GetSharedModule (module_spec,
1475 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001476 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001477 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001478 &did_create_module);
1479 }
1480 else
1481 {
1482 error.SetErrorString("no platform is currently set");
1483 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001484 }
1485 }
Chris Lattner24943d22010-06-08 16:52:24 +00001486
Jim Ingham03e5e512012-05-17 18:38:42 +00001487 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1488 // module in the list already, and if there was, let's remove it.
1489 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001490 {
Greg Clayton1649a722012-11-29 22:16:27 +00001491 ObjectFile *objfile = module_sp->GetObjectFile();
1492 if (objfile)
Jim Ingham03e5e512012-05-17 18:38:42 +00001493 {
Greg Clayton1649a722012-11-29 22:16:27 +00001494 switch (objfile->GetType())
Jim Ingham03e5e512012-05-17 18:38:42 +00001495 {
Greg Clayton1649a722012-11-29 22:16:27 +00001496 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1497 case ObjectFile::eTypeExecutable: /// A normal executable
1498 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1499 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1500 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1501 break;
1502 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1503 if (error_ptr)
1504 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1505 return ModuleSP();
1506 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1507 if (error_ptr)
1508 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1509 return ModuleSP();
1510 default:
1511 if (error_ptr)
1512 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1513 return ModuleSP();
1514 }
1515 // GetSharedModule is not guaranteed to find the old shared module, for instance
1516 // in the common case where you pass in the UUID, it is only going to find the one
1517 // module matching the UUID. In fact, it has no good way to know what the "old module"
1518 // relevant to this target is, since there might be many copies of a module with this file spec
1519 // in various running debug sessions, but only one of them will belong to this target.
1520 // So let's remove the UUID from the module list, and look in the target's module list.
1521 // Only do this if there is SOMETHING else in the module spec...
1522 if (!old_module_sp)
1523 {
1524 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham03e5e512012-05-17 18:38:42 +00001525 {
Greg Clayton1649a722012-11-29 22:16:27 +00001526 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1527 module_spec_copy.GetUUID().Clear();
1528
1529 ModuleList found_modules;
1530 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1531 if (num_found == 1)
1532 {
1533 old_module_sp = found_modules.GetModuleAtIndex(0);
1534 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001535 }
1536 }
Greg Clayton1649a722012-11-29 22:16:27 +00001537
1538 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1539 {
1540 m_images.ReplaceModule(old_module_sp, module_sp);
1541 Module *old_module_ptr = old_module_sp.get();
1542 old_module_sp.reset();
1543 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1544 }
1545 else
1546 m_images.Append(module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001547 }
Chris Lattner24943d22010-06-08 16:52:24 +00001548 }
1549 }
1550 if (error_ptr)
1551 *error_ptr = error;
1552 return module_sp;
1553}
1554
1555
Greg Clayton289afcb2012-02-18 05:35:26 +00001556TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001557Target::CalculateTarget ()
1558{
Greg Clayton289afcb2012-02-18 05:35:26 +00001559 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001560}
1561
Greg Clayton289afcb2012-02-18 05:35:26 +00001562ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001563Target::CalculateProcess ()
1564{
Greg Clayton289afcb2012-02-18 05:35:26 +00001565 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001566}
1567
Greg Clayton289afcb2012-02-18 05:35:26 +00001568ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001569Target::CalculateThread ()
1570{
Greg Clayton289afcb2012-02-18 05:35:26 +00001571 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001572}
1573
Greg Clayton289afcb2012-02-18 05:35:26 +00001574StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001575Target::CalculateStackFrame ()
1576{
Greg Clayton289afcb2012-02-18 05:35:26 +00001577 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001578}
1579
1580void
Greg Claytona830adb2010-10-04 01:05:56 +00001581Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001582{
Greg Clayton567e7f32011-09-22 04:58:26 +00001583 exe_ctx.Clear();
1584 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001585}
1586
1587PathMappingList &
1588Target::GetImageSearchPathList ()
1589{
1590 return m_image_search_paths;
1591}
1592
1593void
1594Target::ImageSearchPathsChanged
1595(
1596 const PathMappingList &path_list,
1597 void *baton
1598)
1599{
1600 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001601 ModuleSP exe_module_sp (target->GetExecutableModule());
1602 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001603 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001604 target->m_images.Clear();
1605 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001606 }
1607}
1608
1609ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001610Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001611{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001612 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001613 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001614 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001615 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001616 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001617 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1618 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1619 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1620 }
Chris Lattner24943d22010-06-08 16:52:24 +00001621 return m_scratch_ast_context_ap.get();
1622}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001623
Sean Callanan4938bd62011-11-16 18:20:47 +00001624ClangASTImporter *
1625Target::GetClangASTImporter()
1626{
1627 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1628
1629 if (!ast_importer)
1630 {
1631 ast_importer = new ClangASTImporter();
1632 m_ast_importer_ap.reset(ast_importer);
1633 }
1634
1635 return ast_importer;
1636}
1637
Greg Clayton990de7b2010-11-18 23:32:35 +00001638void
Caroline Tice2a456812011-03-10 22:14:10 +00001639Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001640{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001641 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001642}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001643
Greg Clayton990de7b2010-11-18 23:32:35 +00001644void
Caroline Tice2a456812011-03-10 22:14:10 +00001645Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001646{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001647 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001648}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001649
Greg Clayton9ce95382012-02-13 23:10:39 +00001650FileSpecList
1651Target::GetDefaultExecutableSearchPaths ()
1652{
Greg Clayton73844aa2012-08-22 17:17:09 +00001653 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1654 if (properties_sp)
1655 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001656 return FileSpecList();
1657}
1658
Caroline Tice5bc8c972010-09-20 20:44:43 +00001659ArchSpec
1660Target::GetDefaultArchitecture ()
1661{
Greg Clayton73844aa2012-08-22 17:17:09 +00001662 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1663 if (properties_sp)
1664 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001665 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001666}
1667
1668void
Greg Clayton73844aa2012-08-22 17:17:09 +00001669Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001670{
Greg Clayton73844aa2012-08-22 17:17:09 +00001671 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1672 if (properties_sp)
Jason Molenda332dc002012-12-12 02:23:56 +00001673 {
1674 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::SetDefaultArchitecture setting target's default architecture to %s (%s)", arch.GetArchitectureName(), arch.GetTriple().getTriple().c_str());
Greg Clayton73844aa2012-08-22 17:17:09 +00001675 return properties_sp->SetDefaultArchitecture(arch);
Jason Molenda332dc002012-12-12 02:23:56 +00001676 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001677}
1678
Greg Claytona830adb2010-10-04 01:05:56 +00001679Target *
1680Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1681{
1682 // The target can either exist in the "process" of ExecutionContext, or in
1683 // the "target_sp" member of SymbolContext. This accessor helper function
1684 // will get the target from one of these locations.
1685
1686 Target *target = NULL;
1687 if (sc_ptr != NULL)
1688 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001689 if (target == NULL && exe_ctx_ptr)
1690 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001691 return target;
1692}
1693
Greg Clayton427f2902010-12-14 02:59:59 +00001694ExecutionResults
1695Target::EvaluateExpression
1696(
1697 const char *expr_cstr,
1698 StackFrame *frame,
Enrico Granata6cca9692012-07-16 23:10:35 +00001699 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001700 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001701)
1702{
Enrico Granata3a08fd12012-09-18 17:43:16 +00001703 result_valobj_sp.reset();
1704
Greg Clayton427f2902010-12-14 02:59:59 +00001705 ExecutionResults execution_results = eExecutionSetupError;
1706
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001707 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1708 return execution_results;
1709
Jim Ingham3613ae12011-05-12 02:06:14 +00001710 // We shouldn't run stop hooks in expressions.
1711 // Be sure to reset this if you return anywhere within this function.
1712 bool old_suppress_value = m_suppress_stop_hooks;
1713 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001714
1715 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001716
1717 const size_t expr_cstr_len = ::strlen (expr_cstr);
1718
Greg Clayton427f2902010-12-14 02:59:59 +00001719 if (frame)
1720 {
1721 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001722 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001723 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001724 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1725 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001726 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001727
1728 // Make sure we don't have any things that we know a variable expression
1729 // won't be able to deal with before calling into it
1730 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1731 {
1732 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
Enrico Granatad27026e2012-09-05 20:41:26 +00001733 options.GetUseDynamic(),
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001734 expr_path_options,
1735 var_sp,
1736 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001737 // if this expression results in a bitfield, we give up and let the IR handle it
1738 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1739 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001740 }
Greg Clayton427f2902010-12-14 02:59:59 +00001741 }
1742 else if (m_process_sp)
1743 {
1744 m_process_sp->CalculateExecutionContext(exe_ctx);
1745 }
1746 else
1747 {
1748 CalculateExecutionContext(exe_ctx);
1749 }
1750
1751 if (result_valobj_sp)
1752 {
1753 execution_results = eExecutionCompleted;
1754 // We got a result from the frame variable expression path above...
1755 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1756
1757 lldb::ValueObjectSP const_valobj_sp;
1758
1759 // Check in case our value is already a constant value
1760 if (result_valobj_sp->GetIsConstant())
1761 {
1762 const_valobj_sp = result_valobj_sp;
1763 const_valobj_sp->SetName (persistent_variable_name);
1764 }
1765 else
Jim Inghame41494a2011-04-16 00:01:13 +00001766 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001767 if (options.GetUseDynamic() != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001768 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001769 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(options.GetUseDynamic());
Jim Inghame41494a2011-04-16 00:01:13 +00001770 if (dynamic_sp)
1771 result_valobj_sp = dynamic_sp;
1772 }
1773
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001774 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001775 }
Greg Clayton427f2902010-12-14 02:59:59 +00001776
Sean Callanan6a925532011-01-13 08:53:35 +00001777 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1778
Greg Clayton427f2902010-12-14 02:59:59 +00001779 result_valobj_sp = const_valobj_sp;
1780
Sean Callanan6a925532011-01-13 08:53:35 +00001781 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1782 assert (clang_expr_variable_sp.get());
1783
1784 // Set flags and live data as appropriate
1785
1786 const Value &result_value = live_valobj_sp->GetValue();
1787
1788 switch (result_value.GetValueType())
1789 {
1790 case Value::eValueTypeHostAddress:
1791 case Value::eValueTypeFileAddress:
1792 // we don't do anything with these for now
1793 break;
1794 case Value::eValueTypeScalar:
Greg Claytonf0fab4f2012-10-30 23:56:14 +00001795 case Value::eValueTypeVector:
Sean Callanan6a925532011-01-13 08:53:35 +00001796 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1797 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1798 break;
1799 case Value::eValueTypeLoadAddress:
1800 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1801 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1802 break;
1803 }
Greg Clayton427f2902010-12-14 02:59:59 +00001804 }
1805 else
1806 {
1807 // Make sure we aren't just trying to see the value of a persistent
1808 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001809 lldb::ClangExpressionVariableSP persistent_var_sp;
1810 // Only check for persistent variables the expression starts with a '$'
1811 if (expr_cstr[0] == '$')
1812 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1813
Greg Clayton427f2902010-12-14 02:59:59 +00001814 if (persistent_var_sp)
1815 {
1816 result_valobj_sp = persistent_var_sp->GetValueObject ();
1817 execution_results = eExecutionCompleted;
1818 }
1819 else
1820 {
1821 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001822
Greg Clayton427f2902010-12-14 02:59:59 +00001823 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Enrico Granatad27026e2012-09-05 20:41:26 +00001824 options.GetExecutionPolicy(),
Sean Callanan5b658cc2011-11-07 23:35:40 +00001825 lldb::eLanguageTypeUnknown,
Enrico Granatad27026e2012-09-05 20:41:26 +00001826 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1827 options.DoesUnwindOnError(),
Greg Clayton427f2902010-12-14 02:59:59 +00001828 expr_cstr,
1829 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001830 result_valobj_sp,
Jim Ingham47beabb2012-10-16 21:41:58 +00001831 options.GetRunOthers(),
1832 options.GetTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001833 }
1834 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001835
1836 m_suppress_stop_hooks = old_suppress_value;
1837
Greg Clayton427f2902010-12-14 02:59:59 +00001838 return execution_results;
1839}
1840
Greg Claytonc0fa5332011-05-22 22:46:53 +00001841lldb::addr_t
1842Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1843{
1844 addr_t code_addr = load_addr;
1845 switch (m_arch.GetMachine())
1846 {
1847 case llvm::Triple::arm:
1848 case llvm::Triple::thumb:
1849 switch (addr_class)
1850 {
1851 case eAddressClassData:
1852 case eAddressClassDebug:
1853 return LLDB_INVALID_ADDRESS;
1854
1855 case eAddressClassUnknown:
1856 case eAddressClassInvalid:
1857 case eAddressClassCode:
1858 case eAddressClassCodeAlternateISA:
1859 case eAddressClassRuntime:
1860 // Check if bit zero it no set?
1861 if ((code_addr & 1ull) == 0)
1862 {
1863 // Bit zero isn't set, check if the address is a multiple of 2?
1864 if (code_addr & 2ull)
1865 {
1866 // The address is a multiple of 2 so it must be thumb, set bit zero
1867 code_addr |= 1ull;
1868 }
1869 else if (addr_class == eAddressClassCodeAlternateISA)
1870 {
1871 // We checked the address and the address claims to be the alternate ISA
1872 // which means thumb, so set bit zero.
1873 code_addr |= 1ull;
1874 }
1875 }
1876 break;
1877 }
1878 break;
1879
1880 default:
1881 break;
1882 }
1883 return code_addr;
1884}
1885
1886lldb::addr_t
1887Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1888{
1889 addr_t opcode_addr = load_addr;
1890 switch (m_arch.GetMachine())
1891 {
1892 case llvm::Triple::arm:
1893 case llvm::Triple::thumb:
1894 switch (addr_class)
1895 {
1896 case eAddressClassData:
1897 case eAddressClassDebug:
1898 return LLDB_INVALID_ADDRESS;
1899
1900 case eAddressClassInvalid:
1901 case eAddressClassUnknown:
1902 case eAddressClassCode:
1903 case eAddressClassCodeAlternateISA:
1904 case eAddressClassRuntime:
1905 opcode_addr &= ~(1ull);
1906 break;
1907 }
1908 break;
1909
1910 default:
1911 break;
1912 }
1913 return opcode_addr;
1914}
1915
Jim Inghamd60d94a2011-03-11 03:53:59 +00001916lldb::user_id_t
1917Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1918{
1919 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001920 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001921 m_stop_hooks[new_uid] = new_hook_sp;
1922 return new_uid;
1923}
1924
1925bool
1926Target::RemoveStopHookByID (lldb::user_id_t user_id)
1927{
1928 size_t num_removed;
1929 num_removed = m_stop_hooks.erase (user_id);
1930 if (num_removed == 0)
1931 return false;
1932 else
1933 return true;
1934}
1935
1936void
1937Target::RemoveAllStopHooks ()
1938{
1939 m_stop_hooks.clear();
1940}
1941
1942Target::StopHookSP
1943Target::GetStopHookByID (lldb::user_id_t user_id)
1944{
1945 StopHookSP found_hook;
1946
1947 StopHookCollection::iterator specified_hook_iter;
1948 specified_hook_iter = m_stop_hooks.find (user_id);
1949 if (specified_hook_iter != m_stop_hooks.end())
1950 found_hook = (*specified_hook_iter).second;
1951 return found_hook;
1952}
1953
1954bool
1955Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1956{
1957 StopHookCollection::iterator specified_hook_iter;
1958 specified_hook_iter = m_stop_hooks.find (user_id);
1959 if (specified_hook_iter == m_stop_hooks.end())
1960 return false;
1961
1962 (*specified_hook_iter).second->SetIsActive (active_state);
1963 return true;
1964}
1965
1966void
1967Target::SetAllStopHooksActiveState (bool active_state)
1968{
1969 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1970 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1971 {
1972 (*pos).second->SetIsActive (active_state);
1973 }
1974}
1975
1976void
1977Target::RunStopHooks ()
1978{
Jim Ingham3613ae12011-05-12 02:06:14 +00001979 if (m_suppress_stop_hooks)
1980 return;
1981
Jim Inghamd60d94a2011-03-11 03:53:59 +00001982 if (!m_process_sp)
1983 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001984
1985 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1986 // since in that case we do not want to run the stop-hooks
1987 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1988 return;
1989
Jim Inghamd60d94a2011-03-11 03:53:59 +00001990 if (m_stop_hooks.empty())
1991 return;
1992
1993 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1994
1995 // If there aren't any active stop hooks, don't bother either:
1996 bool any_active_hooks = false;
1997 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1998 {
1999 if ((*pos).second->IsActive())
2000 {
2001 any_active_hooks = true;
2002 break;
2003 }
2004 }
2005 if (!any_active_hooks)
2006 return;
2007
2008 CommandReturnObject result;
2009
2010 std::vector<ExecutionContext> exc_ctx_with_reasons;
2011 std::vector<SymbolContext> sym_ctx_with_reasons;
2012
2013 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2014 size_t num_threads = cur_threadlist.GetSize();
2015 for (size_t i = 0; i < num_threads; i++)
2016 {
2017 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2018 if (cur_thread_sp->ThreadStoppedForAReason())
2019 {
2020 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2021 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2022 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2023 }
2024 }
2025
2026 // If no threads stopped for a reason, don't run the stop-hooks.
2027 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2028 if (num_exe_ctx == 0)
2029 return;
2030
Jim Inghame5ed8e92011-06-02 23:58:26 +00002031 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2032 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002033
2034 bool keep_going = true;
2035 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00002036 bool print_hook_header;
2037 bool print_thread_header;
2038
2039 if (num_exe_ctx == 1)
2040 print_thread_header = false;
2041 else
2042 print_thread_header = true;
2043
2044 if (m_stop_hooks.size() == 1)
2045 print_hook_header = false;
2046 else
2047 print_hook_header = true;
2048
Jim Inghamd60d94a2011-03-11 03:53:59 +00002049 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2050 {
2051 // result.Clear();
2052 StopHookSP cur_hook_sp = (*pos).second;
2053 if (!cur_hook_sp->IsActive())
2054 continue;
2055
2056 bool any_thread_matched = false;
2057 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2058 {
2059 if ((cur_hook_sp->GetSpecifier () == NULL
2060 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2061 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00002062 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002063 {
2064 if (!hooks_ran)
2065 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00002066 hooks_ran = true;
2067 }
Jim Inghamc54840c2011-03-22 01:47:27 +00002068 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002069 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002070 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2071 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2072 NULL);
2073 if (cmd)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002074 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chen4d96a742011-10-24 23:01:06 +00002075 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002076 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002077 any_thread_matched = true;
2078 }
2079
Jim Inghamc54840c2011-03-22 01:47:27 +00002080 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002081 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002082
2083 bool stop_on_continue = true;
2084 bool stop_on_error = true;
2085 bool echo_commands = false;
2086 bool print_results = true;
2087 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002088 &exc_ctx_with_reasons[i],
2089 stop_on_continue,
2090 stop_on_error,
2091 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002092 print_results,
2093 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002094 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002095
2096 // If the command started the target going again, we should bag out of
2097 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002098 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2099 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002100 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002101 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002102 keep_going = false;
2103 }
2104 }
2105 }
2106 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002107
Caroline Tice4a348082011-05-02 20:41:46 +00002108 result.GetImmediateOutputStream()->Flush();
2109 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002110}
2111
Greg Claytonbbea1332011-07-08 00:48:09 +00002112
Jim Inghamd60d94a2011-03-11 03:53:59 +00002113//--------------------------------------------------------------
2114// class Target::StopHook
2115//--------------------------------------------------------------
2116
2117
2118Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2119 UserID (uid),
2120 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002121 m_commands (),
2122 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002123 m_thread_spec_ap(NULL),
2124 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002125{
2126}
2127
2128Target::StopHook::StopHook (const StopHook &rhs) :
2129 UserID (rhs.GetID()),
2130 m_target_sp (rhs.m_target_sp),
2131 m_commands (rhs.m_commands),
2132 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002133 m_thread_spec_ap (NULL),
2134 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002135{
2136 if (rhs.m_thread_spec_ap.get() != NULL)
2137 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2138}
2139
2140
2141Target::StopHook::~StopHook ()
2142{
2143}
2144
2145void
2146Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2147{
2148 m_thread_spec_ap.reset (specifier);
2149}
2150
2151
2152void
2153Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2154{
2155 int indent_level = s->GetIndentLevel();
2156
2157 s->SetIndentLevel(indent_level + 2);
2158
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002159 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002160 if (m_active)
2161 s->Indent ("State: enabled\n");
2162 else
2163 s->Indent ("State: disabled\n");
2164
2165 if (m_specifier_sp)
2166 {
2167 s->Indent();
2168 s->PutCString ("Specifier:\n");
2169 s->SetIndentLevel (indent_level + 4);
2170 m_specifier_sp->GetDescription (s, level);
2171 s->SetIndentLevel (indent_level + 2);
2172 }
2173
2174 if (m_thread_spec_ap.get() != NULL)
2175 {
2176 StreamString tmp;
2177 s->Indent("Thread:\n");
2178 m_thread_spec_ap->GetDescription (&tmp, level);
2179 s->SetIndentLevel (indent_level + 4);
2180 s->Indent (tmp.GetData());
2181 s->PutCString ("\n");
2182 s->SetIndentLevel (indent_level + 2);
2183 }
2184
2185 s->Indent ("Commands: \n");
2186 s->SetIndentLevel (indent_level + 4);
2187 uint32_t num_commands = m_commands.GetSize();
2188 for (uint32_t i = 0; i < num_commands; i++)
2189 {
2190 s->Indent(m_commands.GetStringAtIndex(i));
2191 s->PutCString ("\n");
2192 }
2193 s->SetIndentLevel (indent_level);
2194}
2195
Greg Clayton73844aa2012-08-22 17:17:09 +00002196//--------------------------------------------------------------
2197// class TargetProperties
2198//--------------------------------------------------------------
2199
2200OptionEnumValueElement
2201lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002202{
Greg Clayton73844aa2012-08-22 17:17:09 +00002203 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2204 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2205 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2206 { 0, NULL, NULL }
2207};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002208
Greg Clayton49ce8962012-08-29 21:13:06 +00002209static OptionEnumValueElement
2210g_inline_breakpoint_enums[] =
2211{
2212 { 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."},
2213 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2214 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2215 { 0, NULL, NULL }
2216};
2217
Greg Clayton73844aa2012-08-22 17:17:09 +00002218static PropertyDefinition
2219g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002220{
Greg Clayton73844aa2012-08-22 17:17:09 +00002221 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2222 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2223 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2224 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2225 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2226 { "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 "
2227 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2228 "some part (starting at the root) of the path to the file when it was built, "
2229 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2230 "Each element of the array is checked in order and the first one that results in a match wins." },
2231 { "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." },
2232 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2233 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2234 { "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 +00002235 { "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." },
2236 { "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 +00002237 { "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." },
2238 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2239 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2240 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2241 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2242 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2243 { "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 +00002244 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2245 "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. "
2246 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2247 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2248 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2249 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2250 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002251 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2252};
2253enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002254{
Greg Clayton73844aa2012-08-22 17:17:09 +00002255 ePropertyDefaultArch,
2256 ePropertyExprPrefix,
2257 ePropertyPreferDynamic,
2258 ePropertyEnableSynthetic,
2259 ePropertySkipPrologue,
2260 ePropertySourceMap,
2261 ePropertyExecutableSearchPaths,
2262 ePropertyMaxChildrenCount,
2263 ePropertyMaxSummaryLength,
2264 ePropertyBreakpointUseAvoidList,
Greg Clayton0c8446c2012-10-17 22:57:12 +00002265 ePropertyArg0,
Greg Clayton73844aa2012-08-22 17:17:09 +00002266 ePropertyRunArgs,
2267 ePropertyEnvVars,
2268 ePropertyInheritEnv,
2269 ePropertyInputPath,
2270 ePropertyOutputPath,
2271 ePropertyErrorPath,
2272 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002273 ePropertyDisableSTDIO,
Greg Clayton87e9d322012-10-19 18:02:49 +00002274 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002275};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002276
Caroline Tice5bc8c972010-09-20 20:44:43 +00002277
Greg Clayton73844aa2012-08-22 17:17:09 +00002278class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002279{
Greg Clayton73844aa2012-08-22 17:17:09 +00002280public:
2281 TargetOptionValueProperties (const ConstString &name) :
2282 OptionValueProperties (name),
2283 m_target (NULL),
2284 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002285 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002286 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002287
Greg Clayton73844aa2012-08-22 17:17:09 +00002288 // This constructor is used when creating TargetOptionValueProperties when it
2289 // is part of a new lldb_private::Target instance. It will copy all current
2290 // global property values as needed
2291 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2292 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2293 m_target (target),
2294 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002295 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002296 }
2297
2298 virtual const Property *
2299 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2300 {
2301 // When gettings the value for a key from the target options, we will always
2302 // try and grab the setting from the current target if there is one. Else we just
2303 // use the one from this instance.
2304 if (idx == ePropertyEnvVars)
2305 GetHostEnvironmentIfNeeded ();
2306
2307 if (exe_ctx)
2308 {
2309 Target *target = exe_ctx->GetTargetPtr();
2310 if (target)
2311 {
2312 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2313 if (this != target_properties)
2314 return target_properties->ProtectedGetPropertyAtIndex (idx);
2315 }
2316 }
2317 return ProtectedGetPropertyAtIndex (idx);
2318 }
2319protected:
2320
2321 void
2322 GetHostEnvironmentIfNeeded () const
2323 {
2324 if (!m_got_host_env)
2325 {
2326 if (m_target)
2327 {
2328 m_got_host_env = true;
2329 const uint32_t idx = ePropertyInheritEnv;
2330 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2331 {
2332 PlatformSP platform_sp (m_target->GetPlatform());
2333 if (platform_sp)
2334 {
2335 StringList env;
2336 if (platform_sp->GetEnvironment(env))
2337 {
2338 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2339 if (env_dict)
2340 {
2341 const bool can_replace = false;
2342 const size_t envc = env.GetSize();
2343 for (size_t idx=0; idx<envc; idx++)
2344 {
2345 const char *env_entry = env.GetStringAtIndex (idx);
2346 if (env_entry)
2347 {
2348 const char *equal_pos = ::strchr(env_entry, '=');
2349 ConstString key;
2350 // It is ok to have environment variables with no values
2351 const char *value = NULL;
2352 if (equal_pos)
2353 {
2354 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2355 if (equal_pos[1])
2356 value = equal_pos + 1;
2357 }
2358 else
2359 {
2360 key.SetCString(env_entry);
2361 }
2362 // Don't allow existing keys to be replaced with ones we get from the platform environment
2363 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2364 }
2365 }
2366 }
2367 }
2368 }
2369 }
2370 }
2371 }
2372 }
2373 Target *m_target;
2374 mutable bool m_got_host_env;
2375};
2376
2377TargetProperties::TargetProperties (Target *target) :
2378 Properties ()
2379{
2380 if (target)
2381 {
2382 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002383 }
2384 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002385 {
2386 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2387 m_collection_sp->Initialize(g_properties);
2388 m_collection_sp->AppendProperty(ConstString("process"),
2389 ConstString("Settings specify to processes."),
2390 true,
2391 Process::GetGlobalProperties()->GetValueProperties());
2392 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002393}
2394
Greg Clayton73844aa2012-08-22 17:17:09 +00002395TargetProperties::~TargetProperties ()
2396{
2397}
2398ArchSpec
2399TargetProperties::GetDefaultArchitecture () const
2400{
2401 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2402 if (value)
2403 return value->GetCurrentValue();
2404 return ArchSpec();
2405}
2406
2407void
2408TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2409{
2410 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2411 if (value)
2412 return value->SetCurrentValue(arch, true);
2413}
2414
2415lldb::DynamicValueType
2416TargetProperties::GetPreferDynamicValue() const
2417{
2418 const uint32_t idx = ePropertyPreferDynamic;
2419 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2420}
2421
2422bool
2423TargetProperties::GetDisableASLR () const
2424{
2425 const uint32_t idx = ePropertyDisableASLR;
2426 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2427}
2428
2429void
2430TargetProperties::SetDisableASLR (bool b)
2431{
2432 const uint32_t idx = ePropertyDisableASLR;
2433 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2434}
2435
2436bool
2437TargetProperties::GetDisableSTDIO () const
2438{
2439 const uint32_t idx = ePropertyDisableSTDIO;
2440 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2441}
2442
2443void
2444TargetProperties::SetDisableSTDIO (bool b)
2445{
2446 const uint32_t idx = ePropertyDisableSTDIO;
2447 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2448}
2449
Greg Clayton49ce8962012-08-29 21:13:06 +00002450InlineStrategy
2451TargetProperties::GetInlineStrategy () const
2452{
2453 const uint32_t idx = ePropertyInlineStrategy;
2454 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2455}
2456
Greg Clayton0c8446c2012-10-17 22:57:12 +00002457const char *
2458TargetProperties::GetArg0 () const
2459{
2460 const uint32_t idx = ePropertyArg0;
2461 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2462}
2463
2464void
2465TargetProperties::SetArg0 (const char *arg)
2466{
2467 const uint32_t idx = ePropertyArg0;
2468 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2469}
2470
Greg Clayton73844aa2012-08-22 17:17:09 +00002471bool
2472TargetProperties::GetRunArguments (Args &args) const
2473{
2474 const uint32_t idx = ePropertyRunArgs;
2475 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2476}
2477
2478void
2479TargetProperties::SetRunArguments (const Args &args)
2480{
2481 const uint32_t idx = ePropertyRunArgs;
2482 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2483}
2484
2485size_t
2486TargetProperties::GetEnvironmentAsArgs (Args &env) const
2487{
2488 const uint32_t idx = ePropertyEnvVars;
2489 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2490}
2491
2492bool
2493TargetProperties::GetSkipPrologue() const
2494{
2495 const uint32_t idx = ePropertySkipPrologue;
2496 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2497}
2498
2499PathMappingList &
2500TargetProperties::GetSourcePathMap () const
2501{
2502 const uint32_t idx = ePropertySourceMap;
2503 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2504 assert(option_value);
2505 return option_value->GetCurrentValue();
2506}
2507
2508FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002509TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002510{
2511 const uint32_t idx = ePropertyExecutableSearchPaths;
2512 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2513 assert(option_value);
2514 return option_value->GetCurrentValue();
2515}
2516
2517bool
2518TargetProperties::GetEnableSyntheticValue () const
2519{
2520 const uint32_t idx = ePropertyEnableSynthetic;
2521 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2522}
2523
2524uint32_t
2525TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2526{
2527 const uint32_t idx = ePropertyMaxChildrenCount;
2528 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2529}
2530
2531uint32_t
2532TargetProperties::GetMaximumSizeOfStringSummary() const
2533{
2534 const uint32_t idx = ePropertyMaxSummaryLength;
2535 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2536}
2537
2538FileSpec
2539TargetProperties::GetStandardInputPath () const
2540{
2541 const uint32_t idx = ePropertyInputPath;
2542 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2543}
2544
2545void
2546TargetProperties::SetStandardInputPath (const char *p)
2547{
2548 const uint32_t idx = ePropertyInputPath;
2549 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2550}
2551
2552FileSpec
2553TargetProperties::GetStandardOutputPath () const
2554{
2555 const uint32_t idx = ePropertyOutputPath;
2556 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2557}
2558
2559void
2560TargetProperties::SetStandardOutputPath (const char *p)
2561{
2562 const uint32_t idx = ePropertyOutputPath;
2563 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2564}
2565
2566FileSpec
2567TargetProperties::GetStandardErrorPath () const
2568{
2569 const uint32_t idx = ePropertyErrorPath;
2570 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2571}
2572
Greg Claytonc6e82e42012-08-22 18:39:03 +00002573const char *
2574TargetProperties::GetExpressionPrefixContentsAsCString ()
2575{
2576 const uint32_t idx = ePropertyExprPrefix;
2577 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2578 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002579 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002580 const bool null_terminate = true;
2581 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002582 if (data_sp)
2583 return (const char *) data_sp->GetBytes();
2584 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002585 return NULL;
2586}
2587
Greg Clayton73844aa2012-08-22 17:17:09 +00002588void
2589TargetProperties::SetStandardErrorPath (const char *p)
2590{
2591 const uint32_t idx = ePropertyErrorPath;
2592 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2593}
2594
2595bool
2596TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2597{
2598 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2599 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2600}
2601
2602const TargetPropertiesSP &
2603Target::GetGlobalProperties()
2604{
2605 static TargetPropertiesSP g_settings_sp;
2606 if (!g_settings_sp)
2607 {
2608 g_settings_sp.reset (new TargetProperties (NULL));
2609 }
2610 return g_settings_sp;
2611}
2612
Jim Ingham5a15e692012-02-16 06:50:00 +00002613const ConstString &
2614Target::TargetEventData::GetFlavorString ()
2615{
2616 static ConstString g_flavor ("Target::TargetEventData");
2617 return g_flavor;
2618}
2619
2620const ConstString &
2621Target::TargetEventData::GetFlavor () const
2622{
2623 return TargetEventData::GetFlavorString ();
2624}
2625
2626Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2627 EventData(),
2628 m_target_sp (new_target_sp)
2629{
2630}
2631
2632Target::TargetEventData::~TargetEventData()
2633{
2634
2635}
2636
2637void
2638Target::TargetEventData::Dump (Stream *s) const
2639{
2640
2641}
2642
2643const TargetSP
2644Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2645{
2646 TargetSP target_sp;
2647
2648 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2649 if (data)
2650 target_sp = data->m_target_sp;
2651
2652 return target_sp;
2653}
2654
2655const Target::TargetEventData *
2656Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2657{
2658 if (event_ptr)
2659 {
2660 const EventData *event_data = event_ptr->GetData();
2661 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2662 return static_cast <const TargetEventData *> (event_ptr->GetData());
2663 }
2664 return NULL;
2665}
2666