blob: f07cfdd7cda4d5e856ba97896041d8e6092892c4 [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"
Greg Clayton535f53c2013-03-19 00:20:55 +000030#include "lldb/Core/SourceManager.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000034#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000035#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000037#include "lldb/Interpreter/CommandInterpreter.h"
38#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chen3f883492012-06-04 23:19:54 +000039#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton73844aa2012-08-22 17:17:09 +000040#include "lldb/Interpreter/OptionValues.h"
41#include "lldb/Interpreter/Property.h"
Chris Lattner24943d22010-06-08 16:52:24 +000042#include "lldb/lldb-private-log.h"
43#include "lldb/Symbol/ObjectFile.h"
44#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000045#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000046#include "lldb/Target/Thread.h"
47#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000048
49using namespace lldb;
50using namespace lldb_private;
51
Jim Ingham5a15e692012-02-16 06:50:00 +000052ConstString &
53Target::GetStaticBroadcasterClass ()
54{
55 static ConstString class_name ("lldb.target");
56 return class_name;
57}
58
Chris Lattner24943d22010-06-08 16:52:24 +000059//----------------------------------------------------------------------
60// Target constructor
61//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000062Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton73844aa2012-08-22 17:17:09 +000063 TargetProperties (this),
Jim Ingham94a5d0d2012-10-10 18:32:14 +000064 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton24bc5d92011-03-30 18:16:51 +000065 ExecutionContextScope (),
Greg Clayton63094e02010-06-23 01:19:29 +000066 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000067 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000068 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000069 m_arch (target_arch),
Enrico Granata146d9522012-11-08 02:22:02 +000070 m_images (this),
Greg Claytoneea26402010-09-14 23:36:40 +000071 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000072 m_breakpoint_list (false),
73 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000074 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000075 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000076 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000077 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000078 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton81a96aa2013-04-18 18:10:51 +000079 m_scratch_ast_context_ap (),
80 m_scratch_ast_source_ap (),
81 m_ast_importer_ap (),
Jim Inghamd60d94a2011-03-11 03:53:59 +000082 m_persistent_variables (),
Greg Clayton535f53c2013-03-19 00:20:55 +000083 m_source_manager_ap(),
Greg Clayton24bc5d92011-03-30 18:16:51 +000084 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000085 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000086 m_suppress_stop_hooks (false),
87 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000088{
Greg Clayton49ce6822010-10-31 03:01:06 +000089 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
90 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
91 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham9c970a32012-12-18 02:03:49 +000092 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granata12fbcf52013-04-05 18:49:06 +000093 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000094
95 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000096
Greg Clayton952e9dc2013-03-27 23:08:40 +000097 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000098 if (log)
99 log->Printf ("%p Target::Target()", this);
Jason Molenda8c6cf432012-12-05 00:25:49 +0000100 if (m_arch.IsValid())
101 {
Jason Molenda332dc002012-12-12 02:23:56 +0000102 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 +0000103 }
Chris Lattner24943d22010-06-08 16:52:24 +0000104}
105
106//----------------------------------------------------------------------
107// Destructor
108//----------------------------------------------------------------------
109Target::~Target()
110{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000111 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000112 if (log)
113 log->Printf ("%p Target::~Target()", this);
114 DeleteCurrentProcess ();
115}
116
117void
Caroline Tice7826c882010-10-26 03:11:13 +0000118Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000119{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000120// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000121 if (description_level != lldb::eDescriptionLevelBrief)
122 {
123 s->Indent();
124 s->PutCString("Target\n");
125 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000126 m_images.Dump(s);
127 m_breakpoint_list.Dump(s);
128 m_internal_breakpoint_list.Dump(s);
129 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000130 }
131 else
132 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000133 Module *exe_module = GetExecutableModulePointer();
134 if (exe_module)
135 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000136 else
137 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000138 }
Chris Lattner24943d22010-06-08 16:52:24 +0000139}
140
141void
Greg Clayton0bce9a22012-12-05 00:16:59 +0000142Target::CleanupProcess ()
143{
144 // Do any cleanup of the target we need to do between process instances.
145 // NB It is better to do this before destroying the process in case the
146 // clean up needs some help from the process.
147 m_breakpoint_list.ClearAllBreakpointSites();
148 m_internal_breakpoint_list.ClearAllBreakpointSites();
149 // Disable watchpoints just on the debugger side.
150 Mutex::Locker locker;
151 this->GetWatchpointList().GetListMutex(locker);
152 DisableAllWatchpoints(false);
153 ClearAllWatchpointHitCounts();
154}
155
156void
Chris Lattner24943d22010-06-08 16:52:24 +0000157Target::DeleteCurrentProcess ()
158{
159 if (m_process_sp.get())
160 {
Greg Clayton49480b12010-09-14 23:52:43 +0000161 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000162 if (m_process_sp->IsAlive())
163 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000164
165 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000166
Greg Clayton0bce9a22012-12-05 00:16:59 +0000167 CleanupProcess ();
168
Chris Lattner24943d22010-06-08 16:52:24 +0000169 m_process_sp.reset();
170 }
171}
172
173const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000174Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000175{
176 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000177 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000178 return m_process_sp;
179}
180
181const lldb::ProcessSP &
182Target::GetProcessSP () const
183{
184 return m_process_sp;
185}
186
Greg Clayton153ccd72011-08-10 02:10:13 +0000187void
188Target::Destroy()
189{
190 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000191 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000192 DeleteCurrentProcess ();
193 m_platform_sp.reset();
194 m_arch.Clear();
195 m_images.Clear();
196 m_section_load_list.Clear();
197 const bool notify = false;
198 m_breakpoint_list.RemoveAll(notify);
199 m_internal_breakpoint_list.RemoveAll(notify);
200 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000201 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000202 m_search_filter_sp.reset();
203 m_image_search_paths.Clear(notify);
204 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000205 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000206 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000207 m_persistent_variables.Clear();
208 m_stop_hooks.clear();
209 m_stop_hook_next_id = 0;
210 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000211 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000212}
213
214
Chris Lattner24943d22010-06-08 16:52:24 +0000215BreakpointList &
216Target::GetBreakpointList(bool internal)
217{
218 if (internal)
219 return m_internal_breakpoint_list;
220 else
221 return m_breakpoint_list;
222}
223
224const BreakpointList &
225Target::GetBreakpointList(bool internal) const
226{
227 if (internal)
228 return m_internal_breakpoint_list;
229 else
230 return m_breakpoint_list;
231}
232
233BreakpointSP
234Target::GetBreakpointByID (break_id_t break_id)
235{
236 BreakpointSP bp_sp;
237
238 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
239 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
240 else
241 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
242
243 return bp_sp;
244}
245
246BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000247Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000248 const FileSpecList *source_file_spec_list,
249 RegularExpression &source_regex,
250 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000251{
Jim Inghamd6d47972011-09-23 00:54:11 +0000252 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
253 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000254 return CreateBreakpoint (filter_sp, resolver_sp, internal);
255}
256
257
258BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000259Target::CreateBreakpoint (const FileSpecList *containingModules,
260 const FileSpec &file,
261 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000262 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000263 LazyBool skip_prologue,
264 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000265{
Greg Clayton49ce8962012-08-29 21:13:06 +0000266 if (check_inlines == eLazyBoolCalculate)
267 {
268 const InlineStrategy inline_strategy = GetInlineStrategy();
269 switch (inline_strategy)
270 {
271 case eInlineBreakpointsNever:
272 check_inlines = eLazyBoolNo;
273 break;
274
275 case eInlineBreakpointsHeaders:
276 if (file.IsSourceImplementationFile())
277 check_inlines = eLazyBoolNo;
278 else
279 check_inlines = eLazyBoolYes;
280 break;
281
282 case eInlineBreakpointsAlways:
283 check_inlines = eLazyBoolYes;
284 break;
285 }
286 }
Greg Clayton46365522012-09-07 23:48:57 +0000287 SearchFilterSP filter_sp;
288 if (check_inlines == eLazyBoolNo)
289 {
290 // Not checking for inlines, we are looking only for matching compile units
291 FileSpecList compile_unit_list;
292 compile_unit_list.Append (file);
293 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
294 }
295 else
296 {
297 filter_sp = GetSearchFilterForModuleList (containingModules);
298 }
Greg Claytond387b462013-04-19 21:31:16 +0000299 if (skip_prologue == eLazyBoolCalculate)
300 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
301
Greg Clayton49ce8962012-08-29 21:13:06 +0000302 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
303 file,
304 line_no,
305 check_inlines,
Greg Claytond387b462013-04-19 21:31:16 +0000306 skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000307 return CreateBreakpoint (filter_sp, resolver_sp, internal);
308}
309
310
311BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000312Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000313{
Chris Lattner24943d22010-06-08 16:52:24 +0000314 Address so_addr;
315 // Attempt to resolve our load address if possible, though it is ok if
316 // it doesn't resolve to section/offset.
317
Greg Clayton33ed1702010-08-24 00:45:41 +0000318 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000319 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000320 if (!so_addr.IsValid())
321 {
322 // The address didn't resolve, so just set this as an absolute address
323 so_addr.SetOffset (addr);
324 }
325 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000326 return bp_sp;
327}
328
329BreakpointSP
330Target::CreateBreakpoint (Address &addr, bool internal)
331{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000332 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000333 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
334 return CreateBreakpoint (filter_sp, resolver_sp, internal);
335}
336
337BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000338Target::CreateBreakpoint (const FileSpecList *containingModules,
339 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000340 const char *func_name,
341 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000342 LazyBool skip_prologue,
343 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000344{
Greg Clayton12bec712010-06-28 21:30:43 +0000345 BreakpointSP bp_sp;
346 if (func_name)
347 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000348 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond387b462013-04-19 21:31:16 +0000349
350 if (skip_prologue == eLazyBoolCalculate)
351 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
352
Greg Clayton7dd98df2011-07-12 17:06:17 +0000353 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
354 func_name,
355 func_name_type_mask,
356 Breakpoint::Exact,
Greg Claytond387b462013-04-19 21:31:16 +0000357 skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000358 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
359 }
360 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000361}
362
Jim Ingham4722b102012-03-06 00:37:27 +0000363lldb::BreakpointSP
364Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000365 const FileSpecList *containingSourceFiles,
366 const std::vector<std::string> &func_names,
367 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000368 LazyBool skip_prologue,
369 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000370{
371 BreakpointSP bp_sp;
372 size_t num_names = func_names.size();
373 if (num_names > 0)
374 {
375 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond387b462013-04-19 21:31:16 +0000376
377 if (skip_prologue == eLazyBoolCalculate)
378 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
379
380 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham4722b102012-03-06 00:37:27 +0000381 func_names,
382 func_name_type_mask,
Greg Claytond387b462013-04-19 21:31:16 +0000383 skip_prologue));
Jim Ingham4722b102012-03-06 00:37:27 +0000384 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
385 }
386 return bp_sp;
387}
388
Jim Inghamc1053622012-03-03 02:05:11 +0000389BreakpointSP
390Target::CreateBreakpoint (const FileSpecList *containingModules,
391 const FileSpecList *containingSourceFiles,
392 const char *func_names[],
393 size_t num_names,
394 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000395 LazyBool skip_prologue,
396 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000397{
398 BreakpointSP bp_sp;
399 if (num_names > 0)
400 {
401 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
402
Greg Claytond387b462013-04-19 21:31:16 +0000403 if (skip_prologue == eLazyBoolCalculate)
404 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
405
406 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamc1053622012-03-03 02:05:11 +0000407 func_names,
408 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000409 func_name_type_mask,
Greg Claytond387b462013-04-19 21:31:16 +0000410 skip_prologue));
Jim Inghamc1053622012-03-03 02:05:11 +0000411 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
412 }
413 return bp_sp;
414}
Chris Lattner24943d22010-06-08 16:52:24 +0000415
416SearchFilterSP
417Target::GetSearchFilterForModule (const FileSpec *containingModule)
418{
419 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000420 if (containingModule != NULL)
421 {
422 // TODO: We should look into sharing module based search filters
423 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000424 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000425 }
426 else
427 {
428 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000429 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000430 filter_sp = m_search_filter_sp;
431 }
432 return filter_sp;
433}
434
Jim Ingham03c8ee52011-09-21 01:17:13 +0000435SearchFilterSP
436Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
437{
438 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000439 if (containingModules && containingModules->GetSize() != 0)
440 {
441 // TODO: We should look into sharing module based search filters
442 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000443 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000444 }
445 else
446 {
447 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000448 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000449 filter_sp = m_search_filter_sp;
450 }
451 return filter_sp;
452}
453
Jim Inghamd6d47972011-09-23 00:54:11 +0000454SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000455Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
456 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000457{
458 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
459 return GetSearchFilterForModuleList(containingModules);
460
461 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000462 if (containingModules == NULL)
463 {
464 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
465 // but that will take a little reworking.
466
Greg Clayton13d24fb2012-01-29 20:56:30 +0000467 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000468 }
469 else
470 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000471 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000472 }
473 return filter_sp;
474}
475
Chris Lattner24943d22010-06-08 16:52:24 +0000476BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000477Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000478 const FileSpecList *containingSourceFiles,
479 RegularExpression &func_regex,
480 LazyBool skip_prologue,
481 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000482{
Jim Inghamd6d47972011-09-23 00:54:11 +0000483 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000484 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
485 func_regex,
486 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000487
488 return CreateBreakpoint (filter_sp, resolver_sp, internal);
489}
490
Jim Ingham3df164e2012-03-05 04:47:34 +0000491lldb::BreakpointSP
492Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
493{
494 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
495}
496
Chris Lattner24943d22010-06-08 16:52:24 +0000497BreakpointSP
498Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
499{
500 BreakpointSP bp_sp;
501 if (filter_sp && resolver_sp)
502 {
503 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
504 resolver_sp->SetBreakpoint (bp_sp.get());
505
506 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000507 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000508 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000509 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000510
Greg Clayton952e9dc2013-03-27 23:08:40 +0000511 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000512 if (log)
513 {
514 StreamString s;
515 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
516 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
517 }
518
Chris Lattner24943d22010-06-08 16:52:24 +0000519 bp_sp->ResolveBreakpoint();
520 }
Jim Inghamd1686902010-10-14 23:45:03 +0000521
522 if (!internal && bp_sp)
523 {
524 m_last_created_breakpoint = bp_sp;
525 }
526
Chris Lattner24943d22010-06-08 16:52:24 +0000527 return bp_sp;
528}
529
Johnny Chenda5a8022011-09-20 23:28:55 +0000530bool
531Target::ProcessIsValid()
532{
533 return (m_process_sp && m_process_sp->IsAlive());
534}
535
Johnny Chen3f883492012-06-04 23:19:54 +0000536static bool
537CheckIfWatchpointsExhausted(Target *target, Error &error)
538{
539 uint32_t num_supported_hardware_watchpoints;
540 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
541 if (rc.Success())
542 {
543 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
544 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
545 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
546 num_supported_hardware_watchpoints);
547 }
548 return false;
549}
550
Johnny Chenecd4feb2011-10-14 00:42:25 +0000551// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000552// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000553WatchpointSP
Jim Ingham9e376622012-10-23 07:20:06 +0000554Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000555{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000556 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen5b2fc572011-09-14 20:23:45 +0000557 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000558 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Ingham9e376622012-10-23 07:20:06 +0000559 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000560
Johnny Chenecd4feb2011-10-14 00:42:25 +0000561 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000562 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000563 {
564 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000565 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000566 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000567 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000568 {
569 if (size == 0)
570 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
571 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000572 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000573 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000574 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000575
Johnny Chenecd4feb2011-10-14 00:42:25 +0000576 // Currently we only support one watchpoint per address, with total number
577 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000578
579 // Grab the list mutex while doing operations.
Jim Ingham9c970a32012-12-18 02:03:49 +0000580 const bool notify = false; // Don't notify about all the state changes we do on creating the watchpoint.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000581 Mutex::Locker locker;
582 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000583 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000584 if (matched_sp)
585 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000586 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000587 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000588 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
589 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000590 // Return the existing watchpoint if both size and type match.
Jim Ingham9e376622012-10-23 07:20:06 +0000591 if (size == old_size && kind == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000592 wp_sp = matched_sp;
Jim Ingham9c970a32012-12-18 02:03:49 +0000593 wp_sp->SetEnabled(false, notify);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000594 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000595 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham9c970a32012-12-18 02:03:49 +0000596 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
597 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000598 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000599 }
600
Jason Molendac4d49fd2012-12-05 23:07:34 +0000601 if (!wp_sp)
602 {
Jim Ingham9c970a32012-12-18 02:03:49 +0000603 wp_sp.reset(new Watchpoint(*this, addr, size, type));
604 wp_sp->SetWatchpointType(kind, notify);
605 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000606 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000607
Jim Ingham9c970a32012-12-18 02:03:49 +0000608 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000609 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +0000610 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
611 __FUNCTION__,
612 error.Success() ? "succeeded" : "failed",
613 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000614
Jason Molendac4d49fd2012-12-05 23:07:34 +0000615 if (error.Fail())
616 {
Johnny Chen155599b2012-03-26 22:00:10 +0000617 // Enabling the watchpoint on the device side failed.
618 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham9c970a32012-12-18 02:03:49 +0000619 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chen3f883492012-06-04 23:19:54 +0000620 // See if we could provide more helpful error message.
621 if (!CheckIfWatchpointsExhausted(this, error))
622 {
623 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
624 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
625 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000626 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000627 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000628 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000629 m_last_created_watchpoint = wp_sp;
630 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000631}
632
Chris Lattner24943d22010-06-08 16:52:24 +0000633void
634Target::RemoveAllBreakpoints (bool internal_also)
635{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000636 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000637 if (log)
638 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
639
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000640 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000641 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000642 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000643
644 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000645}
646
647void
648Target::DisableAllBreakpoints (bool internal_also)
649{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000650 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000651 if (log)
652 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
653
654 m_breakpoint_list.SetEnabledAll (false);
655 if (internal_also)
656 m_internal_breakpoint_list.SetEnabledAll (false);
657}
658
659void
660Target::EnableAllBreakpoints (bool internal_also)
661{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000662 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000663 if (log)
664 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
665
666 m_breakpoint_list.SetEnabledAll (true);
667 if (internal_also)
668 m_internal_breakpoint_list.SetEnabledAll (true);
669}
670
671bool
672Target::RemoveBreakpointByID (break_id_t break_id)
673{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000674 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000675 if (log)
676 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
677
678 if (DisableBreakpointByID (break_id))
679 {
680 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000681 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000682 else
Jim Inghamd1686902010-10-14 23:45:03 +0000683 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000684 if (m_last_created_breakpoint)
685 {
686 if (m_last_created_breakpoint->GetID() == break_id)
687 m_last_created_breakpoint.reset();
688 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000689 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000690 }
Chris Lattner24943d22010-06-08 16:52:24 +0000691 return true;
692 }
693 return false;
694}
695
696bool
697Target::DisableBreakpointByID (break_id_t break_id)
698{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000699 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000700 if (log)
701 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
702
703 BreakpointSP bp_sp;
704
705 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
706 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
707 else
708 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
709 if (bp_sp)
710 {
711 bp_sp->SetEnabled (false);
712 return true;
713 }
714 return false;
715}
716
717bool
718Target::EnableBreakpointByID (break_id_t break_id)
719{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000720 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000721 if (log)
722 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
723 __FUNCTION__,
724 break_id,
725 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
726
727 BreakpointSP bp_sp;
728
729 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
730 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
731 else
732 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
733
734 if (bp_sp)
735 {
736 bp_sp->SetEnabled (true);
737 return true;
738 }
739 return false;
740}
741
Johnny Chenc86582f2011-09-23 21:21:43 +0000742// The flag 'end_to_end', default to true, signifies that the operation is
743// performed end to end, for both the debugger and the debuggee.
744
Johnny Chenecd4feb2011-10-14 00:42:25 +0000745// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
746// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000747bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000748Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000749{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000750 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chenda5a8022011-09-20 23:28:55 +0000751 if (log)
752 log->Printf ("Target::%s\n", __FUNCTION__);
753
Johnny Chenc86582f2011-09-23 21:21:43 +0000754 if (!end_to_end) {
Jim Ingham9c970a32012-12-18 02:03:49 +0000755 m_watchpoint_list.RemoveAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000756 return true;
757 }
758
759 // Otherwise, it's an end to end operation.
760
Johnny Chenda5a8022011-09-20 23:28:55 +0000761 if (!ProcessIsValid())
762 return false;
763
Johnny Chenecd4feb2011-10-14 00:42:25 +0000764 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000765 for (size_t i = 0; i < num_watchpoints; ++i)
766 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000767 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
768 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000769 return false;
770
Johnny Chenecd4feb2011-10-14 00:42:25 +0000771 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000772 if (rc.Fail())
773 return false;
774 }
Jim Ingham9c970a32012-12-18 02:03:49 +0000775 m_watchpoint_list.RemoveAll (true);
Johnny Chenda5a8022011-09-20 23:28:55 +0000776 return true; // Success!
777}
778
Johnny Chenecd4feb2011-10-14 00:42:25 +0000779// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
780// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000781bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000782Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000783{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000784 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chenda5a8022011-09-20 23:28:55 +0000785 if (log)
786 log->Printf ("Target::%s\n", __FUNCTION__);
787
Johnny Chenc86582f2011-09-23 21:21:43 +0000788 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000789 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000790 return true;
791 }
792
793 // Otherwise, it's an end to end operation.
794
Johnny Chenda5a8022011-09-20 23:28:55 +0000795 if (!ProcessIsValid())
796 return false;
797
Johnny Chenecd4feb2011-10-14 00:42:25 +0000798 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000799 for (size_t i = 0; i < num_watchpoints; ++i)
800 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000801 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
802 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000803 return false;
804
Johnny Chenecd4feb2011-10-14 00:42:25 +0000805 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000806 if (rc.Fail())
807 return false;
808 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000809 return true; // Success!
810}
811
Johnny Chenecd4feb2011-10-14 00:42:25 +0000812// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
813// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000814bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000815Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000816{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000817 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chenda5a8022011-09-20 23:28:55 +0000818 if (log)
819 log->Printf ("Target::%s\n", __FUNCTION__);
820
Johnny Chenc86582f2011-09-23 21:21:43 +0000821 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000822 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000823 return true;
824 }
825
826 // Otherwise, it's an end to end operation.
827
Johnny Chenda5a8022011-09-20 23:28:55 +0000828 if (!ProcessIsValid())
829 return false;
830
Johnny Chenecd4feb2011-10-14 00:42:25 +0000831 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000832 for (size_t i = 0; i < num_watchpoints; ++i)
833 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000834 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
835 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000836 return false;
837
Johnny Chenecd4feb2011-10-14 00:42:25 +0000838 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000839 if (rc.Fail())
840 return false;
841 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000842 return true; // Success!
843}
844
Johnny Chen116a5cd2012-02-25 06:44:30 +0000845// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
846bool
847Target::ClearAllWatchpointHitCounts ()
848{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000849 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen116a5cd2012-02-25 06:44:30 +0000850 if (log)
851 log->Printf ("Target::%s\n", __FUNCTION__);
852
853 size_t num_watchpoints = m_watchpoint_list.GetSize();
854 for (size_t i = 0; i < num_watchpoints; ++i)
855 {
856 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
857 if (!wp_sp)
858 return false;
859
860 wp_sp->ResetHitCount();
861 }
862 return true; // Success!
863}
864
Johnny Chenecd4feb2011-10-14 00:42:25 +0000865// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000866// during these operations.
867bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000868Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000869{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000870 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chene14cf4e2011-10-05 21:35:46 +0000871 if (log)
872 log->Printf ("Target::%s\n", __FUNCTION__);
873
874 if (!ProcessIsValid())
875 return false;
876
Johnny Chenecd4feb2011-10-14 00:42:25 +0000877 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000878 for (size_t i = 0; i < num_watchpoints; ++i)
879 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000880 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
881 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000882 return false;
883
Johnny Chenecd4feb2011-10-14 00:42:25 +0000884 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000885 }
886 return true; // Success!
887}
888
Johnny Chenecd4feb2011-10-14 00:42:25 +0000889// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000890bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000891Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000892{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000893 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chenda5a8022011-09-20 23:28:55 +0000894 if (log)
895 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
896
897 if (!ProcessIsValid())
898 return false;
899
Johnny Chenecd4feb2011-10-14 00:42:25 +0000900 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
901 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000902 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000903 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000904 if (rc.Success())
905 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000906
Johnny Chen01acfa72011-09-22 18:04:58 +0000907 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000908 }
909 return false;
910}
911
Johnny Chenecd4feb2011-10-14 00:42:25 +0000912// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000913bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000914Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000915{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000916 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chenda5a8022011-09-20 23:28:55 +0000917 if (log)
918 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
919
920 if (!ProcessIsValid())
921 return false;
922
Johnny Chenecd4feb2011-10-14 00:42:25 +0000923 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
924 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000925 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000926 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000927 if (rc.Success())
928 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000929
Johnny Chen01acfa72011-09-22 18:04:58 +0000930 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000931 }
932 return false;
933}
934
Johnny Chenecd4feb2011-10-14 00:42:25 +0000935// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000936bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000937Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000938{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000939 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chenda5a8022011-09-20 23:28:55 +0000940 if (log)
941 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
942
Johnny Chenecd4feb2011-10-14 00:42:25 +0000943 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000944 {
Jim Ingham9c970a32012-12-18 02:03:49 +0000945 m_watchpoint_list.Remove(watch_id, true);
Johnny Chenda5a8022011-09-20 23:28:55 +0000946 return true;
947 }
948 return false;
949}
950
Johnny Chenecd4feb2011-10-14 00:42:25 +0000951// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000952bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000953Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000954{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000955 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chene14cf4e2011-10-05 21:35:46 +0000956 if (log)
957 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
958
959 if (!ProcessIsValid())
960 return false;
961
Johnny Chenecd4feb2011-10-14 00:42:25 +0000962 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
963 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000964 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000965 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000966 return true;
967 }
968 return false;
969}
970
Chris Lattner24943d22010-06-08 16:52:24 +0000971ModuleSP
972Target::GetExecutableModule ()
973{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000974 return m_images.GetModuleAtIndex(0);
975}
976
977Module*
978Target::GetExecutableModulePointer ()
979{
980 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000981}
982
Enrico Granata146d9522012-11-08 02:22:02 +0000983static void
984LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
985{
986 Error error;
987 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error))
988 {
989 target->GetDebugger().GetOutputStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
990 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
991 error.AsCString());
992 }
993}
994
Chris Lattner24943d22010-06-08 16:52:24 +0000995void
996Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
997{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000998 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Chris Lattner24943d22010-06-08 16:52:24 +0000999 m_images.Clear();
1000 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +00001001 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001002 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001003
1004 if (executable_sp.get())
1005 {
1006 Timer scoped_timer (__PRETTY_FUNCTION__,
1007 "Target::SetExecutableModule (executable = '%s/%s')",
1008 executable_sp->GetFileSpec().GetDirectory().AsCString(),
1009 executable_sp->GetFileSpec().GetFilename().AsCString());
1010
1011 m_images.Append(executable_sp); // The first image is our exectuable file
1012
Jim Ingham7508e732010-08-09 23:31:02 +00001013 // 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 +00001014 if (!m_arch.IsValid())
Jason Molenda8c6cf432012-12-05 00:25:49 +00001015 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001016 m_arch = executable_sp->GetArchitecture();
Jason Molenda8c6cf432012-12-05 00:25:49 +00001017 if (log)
1018 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1019 }
1020
Chris Lattner24943d22010-06-08 16:52:24 +00001021 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001022 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +00001023
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001024 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +00001025 {
1026 executable_objfile->GetDependentModules(dependent_files);
1027 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1028 {
Greg Claytonb1888f22011-03-19 01:12:21 +00001029 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1030 FileSpec platform_dependent_file_spec;
1031 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +00001032 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +00001033 else
1034 platform_dependent_file_spec = dependent_file_spec;
1035
Greg Clayton444fe992012-02-26 05:51:37 +00001036 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1037 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +00001038 if (image_module_sp.get())
1039 {
Chris Lattner24943d22010-06-08 16:52:24 +00001040 ObjectFile *objfile = image_module_sp->GetObjectFile();
1041 if (objfile)
1042 objfile->GetDependentModules(dependent_files);
1043 }
1044 }
1045 }
Chris Lattner24943d22010-06-08 16:52:24 +00001046 }
1047}
1048
1049
Jim Ingham7508e732010-08-09 23:31:02 +00001050bool
1051Target::SetArchitecture (const ArchSpec &arch_spec)
1052{
Greg Clayton952e9dc2013-03-27 23:08:40 +00001053 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callanan40e278c2012-12-13 22:07:14 +00001054 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001055 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001056 // If we haven't got a valid arch spec, or the architectures are
1057 // compatible, so just update the architecture. Architectures can be
1058 // equal, yet the triple OS and vendor might change, so we need to do
1059 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001060 m_arch = arch_spec;
Jason Molenda8c6cf432012-12-05 00:25:49 +00001061 if (log)
1062 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 +00001063 return true;
1064 }
1065 else
1066 {
1067 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molenda8c6cf432012-12-05 00:25:49 +00001068 if (log)
Jason Molendac4d49fd2012-12-05 23:07:34 +00001069 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 +00001070 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001071 ModuleSP executable_sp = GetExecutableModule ();
1072 m_images.Clear();
1073 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001074 m_scratch_ast_source_ap.reset();
1075 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001076 // Need to do something about unsetting breakpoints.
1077
1078 if (executable_sp)
1079 {
Jason Molenda8c6cf432012-12-05 00:25:49 +00001080 if (log)
1081 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 +00001082 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1083 Error error = ModuleList::GetSharedModule (module_spec,
1084 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001085 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001086 NULL,
1087 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001088
1089 if (!error.Fail() && executable_sp)
1090 {
1091 SetExecutableModule (executable_sp, true);
1092 return true;
1093 }
Jim Ingham7508e732010-08-09 23:31:02 +00001094 }
1095 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001096 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001097}
Chris Lattner24943d22010-06-08 16:52:24 +00001098
Chris Lattner24943d22010-06-08 16:52:24 +00001099void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001100Target::WillClearList (const ModuleList& module_list)
Enrico Granata146d9522012-11-08 02:22:02 +00001101{
1102}
1103
1104void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001105Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +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);
Enrico Granata146d9522012-11-08 02:22:02 +00001110 LoadScriptingResourceForModule(module_sp, this);
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001111 ModulesDidLoad (my_module_list);
Chris Lattner24943d22010-06-08 16:52:24 +00001112}
1113
1114void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001115Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata146d9522012-11-08 02:22:02 +00001116{
1117 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001118 ModuleList my_module_list;
1119 my_module_list.Append(module_sp);
1120 ModulesDidUnload (my_module_list);
Enrico Granata146d9522012-11-08 02:22:02 +00001121}
1122
1123void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001124Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001125{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001126 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001127 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001128}
1129
1130void
1131Target::ModulesDidLoad (ModuleList &module_list)
1132{
Enrico Granata146d9522012-11-08 02:22:02 +00001133 if (module_list.GetSize())
1134 {
1135 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1136 // TODO: make event data that packages up the module_list
1137 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1138 }
Chris Lattner24943d22010-06-08 16:52:24 +00001139}
1140
1141void
Enrico Granata12fbcf52013-04-05 18:49:06 +00001142Target::SymbolsDidLoad (ModuleList &module_list)
1143{
1144 if (module_list.GetSize() == 0)
1145 return;
1146 if (m_process_sp)
1147 {
1148 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1149 if (runtime)
1150 {
1151 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1152 objc_runtime->SymbolsDidLoad(module_list);
1153 }
1154 }
1155 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
1156}
1157
1158void
Chris Lattner24943d22010-06-08 16:52:24 +00001159Target::ModulesDidUnload (ModuleList &module_list)
1160{
Enrico Granata146d9522012-11-08 02:22:02 +00001161 if (module_list.GetSize())
1162 {
1163 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1164 // TODO: make event data that packages up the module_list
1165 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1166 }
Chris Lattner24943d22010-06-08 16:52:24 +00001167}
1168
Daniel Dunbar705a0982011-10-31 22:50:37 +00001169bool
Greg Clayton444fe992012-02-26 05:51:37 +00001170Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001171{
Greg Clayton73844aa2012-08-22 17:17:09 +00001172 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001173 {
1174 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001175 ModuleSpec module_spec (module_file_spec);
1176 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001177
1178 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1179 // black list.
1180 if (num_modules > 0)
1181 {
1182 for (int i = 0; i < num_modules; i++)
1183 {
1184 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1185 return false;
1186 }
1187 return true;
1188 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001189 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001190 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001191}
1192
Daniel Dunbar705a0982011-10-31 22:50:37 +00001193bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001194Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1195{
Greg Clayton73844aa2012-08-22 17:17:09 +00001196 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001197 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001198 if (m_platform_sp)
1199 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001200 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001201 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001202}
1203
Chris Lattner24943d22010-06-08 16:52:24 +00001204size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001205Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1206{
Greg Clayton3508c382012-02-24 01:59:29 +00001207 SectionSP section_sp (addr.GetSection());
1208 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001209 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001210 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1211 if (section_sp->IsEncrypted())
1212 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001213 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001214 return 0;
1215 }
Greg Clayton3508c382012-02-24 01:59:29 +00001216 ModuleSP module_sp (section_sp->GetModule());
1217 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001218 {
Greg Clayton3508c382012-02-24 01:59:29 +00001219 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1220 if (objfile)
1221 {
1222 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1223 addr.GetOffset(),
1224 dst,
1225 dst_len);
1226 if (bytes_read > 0)
1227 return bytes_read;
1228 else
1229 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1230 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001231 else
Greg Clayton3508c382012-02-24 01:59:29 +00001232 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001233 }
1234 else
Greg Clayton3508c382012-02-24 01:59:29 +00001235 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001236 }
1237 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001238 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001239
Greg Clayton26100dc2011-01-07 01:57:07 +00001240 return 0;
1241}
1242
1243size_t
Enrico Granata91544802011-09-06 19:20:51 +00001244Target::ReadMemory (const Address& addr,
1245 bool prefer_file_cache,
1246 void *dst,
1247 size_t dst_len,
1248 Error &error,
1249 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001250{
Chris Lattner24943d22010-06-08 16:52:24 +00001251 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001252
Enrico Granata91544802011-09-06 19:20:51 +00001253 // if we end up reading this from process memory, we will fill this
1254 // with the actual load address
1255 if (load_addr_ptr)
1256 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1257
Greg Clayton26100dc2011-01-07 01:57:07 +00001258 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001259
1260 addr_t load_addr = LLDB_INVALID_ADDRESS;
1261 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001262 Address resolved_addr;
1263 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001264 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001265 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001266 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001267 // No sections are loaded, so we must assume we are not running
1268 // yet and anything we are given is a file address.
1269 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1270 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001271 }
Greg Clayton70436352010-06-30 23:03:03 +00001272 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001273 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001274 // We have at least one section loaded. This can be becuase
1275 // we have manually loaded some sections with "target modules load ..."
1276 // or because we have have a live process that has sections loaded
1277 // through the dynamic loader
1278 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1279 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001280 }
Greg Clayton70436352010-06-30 23:03:03 +00001281 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001282 if (!resolved_addr.IsValid())
1283 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001284
Greg Clayton9b82f862011-07-11 05:12:02 +00001285
Greg Clayton26100dc2011-01-07 01:57:07 +00001286 if (prefer_file_cache)
1287 {
1288 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1289 if (bytes_read > 0)
1290 return bytes_read;
1291 }
Greg Clayton70436352010-06-30 23:03:03 +00001292
Johnny Chenda5a8022011-09-20 23:28:55 +00001293 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001294 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001295 if (load_addr == LLDB_INVALID_ADDRESS)
1296 load_addr = resolved_addr.GetLoadAddress (this);
1297
Greg Clayton70436352010-06-30 23:03:03 +00001298 if (load_addr == LLDB_INVALID_ADDRESS)
1299 {
Greg Clayton3508c382012-02-24 01:59:29 +00001300 ModuleSP addr_module_sp (resolved_addr.GetModule());
1301 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001302 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001303 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001304 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001305 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001306 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001307 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001308 }
1309 else
1310 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001311 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001312 if (bytes_read != dst_len)
1313 {
1314 if (error.Success())
1315 {
1316 if (bytes_read == 0)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001317 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001318 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001319 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 +00001320 }
1321 }
Greg Clayton70436352010-06-30 23:03:03 +00001322 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001323 {
1324 if (load_addr_ptr)
1325 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001326 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001327 }
Greg Clayton70436352010-06-30 23:03:03 +00001328 // If the address is not section offset we have an address that
1329 // doesn't resolve to any address in any currently loaded shared
1330 // libaries and we failed to read memory so there isn't anything
1331 // more we can do. If it is section offset, we might be able to
1332 // read cached memory from the object file.
1333 if (!resolved_addr.IsSectionOffset())
1334 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001335 }
Chris Lattner24943d22010-06-08 16:52:24 +00001336 }
Greg Clayton70436352010-06-30 23:03:03 +00001337
Greg Clayton9b82f862011-07-11 05:12:02 +00001338 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001339 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001340 // If we didn't already try and read from the object file cache, then
1341 // try it after failing to read from the process.
1342 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001343 }
1344 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001345}
1346
Greg Clayton7dd98df2011-07-12 17:06:17 +00001347size_t
Enrico Granata45358912013-01-21 19:20:50 +00001348Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1349{
1350 char buf[256];
1351 out_str.clear();
1352 addr_t curr_addr = addr.GetLoadAddress(this);
1353 Address address(addr);
1354 while (1)
1355 {
1356 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1357 if (length == 0)
1358 break;
1359 out_str.append(buf, length);
1360 // If we got "length - 1" bytes, we didn't get the whole C string, we
1361 // need to read some more characters
1362 if (length == sizeof(buf) - 1)
1363 curr_addr += length;
1364 else
1365 break;
1366 address = Address(curr_addr);
1367 }
1368 return out_str.size();
1369}
1370
1371
1372size_t
1373Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1374{
1375 size_t total_cstr_len = 0;
1376 if (dst && dst_max_len)
1377 {
1378 result_error.Clear();
1379 // NULL out everything just to be safe
1380 memset (dst, 0, dst_max_len);
1381 Error error;
1382 addr_t curr_addr = addr.GetLoadAddress(this);
1383 Address address(addr);
1384 const size_t cache_line_size = 512;
1385 size_t bytes_left = dst_max_len - 1;
1386 char *curr_dst = dst;
1387
1388 while (bytes_left > 0)
1389 {
1390 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1391 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1392 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1393
1394 if (bytes_read == 0)
1395 {
1396 result_error = error;
1397 dst[total_cstr_len] = '\0';
1398 break;
1399 }
1400 const size_t len = strlen(curr_dst);
1401
1402 total_cstr_len += len;
1403
1404 if (len < bytes_to_read)
1405 break;
1406
1407 curr_dst += bytes_read;
1408 curr_addr += bytes_read;
1409 bytes_left -= bytes_read;
1410 address = Address(curr_addr);
1411 }
1412 }
1413 else
1414 {
1415 if (dst == NULL)
1416 result_error.SetErrorString("invalid arguments");
1417 else
1418 result_error.Clear();
1419 }
1420 return total_cstr_len;
1421}
1422
1423size_t
Greg Clayton7dd98df2011-07-12 17:06:17 +00001424Target::ReadScalarIntegerFromMemory (const Address& addr,
1425 bool prefer_file_cache,
1426 uint32_t byte_size,
1427 bool is_signed,
1428 Scalar &scalar,
1429 Error &error)
1430{
1431 uint64_t uval;
1432
1433 if (byte_size <= sizeof(uval))
1434 {
1435 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1436 if (bytes_read == byte_size)
1437 {
1438 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Clayton36da2aa2013-01-25 18:06:21 +00001439 lldb::offset_t offset = 0;
Greg Clayton7dd98df2011-07-12 17:06:17 +00001440 if (byte_size <= 4)
1441 scalar = data.GetMaxU32 (&offset, byte_size);
1442 else
1443 scalar = data.GetMaxU64 (&offset, byte_size);
1444
1445 if (is_signed)
1446 scalar.SignExtend(byte_size * 8);
1447 return bytes_read;
1448 }
1449 }
1450 else
1451 {
1452 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1453 }
1454 return 0;
1455}
1456
1457uint64_t
1458Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1459 bool prefer_file_cache,
1460 size_t integer_byte_size,
1461 uint64_t fail_value,
1462 Error &error)
1463{
1464 Scalar scalar;
1465 if (ReadScalarIntegerFromMemory (addr,
1466 prefer_file_cache,
1467 integer_byte_size,
1468 false,
1469 scalar,
1470 error))
1471 return scalar.ULongLong(fail_value);
1472 return fail_value;
1473}
1474
1475bool
1476Target::ReadPointerFromMemory (const Address& addr,
1477 bool prefer_file_cache,
1478 Error &error,
1479 Address &pointer_addr)
1480{
1481 Scalar scalar;
1482 if (ReadScalarIntegerFromMemory (addr,
1483 prefer_file_cache,
1484 m_arch.GetAddressByteSize(),
1485 false,
1486 scalar,
1487 error))
1488 {
1489 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1490 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1491 {
1492 if (m_section_load_list.IsEmpty())
1493 {
1494 // No sections are loaded, so we must assume we are not running
1495 // yet and anything we are given is a file address.
1496 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1497 }
1498 else
1499 {
1500 // We have at least one section loaded. This can be becuase
1501 // we have manually loaded some sections with "target modules load ..."
1502 // or because we have have a live process that has sections loaded
1503 // through the dynamic loader
1504 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1505 }
1506 // We weren't able to resolve the pointer value, so just return
1507 // an address with no section
1508 if (!pointer_addr.IsValid())
1509 pointer_addr.SetOffset (pointer_vm_addr);
1510 return true;
1511
1512 }
1513 }
1514 return false;
1515}
Chris Lattner24943d22010-06-08 16:52:24 +00001516
1517ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001518Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001519{
Chris Lattner24943d22010-06-08 16:52:24 +00001520 ModuleSP module_sp;
1521
Chris Lattner24943d22010-06-08 16:52:24 +00001522 Error error;
1523
Jim Ingham03e5e512012-05-17 18:38:42 +00001524 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1525 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001526
Jim Ingham03e5e512012-05-17 18:38:42 +00001527 if (module_spec.GetUUID().IsValid())
1528 module_sp = m_images.FindFirstModule(module_spec);
1529
Greg Claytone1ef1e32012-04-27 00:58:27 +00001530 if (!module_sp)
1531 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001532 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1533 bool did_create_module = false;
1534
1535 // If there are image search path entries, try to use them first to acquire a suitable image.
1536 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001537 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001538 ModuleSpec transformed_spec (module_spec);
1539 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1540 {
1541 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1542 error = ModuleList::GetSharedModule (transformed_spec,
1543 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001544 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001545 &old_module_sp,
1546 &did_create_module);
1547 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001548 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001549
Greg Claytone1ef1e32012-04-27 00:58:27 +00001550 if (!module_sp)
1551 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001552 // If we have a UUID, we can check our global shared module list in case
1553 // we already have it. If we don't have a valid UUID, then we can't since
1554 // the path in "module_spec" will be a platform path, and we will need to
1555 // let the platform find that file. For example, we could be asking for
1556 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1557 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1558 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1559 // cache.
1560 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001561 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001562 // We have a UUID, it is OK to check the global module list...
1563 error = ModuleList::GetSharedModule (module_spec,
1564 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001565 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001566 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001567 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001568 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001569
1570 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001571 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001572 // The platform is responsible for finding and caching an appropriate
1573 // module in the shared module cache.
1574 if (m_platform_sp)
1575 {
1576 FileSpec platform_file_spec;
1577 error = m_platform_sp->GetSharedModule (module_spec,
1578 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001579 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001580 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001581 &did_create_module);
1582 }
1583 else
1584 {
1585 error.SetErrorString("no platform is currently set");
1586 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001587 }
1588 }
Chris Lattner24943d22010-06-08 16:52:24 +00001589
Jim Ingham03e5e512012-05-17 18:38:42 +00001590 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1591 // module in the list already, and if there was, let's remove it.
1592 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001593 {
Greg Clayton1649a722012-11-29 22:16:27 +00001594 ObjectFile *objfile = module_sp->GetObjectFile();
1595 if (objfile)
Jim Ingham03e5e512012-05-17 18:38:42 +00001596 {
Greg Clayton1649a722012-11-29 22:16:27 +00001597 switch (objfile->GetType())
Jim Ingham03e5e512012-05-17 18:38:42 +00001598 {
Greg Clayton1649a722012-11-29 22:16:27 +00001599 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1600 case ObjectFile::eTypeExecutable: /// A normal executable
1601 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1602 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1603 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1604 break;
1605 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1606 if (error_ptr)
1607 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1608 return ModuleSP();
1609 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1610 if (error_ptr)
1611 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1612 return ModuleSP();
1613 default:
1614 if (error_ptr)
1615 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1616 return ModuleSP();
1617 }
1618 // GetSharedModule is not guaranteed to find the old shared module, for instance
1619 // in the common case where you pass in the UUID, it is only going to find the one
1620 // module matching the UUID. In fact, it has no good way to know what the "old module"
1621 // relevant to this target is, since there might be many copies of a module with this file spec
1622 // in various running debug sessions, but only one of them will belong to this target.
1623 // So let's remove the UUID from the module list, and look in the target's module list.
1624 // Only do this if there is SOMETHING else in the module spec...
1625 if (!old_module_sp)
1626 {
1627 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham03e5e512012-05-17 18:38:42 +00001628 {
Greg Clayton1649a722012-11-29 22:16:27 +00001629 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1630 module_spec_copy.GetUUID().Clear();
1631
1632 ModuleList found_modules;
1633 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1634 if (num_found == 1)
1635 {
1636 old_module_sp = found_modules.GetModuleAtIndex(0);
1637 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001638 }
1639 }
Greg Clayton1649a722012-11-29 22:16:27 +00001640
1641 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1642 {
1643 m_images.ReplaceModule(old_module_sp, module_sp);
1644 Module *old_module_ptr = old_module_sp.get();
1645 old_module_sp.reset();
1646 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1647 }
1648 else
1649 m_images.Append(module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001650 }
Chris Lattner24943d22010-06-08 16:52:24 +00001651 }
1652 }
1653 if (error_ptr)
1654 *error_ptr = error;
1655 return module_sp;
1656}
1657
1658
Greg Clayton289afcb2012-02-18 05:35:26 +00001659TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001660Target::CalculateTarget ()
1661{
Greg Clayton289afcb2012-02-18 05:35:26 +00001662 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001663}
1664
Greg Clayton289afcb2012-02-18 05:35:26 +00001665ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001666Target::CalculateProcess ()
1667{
Greg Clayton289afcb2012-02-18 05:35:26 +00001668 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001669}
1670
Greg Clayton289afcb2012-02-18 05:35:26 +00001671ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001672Target::CalculateThread ()
1673{
Greg Clayton289afcb2012-02-18 05:35:26 +00001674 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001675}
1676
Greg Clayton289afcb2012-02-18 05:35:26 +00001677StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001678Target::CalculateStackFrame ()
1679{
Greg Clayton289afcb2012-02-18 05:35:26 +00001680 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001681}
1682
1683void
Greg Claytona830adb2010-10-04 01:05:56 +00001684Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001685{
Greg Clayton567e7f32011-09-22 04:58:26 +00001686 exe_ctx.Clear();
1687 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001688}
1689
1690PathMappingList &
1691Target::GetImageSearchPathList ()
1692{
1693 return m_image_search_paths;
1694}
1695
1696void
1697Target::ImageSearchPathsChanged
1698(
1699 const PathMappingList &path_list,
1700 void *baton
1701)
1702{
1703 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001704 ModuleSP exe_module_sp (target->GetExecutableModule());
1705 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001706 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001707 target->m_images.Clear();
1708 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001709 }
1710}
1711
1712ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001713Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001714{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001715 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001716 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001717 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001718 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001719 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001720 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1721 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1722 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1723 }
Chris Lattner24943d22010-06-08 16:52:24 +00001724 return m_scratch_ast_context_ap.get();
1725}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001726
Sean Callanan4938bd62011-11-16 18:20:47 +00001727ClangASTImporter *
1728Target::GetClangASTImporter()
1729{
1730 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1731
1732 if (!ast_importer)
1733 {
1734 ast_importer = new ClangASTImporter();
1735 m_ast_importer_ap.reset(ast_importer);
1736 }
1737
1738 return ast_importer;
1739}
1740
Greg Clayton990de7b2010-11-18 23:32:35 +00001741void
Caroline Tice2a456812011-03-10 22:14:10 +00001742Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001743{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001744 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001745}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001746
Greg Clayton990de7b2010-11-18 23:32:35 +00001747void
Caroline Tice2a456812011-03-10 22:14:10 +00001748Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001749{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001750 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001751}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001752
Greg Clayton9ce95382012-02-13 23:10:39 +00001753FileSpecList
1754Target::GetDefaultExecutableSearchPaths ()
1755{
Greg Clayton73844aa2012-08-22 17:17:09 +00001756 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1757 if (properties_sp)
1758 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001759 return FileSpecList();
1760}
1761
Caroline Tice5bc8c972010-09-20 20:44:43 +00001762ArchSpec
1763Target::GetDefaultArchitecture ()
1764{
Greg Clayton73844aa2012-08-22 17:17:09 +00001765 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1766 if (properties_sp)
1767 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001768 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001769}
1770
1771void
Greg Clayton73844aa2012-08-22 17:17:09 +00001772Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001773{
Greg Clayton73844aa2012-08-22 17:17:09 +00001774 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1775 if (properties_sp)
Jason Molenda332dc002012-12-12 02:23:56 +00001776 {
1777 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 +00001778 return properties_sp->SetDefaultArchitecture(arch);
Jason Molenda332dc002012-12-12 02:23:56 +00001779 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001780}
1781
Greg Claytona830adb2010-10-04 01:05:56 +00001782Target *
1783Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1784{
1785 // The target can either exist in the "process" of ExecutionContext, or in
1786 // the "target_sp" member of SymbolContext. This accessor helper function
1787 // will get the target from one of these locations.
1788
1789 Target *target = NULL;
1790 if (sc_ptr != NULL)
1791 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001792 if (target == NULL && exe_ctx_ptr)
1793 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001794 return target;
1795}
1796
Greg Clayton427f2902010-12-14 02:59:59 +00001797ExecutionResults
1798Target::EvaluateExpression
1799(
1800 const char *expr_cstr,
1801 StackFrame *frame,
Enrico Granata6cca9692012-07-16 23:10:35 +00001802 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001803 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001804)
1805{
Enrico Granata3a08fd12012-09-18 17:43:16 +00001806 result_valobj_sp.reset();
1807
Greg Clayton427f2902010-12-14 02:59:59 +00001808 ExecutionResults execution_results = eExecutionSetupError;
1809
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001810 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1811 return execution_results;
1812
Jim Ingham3613ae12011-05-12 02:06:14 +00001813 // We shouldn't run stop hooks in expressions.
1814 // Be sure to reset this if you return anywhere within this function.
1815 bool old_suppress_value = m_suppress_stop_hooks;
1816 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001817
1818 ExecutionContext exe_ctx;
Sean Callanan4ab92782013-01-12 02:04:23 +00001819
Greg Clayton427f2902010-12-14 02:59:59 +00001820 if (frame)
1821 {
1822 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton427f2902010-12-14 02:59:59 +00001823 }
1824 else if (m_process_sp)
1825 {
1826 m_process_sp->CalculateExecutionContext(exe_ctx);
1827 }
1828 else
1829 {
1830 CalculateExecutionContext(exe_ctx);
1831 }
1832
Sean Callanan4ab92782013-01-12 02:04:23 +00001833 // Make sure we aren't just trying to see the value of a persistent
1834 // variable (something like "$0")
1835 lldb::ClangExpressionVariableSP persistent_var_sp;
1836 // Only check for persistent variables the expression starts with a '$'
1837 if (expr_cstr[0] == '$')
1838 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1839
1840 if (persistent_var_sp)
Greg Clayton427f2902010-12-14 02:59:59 +00001841 {
Sean Callanan4ab92782013-01-12 02:04:23 +00001842 result_valobj_sp = persistent_var_sp->GetValueObject ();
Greg Clayton427f2902010-12-14 02:59:59 +00001843 execution_results = eExecutionCompleted;
Greg Clayton427f2902010-12-14 02:59:59 +00001844 }
1845 else
1846 {
Sean Callanan4ab92782013-01-12 02:04:23 +00001847 const char *prefix = GetExpressionPrefixContentsAsCString();
1848
1849 execution_results = ClangUserExpression::Evaluate (exe_ctx,
1850 options.GetExecutionPolicy(),
1851 lldb::eLanguageTypeUnknown,
1852 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1853 options.DoesUnwindOnError(),
Jim Inghamb7940202013-01-15 02:47:48 +00001854 options.DoesIgnoreBreakpoints(),
Sean Callanan4ab92782013-01-12 02:04:23 +00001855 expr_cstr,
1856 prefix,
1857 result_valobj_sp,
1858 options.GetRunOthers(),
1859 options.GetTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001860 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001861
1862 m_suppress_stop_hooks = old_suppress_value;
1863
Greg Clayton427f2902010-12-14 02:59:59 +00001864 return execution_results;
1865}
1866
Greg Claytonc0fa5332011-05-22 22:46:53 +00001867lldb::addr_t
1868Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1869{
1870 addr_t code_addr = load_addr;
1871 switch (m_arch.GetMachine())
1872 {
1873 case llvm::Triple::arm:
1874 case llvm::Triple::thumb:
1875 switch (addr_class)
1876 {
1877 case eAddressClassData:
1878 case eAddressClassDebug:
1879 return LLDB_INVALID_ADDRESS;
1880
1881 case eAddressClassUnknown:
1882 case eAddressClassInvalid:
1883 case eAddressClassCode:
1884 case eAddressClassCodeAlternateISA:
1885 case eAddressClassRuntime:
1886 // Check if bit zero it no set?
1887 if ((code_addr & 1ull) == 0)
1888 {
1889 // Bit zero isn't set, check if the address is a multiple of 2?
1890 if (code_addr & 2ull)
1891 {
1892 // The address is a multiple of 2 so it must be thumb, set bit zero
1893 code_addr |= 1ull;
1894 }
1895 else if (addr_class == eAddressClassCodeAlternateISA)
1896 {
1897 // We checked the address and the address claims to be the alternate ISA
1898 // which means thumb, so set bit zero.
1899 code_addr |= 1ull;
1900 }
1901 }
1902 break;
1903 }
1904 break;
1905
1906 default:
1907 break;
1908 }
1909 return code_addr;
1910}
1911
1912lldb::addr_t
1913Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1914{
1915 addr_t opcode_addr = load_addr;
1916 switch (m_arch.GetMachine())
1917 {
1918 case llvm::Triple::arm:
1919 case llvm::Triple::thumb:
1920 switch (addr_class)
1921 {
1922 case eAddressClassData:
1923 case eAddressClassDebug:
1924 return LLDB_INVALID_ADDRESS;
1925
1926 case eAddressClassInvalid:
1927 case eAddressClassUnknown:
1928 case eAddressClassCode:
1929 case eAddressClassCodeAlternateISA:
1930 case eAddressClassRuntime:
1931 opcode_addr &= ~(1ull);
1932 break;
1933 }
1934 break;
1935
1936 default:
1937 break;
1938 }
1939 return opcode_addr;
1940}
1941
Greg Clayton535f53c2013-03-19 00:20:55 +00001942SourceManager &
1943Target::GetSourceManager ()
1944{
1945 if (m_source_manager_ap.get() == NULL)
1946 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
1947 return *m_source_manager_ap;
1948}
1949
1950
Jim Inghamd60d94a2011-03-11 03:53:59 +00001951lldb::user_id_t
1952Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1953{
1954 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001955 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001956 m_stop_hooks[new_uid] = new_hook_sp;
1957 return new_uid;
1958}
1959
1960bool
1961Target::RemoveStopHookByID (lldb::user_id_t user_id)
1962{
1963 size_t num_removed;
1964 num_removed = m_stop_hooks.erase (user_id);
1965 if (num_removed == 0)
1966 return false;
1967 else
1968 return true;
1969}
1970
1971void
1972Target::RemoveAllStopHooks ()
1973{
1974 m_stop_hooks.clear();
1975}
1976
1977Target::StopHookSP
1978Target::GetStopHookByID (lldb::user_id_t user_id)
1979{
1980 StopHookSP found_hook;
1981
1982 StopHookCollection::iterator specified_hook_iter;
1983 specified_hook_iter = m_stop_hooks.find (user_id);
1984 if (specified_hook_iter != m_stop_hooks.end())
1985 found_hook = (*specified_hook_iter).second;
1986 return found_hook;
1987}
1988
1989bool
1990Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1991{
1992 StopHookCollection::iterator specified_hook_iter;
1993 specified_hook_iter = m_stop_hooks.find (user_id);
1994 if (specified_hook_iter == m_stop_hooks.end())
1995 return false;
1996
1997 (*specified_hook_iter).second->SetIsActive (active_state);
1998 return true;
1999}
2000
2001void
2002Target::SetAllStopHooksActiveState (bool active_state)
2003{
2004 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2005 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2006 {
2007 (*pos).second->SetIsActive (active_state);
2008 }
2009}
2010
2011void
2012Target::RunStopHooks ()
2013{
Jim Ingham3613ae12011-05-12 02:06:14 +00002014 if (m_suppress_stop_hooks)
2015 return;
2016
Jim Inghamd60d94a2011-03-11 03:53:59 +00002017 if (!m_process_sp)
2018 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00002019
2020 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2021 // since in that case we do not want to run the stop-hooks
2022 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2023 return;
2024
Jim Inghamd60d94a2011-03-11 03:53:59 +00002025 if (m_stop_hooks.empty())
2026 return;
2027
2028 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2029
2030 // If there aren't any active stop hooks, don't bother either:
2031 bool any_active_hooks = false;
2032 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2033 {
2034 if ((*pos).second->IsActive())
2035 {
2036 any_active_hooks = true;
2037 break;
2038 }
2039 }
2040 if (!any_active_hooks)
2041 return;
2042
2043 CommandReturnObject result;
2044
2045 std::vector<ExecutionContext> exc_ctx_with_reasons;
2046 std::vector<SymbolContext> sym_ctx_with_reasons;
2047
2048 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2049 size_t num_threads = cur_threadlist.GetSize();
2050 for (size_t i = 0; i < num_threads; i++)
2051 {
2052 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2053 if (cur_thread_sp->ThreadStoppedForAReason())
2054 {
2055 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2056 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2057 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2058 }
2059 }
2060
2061 // If no threads stopped for a reason, don't run the stop-hooks.
2062 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2063 if (num_exe_ctx == 0)
2064 return;
2065
Jim Inghame5ed8e92011-06-02 23:58:26 +00002066 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2067 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002068
2069 bool keep_going = true;
2070 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00002071 bool print_hook_header;
2072 bool print_thread_header;
2073
2074 if (num_exe_ctx == 1)
2075 print_thread_header = false;
2076 else
2077 print_thread_header = true;
2078
2079 if (m_stop_hooks.size() == 1)
2080 print_hook_header = false;
2081 else
2082 print_hook_header = true;
2083
Jim Inghamd60d94a2011-03-11 03:53:59 +00002084 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2085 {
2086 // result.Clear();
2087 StopHookSP cur_hook_sp = (*pos).second;
2088 if (!cur_hook_sp->IsActive())
2089 continue;
2090
2091 bool any_thread_matched = false;
2092 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2093 {
2094 if ((cur_hook_sp->GetSpecifier () == NULL
2095 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2096 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00002097 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002098 {
2099 if (!hooks_ran)
2100 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00002101 hooks_ran = true;
2102 }
Jim Inghamc54840c2011-03-22 01:47:27 +00002103 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002104 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002105 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2106 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2107 NULL);
2108 if (cmd)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002109 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chen4d96a742011-10-24 23:01:06 +00002110 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002111 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002112 any_thread_matched = true;
2113 }
2114
Jim Inghamc54840c2011-03-22 01:47:27 +00002115 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002116 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002117
2118 bool stop_on_continue = true;
2119 bool stop_on_error = true;
2120 bool echo_commands = false;
2121 bool print_results = true;
2122 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002123 &exc_ctx_with_reasons[i],
2124 stop_on_continue,
2125 stop_on_error,
2126 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002127 print_results,
2128 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002129 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002130
2131 // If the command started the target going again, we should bag out of
2132 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002133 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2134 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002135 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002136 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002137 keep_going = false;
2138 }
2139 }
2140 }
2141 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002142
Caroline Tice4a348082011-05-02 20:41:46 +00002143 result.GetImmediateOutputStream()->Flush();
2144 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002145}
2146
Greg Claytonbbea1332011-07-08 00:48:09 +00002147
Jim Inghamd60d94a2011-03-11 03:53:59 +00002148//--------------------------------------------------------------
2149// class Target::StopHook
2150//--------------------------------------------------------------
2151
2152
2153Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2154 UserID (uid),
2155 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002156 m_commands (),
2157 m_specifier_sp (),
Greg Clayton81a96aa2013-04-18 18:10:51 +00002158 m_thread_spec_ap(),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002159 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002160{
2161}
2162
2163Target::StopHook::StopHook (const StopHook &rhs) :
2164 UserID (rhs.GetID()),
2165 m_target_sp (rhs.m_target_sp),
2166 m_commands (rhs.m_commands),
2167 m_specifier_sp (rhs.m_specifier_sp),
Greg Clayton81a96aa2013-04-18 18:10:51 +00002168 m_thread_spec_ap (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002169 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002170{
2171 if (rhs.m_thread_spec_ap.get() != NULL)
2172 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2173}
2174
2175
2176Target::StopHook::~StopHook ()
2177{
2178}
2179
2180void
2181Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2182{
2183 m_thread_spec_ap.reset (specifier);
2184}
2185
2186
2187void
2188Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2189{
2190 int indent_level = s->GetIndentLevel();
2191
2192 s->SetIndentLevel(indent_level + 2);
2193
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002194 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002195 if (m_active)
2196 s->Indent ("State: enabled\n");
2197 else
2198 s->Indent ("State: disabled\n");
2199
2200 if (m_specifier_sp)
2201 {
2202 s->Indent();
2203 s->PutCString ("Specifier:\n");
2204 s->SetIndentLevel (indent_level + 4);
2205 m_specifier_sp->GetDescription (s, level);
2206 s->SetIndentLevel (indent_level + 2);
2207 }
2208
2209 if (m_thread_spec_ap.get() != NULL)
2210 {
2211 StreamString tmp;
2212 s->Indent("Thread:\n");
2213 m_thread_spec_ap->GetDescription (&tmp, level);
2214 s->SetIndentLevel (indent_level + 4);
2215 s->Indent (tmp.GetData());
2216 s->PutCString ("\n");
2217 s->SetIndentLevel (indent_level + 2);
2218 }
2219
2220 s->Indent ("Commands: \n");
2221 s->SetIndentLevel (indent_level + 4);
2222 uint32_t num_commands = m_commands.GetSize();
2223 for (uint32_t i = 0; i < num_commands; i++)
2224 {
2225 s->Indent(m_commands.GetStringAtIndex(i));
2226 s->PutCString ("\n");
2227 }
2228 s->SetIndentLevel (indent_level);
2229}
2230
Greg Clayton73844aa2012-08-22 17:17:09 +00002231//--------------------------------------------------------------
2232// class TargetProperties
2233//--------------------------------------------------------------
2234
2235OptionEnumValueElement
2236lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002237{
Greg Clayton73844aa2012-08-22 17:17:09 +00002238 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2239 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2240 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2241 { 0, NULL, NULL }
2242};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002243
Greg Clayton49ce8962012-08-29 21:13:06 +00002244static OptionEnumValueElement
2245g_inline_breakpoint_enums[] =
2246{
2247 { 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."},
2248 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2249 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2250 { 0, NULL, NULL }
2251};
2252
Jim Ingham7d408382013-03-02 00:26:47 +00002253typedef enum x86DisassemblyFlavor
2254{
2255 eX86DisFlavorDefault,
2256 eX86DisFlavorIntel,
2257 eX86DisFlavorATT
2258} x86DisassemblyFlavor;
2259
2260static OptionEnumValueElement
2261g_x86_dis_flavor_value_types[] =
2262{
2263 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2264 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2265 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2266 { 0, NULL, NULL }
2267};
2268
Greg Clayton73844aa2012-08-22 17:17:09 +00002269static PropertyDefinition
2270g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002271{
Greg Clayton73844aa2012-08-22 17:17:09 +00002272 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2273 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2274 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2275 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2276 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2277 { "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 "
2278 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2279 "some part (starting at the root) of the path to the file when it was built, "
2280 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2281 "Each element of the array is checked in order and the first one that results in a match wins." },
2282 { "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." },
2283 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2284 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2285 { "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 +00002286 { "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." },
2287 { "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 +00002288 { "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." },
2289 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2290 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2291 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2292 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2293 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2294 { "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 +00002295 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2296 "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. "
2297 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2298 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2299 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2300 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2301 "file and line breakpoints." },
Jim Ingham7d408382013-03-02 00:26:47 +00002302 // FIXME: This is the wrong way to do per-architecture settings, but we don't have a general per architecture settings system in place yet.
2303 { "x86-disassembly-flavor" , OptionValue::eTypeEnum , false, eX86DisFlavorDefault, NULL, g_x86_dis_flavor_value_types, "The default disassembly flavor to use for x86 or x86-64 targets." },
Jim Ingham4aac0962013-04-04 01:38:54 +00002304 { "use-fast-stepping" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Use a fast stepping algorithm based on running from branch to branch rather than instruction single-stepping." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002305 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2306};
2307enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002308{
Greg Clayton73844aa2012-08-22 17:17:09 +00002309 ePropertyDefaultArch,
2310 ePropertyExprPrefix,
2311 ePropertyPreferDynamic,
2312 ePropertyEnableSynthetic,
2313 ePropertySkipPrologue,
2314 ePropertySourceMap,
2315 ePropertyExecutableSearchPaths,
2316 ePropertyMaxChildrenCount,
2317 ePropertyMaxSummaryLength,
2318 ePropertyBreakpointUseAvoidList,
Greg Clayton0c8446c2012-10-17 22:57:12 +00002319 ePropertyArg0,
Greg Clayton73844aa2012-08-22 17:17:09 +00002320 ePropertyRunArgs,
2321 ePropertyEnvVars,
2322 ePropertyInheritEnv,
2323 ePropertyInputPath,
2324 ePropertyOutputPath,
2325 ePropertyErrorPath,
2326 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002327 ePropertyDisableSTDIO,
Jim Ingham7d408382013-03-02 00:26:47 +00002328 ePropertyInlineStrategy,
Jim Ingham5e0e3722013-03-13 17:58:04 +00002329 ePropertyDisassemblyFlavor,
2330 ePropertyUseFastStepping
Greg Clayton73844aa2012-08-22 17:17:09 +00002331};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002332
Caroline Tice5bc8c972010-09-20 20:44:43 +00002333
Greg Clayton73844aa2012-08-22 17:17:09 +00002334class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002335{
Greg Clayton73844aa2012-08-22 17:17:09 +00002336public:
2337 TargetOptionValueProperties (const ConstString &name) :
2338 OptionValueProperties (name),
2339 m_target (NULL),
2340 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002341 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002342 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002343
Greg Clayton73844aa2012-08-22 17:17:09 +00002344 // This constructor is used when creating TargetOptionValueProperties when it
2345 // is part of a new lldb_private::Target instance. It will copy all current
2346 // global property values as needed
2347 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2348 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2349 m_target (target),
2350 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002351 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002352 }
2353
2354 virtual const Property *
2355 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2356 {
2357 // When gettings the value for a key from the target options, we will always
2358 // try and grab the setting from the current target if there is one. Else we just
2359 // use the one from this instance.
2360 if (idx == ePropertyEnvVars)
2361 GetHostEnvironmentIfNeeded ();
2362
2363 if (exe_ctx)
2364 {
2365 Target *target = exe_ctx->GetTargetPtr();
2366 if (target)
2367 {
2368 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2369 if (this != target_properties)
2370 return target_properties->ProtectedGetPropertyAtIndex (idx);
2371 }
2372 }
2373 return ProtectedGetPropertyAtIndex (idx);
2374 }
2375protected:
2376
2377 void
2378 GetHostEnvironmentIfNeeded () const
2379 {
2380 if (!m_got_host_env)
2381 {
2382 if (m_target)
2383 {
2384 m_got_host_env = true;
2385 const uint32_t idx = ePropertyInheritEnv;
2386 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2387 {
2388 PlatformSP platform_sp (m_target->GetPlatform());
2389 if (platform_sp)
2390 {
2391 StringList env;
2392 if (platform_sp->GetEnvironment(env))
2393 {
2394 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2395 if (env_dict)
2396 {
2397 const bool can_replace = false;
2398 const size_t envc = env.GetSize();
2399 for (size_t idx=0; idx<envc; idx++)
2400 {
2401 const char *env_entry = env.GetStringAtIndex (idx);
2402 if (env_entry)
2403 {
2404 const char *equal_pos = ::strchr(env_entry, '=');
2405 ConstString key;
2406 // It is ok to have environment variables with no values
2407 const char *value = NULL;
2408 if (equal_pos)
2409 {
2410 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2411 if (equal_pos[1])
2412 value = equal_pos + 1;
2413 }
2414 else
2415 {
2416 key.SetCString(env_entry);
2417 }
2418 // Don't allow existing keys to be replaced with ones we get from the platform environment
2419 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2420 }
2421 }
2422 }
2423 }
2424 }
2425 }
2426 }
2427 }
2428 }
2429 Target *m_target;
2430 mutable bool m_got_host_env;
2431};
2432
2433TargetProperties::TargetProperties (Target *target) :
2434 Properties ()
2435{
2436 if (target)
2437 {
2438 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002439 }
2440 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002441 {
2442 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2443 m_collection_sp->Initialize(g_properties);
2444 m_collection_sp->AppendProperty(ConstString("process"),
2445 ConstString("Settings specify to processes."),
2446 true,
2447 Process::GetGlobalProperties()->GetValueProperties());
2448 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002449}
2450
Greg Clayton73844aa2012-08-22 17:17:09 +00002451TargetProperties::~TargetProperties ()
2452{
2453}
2454ArchSpec
2455TargetProperties::GetDefaultArchitecture () const
2456{
2457 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2458 if (value)
2459 return value->GetCurrentValue();
2460 return ArchSpec();
2461}
2462
2463void
2464TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2465{
2466 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2467 if (value)
2468 return value->SetCurrentValue(arch, true);
2469}
2470
2471lldb::DynamicValueType
2472TargetProperties::GetPreferDynamicValue() const
2473{
2474 const uint32_t idx = ePropertyPreferDynamic;
2475 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2476}
2477
2478bool
2479TargetProperties::GetDisableASLR () const
2480{
2481 const uint32_t idx = ePropertyDisableASLR;
2482 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2483}
2484
2485void
2486TargetProperties::SetDisableASLR (bool b)
2487{
2488 const uint32_t idx = ePropertyDisableASLR;
2489 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2490}
2491
2492bool
2493TargetProperties::GetDisableSTDIO () const
2494{
2495 const uint32_t idx = ePropertyDisableSTDIO;
2496 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2497}
2498
2499void
2500TargetProperties::SetDisableSTDIO (bool b)
2501{
2502 const uint32_t idx = ePropertyDisableSTDIO;
2503 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2504}
2505
Jim Ingham7d408382013-03-02 00:26:47 +00002506const char *
2507TargetProperties::GetDisassemblyFlavor () const
2508{
2509 const uint32_t idx = ePropertyDisassemblyFlavor;
2510 const char *return_value;
2511
2512 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2513 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
2514 return return_value;
2515}
2516
Greg Clayton49ce8962012-08-29 21:13:06 +00002517InlineStrategy
2518TargetProperties::GetInlineStrategy () const
2519{
2520 const uint32_t idx = ePropertyInlineStrategy;
2521 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2522}
2523
Greg Clayton0c8446c2012-10-17 22:57:12 +00002524const char *
2525TargetProperties::GetArg0 () const
2526{
2527 const uint32_t idx = ePropertyArg0;
2528 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2529}
2530
2531void
2532TargetProperties::SetArg0 (const char *arg)
2533{
2534 const uint32_t idx = ePropertyArg0;
2535 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2536}
2537
Greg Clayton73844aa2012-08-22 17:17:09 +00002538bool
2539TargetProperties::GetRunArguments (Args &args) const
2540{
2541 const uint32_t idx = ePropertyRunArgs;
2542 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2543}
2544
2545void
2546TargetProperties::SetRunArguments (const Args &args)
2547{
2548 const uint32_t idx = ePropertyRunArgs;
2549 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2550}
2551
2552size_t
2553TargetProperties::GetEnvironmentAsArgs (Args &env) const
2554{
2555 const uint32_t idx = ePropertyEnvVars;
2556 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2557}
2558
2559bool
2560TargetProperties::GetSkipPrologue() const
2561{
2562 const uint32_t idx = ePropertySkipPrologue;
2563 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2564}
2565
2566PathMappingList &
2567TargetProperties::GetSourcePathMap () const
2568{
2569 const uint32_t idx = ePropertySourceMap;
2570 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2571 assert(option_value);
2572 return option_value->GetCurrentValue();
2573}
2574
2575FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002576TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002577{
2578 const uint32_t idx = ePropertyExecutableSearchPaths;
2579 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2580 assert(option_value);
2581 return option_value->GetCurrentValue();
2582}
2583
2584bool
2585TargetProperties::GetEnableSyntheticValue () const
2586{
2587 const uint32_t idx = ePropertyEnableSynthetic;
2588 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2589}
2590
2591uint32_t
2592TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2593{
2594 const uint32_t idx = ePropertyMaxChildrenCount;
2595 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2596}
2597
2598uint32_t
2599TargetProperties::GetMaximumSizeOfStringSummary() const
2600{
2601 const uint32_t idx = ePropertyMaxSummaryLength;
2602 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2603}
2604
2605FileSpec
2606TargetProperties::GetStandardInputPath () const
2607{
2608 const uint32_t idx = ePropertyInputPath;
2609 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2610}
2611
2612void
2613TargetProperties::SetStandardInputPath (const char *p)
2614{
2615 const uint32_t idx = ePropertyInputPath;
2616 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2617}
2618
2619FileSpec
2620TargetProperties::GetStandardOutputPath () const
2621{
2622 const uint32_t idx = ePropertyOutputPath;
2623 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2624}
2625
2626void
2627TargetProperties::SetStandardOutputPath (const char *p)
2628{
2629 const uint32_t idx = ePropertyOutputPath;
2630 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2631}
2632
2633FileSpec
2634TargetProperties::GetStandardErrorPath () const
2635{
2636 const uint32_t idx = ePropertyErrorPath;
2637 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2638}
2639
Greg Claytonc6e82e42012-08-22 18:39:03 +00002640const char *
2641TargetProperties::GetExpressionPrefixContentsAsCString ()
2642{
2643 const uint32_t idx = ePropertyExprPrefix;
2644 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2645 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002646 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002647 const bool null_terminate = true;
2648 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002649 if (data_sp)
2650 return (const char *) data_sp->GetBytes();
2651 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002652 return NULL;
2653}
2654
Greg Clayton73844aa2012-08-22 17:17:09 +00002655void
2656TargetProperties::SetStandardErrorPath (const char *p)
2657{
2658 const uint32_t idx = ePropertyErrorPath;
2659 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2660}
2661
2662bool
2663TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2664{
2665 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2666 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2667}
2668
Jim Ingham5e0e3722013-03-13 17:58:04 +00002669bool
2670TargetProperties::GetUseFastStepping () const
2671{
2672 const uint32_t idx = ePropertyUseFastStepping;
2673 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2674}
2675
Greg Clayton73844aa2012-08-22 17:17:09 +00002676const TargetPropertiesSP &
2677Target::GetGlobalProperties()
2678{
2679 static TargetPropertiesSP g_settings_sp;
2680 if (!g_settings_sp)
2681 {
2682 g_settings_sp.reset (new TargetProperties (NULL));
2683 }
2684 return g_settings_sp;
2685}
2686
Jim Ingham5a15e692012-02-16 06:50:00 +00002687const ConstString &
2688Target::TargetEventData::GetFlavorString ()
2689{
2690 static ConstString g_flavor ("Target::TargetEventData");
2691 return g_flavor;
2692}
2693
2694const ConstString &
2695Target::TargetEventData::GetFlavor () const
2696{
2697 return TargetEventData::GetFlavorString ();
2698}
2699
2700Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2701 EventData(),
2702 m_target_sp (new_target_sp)
2703{
2704}
2705
2706Target::TargetEventData::~TargetEventData()
2707{
2708
2709}
2710
2711void
2712Target::TargetEventData::Dump (Stream *s) const
2713{
2714
2715}
2716
2717const TargetSP
2718Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2719{
2720 TargetSP target_sp;
2721
2722 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2723 if (data)
2724 target_sp = data->m_target_sp;
2725
2726 return target_sp;
2727}
2728
2729const Target::TargetEventData *
2730Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2731{
2732 if (event_ptr)
2733 {
2734 const EventData *event_data = event_ptr->GetData();
2735 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2736 return static_cast <const TargetEventData *> (event_ptr->GetData());
2737 }
2738 return NULL;
2739}
2740