blob: f614af15912e3fa36707cee8ddaaae3d9e7732bb [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham03c8ee52011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton427f2902010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000031#include "lldb/Core/Timer.h"
32#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000033#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000034#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000036#include "lldb/Interpreter/CommandInterpreter.h"
37#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chen3f883492012-06-04 23:19:54 +000038#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton73844aa2012-08-22 17:17:09 +000039#include "lldb/Interpreter/OptionValues.h"
40#include "lldb/Interpreter/Property.h"
Chris Lattner24943d22010-06-08 16:52:24 +000041#include "lldb/lldb-private-log.h"
42#include "lldb/Symbol/ObjectFile.h"
43#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000044#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000045#include "lldb/Target/Thread.h"
46#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000047
48using namespace lldb;
49using namespace lldb_private;
50
Jim Ingham5a15e692012-02-16 06:50:00 +000051ConstString &
52Target::GetStaticBroadcasterClass ()
53{
54 static ConstString class_name ("lldb.target");
55 return class_name;
56}
57
Chris Lattner24943d22010-06-08 16:52:24 +000058//----------------------------------------------------------------------
59// Target constructor
60//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000061Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton73844aa2012-08-22 17:17:09 +000062 TargetProperties (this),
Jim Ingham94a5d0d2012-10-10 18:32:14 +000063 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton24bc5d92011-03-30 18:16:51 +000064 ExecutionContextScope (),
Greg Clayton63094e02010-06-23 01:19:29 +000065 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000066 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000067 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000068 m_arch (target_arch),
Enrico Granata146d9522012-11-08 02:22:02 +000069 m_images (this),
Greg Claytoneea26402010-09-14 23:36:40 +000070 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000071 m_breakpoint_list (false),
72 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000073 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000074 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000075 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000076 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000077 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000078 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000079 m_scratch_ast_source_ap (NULL),
80 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000081 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000082 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000083 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000084 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000085 m_suppress_stop_hooks (false),
86 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000087{
Greg Clayton49ce6822010-10-31 03:01:06 +000088 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
89 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
90 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000091
92 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000093
Greg Claytone005f2c2010-11-06 01:53:30 +000094 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000095 if (log)
96 log->Printf ("%p Target::Target()", this);
97}
98
99//----------------------------------------------------------------------
100// Destructor
101//----------------------------------------------------------------------
102Target::~Target()
103{
Greg Claytone005f2c2010-11-06 01:53:30 +0000104 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000105 if (log)
106 log->Printf ("%p Target::~Target()", this);
107 DeleteCurrentProcess ();
108}
109
110void
Caroline Tice7826c882010-10-26 03:11:13 +0000111Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000112{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000113// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000114 if (description_level != lldb::eDescriptionLevelBrief)
115 {
116 s->Indent();
117 s->PutCString("Target\n");
118 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000119 m_images.Dump(s);
120 m_breakpoint_list.Dump(s);
121 m_internal_breakpoint_list.Dump(s);
122 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000123 }
124 else
125 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000126 Module *exe_module = GetExecutableModulePointer();
127 if (exe_module)
128 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000129 else
130 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000131 }
Chris Lattner24943d22010-06-08 16:52:24 +0000132}
133
134void
Greg Clayton0bce9a22012-12-05 00:16:59 +0000135Target::CleanupProcess ()
136{
137 // Do any cleanup of the target we need to do between process instances.
138 // NB It is better to do this before destroying the process in case the
139 // clean up needs some help from the process.
140 m_breakpoint_list.ClearAllBreakpointSites();
141 m_internal_breakpoint_list.ClearAllBreakpointSites();
142 // Disable watchpoints just on the debugger side.
143 Mutex::Locker locker;
144 this->GetWatchpointList().GetListMutex(locker);
145 DisableAllWatchpoints(false);
146 ClearAllWatchpointHitCounts();
147}
148
149void
Chris Lattner24943d22010-06-08 16:52:24 +0000150Target::DeleteCurrentProcess ()
151{
152 if (m_process_sp.get())
153 {
Greg Clayton49480b12010-09-14 23:52:43 +0000154 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000155 if (m_process_sp->IsAlive())
156 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000157
158 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000159
Greg Clayton0bce9a22012-12-05 00:16:59 +0000160 CleanupProcess ();
161
Chris Lattner24943d22010-06-08 16:52:24 +0000162 m_process_sp.reset();
163 }
164}
165
166const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000167Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000168{
169 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000170 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000171 return m_process_sp;
172}
173
174const lldb::ProcessSP &
175Target::GetProcessSP () const
176{
177 return m_process_sp;
178}
179
Greg Clayton153ccd72011-08-10 02:10:13 +0000180void
181Target::Destroy()
182{
183 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000184 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000185 DeleteCurrentProcess ();
186 m_platform_sp.reset();
187 m_arch.Clear();
188 m_images.Clear();
189 m_section_load_list.Clear();
190 const bool notify = false;
191 m_breakpoint_list.RemoveAll(notify);
192 m_internal_breakpoint_list.RemoveAll(notify);
193 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000194 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000195 m_search_filter_sp.reset();
196 m_image_search_paths.Clear(notify);
197 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000198 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000199 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000200 m_persistent_variables.Clear();
201 m_stop_hooks.clear();
202 m_stop_hook_next_id = 0;
203 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000204 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000205}
206
207
Chris Lattner24943d22010-06-08 16:52:24 +0000208BreakpointList &
209Target::GetBreakpointList(bool internal)
210{
211 if (internal)
212 return m_internal_breakpoint_list;
213 else
214 return m_breakpoint_list;
215}
216
217const BreakpointList &
218Target::GetBreakpointList(bool internal) const
219{
220 if (internal)
221 return m_internal_breakpoint_list;
222 else
223 return m_breakpoint_list;
224}
225
226BreakpointSP
227Target::GetBreakpointByID (break_id_t break_id)
228{
229 BreakpointSP bp_sp;
230
231 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
232 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
233 else
234 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
235
236 return bp_sp;
237}
238
239BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000240Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000241 const FileSpecList *source_file_spec_list,
242 RegularExpression &source_regex,
243 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000244{
Jim Inghamd6d47972011-09-23 00:54:11 +0000245 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
246 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000247 return CreateBreakpoint (filter_sp, resolver_sp, internal);
248}
249
250
251BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000252Target::CreateBreakpoint (const FileSpecList *containingModules,
253 const FileSpec &file,
254 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000255 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000256 LazyBool skip_prologue,
257 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000258{
Greg Clayton49ce8962012-08-29 21:13:06 +0000259 if (check_inlines == eLazyBoolCalculate)
260 {
261 const InlineStrategy inline_strategy = GetInlineStrategy();
262 switch (inline_strategy)
263 {
264 case eInlineBreakpointsNever:
265 check_inlines = eLazyBoolNo;
266 break;
267
268 case eInlineBreakpointsHeaders:
269 if (file.IsSourceImplementationFile())
270 check_inlines = eLazyBoolNo;
271 else
272 check_inlines = eLazyBoolYes;
273 break;
274
275 case eInlineBreakpointsAlways:
276 check_inlines = eLazyBoolYes;
277 break;
278 }
279 }
Greg Clayton46365522012-09-07 23:48:57 +0000280 SearchFilterSP filter_sp;
281 if (check_inlines == eLazyBoolNo)
282 {
283 // Not checking for inlines, we are looking only for matching compile units
284 FileSpecList compile_unit_list;
285 compile_unit_list.Append (file);
286 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
287 }
288 else
289 {
290 filter_sp = GetSearchFilterForModuleList (containingModules);
291 }
Greg Clayton49ce8962012-08-29 21:13:06 +0000292 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
293 file,
294 line_no,
295 check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000296 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000297 return CreateBreakpoint (filter_sp, resolver_sp, internal);
298}
299
300
301BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000302Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000303{
Chris Lattner24943d22010-06-08 16:52:24 +0000304 Address so_addr;
305 // Attempt to resolve our load address if possible, though it is ok if
306 // it doesn't resolve to section/offset.
307
Greg Clayton33ed1702010-08-24 00:45:41 +0000308 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000309 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000310 if (!so_addr.IsValid())
311 {
312 // The address didn't resolve, so just set this as an absolute address
313 so_addr.SetOffset (addr);
314 }
315 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000316 return bp_sp;
317}
318
319BreakpointSP
320Target::CreateBreakpoint (Address &addr, bool internal)
321{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000322 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000323 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
324 return CreateBreakpoint (filter_sp, resolver_sp, internal);
325}
326
327BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000328Target::CreateBreakpoint (const FileSpecList *containingModules,
329 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000330 const char *func_name,
331 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000332 LazyBool skip_prologue,
333 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000334{
Greg Clayton12bec712010-06-28 21:30:43 +0000335 BreakpointSP bp_sp;
336 if (func_name)
337 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000338 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000339
340 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
341 func_name,
342 func_name_type_mask,
343 Breakpoint::Exact,
344 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000345 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
346 }
347 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000348}
349
Jim Ingham4722b102012-03-06 00:37:27 +0000350lldb::BreakpointSP
351Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000352 const FileSpecList *containingSourceFiles,
353 const std::vector<std::string> &func_names,
354 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000355 LazyBool skip_prologue,
356 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000357{
358 BreakpointSP bp_sp;
359 size_t num_names = func_names.size();
360 if (num_names > 0)
361 {
362 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
363
364 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
365 func_names,
366 func_name_type_mask,
367 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
368 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
369 }
370 return bp_sp;
371}
372
Jim Inghamc1053622012-03-03 02:05:11 +0000373BreakpointSP
374Target::CreateBreakpoint (const FileSpecList *containingModules,
375 const FileSpecList *containingSourceFiles,
376 const char *func_names[],
377 size_t num_names,
378 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000379 LazyBool skip_prologue,
380 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000381{
382 BreakpointSP bp_sp;
383 if (num_names > 0)
384 {
385 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
386
387 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
388 func_names,
389 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000390 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000391 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
392 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
393 }
394 return bp_sp;
395}
Chris Lattner24943d22010-06-08 16:52:24 +0000396
397SearchFilterSP
398Target::GetSearchFilterForModule (const FileSpec *containingModule)
399{
400 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000401 if (containingModule != NULL)
402 {
403 // TODO: We should look into sharing module based search filters
404 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000405 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000406 }
407 else
408 {
409 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000410 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000411 filter_sp = m_search_filter_sp;
412 }
413 return filter_sp;
414}
415
Jim Ingham03c8ee52011-09-21 01:17:13 +0000416SearchFilterSP
417Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
418{
419 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000420 if (containingModules && containingModules->GetSize() != 0)
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 SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +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()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000430 filter_sp = m_search_filter_sp;
431 }
432 return filter_sp;
433}
434
Jim Inghamd6d47972011-09-23 00:54:11 +0000435SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000436Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
437 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000438{
439 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
440 return GetSearchFilterForModuleList(containingModules);
441
442 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000443 if (containingModules == NULL)
444 {
445 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
446 // but that will take a little reworking.
447
Greg Clayton13d24fb2012-01-29 20:56:30 +0000448 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000449 }
450 else
451 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000452 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000453 }
454 return filter_sp;
455}
456
Chris Lattner24943d22010-06-08 16:52:24 +0000457BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000458Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000459 const FileSpecList *containingSourceFiles,
460 RegularExpression &func_regex,
461 LazyBool skip_prologue,
462 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000463{
Jim Inghamd6d47972011-09-23 00:54:11 +0000464 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000465 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
466 func_regex,
467 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000468
469 return CreateBreakpoint (filter_sp, resolver_sp, internal);
470}
471
Jim Ingham3df164e2012-03-05 04:47:34 +0000472lldb::BreakpointSP
473Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
474{
475 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
476}
477
Chris Lattner24943d22010-06-08 16:52:24 +0000478BreakpointSP
479Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
480{
481 BreakpointSP bp_sp;
482 if (filter_sp && resolver_sp)
483 {
484 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
485 resolver_sp->SetBreakpoint (bp_sp.get());
486
487 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000488 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000489 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000490 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000491
Greg Claytone005f2c2010-11-06 01:53:30 +0000492 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000493 if (log)
494 {
495 StreamString s;
496 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
497 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
498 }
499
Chris Lattner24943d22010-06-08 16:52:24 +0000500 bp_sp->ResolveBreakpoint();
501 }
Jim Inghamd1686902010-10-14 23:45:03 +0000502
503 if (!internal && bp_sp)
504 {
505 m_last_created_breakpoint = bp_sp;
506 }
507
Chris Lattner24943d22010-06-08 16:52:24 +0000508 return bp_sp;
509}
510
Johnny Chenda5a8022011-09-20 23:28:55 +0000511bool
512Target::ProcessIsValid()
513{
514 return (m_process_sp && m_process_sp->IsAlive());
515}
516
Johnny Chen3f883492012-06-04 23:19:54 +0000517static bool
518CheckIfWatchpointsExhausted(Target *target, Error &error)
519{
520 uint32_t num_supported_hardware_watchpoints;
521 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
522 if (rc.Success())
523 {
524 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
525 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
526 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
527 num_supported_hardware_watchpoints);
528 }
529 return false;
530}
531
Johnny Chenecd4feb2011-10-14 00:42:25 +0000532// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000533// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000534WatchpointSP
Jim Ingham9e376622012-10-23 07:20:06 +0000535Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000536{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000537 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
538 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000539 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Ingham9e376622012-10-23 07:20:06 +0000540 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000541
Johnny Chenecd4feb2011-10-14 00:42:25 +0000542 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000543 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000544 {
545 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000546 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000547 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000548 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000549 {
550 if (size == 0)
551 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
552 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000553 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000554 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000555 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000556
Johnny Chenecd4feb2011-10-14 00:42:25 +0000557 // Currently we only support one watchpoint per address, with total number
558 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000559
560 // Grab the list mutex while doing operations.
561 Mutex::Locker locker;
562 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000563 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000564 if (matched_sp)
565 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000566 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000567 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000568 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
569 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000570 // Return the existing watchpoint if both size and type match.
Jim Ingham9e376622012-10-23 07:20:06 +0000571 if (size == old_size && kind == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000572 wp_sp = matched_sp;
573 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000574 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000575 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000576 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000577 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000578 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000579 }
580
Johnny Chenecd4feb2011-10-14 00:42:25 +0000581 if (!wp_sp) {
Jim Ingham9e376622012-10-23 07:20:06 +0000582 Watchpoint *new_wp = new Watchpoint(*this, addr, size, type);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000583 if (!new_wp) {
584 printf("Watchpoint ctor failed, out of memory?\n");
585 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000586 }
Jim Ingham9e376622012-10-23 07:20:06 +0000587 new_wp->SetWatchpointType(kind);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000588 wp_sp.reset(new_wp);
589 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000590 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000591
Johnny Chen3f883492012-06-04 23:19:54 +0000592 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000593 if (log)
594 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
595 __FUNCTION__,
Johnny Chen3f883492012-06-04 23:19:54 +0000596 error.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000597 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000598
Johnny Chen3f883492012-06-04 23:19:54 +0000599 if (error.Fail()) {
Johnny Chen155599b2012-03-26 22:00:10 +0000600 // Enabling the watchpoint on the device side failed.
601 // Remove the said watchpoint from the list maintained by the target instance.
602 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000603 // See if we could provide more helpful error message.
604 if (!CheckIfWatchpointsExhausted(this, error))
605 {
606 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
607 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
608 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000609 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000610 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000611 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000612 m_last_created_watchpoint = wp_sp;
613 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000614}
615
Chris Lattner24943d22010-06-08 16:52:24 +0000616void
617Target::RemoveAllBreakpoints (bool internal_also)
618{
Greg Claytone005f2c2010-11-06 01:53:30 +0000619 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000620 if (log)
621 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
622
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000623 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000624 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000625 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000626
627 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000628}
629
630void
631Target::DisableAllBreakpoints (bool internal_also)
632{
Greg Claytone005f2c2010-11-06 01:53:30 +0000633 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000634 if (log)
635 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
636
637 m_breakpoint_list.SetEnabledAll (false);
638 if (internal_also)
639 m_internal_breakpoint_list.SetEnabledAll (false);
640}
641
642void
643Target::EnableAllBreakpoints (bool internal_also)
644{
Greg Claytone005f2c2010-11-06 01:53:30 +0000645 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000646 if (log)
647 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
648
649 m_breakpoint_list.SetEnabledAll (true);
650 if (internal_also)
651 m_internal_breakpoint_list.SetEnabledAll (true);
652}
653
654bool
655Target::RemoveBreakpointByID (break_id_t break_id)
656{
Greg Claytone005f2c2010-11-06 01:53:30 +0000657 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000658 if (log)
659 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
660
661 if (DisableBreakpointByID (break_id))
662 {
663 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000664 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000665 else
Jim Inghamd1686902010-10-14 23:45:03 +0000666 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000667 if (m_last_created_breakpoint)
668 {
669 if (m_last_created_breakpoint->GetID() == break_id)
670 m_last_created_breakpoint.reset();
671 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000672 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000673 }
Chris Lattner24943d22010-06-08 16:52:24 +0000674 return true;
675 }
676 return false;
677}
678
679bool
680Target::DisableBreakpointByID (break_id_t break_id)
681{
Greg Claytone005f2c2010-11-06 01:53:30 +0000682 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000683 if (log)
684 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
685
686 BreakpointSP bp_sp;
687
688 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
689 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
690 else
691 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
692 if (bp_sp)
693 {
694 bp_sp->SetEnabled (false);
695 return true;
696 }
697 return false;
698}
699
700bool
701Target::EnableBreakpointByID (break_id_t break_id)
702{
Greg Claytone005f2c2010-11-06 01:53:30 +0000703 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000704 if (log)
705 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
706 __FUNCTION__,
707 break_id,
708 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
709
710 BreakpointSP bp_sp;
711
712 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
713 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
714 else
715 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
716
717 if (bp_sp)
718 {
719 bp_sp->SetEnabled (true);
720 return true;
721 }
722 return false;
723}
724
Johnny Chenc86582f2011-09-23 21:21:43 +0000725// The flag 'end_to_end', default to true, signifies that the operation is
726// performed end to end, for both the debugger and the debuggee.
727
Johnny Chenecd4feb2011-10-14 00:42:25 +0000728// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
729// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000730bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000731Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000732{
733 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
734 if (log)
735 log->Printf ("Target::%s\n", __FUNCTION__);
736
Johnny Chenc86582f2011-09-23 21:21:43 +0000737 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000738 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000739 return true;
740 }
741
742 // Otherwise, it's an end to end operation.
743
Johnny Chenda5a8022011-09-20 23:28:55 +0000744 if (!ProcessIsValid())
745 return false;
746
Johnny Chenecd4feb2011-10-14 00:42:25 +0000747 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000748 for (size_t i = 0; i < num_watchpoints; ++i)
749 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000750 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
751 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000752 return false;
753
Johnny Chenecd4feb2011-10-14 00:42:25 +0000754 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000755 if (rc.Fail())
756 return false;
757 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000758 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000759 return true; // Success!
760}
761
Johnny Chenecd4feb2011-10-14 00:42:25 +0000762// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
763// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000764bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000765Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000766{
767 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
768 if (log)
769 log->Printf ("Target::%s\n", __FUNCTION__);
770
Johnny Chenc86582f2011-09-23 21:21:43 +0000771 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000772 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000773 return true;
774 }
775
776 // Otherwise, it's an end to end operation.
777
Johnny Chenda5a8022011-09-20 23:28:55 +0000778 if (!ProcessIsValid())
779 return false;
780
Johnny Chenecd4feb2011-10-14 00:42:25 +0000781 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000782 for (size_t i = 0; i < num_watchpoints; ++i)
783 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000784 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
785 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000786 return false;
787
Johnny Chenecd4feb2011-10-14 00:42:25 +0000788 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000789 if (rc.Fail())
790 return false;
791 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000792 return true; // Success!
793}
794
Johnny Chenecd4feb2011-10-14 00:42:25 +0000795// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
796// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000797bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000798Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000799{
800 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
801 if (log)
802 log->Printf ("Target::%s\n", __FUNCTION__);
803
Johnny Chenc86582f2011-09-23 21:21:43 +0000804 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000805 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000806 return true;
807 }
808
809 // Otherwise, it's an end to end operation.
810
Johnny Chenda5a8022011-09-20 23:28:55 +0000811 if (!ProcessIsValid())
812 return false;
813
Johnny Chenecd4feb2011-10-14 00:42:25 +0000814 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000815 for (size_t i = 0; i < num_watchpoints; ++i)
816 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000817 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
818 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000819 return false;
820
Johnny Chenecd4feb2011-10-14 00:42:25 +0000821 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000822 if (rc.Fail())
823 return false;
824 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000825 return true; // Success!
826}
827
Johnny Chen116a5cd2012-02-25 06:44:30 +0000828// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
829bool
830Target::ClearAllWatchpointHitCounts ()
831{
832 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
833 if (log)
834 log->Printf ("Target::%s\n", __FUNCTION__);
835
836 size_t num_watchpoints = m_watchpoint_list.GetSize();
837 for (size_t i = 0; i < num_watchpoints; ++i)
838 {
839 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
840 if (!wp_sp)
841 return false;
842
843 wp_sp->ResetHitCount();
844 }
845 return true; // Success!
846}
847
Johnny Chenecd4feb2011-10-14 00:42:25 +0000848// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000849// during these operations.
850bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000851Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000852{
853 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
854 if (log)
855 log->Printf ("Target::%s\n", __FUNCTION__);
856
857 if (!ProcessIsValid())
858 return false;
859
Johnny Chenecd4feb2011-10-14 00:42:25 +0000860 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000861 for (size_t i = 0; i < num_watchpoints; ++i)
862 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000863 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
864 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000865 return false;
866
Johnny Chenecd4feb2011-10-14 00:42:25 +0000867 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000868 }
869 return true; // Success!
870}
871
Johnny Chenecd4feb2011-10-14 00:42:25 +0000872// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000873bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000874Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000875{
876 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
877 if (log)
878 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
879
880 if (!ProcessIsValid())
881 return false;
882
Johnny Chenecd4feb2011-10-14 00:42:25 +0000883 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
884 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000885 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000886 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000887 if (rc.Success())
888 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000889
Johnny Chen01acfa72011-09-22 18:04:58 +0000890 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000891 }
892 return false;
893}
894
Johnny Chenecd4feb2011-10-14 00:42:25 +0000895// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000896bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000897Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000898{
899 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
900 if (log)
901 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
902
903 if (!ProcessIsValid())
904 return false;
905
Johnny Chenecd4feb2011-10-14 00:42:25 +0000906 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
907 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000908 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000909 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000910 if (rc.Success())
911 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000912
Johnny Chen01acfa72011-09-22 18:04:58 +0000913 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000914 }
915 return false;
916}
917
Johnny Chenecd4feb2011-10-14 00:42:25 +0000918// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000919bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000920Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000921{
922 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
923 if (log)
924 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
925
Johnny Chenecd4feb2011-10-14 00:42:25 +0000926 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000927 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000928 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000929 return true;
930 }
931 return false;
932}
933
Johnny Chenecd4feb2011-10-14 00:42:25 +0000934// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000935bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000936Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000937{
938 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
939 if (log)
940 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
941
942 if (!ProcessIsValid())
943 return false;
944
Johnny Chenecd4feb2011-10-14 00:42:25 +0000945 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
946 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000947 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000948 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000949 return true;
950 }
951 return false;
952}
953
Chris Lattner24943d22010-06-08 16:52:24 +0000954ModuleSP
955Target::GetExecutableModule ()
956{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000957 return m_images.GetModuleAtIndex(0);
958}
959
960Module*
961Target::GetExecutableModulePointer ()
962{
963 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000964}
965
Enrico Granata146d9522012-11-08 02:22:02 +0000966static void
967LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
968{
969 Error error;
970 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error))
971 {
972 target->GetDebugger().GetOutputStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
973 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
974 error.AsCString());
975 }
976}
977
Chris Lattner24943d22010-06-08 16:52:24 +0000978void
979Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
980{
981 m_images.Clear();
982 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000983 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000984 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000985
986 if (executable_sp.get())
987 {
988 Timer scoped_timer (__PRETTY_FUNCTION__,
989 "Target::SetExecutableModule (executable = '%s/%s')",
990 executable_sp->GetFileSpec().GetDirectory().AsCString(),
991 executable_sp->GetFileSpec().GetFilename().AsCString());
992
993 m_images.Append(executable_sp); // The first image is our exectuable file
994
Jim Ingham7508e732010-08-09 23:31:02 +0000995 // 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 +0000996 if (!m_arch.IsValid())
997 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000998
Chris Lattner24943d22010-06-08 16:52:24 +0000999 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001000 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +00001001
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001002 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +00001003 {
1004 executable_objfile->GetDependentModules(dependent_files);
1005 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1006 {
Greg Claytonb1888f22011-03-19 01:12:21 +00001007 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1008 FileSpec platform_dependent_file_spec;
1009 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +00001010 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +00001011 else
1012 platform_dependent_file_spec = dependent_file_spec;
1013
Greg Clayton444fe992012-02-26 05:51:37 +00001014 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1015 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +00001016 if (image_module_sp.get())
1017 {
Chris Lattner24943d22010-06-08 16:52:24 +00001018 ObjectFile *objfile = image_module_sp->GetObjectFile();
1019 if (objfile)
1020 objfile->GetDependentModules(dependent_files);
1021 }
1022 }
1023 }
Chris Lattner24943d22010-06-08 16:52:24 +00001024 }
1025}
1026
1027
Jim Ingham7508e732010-08-09 23:31:02 +00001028bool
1029Target::SetArchitecture (const ArchSpec &arch_spec)
1030{
Greg Claytonb170aee2012-05-08 01:45:38 +00001031 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001032 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001033 // If we haven't got a valid arch spec, or the architectures are
1034 // compatible, so just update the architecture. Architectures can be
1035 // equal, yet the triple OS and vendor might change, so we need to do
1036 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001037 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001038 return true;
1039 }
1040 else
1041 {
1042 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +00001043 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001044 ModuleSP executable_sp = GetExecutableModule ();
1045 m_images.Clear();
1046 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001047 m_scratch_ast_source_ap.reset();
1048 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001049 // Need to do something about unsetting breakpoints.
1050
1051 if (executable_sp)
1052 {
Greg Clayton444fe992012-02-26 05:51:37 +00001053 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1054 Error error = ModuleList::GetSharedModule (module_spec,
1055 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001056 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001057 NULL,
1058 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001059
1060 if (!error.Fail() && executable_sp)
1061 {
1062 SetExecutableModule (executable_sp, true);
1063 return true;
1064 }
Jim Ingham7508e732010-08-09 23:31:02 +00001065 }
1066 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001067 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001068}
Chris Lattner24943d22010-06-08 16:52:24 +00001069
Chris Lattner24943d22010-06-08 16:52:24 +00001070void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001071Target::WillClearList (const ModuleList& module_list)
Enrico Granata146d9522012-11-08 02:22:02 +00001072{
1073}
1074
1075void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001076Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001077{
1078 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001079 ModuleList my_module_list;
1080 my_module_list.Append(module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +00001081 LoadScriptingResourceForModule(module_sp, this);
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001082 ModulesDidLoad (my_module_list);
Chris Lattner24943d22010-06-08 16:52:24 +00001083}
1084
1085void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001086Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata146d9522012-11-08 02:22:02 +00001087{
1088 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001089 ModuleList my_module_list;
1090 my_module_list.Append(module_sp);
1091 ModulesDidUnload (my_module_list);
Enrico Granata146d9522012-11-08 02:22:02 +00001092}
1093
1094void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001095Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001096{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001097 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001098 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001099}
1100
1101void
1102Target::ModulesDidLoad (ModuleList &module_list)
1103{
Enrico Granata146d9522012-11-08 02:22:02 +00001104 if (module_list.GetSize())
1105 {
1106 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1107 // TODO: make event data that packages up the module_list
1108 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1109 }
Chris Lattner24943d22010-06-08 16:52:24 +00001110}
1111
1112void
1113Target::ModulesDidUnload (ModuleList &module_list)
1114{
Enrico Granata146d9522012-11-08 02:22:02 +00001115 if (module_list.GetSize())
1116 {
1117 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1118 // TODO: make event data that packages up the module_list
1119 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1120 }
Chris Lattner24943d22010-06-08 16:52:24 +00001121}
1122
Daniel Dunbar705a0982011-10-31 22:50:37 +00001123bool
Greg Clayton444fe992012-02-26 05:51:37 +00001124Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001125{
Greg Clayton73844aa2012-08-22 17:17:09 +00001126 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001127 {
1128 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001129 ModuleSpec module_spec (module_file_spec);
1130 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001131
1132 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1133 // black list.
1134 if (num_modules > 0)
1135 {
1136 for (int i = 0; i < num_modules; i++)
1137 {
1138 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1139 return false;
1140 }
1141 return true;
1142 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001143 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001144 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001145}
1146
Daniel Dunbar705a0982011-10-31 22:50:37 +00001147bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001148Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1149{
Greg Clayton73844aa2012-08-22 17:17:09 +00001150 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001151 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001152 if (m_platform_sp)
1153 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001154 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001155 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001156}
1157
Chris Lattner24943d22010-06-08 16:52:24 +00001158size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001159Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1160{
Greg Clayton3508c382012-02-24 01:59:29 +00001161 SectionSP section_sp (addr.GetSection());
1162 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001163 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001164 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1165 if (section_sp->IsEncrypted())
1166 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001167 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001168 return 0;
1169 }
Greg Clayton3508c382012-02-24 01:59:29 +00001170 ModuleSP module_sp (section_sp->GetModule());
1171 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001172 {
Greg Clayton3508c382012-02-24 01:59:29 +00001173 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1174 if (objfile)
1175 {
1176 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1177 addr.GetOffset(),
1178 dst,
1179 dst_len);
1180 if (bytes_read > 0)
1181 return bytes_read;
1182 else
1183 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1184 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001185 else
Greg Clayton3508c382012-02-24 01:59:29 +00001186 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001187 }
1188 else
Greg Clayton3508c382012-02-24 01:59:29 +00001189 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001190 }
1191 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001192 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001193
Greg Clayton26100dc2011-01-07 01:57:07 +00001194 return 0;
1195}
1196
1197size_t
Enrico Granata91544802011-09-06 19:20:51 +00001198Target::ReadMemory (const Address& addr,
1199 bool prefer_file_cache,
1200 void *dst,
1201 size_t dst_len,
1202 Error &error,
1203 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001204{
Chris Lattner24943d22010-06-08 16:52:24 +00001205 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001206
Enrico Granata91544802011-09-06 19:20:51 +00001207 // if we end up reading this from process memory, we will fill this
1208 // with the actual load address
1209 if (load_addr_ptr)
1210 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1211
Greg Clayton26100dc2011-01-07 01:57:07 +00001212 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001213
1214 addr_t load_addr = LLDB_INVALID_ADDRESS;
1215 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001216 Address resolved_addr;
1217 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001218 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001219 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001220 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001221 // No sections are loaded, so we must assume we are not running
1222 // yet and anything we are given is a file address.
1223 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1224 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001225 }
Greg Clayton70436352010-06-30 23:03:03 +00001226 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001227 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001228 // We have at least one section loaded. This can be becuase
1229 // we have manually loaded some sections with "target modules load ..."
1230 // or because we have have a live process that has sections loaded
1231 // through the dynamic loader
1232 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1233 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001234 }
Greg Clayton70436352010-06-30 23:03:03 +00001235 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001236 if (!resolved_addr.IsValid())
1237 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001238
Greg Clayton9b82f862011-07-11 05:12:02 +00001239
Greg Clayton26100dc2011-01-07 01:57:07 +00001240 if (prefer_file_cache)
1241 {
1242 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1243 if (bytes_read > 0)
1244 return bytes_read;
1245 }
Greg Clayton70436352010-06-30 23:03:03 +00001246
Johnny Chenda5a8022011-09-20 23:28:55 +00001247 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001248 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001249 if (load_addr == LLDB_INVALID_ADDRESS)
1250 load_addr = resolved_addr.GetLoadAddress (this);
1251
Greg Clayton70436352010-06-30 23:03:03 +00001252 if (load_addr == LLDB_INVALID_ADDRESS)
1253 {
Greg Clayton3508c382012-02-24 01:59:29 +00001254 ModuleSP addr_module_sp (resolved_addr.GetModule());
1255 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001256 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001257 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001258 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001259 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001260 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001261 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001262 }
1263 else
1264 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001265 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001266 if (bytes_read != dst_len)
1267 {
1268 if (error.Success())
1269 {
1270 if (bytes_read == 0)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001271 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001272 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001273 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 +00001274 }
1275 }
Greg Clayton70436352010-06-30 23:03:03 +00001276 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001277 {
1278 if (load_addr_ptr)
1279 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001280 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001281 }
Greg Clayton70436352010-06-30 23:03:03 +00001282 // If the address is not section offset we have an address that
1283 // doesn't resolve to any address in any currently loaded shared
1284 // libaries and we failed to read memory so there isn't anything
1285 // more we can do. If it is section offset, we might be able to
1286 // read cached memory from the object file.
1287 if (!resolved_addr.IsSectionOffset())
1288 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001289 }
Chris Lattner24943d22010-06-08 16:52:24 +00001290 }
Greg Clayton70436352010-06-30 23:03:03 +00001291
Greg Clayton9b82f862011-07-11 05:12:02 +00001292 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001293 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001294 // If we didn't already try and read from the object file cache, then
1295 // try it after failing to read from the process.
1296 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001297 }
1298 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001299}
1300
Greg Clayton7dd98df2011-07-12 17:06:17 +00001301size_t
1302Target::ReadScalarIntegerFromMemory (const Address& addr,
1303 bool prefer_file_cache,
1304 uint32_t byte_size,
1305 bool is_signed,
1306 Scalar &scalar,
1307 Error &error)
1308{
1309 uint64_t uval;
1310
1311 if (byte_size <= sizeof(uval))
1312 {
1313 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1314 if (bytes_read == byte_size)
1315 {
1316 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1317 uint32_t offset = 0;
1318 if (byte_size <= 4)
1319 scalar = data.GetMaxU32 (&offset, byte_size);
1320 else
1321 scalar = data.GetMaxU64 (&offset, byte_size);
1322
1323 if (is_signed)
1324 scalar.SignExtend(byte_size * 8);
1325 return bytes_read;
1326 }
1327 }
1328 else
1329 {
1330 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1331 }
1332 return 0;
1333}
1334
1335uint64_t
1336Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1337 bool prefer_file_cache,
1338 size_t integer_byte_size,
1339 uint64_t fail_value,
1340 Error &error)
1341{
1342 Scalar scalar;
1343 if (ReadScalarIntegerFromMemory (addr,
1344 prefer_file_cache,
1345 integer_byte_size,
1346 false,
1347 scalar,
1348 error))
1349 return scalar.ULongLong(fail_value);
1350 return fail_value;
1351}
1352
1353bool
1354Target::ReadPointerFromMemory (const Address& addr,
1355 bool prefer_file_cache,
1356 Error &error,
1357 Address &pointer_addr)
1358{
1359 Scalar scalar;
1360 if (ReadScalarIntegerFromMemory (addr,
1361 prefer_file_cache,
1362 m_arch.GetAddressByteSize(),
1363 false,
1364 scalar,
1365 error))
1366 {
1367 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1368 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1369 {
1370 if (m_section_load_list.IsEmpty())
1371 {
1372 // No sections are loaded, so we must assume we are not running
1373 // yet and anything we are given is a file address.
1374 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1375 }
1376 else
1377 {
1378 // We have at least one section loaded. This can be becuase
1379 // we have manually loaded some sections with "target modules load ..."
1380 // or because we have have a live process that has sections loaded
1381 // through the dynamic loader
1382 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1383 }
1384 // We weren't able to resolve the pointer value, so just return
1385 // an address with no section
1386 if (!pointer_addr.IsValid())
1387 pointer_addr.SetOffset (pointer_vm_addr);
1388 return true;
1389
1390 }
1391 }
1392 return false;
1393}
Chris Lattner24943d22010-06-08 16:52:24 +00001394
1395ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001396Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001397{
Chris Lattner24943d22010-06-08 16:52:24 +00001398 ModuleSP module_sp;
1399
Chris Lattner24943d22010-06-08 16:52:24 +00001400 Error error;
1401
Jim Ingham03e5e512012-05-17 18:38:42 +00001402 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1403 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001404
Jim Ingham03e5e512012-05-17 18:38:42 +00001405 if (module_spec.GetUUID().IsValid())
1406 module_sp = m_images.FindFirstModule(module_spec);
1407
Greg Claytone1ef1e32012-04-27 00:58:27 +00001408 if (!module_sp)
1409 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001410 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1411 bool did_create_module = false;
1412
1413 // If there are image search path entries, try to use them first to acquire a suitable image.
1414 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001415 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001416 ModuleSpec transformed_spec (module_spec);
1417 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1418 {
1419 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1420 error = ModuleList::GetSharedModule (transformed_spec,
1421 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001422 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001423 &old_module_sp,
1424 &did_create_module);
1425 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001426 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001427
Greg Claytone1ef1e32012-04-27 00:58:27 +00001428 if (!module_sp)
1429 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001430 // If we have a UUID, we can check our global shared module list in case
1431 // we already have it. If we don't have a valid UUID, then we can't since
1432 // the path in "module_spec" will be a platform path, and we will need to
1433 // let the platform find that file. For example, we could be asking for
1434 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1435 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1436 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1437 // cache.
1438 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001439 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001440 // We have a UUID, it is OK to check the global module list...
1441 error = ModuleList::GetSharedModule (module_spec,
1442 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001443 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001444 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001445 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001446 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001447
1448 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001449 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001450 // The platform is responsible for finding and caching an appropriate
1451 // module in the shared module cache.
1452 if (m_platform_sp)
1453 {
1454 FileSpec platform_file_spec;
1455 error = m_platform_sp->GetSharedModule (module_spec,
1456 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001457 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001458 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001459 &did_create_module);
1460 }
1461 else
1462 {
1463 error.SetErrorString("no platform is currently set");
1464 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001465 }
1466 }
Chris Lattner24943d22010-06-08 16:52:24 +00001467
Jim Ingham03e5e512012-05-17 18:38:42 +00001468 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1469 // module in the list already, and if there was, let's remove it.
1470 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001471 {
Greg Clayton1649a722012-11-29 22:16:27 +00001472 ObjectFile *objfile = module_sp->GetObjectFile();
1473 if (objfile)
Jim Ingham03e5e512012-05-17 18:38:42 +00001474 {
Greg Clayton1649a722012-11-29 22:16:27 +00001475 switch (objfile->GetType())
Jim Ingham03e5e512012-05-17 18:38:42 +00001476 {
Greg Clayton1649a722012-11-29 22:16:27 +00001477 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1478 case ObjectFile::eTypeExecutable: /// A normal executable
1479 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1480 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1481 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1482 break;
1483 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1484 if (error_ptr)
1485 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1486 return ModuleSP();
1487 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1488 if (error_ptr)
1489 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1490 return ModuleSP();
1491 default:
1492 if (error_ptr)
1493 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1494 return ModuleSP();
1495 }
1496 // GetSharedModule is not guaranteed to find the old shared module, for instance
1497 // in the common case where you pass in the UUID, it is only going to find the one
1498 // module matching the UUID. In fact, it has no good way to know what the "old module"
1499 // relevant to this target is, since there might be many copies of a module with this file spec
1500 // in various running debug sessions, but only one of them will belong to this target.
1501 // So let's remove the UUID from the module list, and look in the target's module list.
1502 // Only do this if there is SOMETHING else in the module spec...
1503 if (!old_module_sp)
1504 {
1505 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham03e5e512012-05-17 18:38:42 +00001506 {
Greg Clayton1649a722012-11-29 22:16:27 +00001507 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1508 module_spec_copy.GetUUID().Clear();
1509
1510 ModuleList found_modules;
1511 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1512 if (num_found == 1)
1513 {
1514 old_module_sp = found_modules.GetModuleAtIndex(0);
1515 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001516 }
1517 }
Greg Clayton1649a722012-11-29 22:16:27 +00001518
1519 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1520 {
1521 m_images.ReplaceModule(old_module_sp, module_sp);
1522 Module *old_module_ptr = old_module_sp.get();
1523 old_module_sp.reset();
1524 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1525 }
1526 else
1527 m_images.Append(module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001528 }
Chris Lattner24943d22010-06-08 16:52:24 +00001529 }
1530 }
1531 if (error_ptr)
1532 *error_ptr = error;
1533 return module_sp;
1534}
1535
1536
Greg Clayton289afcb2012-02-18 05:35:26 +00001537TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001538Target::CalculateTarget ()
1539{
Greg Clayton289afcb2012-02-18 05:35:26 +00001540 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001541}
1542
Greg Clayton289afcb2012-02-18 05:35:26 +00001543ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001544Target::CalculateProcess ()
1545{
Greg Clayton289afcb2012-02-18 05:35:26 +00001546 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001547}
1548
Greg Clayton289afcb2012-02-18 05:35:26 +00001549ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001550Target::CalculateThread ()
1551{
Greg Clayton289afcb2012-02-18 05:35:26 +00001552 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001553}
1554
Greg Clayton289afcb2012-02-18 05:35:26 +00001555StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001556Target::CalculateStackFrame ()
1557{
Greg Clayton289afcb2012-02-18 05:35:26 +00001558 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001559}
1560
1561void
Greg Claytona830adb2010-10-04 01:05:56 +00001562Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001563{
Greg Clayton567e7f32011-09-22 04:58:26 +00001564 exe_ctx.Clear();
1565 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001566}
1567
1568PathMappingList &
1569Target::GetImageSearchPathList ()
1570{
1571 return m_image_search_paths;
1572}
1573
1574void
1575Target::ImageSearchPathsChanged
1576(
1577 const PathMappingList &path_list,
1578 void *baton
1579)
1580{
1581 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001582 ModuleSP exe_module_sp (target->GetExecutableModule());
1583 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001584 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001585 target->m_images.Clear();
1586 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001587 }
1588}
1589
1590ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001591Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001592{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001593 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001594 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001595 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001596 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001597 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001598 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1599 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1600 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1601 }
Chris Lattner24943d22010-06-08 16:52:24 +00001602 return m_scratch_ast_context_ap.get();
1603}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001604
Sean Callanan4938bd62011-11-16 18:20:47 +00001605ClangASTImporter *
1606Target::GetClangASTImporter()
1607{
1608 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1609
1610 if (!ast_importer)
1611 {
1612 ast_importer = new ClangASTImporter();
1613 m_ast_importer_ap.reset(ast_importer);
1614 }
1615
1616 return ast_importer;
1617}
1618
Greg Clayton990de7b2010-11-18 23:32:35 +00001619void
Caroline Tice2a456812011-03-10 22:14:10 +00001620Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001621{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001622 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001623}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001624
Greg Clayton990de7b2010-11-18 23:32:35 +00001625void
Caroline Tice2a456812011-03-10 22:14:10 +00001626Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001627{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001628 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001629}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001630
Greg Clayton9ce95382012-02-13 23:10:39 +00001631FileSpecList
1632Target::GetDefaultExecutableSearchPaths ()
1633{
Greg Clayton73844aa2012-08-22 17:17:09 +00001634 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1635 if (properties_sp)
1636 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001637 return FileSpecList();
1638}
1639
Caroline Tice5bc8c972010-09-20 20:44:43 +00001640ArchSpec
1641Target::GetDefaultArchitecture ()
1642{
Greg Clayton73844aa2012-08-22 17:17:09 +00001643 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1644 if (properties_sp)
1645 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001646 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001647}
1648
1649void
Greg Clayton73844aa2012-08-22 17:17:09 +00001650Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001651{
Greg Clayton73844aa2012-08-22 17:17:09 +00001652 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1653 if (properties_sp)
1654 return properties_sp->SetDefaultArchitecture(arch);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001655}
1656
Greg Claytona830adb2010-10-04 01:05:56 +00001657Target *
1658Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1659{
1660 // The target can either exist in the "process" of ExecutionContext, or in
1661 // the "target_sp" member of SymbolContext. This accessor helper function
1662 // will get the target from one of these locations.
1663
1664 Target *target = NULL;
1665 if (sc_ptr != NULL)
1666 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001667 if (target == NULL && exe_ctx_ptr)
1668 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001669 return target;
1670}
1671
Greg Clayton427f2902010-12-14 02:59:59 +00001672ExecutionResults
1673Target::EvaluateExpression
1674(
1675 const char *expr_cstr,
1676 StackFrame *frame,
Enrico Granata6cca9692012-07-16 23:10:35 +00001677 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001678 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001679)
1680{
Enrico Granata3a08fd12012-09-18 17:43:16 +00001681 result_valobj_sp.reset();
1682
Greg Clayton427f2902010-12-14 02:59:59 +00001683 ExecutionResults execution_results = eExecutionSetupError;
1684
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001685 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1686 return execution_results;
1687
Jim Ingham3613ae12011-05-12 02:06:14 +00001688 // We shouldn't run stop hooks in expressions.
1689 // Be sure to reset this if you return anywhere within this function.
1690 bool old_suppress_value = m_suppress_stop_hooks;
1691 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001692
1693 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001694
1695 const size_t expr_cstr_len = ::strlen (expr_cstr);
1696
Greg Clayton427f2902010-12-14 02:59:59 +00001697 if (frame)
1698 {
1699 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001700 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001701 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001702 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1703 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001704 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001705
1706 // Make sure we don't have any things that we know a variable expression
1707 // won't be able to deal with before calling into it
1708 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1709 {
1710 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
Enrico Granatad27026e2012-09-05 20:41:26 +00001711 options.GetUseDynamic(),
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001712 expr_path_options,
1713 var_sp,
1714 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001715 // if this expression results in a bitfield, we give up and let the IR handle it
1716 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1717 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001718 }
Greg Clayton427f2902010-12-14 02:59:59 +00001719 }
1720 else if (m_process_sp)
1721 {
1722 m_process_sp->CalculateExecutionContext(exe_ctx);
1723 }
1724 else
1725 {
1726 CalculateExecutionContext(exe_ctx);
1727 }
1728
1729 if (result_valobj_sp)
1730 {
1731 execution_results = eExecutionCompleted;
1732 // We got a result from the frame variable expression path above...
1733 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1734
1735 lldb::ValueObjectSP const_valobj_sp;
1736
1737 // Check in case our value is already a constant value
1738 if (result_valobj_sp->GetIsConstant())
1739 {
1740 const_valobj_sp = result_valobj_sp;
1741 const_valobj_sp->SetName (persistent_variable_name);
1742 }
1743 else
Jim Inghame41494a2011-04-16 00:01:13 +00001744 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001745 if (options.GetUseDynamic() != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001746 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001747 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(options.GetUseDynamic());
Jim Inghame41494a2011-04-16 00:01:13 +00001748 if (dynamic_sp)
1749 result_valobj_sp = dynamic_sp;
1750 }
1751
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001752 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001753 }
Greg Clayton427f2902010-12-14 02:59:59 +00001754
Sean Callanan6a925532011-01-13 08:53:35 +00001755 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1756
Greg Clayton427f2902010-12-14 02:59:59 +00001757 result_valobj_sp = const_valobj_sp;
1758
Sean Callanan6a925532011-01-13 08:53:35 +00001759 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1760 assert (clang_expr_variable_sp.get());
1761
1762 // Set flags and live data as appropriate
1763
1764 const Value &result_value = live_valobj_sp->GetValue();
1765
1766 switch (result_value.GetValueType())
1767 {
1768 case Value::eValueTypeHostAddress:
1769 case Value::eValueTypeFileAddress:
1770 // we don't do anything with these for now
1771 break;
1772 case Value::eValueTypeScalar:
Greg Claytonf0fab4f2012-10-30 23:56:14 +00001773 case Value::eValueTypeVector:
Sean Callanan6a925532011-01-13 08:53:35 +00001774 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1775 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1776 break;
1777 case Value::eValueTypeLoadAddress:
1778 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1779 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1780 break;
1781 }
Greg Clayton427f2902010-12-14 02:59:59 +00001782 }
1783 else
1784 {
1785 // Make sure we aren't just trying to see the value of a persistent
1786 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001787 lldb::ClangExpressionVariableSP persistent_var_sp;
1788 // Only check for persistent variables the expression starts with a '$'
1789 if (expr_cstr[0] == '$')
1790 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1791
Greg Clayton427f2902010-12-14 02:59:59 +00001792 if (persistent_var_sp)
1793 {
1794 result_valobj_sp = persistent_var_sp->GetValueObject ();
1795 execution_results = eExecutionCompleted;
1796 }
1797 else
1798 {
1799 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001800
Greg Clayton427f2902010-12-14 02:59:59 +00001801 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Enrico Granatad27026e2012-09-05 20:41:26 +00001802 options.GetExecutionPolicy(),
Sean Callanan5b658cc2011-11-07 23:35:40 +00001803 lldb::eLanguageTypeUnknown,
Enrico Granatad27026e2012-09-05 20:41:26 +00001804 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1805 options.DoesUnwindOnError(),
Greg Clayton427f2902010-12-14 02:59:59 +00001806 expr_cstr,
1807 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001808 result_valobj_sp,
Jim Ingham47beabb2012-10-16 21:41:58 +00001809 options.GetRunOthers(),
1810 options.GetTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001811 }
1812 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001813
1814 m_suppress_stop_hooks = old_suppress_value;
1815
Greg Clayton427f2902010-12-14 02:59:59 +00001816 return execution_results;
1817}
1818
Greg Claytonc0fa5332011-05-22 22:46:53 +00001819lldb::addr_t
1820Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1821{
1822 addr_t code_addr = load_addr;
1823 switch (m_arch.GetMachine())
1824 {
1825 case llvm::Triple::arm:
1826 case llvm::Triple::thumb:
1827 switch (addr_class)
1828 {
1829 case eAddressClassData:
1830 case eAddressClassDebug:
1831 return LLDB_INVALID_ADDRESS;
1832
1833 case eAddressClassUnknown:
1834 case eAddressClassInvalid:
1835 case eAddressClassCode:
1836 case eAddressClassCodeAlternateISA:
1837 case eAddressClassRuntime:
1838 // Check if bit zero it no set?
1839 if ((code_addr & 1ull) == 0)
1840 {
1841 // Bit zero isn't set, check if the address is a multiple of 2?
1842 if (code_addr & 2ull)
1843 {
1844 // The address is a multiple of 2 so it must be thumb, set bit zero
1845 code_addr |= 1ull;
1846 }
1847 else if (addr_class == eAddressClassCodeAlternateISA)
1848 {
1849 // We checked the address and the address claims to be the alternate ISA
1850 // which means thumb, so set bit zero.
1851 code_addr |= 1ull;
1852 }
1853 }
1854 break;
1855 }
1856 break;
1857
1858 default:
1859 break;
1860 }
1861 return code_addr;
1862}
1863
1864lldb::addr_t
1865Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1866{
1867 addr_t opcode_addr = load_addr;
1868 switch (m_arch.GetMachine())
1869 {
1870 case llvm::Triple::arm:
1871 case llvm::Triple::thumb:
1872 switch (addr_class)
1873 {
1874 case eAddressClassData:
1875 case eAddressClassDebug:
1876 return LLDB_INVALID_ADDRESS;
1877
1878 case eAddressClassInvalid:
1879 case eAddressClassUnknown:
1880 case eAddressClassCode:
1881 case eAddressClassCodeAlternateISA:
1882 case eAddressClassRuntime:
1883 opcode_addr &= ~(1ull);
1884 break;
1885 }
1886 break;
1887
1888 default:
1889 break;
1890 }
1891 return opcode_addr;
1892}
1893
Jim Inghamd60d94a2011-03-11 03:53:59 +00001894lldb::user_id_t
1895Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1896{
1897 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001898 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001899 m_stop_hooks[new_uid] = new_hook_sp;
1900 return new_uid;
1901}
1902
1903bool
1904Target::RemoveStopHookByID (lldb::user_id_t user_id)
1905{
1906 size_t num_removed;
1907 num_removed = m_stop_hooks.erase (user_id);
1908 if (num_removed == 0)
1909 return false;
1910 else
1911 return true;
1912}
1913
1914void
1915Target::RemoveAllStopHooks ()
1916{
1917 m_stop_hooks.clear();
1918}
1919
1920Target::StopHookSP
1921Target::GetStopHookByID (lldb::user_id_t user_id)
1922{
1923 StopHookSP found_hook;
1924
1925 StopHookCollection::iterator specified_hook_iter;
1926 specified_hook_iter = m_stop_hooks.find (user_id);
1927 if (specified_hook_iter != m_stop_hooks.end())
1928 found_hook = (*specified_hook_iter).second;
1929 return found_hook;
1930}
1931
1932bool
1933Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1934{
1935 StopHookCollection::iterator specified_hook_iter;
1936 specified_hook_iter = m_stop_hooks.find (user_id);
1937 if (specified_hook_iter == m_stop_hooks.end())
1938 return false;
1939
1940 (*specified_hook_iter).second->SetIsActive (active_state);
1941 return true;
1942}
1943
1944void
1945Target::SetAllStopHooksActiveState (bool active_state)
1946{
1947 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1948 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1949 {
1950 (*pos).second->SetIsActive (active_state);
1951 }
1952}
1953
1954void
1955Target::RunStopHooks ()
1956{
Jim Ingham3613ae12011-05-12 02:06:14 +00001957 if (m_suppress_stop_hooks)
1958 return;
1959
Jim Inghamd60d94a2011-03-11 03:53:59 +00001960 if (!m_process_sp)
1961 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001962
1963 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1964 // since in that case we do not want to run the stop-hooks
1965 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1966 return;
1967
Jim Inghamd60d94a2011-03-11 03:53:59 +00001968 if (m_stop_hooks.empty())
1969 return;
1970
1971 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1972
1973 // If there aren't any active stop hooks, don't bother either:
1974 bool any_active_hooks = false;
1975 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1976 {
1977 if ((*pos).second->IsActive())
1978 {
1979 any_active_hooks = true;
1980 break;
1981 }
1982 }
1983 if (!any_active_hooks)
1984 return;
1985
1986 CommandReturnObject result;
1987
1988 std::vector<ExecutionContext> exc_ctx_with_reasons;
1989 std::vector<SymbolContext> sym_ctx_with_reasons;
1990
1991 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1992 size_t num_threads = cur_threadlist.GetSize();
1993 for (size_t i = 0; i < num_threads; i++)
1994 {
1995 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1996 if (cur_thread_sp->ThreadStoppedForAReason())
1997 {
1998 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1999 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2000 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2001 }
2002 }
2003
2004 // If no threads stopped for a reason, don't run the stop-hooks.
2005 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2006 if (num_exe_ctx == 0)
2007 return;
2008
Jim Inghame5ed8e92011-06-02 23:58:26 +00002009 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2010 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002011
2012 bool keep_going = true;
2013 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00002014 bool print_hook_header;
2015 bool print_thread_header;
2016
2017 if (num_exe_ctx == 1)
2018 print_thread_header = false;
2019 else
2020 print_thread_header = true;
2021
2022 if (m_stop_hooks.size() == 1)
2023 print_hook_header = false;
2024 else
2025 print_hook_header = true;
2026
Jim Inghamd60d94a2011-03-11 03:53:59 +00002027 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2028 {
2029 // result.Clear();
2030 StopHookSP cur_hook_sp = (*pos).second;
2031 if (!cur_hook_sp->IsActive())
2032 continue;
2033
2034 bool any_thread_matched = false;
2035 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2036 {
2037 if ((cur_hook_sp->GetSpecifier () == NULL
2038 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2039 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00002040 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002041 {
2042 if (!hooks_ran)
2043 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00002044 hooks_ran = true;
2045 }
Jim Inghamc54840c2011-03-22 01:47:27 +00002046 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002047 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002048 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2049 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2050 NULL);
2051 if (cmd)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002052 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chen4d96a742011-10-24 23:01:06 +00002053 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002054 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002055 any_thread_matched = true;
2056 }
2057
Jim Inghamc54840c2011-03-22 01:47:27 +00002058 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002059 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002060
2061 bool stop_on_continue = true;
2062 bool stop_on_error = true;
2063 bool echo_commands = false;
2064 bool print_results = true;
2065 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002066 &exc_ctx_with_reasons[i],
2067 stop_on_continue,
2068 stop_on_error,
2069 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002070 print_results,
2071 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002072 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002073
2074 // If the command started the target going again, we should bag out of
2075 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002076 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2077 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002078 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002079 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002080 keep_going = false;
2081 }
2082 }
2083 }
2084 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002085
Caroline Tice4a348082011-05-02 20:41:46 +00002086 result.GetImmediateOutputStream()->Flush();
2087 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002088}
2089
Greg Claytonbbea1332011-07-08 00:48:09 +00002090
Jim Inghamd60d94a2011-03-11 03:53:59 +00002091//--------------------------------------------------------------
2092// class Target::StopHook
2093//--------------------------------------------------------------
2094
2095
2096Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2097 UserID (uid),
2098 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002099 m_commands (),
2100 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002101 m_thread_spec_ap(NULL),
2102 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002103{
2104}
2105
2106Target::StopHook::StopHook (const StopHook &rhs) :
2107 UserID (rhs.GetID()),
2108 m_target_sp (rhs.m_target_sp),
2109 m_commands (rhs.m_commands),
2110 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002111 m_thread_spec_ap (NULL),
2112 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002113{
2114 if (rhs.m_thread_spec_ap.get() != NULL)
2115 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2116}
2117
2118
2119Target::StopHook::~StopHook ()
2120{
2121}
2122
2123void
2124Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2125{
2126 m_thread_spec_ap.reset (specifier);
2127}
2128
2129
2130void
2131Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2132{
2133 int indent_level = s->GetIndentLevel();
2134
2135 s->SetIndentLevel(indent_level + 2);
2136
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002137 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002138 if (m_active)
2139 s->Indent ("State: enabled\n");
2140 else
2141 s->Indent ("State: disabled\n");
2142
2143 if (m_specifier_sp)
2144 {
2145 s->Indent();
2146 s->PutCString ("Specifier:\n");
2147 s->SetIndentLevel (indent_level + 4);
2148 m_specifier_sp->GetDescription (s, level);
2149 s->SetIndentLevel (indent_level + 2);
2150 }
2151
2152 if (m_thread_spec_ap.get() != NULL)
2153 {
2154 StreamString tmp;
2155 s->Indent("Thread:\n");
2156 m_thread_spec_ap->GetDescription (&tmp, level);
2157 s->SetIndentLevel (indent_level + 4);
2158 s->Indent (tmp.GetData());
2159 s->PutCString ("\n");
2160 s->SetIndentLevel (indent_level + 2);
2161 }
2162
2163 s->Indent ("Commands: \n");
2164 s->SetIndentLevel (indent_level + 4);
2165 uint32_t num_commands = m_commands.GetSize();
2166 for (uint32_t i = 0; i < num_commands; i++)
2167 {
2168 s->Indent(m_commands.GetStringAtIndex(i));
2169 s->PutCString ("\n");
2170 }
2171 s->SetIndentLevel (indent_level);
2172}
2173
Greg Clayton73844aa2012-08-22 17:17:09 +00002174//--------------------------------------------------------------
2175// class TargetProperties
2176//--------------------------------------------------------------
2177
2178OptionEnumValueElement
2179lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002180{
Greg Clayton73844aa2012-08-22 17:17:09 +00002181 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2182 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2183 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2184 { 0, NULL, NULL }
2185};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002186
Greg Clayton49ce8962012-08-29 21:13:06 +00002187static OptionEnumValueElement
2188g_inline_breakpoint_enums[] =
2189{
2190 { 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."},
2191 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2192 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2193 { 0, NULL, NULL }
2194};
2195
Greg Clayton73844aa2012-08-22 17:17:09 +00002196static PropertyDefinition
2197g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002198{
Greg Clayton73844aa2012-08-22 17:17:09 +00002199 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2200 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2201 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2202 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2203 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2204 { "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 "
2205 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2206 "some part (starting at the root) of the path to the file when it was built, "
2207 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2208 "Each element of the array is checked in order and the first one that results in a match wins." },
2209 { "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." },
2210 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2211 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2212 { "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 +00002213 { "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." },
2214 { "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 +00002215 { "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." },
2216 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2217 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2218 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2219 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2220 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2221 { "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 +00002222 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2223 "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. "
2224 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2225 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2226 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2227 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2228 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002229 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2230};
2231enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002232{
Greg Clayton73844aa2012-08-22 17:17:09 +00002233 ePropertyDefaultArch,
2234 ePropertyExprPrefix,
2235 ePropertyPreferDynamic,
2236 ePropertyEnableSynthetic,
2237 ePropertySkipPrologue,
2238 ePropertySourceMap,
2239 ePropertyExecutableSearchPaths,
2240 ePropertyMaxChildrenCount,
2241 ePropertyMaxSummaryLength,
2242 ePropertyBreakpointUseAvoidList,
Greg Clayton0c8446c2012-10-17 22:57:12 +00002243 ePropertyArg0,
Greg Clayton73844aa2012-08-22 17:17:09 +00002244 ePropertyRunArgs,
2245 ePropertyEnvVars,
2246 ePropertyInheritEnv,
2247 ePropertyInputPath,
2248 ePropertyOutputPath,
2249 ePropertyErrorPath,
2250 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002251 ePropertyDisableSTDIO,
Greg Clayton87e9d322012-10-19 18:02:49 +00002252 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002253};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002254
Caroline Tice5bc8c972010-09-20 20:44:43 +00002255
Greg Clayton73844aa2012-08-22 17:17:09 +00002256class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002257{
Greg Clayton73844aa2012-08-22 17:17:09 +00002258public:
2259 TargetOptionValueProperties (const ConstString &name) :
2260 OptionValueProperties (name),
2261 m_target (NULL),
2262 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002263 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002264 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002265
Greg Clayton73844aa2012-08-22 17:17:09 +00002266 // This constructor is used when creating TargetOptionValueProperties when it
2267 // is part of a new lldb_private::Target instance. It will copy all current
2268 // global property values as needed
2269 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2270 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2271 m_target (target),
2272 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002273 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002274 }
2275
2276 virtual const Property *
2277 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2278 {
2279 // When gettings the value for a key from the target options, we will always
2280 // try and grab the setting from the current target if there is one. Else we just
2281 // use the one from this instance.
2282 if (idx == ePropertyEnvVars)
2283 GetHostEnvironmentIfNeeded ();
2284
2285 if (exe_ctx)
2286 {
2287 Target *target = exe_ctx->GetTargetPtr();
2288 if (target)
2289 {
2290 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2291 if (this != target_properties)
2292 return target_properties->ProtectedGetPropertyAtIndex (idx);
2293 }
2294 }
2295 return ProtectedGetPropertyAtIndex (idx);
2296 }
2297protected:
2298
2299 void
2300 GetHostEnvironmentIfNeeded () const
2301 {
2302 if (!m_got_host_env)
2303 {
2304 if (m_target)
2305 {
2306 m_got_host_env = true;
2307 const uint32_t idx = ePropertyInheritEnv;
2308 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2309 {
2310 PlatformSP platform_sp (m_target->GetPlatform());
2311 if (platform_sp)
2312 {
2313 StringList env;
2314 if (platform_sp->GetEnvironment(env))
2315 {
2316 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2317 if (env_dict)
2318 {
2319 const bool can_replace = false;
2320 const size_t envc = env.GetSize();
2321 for (size_t idx=0; idx<envc; idx++)
2322 {
2323 const char *env_entry = env.GetStringAtIndex (idx);
2324 if (env_entry)
2325 {
2326 const char *equal_pos = ::strchr(env_entry, '=');
2327 ConstString key;
2328 // It is ok to have environment variables with no values
2329 const char *value = NULL;
2330 if (equal_pos)
2331 {
2332 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2333 if (equal_pos[1])
2334 value = equal_pos + 1;
2335 }
2336 else
2337 {
2338 key.SetCString(env_entry);
2339 }
2340 // Don't allow existing keys to be replaced with ones we get from the platform environment
2341 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2342 }
2343 }
2344 }
2345 }
2346 }
2347 }
2348 }
2349 }
2350 }
2351 Target *m_target;
2352 mutable bool m_got_host_env;
2353};
2354
2355TargetProperties::TargetProperties (Target *target) :
2356 Properties ()
2357{
2358 if (target)
2359 {
2360 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002361 }
2362 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002363 {
2364 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2365 m_collection_sp->Initialize(g_properties);
2366 m_collection_sp->AppendProperty(ConstString("process"),
2367 ConstString("Settings specify to processes."),
2368 true,
2369 Process::GetGlobalProperties()->GetValueProperties());
2370 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002371}
2372
Greg Clayton73844aa2012-08-22 17:17:09 +00002373TargetProperties::~TargetProperties ()
2374{
2375}
2376ArchSpec
2377TargetProperties::GetDefaultArchitecture () const
2378{
2379 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2380 if (value)
2381 return value->GetCurrentValue();
2382 return ArchSpec();
2383}
2384
2385void
2386TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2387{
2388 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2389 if (value)
2390 return value->SetCurrentValue(arch, true);
2391}
2392
2393lldb::DynamicValueType
2394TargetProperties::GetPreferDynamicValue() const
2395{
2396 const uint32_t idx = ePropertyPreferDynamic;
2397 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2398}
2399
2400bool
2401TargetProperties::GetDisableASLR () const
2402{
2403 const uint32_t idx = ePropertyDisableASLR;
2404 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2405}
2406
2407void
2408TargetProperties::SetDisableASLR (bool b)
2409{
2410 const uint32_t idx = ePropertyDisableASLR;
2411 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2412}
2413
2414bool
2415TargetProperties::GetDisableSTDIO () const
2416{
2417 const uint32_t idx = ePropertyDisableSTDIO;
2418 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2419}
2420
2421void
2422TargetProperties::SetDisableSTDIO (bool b)
2423{
2424 const uint32_t idx = ePropertyDisableSTDIO;
2425 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2426}
2427
Greg Clayton49ce8962012-08-29 21:13:06 +00002428InlineStrategy
2429TargetProperties::GetInlineStrategy () const
2430{
2431 const uint32_t idx = ePropertyInlineStrategy;
2432 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2433}
2434
Greg Clayton0c8446c2012-10-17 22:57:12 +00002435const char *
2436TargetProperties::GetArg0 () const
2437{
2438 const uint32_t idx = ePropertyArg0;
2439 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2440}
2441
2442void
2443TargetProperties::SetArg0 (const char *arg)
2444{
2445 const uint32_t idx = ePropertyArg0;
2446 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2447}
2448
Greg Clayton73844aa2012-08-22 17:17:09 +00002449bool
2450TargetProperties::GetRunArguments (Args &args) const
2451{
2452 const uint32_t idx = ePropertyRunArgs;
2453 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2454}
2455
2456void
2457TargetProperties::SetRunArguments (const Args &args)
2458{
2459 const uint32_t idx = ePropertyRunArgs;
2460 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2461}
2462
2463size_t
2464TargetProperties::GetEnvironmentAsArgs (Args &env) const
2465{
2466 const uint32_t idx = ePropertyEnvVars;
2467 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2468}
2469
2470bool
2471TargetProperties::GetSkipPrologue() const
2472{
2473 const uint32_t idx = ePropertySkipPrologue;
2474 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2475}
2476
2477PathMappingList &
2478TargetProperties::GetSourcePathMap () const
2479{
2480 const uint32_t idx = ePropertySourceMap;
2481 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2482 assert(option_value);
2483 return option_value->GetCurrentValue();
2484}
2485
2486FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002487TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002488{
2489 const uint32_t idx = ePropertyExecutableSearchPaths;
2490 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2491 assert(option_value);
2492 return option_value->GetCurrentValue();
2493}
2494
2495bool
2496TargetProperties::GetEnableSyntheticValue () const
2497{
2498 const uint32_t idx = ePropertyEnableSynthetic;
2499 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2500}
2501
2502uint32_t
2503TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2504{
2505 const uint32_t idx = ePropertyMaxChildrenCount;
2506 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2507}
2508
2509uint32_t
2510TargetProperties::GetMaximumSizeOfStringSummary() const
2511{
2512 const uint32_t idx = ePropertyMaxSummaryLength;
2513 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2514}
2515
2516FileSpec
2517TargetProperties::GetStandardInputPath () const
2518{
2519 const uint32_t idx = ePropertyInputPath;
2520 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2521}
2522
2523void
2524TargetProperties::SetStandardInputPath (const char *p)
2525{
2526 const uint32_t idx = ePropertyInputPath;
2527 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2528}
2529
2530FileSpec
2531TargetProperties::GetStandardOutputPath () const
2532{
2533 const uint32_t idx = ePropertyOutputPath;
2534 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2535}
2536
2537void
2538TargetProperties::SetStandardOutputPath (const char *p)
2539{
2540 const uint32_t idx = ePropertyOutputPath;
2541 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2542}
2543
2544FileSpec
2545TargetProperties::GetStandardErrorPath () const
2546{
2547 const uint32_t idx = ePropertyErrorPath;
2548 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2549}
2550
Greg Claytonc6e82e42012-08-22 18:39:03 +00002551const char *
2552TargetProperties::GetExpressionPrefixContentsAsCString ()
2553{
2554 const uint32_t idx = ePropertyExprPrefix;
2555 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2556 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002557 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002558 const bool null_terminate = true;
2559 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002560 if (data_sp)
2561 return (const char *) data_sp->GetBytes();
2562 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002563 return NULL;
2564}
2565
Greg Clayton73844aa2012-08-22 17:17:09 +00002566void
2567TargetProperties::SetStandardErrorPath (const char *p)
2568{
2569 const uint32_t idx = ePropertyErrorPath;
2570 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2571}
2572
2573bool
2574TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2575{
2576 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2577 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2578}
2579
2580const TargetPropertiesSP &
2581Target::GetGlobalProperties()
2582{
2583 static TargetPropertiesSP g_settings_sp;
2584 if (!g_settings_sp)
2585 {
2586 g_settings_sp.reset (new TargetProperties (NULL));
2587 }
2588 return g_settings_sp;
2589}
2590
Jim Ingham5a15e692012-02-16 06:50:00 +00002591const ConstString &
2592Target::TargetEventData::GetFlavorString ()
2593{
2594 static ConstString g_flavor ("Target::TargetEventData");
2595 return g_flavor;
2596}
2597
2598const ConstString &
2599Target::TargetEventData::GetFlavor () const
2600{
2601 return TargetEventData::GetFlavorString ();
2602}
2603
2604Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2605 EventData(),
2606 m_target_sp (new_target_sp)
2607{
2608}
2609
2610Target::TargetEventData::~TargetEventData()
2611{
2612
2613}
2614
2615void
2616Target::TargetEventData::Dump (Stream *s) const
2617{
2618
2619}
2620
2621const TargetSP
2622Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2623{
2624 TargetSP target_sp;
2625
2626 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2627 if (data)
2628 target_sp = data->m_target_sp;
2629
2630 return target_sp;
2631}
2632
2633const Target::TargetEventData *
2634Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2635{
2636 if (event_ptr)
2637 {
2638 const EventData *event_data = event_ptr->GetData();
2639 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2640 return static_cast <const TargetEventData *> (event_ptr->GetData());
2641 }
2642 return NULL;
2643}
2644