blob: 0dd34631da70f5c38b437b282b01b69305dd3bef [file] [log] [blame]
Chris Lattner30fdc8d2010-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
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000029#include "lldb/Core/Timer.h"
30#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000031#include "lldb/Expression/ClangASTSource.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000032#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000034#include "lldb/Interpreter/CommandInterpreter.h"
35#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000036#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000037#include "lldb/Interpreter/OptionValues.h"
38#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/lldb-private-log.h"
40#include "lldb/Symbol/ObjectFile.h"
41#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000042#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:59 +000043#include "lldb/Target/Thread.h"
44#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045
46using namespace lldb;
47using namespace lldb_private;
48
Jim Ingham4bddaeb2012-02-16 06:50:00 +000049ConstString &
50Target::GetStaticBroadcasterClass ()
51{
52 static ConstString class_name ("lldb.target");
53 return class_name;
54}
55
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056//----------------------------------------------------------------------
57// Target constructor
58//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:51 +000059Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +000060 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000061 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000062 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000063 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000064 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000065 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000066 m_arch (target_arch),
67 m_images (),
Greg Claytonf5e56de2010-09-14 23:36:40 +000068 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 m_breakpoint_list (false),
70 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000071 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000072 m_process_sp (),
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +000073 m_valid (true),
Greg Clayton32e0a752011-03-30 18:16:51 +000074 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000076 m_scratch_ast_context_ap (NULL),
Sean Callanan686b2312011-11-16 18:20:47 +000077 m_scratch_ast_source_ap (NULL),
78 m_ast_importer_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:59 +000079 m_persistent_variables (),
Jim Inghame37d6052011-09-13 00:29:56 +000080 m_source_manager(*this),
Greg Clayton32e0a752011-03-30 18:16:51 +000081 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000082 m_stop_hook_next_id (0),
Enrico Granatac5bc4122012-03-27 02:35:13 +000083 m_suppress_stop_hooks (false),
84 m_suppress_synthetic_value(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000086 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
87 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
88 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham4bddaeb2012-02-16 06:50:00 +000089
90 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +000091
Greg Clayton2d4edfb2010-11-06 01:53:30 +000092 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093 if (log)
94 log->Printf ("%p Target::Target()", this);
95}
96
97//----------------------------------------------------------------------
98// Destructor
99//----------------------------------------------------------------------
100Target::~Target()
101{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000102 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 if (log)
104 log->Printf ("%p Target::~Target()", this);
105 DeleteCurrentProcess ();
106}
107
108void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000109Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110{
Greg Clayton89411422010-10-08 00:21:05 +0000111// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000112 if (description_level != lldb::eDescriptionLevelBrief)
113 {
114 s->Indent();
115 s->PutCString("Target\n");
116 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000117 m_images.Dump(s);
118 m_breakpoint_list.Dump(s);
119 m_internal_breakpoint_list.Dump(s);
120 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000121 }
122 else
123 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000124 Module *exe_module = GetExecutableModulePointer();
125 if (exe_module)
126 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000127 else
128 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000129 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130}
131
132void
133Target::DeleteCurrentProcess ()
134{
135 if (m_process_sp.get())
136 {
Greg Clayton17f69202010-09-14 23:52:43 +0000137 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 if (m_process_sp->IsAlive())
139 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000140
141 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
143 // Do any cleanup of the target we need to do between process instances.
144 // NB It is better to do this before destroying the process in case the
145 // clean up needs some help from the process.
146 m_breakpoint_list.ClearAllBreakpointSites();
147 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chen01a67862011-10-14 00:42:25 +0000148 // Disable watchpoints just on the debugger side.
Johnny Chena4d6bc92012-02-25 06:44:30 +0000149 Mutex::Locker locker;
150 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000151 DisableAllWatchpoints(false);
Johnny Chena4d6bc92012-02-25 06:44:30 +0000152 ClearAllWatchpointHitCounts();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 m_process_sp.reset();
154 }
155}
156
157const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000158Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159{
160 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000161 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 return m_process_sp;
163}
164
165const lldb::ProcessSP &
166Target::GetProcessSP () const
167{
168 return m_process_sp;
169}
170
Greg Clayton3418c852011-08-10 02:10:13 +0000171void
172Target::Destroy()
173{
174 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000175 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000176 DeleteCurrentProcess ();
177 m_platform_sp.reset();
178 m_arch.Clear();
179 m_images.Clear();
180 m_section_load_list.Clear();
181 const bool notify = false;
182 m_breakpoint_list.RemoveAll(notify);
183 m_internal_breakpoint_list.RemoveAll(notify);
184 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000185 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000186 m_search_filter_sp.reset();
187 m_image_search_paths.Clear(notify);
188 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +0000189 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +0000190 m_ast_importer_ap.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000191 m_persistent_variables.Clear();
192 m_stop_hooks.clear();
193 m_stop_hook_next_id = 0;
194 m_suppress_stop_hooks = false;
Enrico Granatac5bc4122012-03-27 02:35:13 +0000195 m_suppress_synthetic_value = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000196}
197
198
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199BreakpointList &
200Target::GetBreakpointList(bool internal)
201{
202 if (internal)
203 return m_internal_breakpoint_list;
204 else
205 return m_breakpoint_list;
206}
207
208const BreakpointList &
209Target::GetBreakpointList(bool internal) const
210{
211 if (internal)
212 return m_internal_breakpoint_list;
213 else
214 return m_breakpoint_list;
215}
216
217BreakpointSP
218Target::GetBreakpointByID (break_id_t break_id)
219{
220 BreakpointSP bp_sp;
221
222 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
223 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
224 else
225 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
226
227 return bp_sp;
228}
229
230BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000231Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000232 const FileSpecList *source_file_spec_list,
233 RegularExpression &source_regex,
234 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235{
Jim Ingham87df91b2011-09-23 00:54:11 +0000236 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
237 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham969795f2011-09-21 01:17:13 +0000238 return CreateBreakpoint (filter_sp, resolver_sp, internal);
239}
240
241
242BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000243Target::CreateBreakpoint (const FileSpecList *containingModules,
244 const FileSpec &file,
245 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000246 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000247 LazyBool skip_prologue,
248 bool internal)
Jim Ingham969795f2011-09-21 01:17:13 +0000249{
Greg Clayton1f746072012-08-29 21:13:06 +0000250 if (check_inlines == eLazyBoolCalculate)
251 {
252 const InlineStrategy inline_strategy = GetInlineStrategy();
253 switch (inline_strategy)
254 {
255 case eInlineBreakpointsNever:
256 check_inlines = eLazyBoolNo;
257 break;
258
259 case eInlineBreakpointsHeaders:
260 if (file.IsSourceImplementationFile())
261 check_inlines = eLazyBoolNo;
262 else
263 check_inlines = eLazyBoolYes;
264 break;
265
266 case eInlineBreakpointsAlways:
267 check_inlines = eLazyBoolYes;
268 break;
269 }
270 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000271 SearchFilterSP filter_sp;
272 if (check_inlines == eLazyBoolNo)
273 {
274 // Not checking for inlines, we are looking only for matching compile units
275 FileSpecList compile_unit_list;
276 compile_unit_list.Append (file);
277 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
278 }
279 else
280 {
281 filter_sp = GetSearchFilterForModuleList (containingModules);
282 }
Greg Clayton1f746072012-08-29 21:13:06 +0000283 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
284 file,
285 line_no,
286 check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000287 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 return CreateBreakpoint (filter_sp, resolver_sp, internal);
289}
290
291
292BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000293Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 Address so_addr;
296 // Attempt to resolve our load address if possible, though it is ok if
297 // it doesn't resolve to section/offset.
298
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000299 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40 +0000300 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000301 if (!so_addr.IsValid())
302 {
303 // The address didn't resolve, so just set this as an absolute address
304 so_addr.SetOffset (addr);
305 }
306 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307 return bp_sp;
308}
309
310BreakpointSP
311Target::CreateBreakpoint (Address &addr, bool internal)
312{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000313 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
315 return CreateBreakpoint (filter_sp, resolver_sp, internal);
316}
317
318BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000319Target::CreateBreakpoint (const FileSpecList *containingModules,
320 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000321 const char *func_name,
322 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000323 LazyBool skip_prologue,
324 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000326 BreakpointSP bp_sp;
327 if (func_name)
328 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000329 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17 +0000330
331 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
332 func_name,
333 func_name_type_mask,
334 Breakpoint::Exact,
335 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43 +0000336 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
337 }
338 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339}
340
Jim Inghamfab10e82012-03-06 00:37:27 +0000341lldb::BreakpointSP
342Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000343 const FileSpecList *containingSourceFiles,
344 const std::vector<std::string> &func_names,
345 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000346 LazyBool skip_prologue,
347 bool internal)
Jim Inghamfab10e82012-03-06 00:37:27 +0000348{
349 BreakpointSP bp_sp;
350 size_t num_names = func_names.size();
351 if (num_names > 0)
352 {
353 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
354
355 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
356 func_names,
357 func_name_type_mask,
358 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
359 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
360 }
361 return bp_sp;
362}
363
Jim Ingham133e0fb2012-03-03 02:05:11 +0000364BreakpointSP
365Target::CreateBreakpoint (const FileSpecList *containingModules,
366 const FileSpecList *containingSourceFiles,
367 const char *func_names[],
368 size_t num_names,
369 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000370 LazyBool skip_prologue,
371 bool internal)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000372{
373 BreakpointSP bp_sp;
374 if (num_names > 0)
375 {
376 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
377
378 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
379 func_names,
380 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000381 func_name_type_mask,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000382 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
383 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
384 }
385 return bp_sp;
386}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387
388SearchFilterSP
389Target::GetSearchFilterForModule (const FileSpec *containingModule)
390{
391 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 if (containingModule != NULL)
393 {
394 // TODO: We should look into sharing module based search filters
395 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000396 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 }
398 else
399 {
400 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000401 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 filter_sp = m_search_filter_sp;
403 }
404 return filter_sp;
405}
406
Jim Ingham969795f2011-09-21 01:17:13 +0000407SearchFilterSP
408Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
409{
410 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000411 if (containingModules && containingModules->GetSize() != 0)
412 {
413 // TODO: We should look into sharing module based search filters
414 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000415 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000416 }
417 else
418 {
419 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000420 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000421 filter_sp = m_search_filter_sp;
422 }
423 return filter_sp;
424}
425
Jim Ingham87df91b2011-09-23 00:54:11 +0000426SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000427Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
428 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000429{
430 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
431 return GetSearchFilterForModuleList(containingModules);
432
433 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000434 if (containingModules == NULL)
435 {
436 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
437 // but that will take a little reworking.
438
Greg Claytone1cd1be2012-01-29 20:56:30 +0000439 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000440 }
441 else
442 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000443 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000444 }
445 return filter_sp;
446}
447
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000449Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000450 const FileSpecList *containingSourceFiles,
451 RegularExpression &func_regex,
452 LazyBool skip_prologue,
453 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454{
Jim Ingham87df91b2011-09-23 00:54:11 +0000455 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17 +0000456 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
457 func_regex,
458 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459
460 return CreateBreakpoint (filter_sp, resolver_sp, internal);
461}
462
Jim Ingham219ba192012-03-05 04:47:34 +0000463lldb::BreakpointSP
464Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
465{
466 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
467}
468
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469BreakpointSP
470Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
471{
472 BreakpointSP bp_sp;
473 if (filter_sp && resolver_sp)
474 {
475 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
476 resolver_sp->SetBreakpoint (bp_sp.get());
477
478 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000479 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 else
Greg Clayton9fed0d82010-07-23 23:33:17 +0000481 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000483 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484 if (log)
485 {
486 StreamString s;
487 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
488 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
489 }
490
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 bp_sp->ResolveBreakpoint();
492 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000493
494 if (!internal && bp_sp)
495 {
496 m_last_created_breakpoint = bp_sp;
497 }
498
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 return bp_sp;
500}
501
Johnny Chen86364b42011-09-20 23:28:55 +0000502bool
503Target::ProcessIsValid()
504{
505 return (m_process_sp && m_process_sp->IsAlive());
506}
507
Johnny Chenb90827e2012-06-04 23:19:54 +0000508static bool
509CheckIfWatchpointsExhausted(Target *target, Error &error)
510{
511 uint32_t num_supported_hardware_watchpoints;
512 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
513 if (rc.Success())
514 {
515 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
516 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
517 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
518 num_supported_hardware_watchpoints);
519 }
520 return false;
521}
522
Johnny Chen01a67862011-10-14 00:42:25 +0000523// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000524// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000525WatchpointSP
Johnny Chenb90827e2012-06-04 23:19:54 +0000526Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000527{
Johnny Chen0c406372011-09-14 20:23:45 +0000528 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
529 if (log)
Greg Clayton43e0af02012-09-18 18:04:04 +0000530 log->Printf("Target::%s (addr = 0x%8.8llx size = %llu type = %u)\n",
531 __FUNCTION__, addr, (uint64_t)size, type);
Johnny Chen0c406372011-09-14 20:23:45 +0000532
Johnny Chen01a67862011-10-14 00:42:25 +0000533 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000534 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000535 {
536 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000537 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000538 }
Johnny Chen45e541f2011-09-14 22:20:15 +0000539 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000540 {
541 if (size == 0)
542 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
543 else
544 error.SetErrorStringWithFormat("invalid watch address: %llu", addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000545 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000546 }
Johnny Chen7313a642011-09-13 01:15:36 +0000547
Johnny Chen01a67862011-10-14 00:42:25 +0000548 // Currently we only support one watchpoint per address, with total number
549 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000550
551 // Grab the list mutex while doing operations.
552 Mutex::Locker locker;
553 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000554 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000555 if (matched_sp)
556 {
Johnny Chen0c406372011-09-14 20:23:45 +0000557 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000558 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000559 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
560 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000561 // Return the existing watchpoint if both size and type match.
Johnny Chen45e541f2011-09-14 22:20:15 +0000562 if (size == old_size && type == old_type) {
Johnny Chen01a67862011-10-14 00:42:25 +0000563 wp_sp = matched_sp;
564 wp_sp->SetEnabled(false);
Johnny Chen45e541f2011-09-14 22:20:15 +0000565 } else {
Johnny Chen01a67862011-10-14 00:42:25 +0000566 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen45e541f2011-09-14 22:20:15 +0000567 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chen01a67862011-10-14 00:42:25 +0000568 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen45e541f2011-09-14 22:20:15 +0000569 }
Johnny Chen3c532582011-09-13 23:29:31 +0000570 }
571
Johnny Chen01a67862011-10-14 00:42:25 +0000572 if (!wp_sp) {
573 Watchpoint *new_wp = new Watchpoint(addr, size);
574 if (!new_wp) {
575 printf("Watchpoint ctor failed, out of memory?\n");
576 return wp_sp;
Johnny Chen45e541f2011-09-14 22:20:15 +0000577 }
Johnny Chen01a67862011-10-14 00:42:25 +0000578 new_wp->SetWatchpointType(type);
579 new_wp->SetTarget(this);
580 wp_sp.reset(new_wp);
581 m_watchpoint_list.Add(wp_sp);
Johnny Chen45e541f2011-09-14 22:20:15 +0000582 }
Johnny Chen0c406372011-09-14 20:23:45 +0000583
Johnny Chenb90827e2012-06-04 23:19:54 +0000584 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen0c406372011-09-14 20:23:45 +0000585 if (log)
586 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
587 __FUNCTION__,
Johnny Chenb90827e2012-06-04 23:19:54 +0000588 error.Success() ? "succeeded" : "failed",
Johnny Chen01a67862011-10-14 00:42:25 +0000589 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000590
Johnny Chenb90827e2012-06-04 23:19:54 +0000591 if (error.Fail()) {
Johnny Chen41b77262012-03-26 22:00:10 +0000592 // Enabling the watchpoint on the device side failed.
593 // Remove the said watchpoint from the list maintained by the target instance.
594 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chenb90827e2012-06-04 23:19:54 +0000595 // See if we could provide more helpful error message.
596 if (!CheckIfWatchpointsExhausted(this, error))
597 {
598 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
599 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
600 }
Johnny Chen01a67862011-10-14 00:42:25 +0000601 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000602 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000603 else
Johnny Chen01a67862011-10-14 00:42:25 +0000604 m_last_created_watchpoint = wp_sp;
605 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000606}
607
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608void
609Target::RemoveAllBreakpoints (bool internal_also)
610{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000611 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 if (log)
613 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
614
Greg Clayton9fed0d82010-07-23 23:33:17 +0000615 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000617 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000618
619 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620}
621
622void
623Target::DisableAllBreakpoints (bool internal_also)
624{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000625 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 if (log)
627 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
628
629 m_breakpoint_list.SetEnabledAll (false);
630 if (internal_also)
631 m_internal_breakpoint_list.SetEnabledAll (false);
632}
633
634void
635Target::EnableAllBreakpoints (bool internal_also)
636{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000637 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 if (log)
639 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
640
641 m_breakpoint_list.SetEnabledAll (true);
642 if (internal_also)
643 m_internal_breakpoint_list.SetEnabledAll (true);
644}
645
646bool
647Target::RemoveBreakpointByID (break_id_t break_id)
648{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000649 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 if (log)
651 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
652
653 if (DisableBreakpointByID (break_id))
654 {
655 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000656 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000658 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000659 if (m_last_created_breakpoint)
660 {
661 if (m_last_created_breakpoint->GetID() == break_id)
662 m_last_created_breakpoint.reset();
663 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000664 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000665 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 return true;
667 }
668 return false;
669}
670
671bool
672Target::DisableBreakpointByID (break_id_t break_id)
673{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000674 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 if (log)
676 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
677
678 BreakpointSP bp_sp;
679
680 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
681 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
682 else
683 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
684 if (bp_sp)
685 {
686 bp_sp->SetEnabled (false);
687 return true;
688 }
689 return false;
690}
691
692bool
693Target::EnableBreakpointByID (break_id_t break_id)
694{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000695 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 if (log)
697 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
698 __FUNCTION__,
699 break_id,
700 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
701
702 BreakpointSP bp_sp;
703
704 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
705 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
706 else
707 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
708
709 if (bp_sp)
710 {
711 bp_sp->SetEnabled (true);
712 return true;
713 }
714 return false;
715}
716
Johnny Chenedf50372011-09-23 21:21:43 +0000717// The flag 'end_to_end', default to true, signifies that the operation is
718// performed end to end, for both the debugger and the debuggee.
719
Johnny Chen01a67862011-10-14 00:42:25 +0000720// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
721// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000722bool
Johnny Chen01a67862011-10-14 00:42:25 +0000723Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000724{
725 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
726 if (log)
727 log->Printf ("Target::%s\n", __FUNCTION__);
728
Johnny Chenedf50372011-09-23 21:21:43 +0000729 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000730 m_watchpoint_list.RemoveAll();
Johnny Chenedf50372011-09-23 21:21:43 +0000731 return true;
732 }
733
734 // Otherwise, it's an end to end operation.
735
Johnny Chen86364b42011-09-20 23:28:55 +0000736 if (!ProcessIsValid())
737 return false;
738
Johnny Chen01a67862011-10-14 00:42:25 +0000739 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000740 for (size_t i = 0; i < num_watchpoints; ++i)
741 {
Johnny Chen01a67862011-10-14 00:42:25 +0000742 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
743 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000744 return false;
745
Johnny Chen01a67862011-10-14 00:42:25 +0000746 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000747 if (rc.Fail())
748 return false;
749 }
Johnny Chen01a67862011-10-14 00:42:25 +0000750 m_watchpoint_list.RemoveAll ();
Johnny Chen86364b42011-09-20 23:28:55 +0000751 return true; // Success!
752}
753
Johnny Chen01a67862011-10-14 00:42:25 +0000754// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
755// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000756bool
Johnny Chen01a67862011-10-14 00:42:25 +0000757Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000758{
759 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
760 if (log)
761 log->Printf ("Target::%s\n", __FUNCTION__);
762
Johnny Chenedf50372011-09-23 21:21:43 +0000763 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000764 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000765 return true;
766 }
767
768 // Otherwise, it's an end to end operation.
769
Johnny Chen86364b42011-09-20 23:28:55 +0000770 if (!ProcessIsValid())
771 return false;
772
Johnny Chen01a67862011-10-14 00:42:25 +0000773 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000774 for (size_t i = 0; i < num_watchpoints; ++i)
775 {
Johnny Chen01a67862011-10-14 00:42:25 +0000776 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
777 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000778 return false;
779
Johnny Chen01a67862011-10-14 00:42:25 +0000780 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000781 if (rc.Fail())
782 return false;
783 }
Johnny Chen86364b42011-09-20 23:28:55 +0000784 return true; // Success!
785}
786
Johnny Chen01a67862011-10-14 00:42:25 +0000787// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
788// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000789bool
Johnny Chen01a67862011-10-14 00:42:25 +0000790Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000791{
792 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
793 if (log)
794 log->Printf ("Target::%s\n", __FUNCTION__);
795
Johnny Chenedf50372011-09-23 21:21:43 +0000796 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000797 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000798 return true;
799 }
800
801 // Otherwise, it's an end to end operation.
802
Johnny Chen86364b42011-09-20 23:28:55 +0000803 if (!ProcessIsValid())
804 return false;
805
Johnny Chen01a67862011-10-14 00:42:25 +0000806 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000807 for (size_t i = 0; i < num_watchpoints; ++i)
808 {
Johnny Chen01a67862011-10-14 00:42:25 +0000809 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
810 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000811 return false;
812
Johnny Chen01a67862011-10-14 00:42:25 +0000813 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000814 if (rc.Fail())
815 return false;
816 }
Johnny Chen86364b42011-09-20 23:28:55 +0000817 return true; // Success!
818}
819
Johnny Chena4d6bc92012-02-25 06:44:30 +0000820// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
821bool
822Target::ClearAllWatchpointHitCounts ()
823{
824 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
825 if (log)
826 log->Printf ("Target::%s\n", __FUNCTION__);
827
828 size_t num_watchpoints = m_watchpoint_list.GetSize();
829 for (size_t i = 0; i < num_watchpoints; ++i)
830 {
831 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
832 if (!wp_sp)
833 return false;
834
835 wp_sp->ResetHitCount();
836 }
837 return true; // Success!
838}
839
Johnny Chen01a67862011-10-14 00:42:25 +0000840// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000841// during these operations.
842bool
Johnny Chen01a67862011-10-14 00:42:25 +0000843Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000844{
845 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
846 if (log)
847 log->Printf ("Target::%s\n", __FUNCTION__);
848
849 if (!ProcessIsValid())
850 return false;
851
Johnny Chen01a67862011-10-14 00:42:25 +0000852 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000853 for (size_t i = 0; i < num_watchpoints; ++i)
854 {
Johnny Chen01a67862011-10-14 00:42:25 +0000855 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
856 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000857 return false;
858
Johnny Chen01a67862011-10-14 00:42:25 +0000859 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000860 }
861 return true; // Success!
862}
863
Johnny Chen01a67862011-10-14 00:42:25 +0000864// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000865bool
Johnny Chen01a67862011-10-14 00:42:25 +0000866Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000867{
868 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
869 if (log)
870 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
871
872 if (!ProcessIsValid())
873 return false;
874
Johnny Chen01a67862011-10-14 00:42:25 +0000875 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
876 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000877 {
Johnny Chen01a67862011-10-14 00:42:25 +0000878 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000879 if (rc.Success())
880 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000881
Johnny Chenf04ee932011-09-22 18:04:58 +0000882 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000883 }
884 return false;
885}
886
Johnny Chen01a67862011-10-14 00:42:25 +0000887// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000888bool
Johnny Chen01a67862011-10-14 00:42:25 +0000889Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000890{
891 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
892 if (log)
893 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
894
895 if (!ProcessIsValid())
896 return false;
897
Johnny Chen01a67862011-10-14 00:42:25 +0000898 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
899 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000900 {
Johnny Chen01a67862011-10-14 00:42:25 +0000901 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000902 if (rc.Success())
903 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000904
Johnny Chenf04ee932011-09-22 18:04:58 +0000905 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000906 }
907 return false;
908}
909
Johnny Chen01a67862011-10-14 00:42:25 +0000910// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000911bool
Johnny Chen01a67862011-10-14 00:42:25 +0000912Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000913{
914 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
915 if (log)
916 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
917
Johnny Chen01a67862011-10-14 00:42:25 +0000918 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +0000919 {
Johnny Chen01a67862011-10-14 00:42:25 +0000920 m_watchpoint_list.Remove(watch_id);
Johnny Chen86364b42011-09-20 23:28:55 +0000921 return true;
922 }
923 return false;
924}
925
Johnny Chen01a67862011-10-14 00:42:25 +0000926// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +0000927bool
Johnny Chen01a67862011-10-14 00:42:25 +0000928Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000929{
930 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
931 if (log)
932 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
933
934 if (!ProcessIsValid())
935 return false;
936
Johnny Chen01a67862011-10-14 00:42:25 +0000937 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
938 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000939 {
Johnny Chen01a67862011-10-14 00:42:25 +0000940 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000941 return true;
942 }
943 return false;
944}
945
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946ModuleSP
947Target::GetExecutableModule ()
948{
Greg Claytonaa149cb2011-08-11 02:48:45 +0000949 return m_images.GetModuleAtIndex(0);
950}
951
952Module*
953Target::GetExecutableModulePointer ()
954{
955 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956}
957
958void
959Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
960{
961 m_images.Clear();
962 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +0000963 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +0000964 m_ast_importer_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965
966 if (executable_sp.get())
967 {
968 Timer scoped_timer (__PRETTY_FUNCTION__,
969 "Target::SetExecutableModule (executable = '%s/%s')",
970 executable_sp->GetFileSpec().GetDirectory().AsCString(),
971 executable_sp->GetFileSpec().GetFilename().AsCString());
972
973 m_images.Append(executable_sp); // The first image is our exectuable file
974
Jim Ingham5aee1622010-08-09 23:31:02 +0000975 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton32e0a752011-03-30 18:16:51 +0000976 if (!m_arch.IsValid())
977 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02 +0000978
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +0000980 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000982 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983 {
984 executable_objfile->GetDependentModules(dependent_files);
985 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
986 {
Greg Claytonded470d2011-03-19 01:12:21 +0000987 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
988 FileSpec platform_dependent_file_spec;
989 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55 +0000990 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +0000991 else
992 platform_dependent_file_spec = dependent_file_spec;
993
Greg Claytonb9a01b32012-02-26 05:51:37 +0000994 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
995 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 if (image_module_sp.get())
997 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998 ObjectFile *objfile = image_module_sp->GetObjectFile();
999 if (objfile)
1000 objfile->GetDependentModules(dependent_files);
1001 }
1002 }
1003 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 }
1005}
1006
1007
Jim Ingham5aee1622010-08-09 23:31:02 +00001008bool
1009Target::SetArchitecture (const ArchSpec &arch_spec)
1010{
Greg Clayton70512312012-05-08 01:45:38 +00001011 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001012 {
Greg Clayton70512312012-05-08 01:45:38 +00001013 // If we haven't got a valid arch spec, or the architectures are
1014 // compatible, so just update the architecture. Architectures can be
1015 // equal, yet the triple OS and vendor might change, so we need to do
1016 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001017 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001018 return true;
1019 }
1020 else
1021 {
1022 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51 +00001023 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001024 ModuleSP executable_sp = GetExecutableModule ();
1025 m_images.Clear();
1026 m_scratch_ast_context_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001027 m_scratch_ast_source_ap.reset();
1028 m_ast_importer_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00001029 // Need to do something about unsetting breakpoints.
1030
1031 if (executable_sp)
1032 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001033 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1034 Error error = ModuleList::GetSharedModule (module_spec,
1035 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001036 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001037 NULL,
1038 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001039
1040 if (!error.Fail() && executable_sp)
1041 {
1042 SetExecutableModule (executable_sp, true);
1043 return true;
1044 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001045 }
1046 }
Greg Clayton70512312012-05-08 01:45:38 +00001047 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001048}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050void
1051Target::ModuleAdded (ModuleSP &module_sp)
1052{
1053 // A module is being added to this target for the first time
1054 ModuleList module_list;
1055 module_list.Append(module_sp);
1056 ModulesDidLoad (module_list);
1057}
1058
1059void
1060Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
1061{
Jim Inghame716ae02011-08-03 01:00:06 +00001062 // A module is replacing an already added module
Jim Ingham4a94c912012-05-17 18:38:42 +00001063 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064}
1065
1066void
1067Target::ModulesDidLoad (ModuleList &module_list)
1068{
1069 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1070 // TODO: make event data that packages up the module_list
1071 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1072}
1073
1074void
1075Target::ModulesDidUnload (ModuleList &module_list)
1076{
1077 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26 +00001078
1079 // Remove the images from the target image list
1080 m_images.Remove(module_list);
1081
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082 // TODO: make event data that packages up the module_list
1083 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1084}
1085
Jim Inghamc6674fd2011-10-28 23:14:11 +00001086
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001087bool
Greg Claytonb9a01b32012-02-26 05:51:37 +00001088Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001089{
1090
Greg Clayton67cc0632012-08-22 17:17:09 +00001091 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001092 {
1093 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001094 ModuleSpec module_spec (module_file_spec);
1095 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001096
1097 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1098 // black list.
1099 if (num_modules > 0)
1100 {
1101 for (int i = 0; i < num_modules; i++)
1102 {
1103 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1104 return false;
1105 }
1106 return true;
1107 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001108 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001109 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001110}
1111
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001112bool
Jim Inghamc6674fd2011-10-28 23:14:11 +00001113Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1114{
Greg Clayton67cc0632012-08-22 17:17:09 +00001115 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001116 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001117 if (m_platform_sp)
1118 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001119 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001120 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001121}
1122
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123size_t
Greg Claytondb598232011-01-07 01:57:07 +00001124Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1125{
Greg Claytone72dfb32012-02-24 01:59:29 +00001126 SectionSP section_sp (addr.GetSection());
1127 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001128 {
Jason Molenda216d91f2012-04-25 00:06:56 +00001129 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1130 if (section_sp->IsEncrypted())
1131 {
Greg Clayton57f06302012-05-25 17:05:55 +00001132 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001133 return 0;
1134 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001135 ModuleSP module_sp (section_sp->GetModule());
1136 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001137 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001138 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1139 if (objfile)
1140 {
1141 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1142 addr.GetOffset(),
1143 dst,
1144 dst_len);
1145 if (bytes_read > 0)
1146 return bytes_read;
1147 else
1148 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1149 }
Greg Claytondb598232011-01-07 01:57:07 +00001150 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001151 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001152 }
1153 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001154 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001155 }
1156 else
Greg Claytondb598232011-01-07 01:57:07 +00001157 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001158
Greg Claytondb598232011-01-07 01:57:07 +00001159 return 0;
1160}
1161
1162size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001163Target::ReadMemory (const Address& addr,
1164 bool prefer_file_cache,
1165 void *dst,
1166 size_t dst_len,
1167 Error &error,
1168 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001171
Enrico Granata9128ee22011-09-06 19:20:51 +00001172 // if we end up reading this from process memory, we will fill this
1173 // with the actual load address
1174 if (load_addr_ptr)
1175 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1176
Greg Claytondb598232011-01-07 01:57:07 +00001177 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001178
1179 addr_t load_addr = LLDB_INVALID_ADDRESS;
1180 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001181 Address resolved_addr;
1182 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001183 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001184 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001185 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001186 // No sections are loaded, so we must assume we are not running
1187 // yet and anything we are given is a file address.
1188 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1189 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001190 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001191 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001192 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001193 // We have at least one section loaded. This can be becuase
1194 // we have manually loaded some sections with "target modules load ..."
1195 // or because we have have a live process that has sections loaded
1196 // through the dynamic loader
1197 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1198 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001199 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001200 }
Greg Clayton357132e2011-03-26 19:14:58 +00001201 if (!resolved_addr.IsValid())
1202 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001203
Greg Claytonc749eb82011-07-11 05:12:02 +00001204
Greg Claytondb598232011-01-07 01:57:07 +00001205 if (prefer_file_cache)
1206 {
1207 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1208 if (bytes_read > 0)
1209 return bytes_read;
1210 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001211
Johnny Chen86364b42011-09-20 23:28:55 +00001212 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001213 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001214 if (load_addr == LLDB_INVALID_ADDRESS)
1215 load_addr = resolved_addr.GetLoadAddress (this);
1216
Greg Claytondda4f7b2010-06-30 23:03:03 +00001217 if (load_addr == LLDB_INVALID_ADDRESS)
1218 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001219 ModuleSP addr_module_sp (resolved_addr.GetModule());
1220 if (addr_module_sp && addr_module_sp->GetFileSpec())
Greg Clayton86edbf42011-10-26 00:56:27 +00001221 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Claytone72dfb32012-02-24 01:59:29 +00001222 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08 +00001223 resolved_addr.GetFileAddress(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001224 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001225 else
Greg Clayton86edbf42011-10-26 00:56:27 +00001226 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001227 }
1228 else
1229 {
Greg Claytondb598232011-01-07 01:57:07 +00001230 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231 if (bytes_read != dst_len)
1232 {
1233 if (error.Success())
1234 {
1235 if (bytes_read == 0)
Greg Clayton86edbf42011-10-26 00:56:27 +00001236 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237 else
Greg Clayton43e0af02012-09-18 18:04:04 +00001238 error.SetErrorStringWithFormat("only %llu of %llu bytes were read from memory at 0x%llx", (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 }
1240 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001241 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001242 {
1243 if (load_addr_ptr)
1244 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001245 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001246 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001247 // If the address is not section offset we have an address that
1248 // doesn't resolve to any address in any currently loaded shared
1249 // libaries and we failed to read memory so there isn't anything
1250 // more we can do. If it is section offset, we might be able to
1251 // read cached memory from the object file.
1252 if (!resolved_addr.IsSectionOffset())
1253 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001256
Greg Claytonc749eb82011-07-11 05:12:02 +00001257 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001258 {
Greg Claytondb598232011-01-07 01:57:07 +00001259 // If we didn't already try and read from the object file cache, then
1260 // try it after failing to read from the process.
1261 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001262 }
1263 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264}
1265
Greg Claytond16e1e52011-07-12 17:06:17 +00001266size_t
1267Target::ReadScalarIntegerFromMemory (const Address& addr,
1268 bool prefer_file_cache,
1269 uint32_t byte_size,
1270 bool is_signed,
1271 Scalar &scalar,
1272 Error &error)
1273{
1274 uint64_t uval;
1275
1276 if (byte_size <= sizeof(uval))
1277 {
1278 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1279 if (bytes_read == byte_size)
1280 {
1281 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1282 uint32_t offset = 0;
1283 if (byte_size <= 4)
1284 scalar = data.GetMaxU32 (&offset, byte_size);
1285 else
1286 scalar = data.GetMaxU64 (&offset, byte_size);
1287
1288 if (is_signed)
1289 scalar.SignExtend(byte_size * 8);
1290 return bytes_read;
1291 }
1292 }
1293 else
1294 {
1295 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1296 }
1297 return 0;
1298}
1299
1300uint64_t
1301Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1302 bool prefer_file_cache,
1303 size_t integer_byte_size,
1304 uint64_t fail_value,
1305 Error &error)
1306{
1307 Scalar scalar;
1308 if (ReadScalarIntegerFromMemory (addr,
1309 prefer_file_cache,
1310 integer_byte_size,
1311 false,
1312 scalar,
1313 error))
1314 return scalar.ULongLong(fail_value);
1315 return fail_value;
1316}
1317
1318bool
1319Target::ReadPointerFromMemory (const Address& addr,
1320 bool prefer_file_cache,
1321 Error &error,
1322 Address &pointer_addr)
1323{
1324 Scalar scalar;
1325 if (ReadScalarIntegerFromMemory (addr,
1326 prefer_file_cache,
1327 m_arch.GetAddressByteSize(),
1328 false,
1329 scalar,
1330 error))
1331 {
1332 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1333 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1334 {
1335 if (m_section_load_list.IsEmpty())
1336 {
1337 // No sections are loaded, so we must assume we are not running
1338 // yet and anything we are given is a file address.
1339 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1340 }
1341 else
1342 {
1343 // We have at least one section loaded. This can be becuase
1344 // we have manually loaded some sections with "target modules load ..."
1345 // or because we have have a live process that has sections loaded
1346 // through the dynamic loader
1347 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1348 }
1349 // We weren't able to resolve the pointer value, so just return
1350 // an address with no section
1351 if (!pointer_addr.IsValid())
1352 pointer_addr.SetOffset (pointer_vm_addr);
1353 return true;
1354
1355 }
1356 }
1357 return false;
1358}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359
1360ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001361Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001362{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363 ModuleSP module_sp;
1364
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 Error error;
1366
Jim Ingham4a94c912012-05-17 18:38:42 +00001367 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1368 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001369
Jim Ingham4a94c912012-05-17 18:38:42 +00001370 if (module_spec.GetUUID().IsValid())
1371 module_sp = m_images.FindFirstModule(module_spec);
1372
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001373 if (!module_sp)
1374 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001375 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1376 bool did_create_module = false;
1377
1378 // If there are image search path entries, try to use them first to acquire a suitable image.
1379 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001380 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001381 ModuleSpec transformed_spec (module_spec);
1382 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1383 {
1384 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1385 error = ModuleList::GetSharedModule (transformed_spec,
1386 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001387 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001388 &old_module_sp,
1389 &did_create_module);
1390 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001391 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001392
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001393 if (!module_sp)
1394 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001395 // If we have a UUID, we can check our global shared module list in case
1396 // we already have it. If we don't have a valid UUID, then we can't since
1397 // the path in "module_spec" will be a platform path, and we will need to
1398 // let the platform find that file. For example, we could be asking for
1399 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1400 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1401 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1402 // cache.
1403 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001404 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001405 // We have a UUID, it is OK to check the global module list...
1406 error = ModuleList::GetSharedModule (module_spec,
1407 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001408 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001409 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001410 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001411 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001412
1413 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001414 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001415 // The platform is responsible for finding and caching an appropriate
1416 // module in the shared module cache.
1417 if (m_platform_sp)
1418 {
1419 FileSpec platform_file_spec;
1420 error = m_platform_sp->GetSharedModule (module_spec,
1421 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001422 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001423 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001424 &did_create_module);
1425 }
1426 else
1427 {
1428 error.SetErrorString("no platform is currently set");
1429 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001430 }
1431 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432
Jim Ingham4a94c912012-05-17 18:38:42 +00001433 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1434 // module in the list already, and if there was, let's remove it.
1435 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001437 // GetSharedModule is not guaranteed to find the old shared module, for instance
1438 // in the common case where you pass in the UUID, it is only going to find the one
1439 // module matching the UUID. In fact, it has no good way to know what the "old module"
1440 // relevant to this target is, since there might be many copies of a module with this file spec
1441 // in various running debug sessions, but only one of them will belong to this target.
1442 // So let's remove the UUID from the module list, and look in the target's module list.
1443 // Only do this if there is SOMETHING else in the module spec...
1444 if (!old_module_sp)
1445 {
1446 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
1447 {
1448 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1449 module_spec_copy.GetUUID().Clear();
1450
1451 ModuleList found_modules;
1452 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1453 if (num_found == 1)
1454 {
1455 old_module_sp = found_modules.GetModuleAtIndex(0);
1456 }
1457 }
1458 }
1459
1460 m_images.Append (module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
Jim Ingham4a94c912012-05-17 18:38:42 +00001462 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463 ModuleUpdated(old_module_sp, module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001464 m_images.Remove (old_module_sp);
1465 Module *old_module_ptr = old_module_sp.get();
1466 old_module_sp.reset();
1467 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1468 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001469 else
1470 ModuleAdded(module_sp);
1471 }
1472 }
1473 if (error_ptr)
1474 *error_ptr = error;
1475 return module_sp;
1476}
1477
1478
Greg Claytond9e416c2012-02-18 05:35:26 +00001479TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480Target::CalculateTarget ()
1481{
Greg Claytond9e416c2012-02-18 05:35:26 +00001482 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483}
1484
Greg Claytond9e416c2012-02-18 05:35:26 +00001485ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001486Target::CalculateProcess ()
1487{
Greg Claytond9e416c2012-02-18 05:35:26 +00001488 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489}
1490
Greg Claytond9e416c2012-02-18 05:35:26 +00001491ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492Target::CalculateThread ()
1493{
Greg Claytond9e416c2012-02-18 05:35:26 +00001494 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495}
1496
Greg Claytond9e416c2012-02-18 05:35:26 +00001497StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001498Target::CalculateStackFrame ()
1499{
Greg Claytond9e416c2012-02-18 05:35:26 +00001500 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501}
1502
1503void
Greg Clayton0603aa92010-10-04 01:05:56 +00001504Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001505{
Greg Claytonc14ee322011-09-22 04:58:26 +00001506 exe_ctx.Clear();
1507 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508}
1509
1510PathMappingList &
1511Target::GetImageSearchPathList ()
1512{
1513 return m_image_search_paths;
1514}
1515
1516void
1517Target::ImageSearchPathsChanged
1518(
1519 const PathMappingList &path_list,
1520 void *baton
1521)
1522{
1523 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001524 ModuleSP exe_module_sp (target->GetExecutableModule());
1525 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001526 {
Greg Claytonaa149cb2011-08-11 02:48:45 +00001527 target->m_images.Clear();
1528 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529 }
1530}
1531
1532ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001533Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001534{
Greg Clayton73da2442011-08-03 01:23:55 +00001535 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001536 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001537 {
Greg Clayton73da2442011-08-03 01:23:55 +00001538 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001539 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001540 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1541 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1542 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1543 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001544 return m_scratch_ast_context_ap.get();
1545}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001546
Sean Callanan686b2312011-11-16 18:20:47 +00001547ClangASTImporter *
1548Target::GetClangASTImporter()
1549{
1550 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1551
1552 if (!ast_importer)
1553 {
1554 ast_importer = new ClangASTImporter();
1555 m_ast_importer_ap.reset(ast_importer);
1556 }
1557
1558 return ast_importer;
1559}
1560
Greg Clayton99d0faf2010-11-18 23:32:35 +00001561void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001562Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001563{
Greg Clayton6920b522012-08-22 18:39:03 +00001564 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001565}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001566
Greg Clayton99d0faf2010-11-18 23:32:35 +00001567void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001568Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001569{
Greg Clayton6920b522012-08-22 18:39:03 +00001570 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001571}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001572
Greg Claytonc859e2d2012-02-13 23:10:39 +00001573FileSpecList
1574Target::GetDefaultExecutableSearchPaths ()
1575{
Greg Clayton67cc0632012-08-22 17:17:09 +00001576 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1577 if (properties_sp)
1578 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001579 return FileSpecList();
1580}
1581
Caroline Ticedaccaa92010-09-20 20:44:43 +00001582ArchSpec
1583Target::GetDefaultArchitecture ()
1584{
Greg Clayton67cc0632012-08-22 17:17:09 +00001585 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1586 if (properties_sp)
1587 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001588 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001589}
1590
1591void
Greg Clayton67cc0632012-08-22 17:17:09 +00001592Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001593{
Greg Clayton67cc0632012-08-22 17:17:09 +00001594 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1595 if (properties_sp)
1596 return properties_sp->SetDefaultArchitecture(arch);
Caroline Ticedaccaa92010-09-20 20:44:43 +00001597}
1598
Greg Clayton0603aa92010-10-04 01:05:56 +00001599Target *
1600Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1601{
1602 // The target can either exist in the "process" of ExecutionContext, or in
1603 // the "target_sp" member of SymbolContext. This accessor helper function
1604 // will get the target from one of these locations.
1605
1606 Target *target = NULL;
1607 if (sc_ptr != NULL)
1608 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001609 if (target == NULL && exe_ctx_ptr)
1610 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001611 return target;
1612}
1613
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001614ExecutionResults
1615Target::EvaluateExpression
1616(
1617 const char *expr_cstr,
1618 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001619 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001620 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001621)
1622{
Enrico Granata97fca502012-09-18 17:43:16 +00001623 result_valobj_sp.reset();
1624
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001625 ExecutionResults execution_results = eExecutionSetupError;
1626
Greg Claytond1767f02011-12-08 02:13:16 +00001627 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1628 return execution_results;
1629
Jim Ingham6026ca32011-05-12 02:06:14 +00001630 // We shouldn't run stop hooks in expressions.
1631 // Be sure to reset this if you return anywhere within this function.
1632 bool old_suppress_value = m_suppress_stop_hooks;
1633 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001634
1635 ExecutionContext exe_ctx;
Greg Claytond1767f02011-12-08 02:13:16 +00001636
1637 const size_t expr_cstr_len = ::strlen (expr_cstr);
1638
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001639 if (frame)
1640 {
1641 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:08 +00001642 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:18 +00001643 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:56 +00001644 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1645 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:18 +00001646 lldb::VariableSP var_sp;
Greg Claytond1767f02011-12-08 02:13:16 +00001647
1648 // Make sure we don't have any things that we know a variable expression
1649 // won't be able to deal with before calling into it
1650 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1651 {
1652 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001653 options.GetUseDynamic(),
Greg Claytond1767f02011-12-08 02:13:16 +00001654 expr_path_options,
1655 var_sp,
1656 error);
Enrico Granata9f1e2042012-04-24 22:15:37 +00001657 // if this expression results in a bitfield, we give up and let the IR handle it
1658 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1659 result_valobj_sp.reset();
Greg Claytond1767f02011-12-08 02:13:16 +00001660 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001661 }
1662 else if (m_process_sp)
1663 {
1664 m_process_sp->CalculateExecutionContext(exe_ctx);
1665 }
1666 else
1667 {
1668 CalculateExecutionContext(exe_ctx);
1669 }
1670
1671 if (result_valobj_sp)
1672 {
1673 execution_results = eExecutionCompleted;
1674 // We got a result from the frame variable expression path above...
1675 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1676
1677 lldb::ValueObjectSP const_valobj_sp;
1678
1679 // Check in case our value is already a constant value
1680 if (result_valobj_sp->GetIsConstant())
1681 {
1682 const_valobj_sp = result_valobj_sp;
1683 const_valobj_sp->SetName (persistent_variable_name);
1684 }
1685 else
Jim Ingham78a685a2011-04-16 00:01:13 +00001686 {
Enrico Granatad4439aa2012-09-05 20:41:26 +00001687 if (options.GetUseDynamic() != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00001688 {
Enrico Granatad4439aa2012-09-05 20:41:26 +00001689 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(options.GetUseDynamic());
Jim Ingham78a685a2011-04-16 00:01:13 +00001690 if (dynamic_sp)
1691 result_valobj_sp = dynamic_sp;
1692 }
1693
Jim Ingham6035b672011-03-31 00:19:25 +00001694 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:13 +00001695 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001696
Sean Callanan92adcac2011-01-13 08:53:35 +00001697 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1698
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001699 result_valobj_sp = const_valobj_sp;
1700
Sean Callanan92adcac2011-01-13 08:53:35 +00001701 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1702 assert (clang_expr_variable_sp.get());
1703
1704 // Set flags and live data as appropriate
1705
1706 const Value &result_value = live_valobj_sp->GetValue();
1707
1708 switch (result_value.GetValueType())
1709 {
1710 case Value::eValueTypeHostAddress:
1711 case Value::eValueTypeFileAddress:
1712 // we don't do anything with these for now
1713 break;
1714 case Value::eValueTypeScalar:
1715 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1716 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1717 break;
1718 case Value::eValueTypeLoadAddress:
1719 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1720 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1721 break;
1722 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001723 }
1724 else
1725 {
1726 // Make sure we aren't just trying to see the value of a persistent
1727 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:35 +00001728 lldb::ClangExpressionVariableSP persistent_var_sp;
1729 // Only check for persistent variables the expression starts with a '$'
1730 if (expr_cstr[0] == '$')
1731 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1732
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001733 if (persistent_var_sp)
1734 {
1735 result_valobj_sp = persistent_var_sp->GetValueObject ();
1736 execution_results = eExecutionCompleted;
1737 }
1738 else
1739 {
1740 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001741
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001742 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001743 options.GetExecutionPolicy(),
Sean Callananc7b65062011-11-07 23:35:40 +00001744 lldb::eLanguageTypeUnknown,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001745 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1746 options.DoesUnwindOnError(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001747 expr_cstr,
1748 prefix,
Enrico Granata3372f582012-07-16 23:10:35 +00001749 result_valobj_sp,
Jim Ingham35e1bda2012-10-16 21:41:58 +00001750 options.GetRunOthers(),
1751 options.GetTimeoutUsec());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001752 }
1753 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001754
1755 m_suppress_stop_hooks = old_suppress_value;
1756
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001757 return execution_results;
1758}
1759
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001760lldb::addr_t
1761Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1762{
1763 addr_t code_addr = load_addr;
1764 switch (m_arch.GetMachine())
1765 {
1766 case llvm::Triple::arm:
1767 case llvm::Triple::thumb:
1768 switch (addr_class)
1769 {
1770 case eAddressClassData:
1771 case eAddressClassDebug:
1772 return LLDB_INVALID_ADDRESS;
1773
1774 case eAddressClassUnknown:
1775 case eAddressClassInvalid:
1776 case eAddressClassCode:
1777 case eAddressClassCodeAlternateISA:
1778 case eAddressClassRuntime:
1779 // Check if bit zero it no set?
1780 if ((code_addr & 1ull) == 0)
1781 {
1782 // Bit zero isn't set, check if the address is a multiple of 2?
1783 if (code_addr & 2ull)
1784 {
1785 // The address is a multiple of 2 so it must be thumb, set bit zero
1786 code_addr |= 1ull;
1787 }
1788 else if (addr_class == eAddressClassCodeAlternateISA)
1789 {
1790 // We checked the address and the address claims to be the alternate ISA
1791 // which means thumb, so set bit zero.
1792 code_addr |= 1ull;
1793 }
1794 }
1795 break;
1796 }
1797 break;
1798
1799 default:
1800 break;
1801 }
1802 return code_addr;
1803}
1804
1805lldb::addr_t
1806Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1807{
1808 addr_t opcode_addr = load_addr;
1809 switch (m_arch.GetMachine())
1810 {
1811 case llvm::Triple::arm:
1812 case llvm::Triple::thumb:
1813 switch (addr_class)
1814 {
1815 case eAddressClassData:
1816 case eAddressClassDebug:
1817 return LLDB_INVALID_ADDRESS;
1818
1819 case eAddressClassInvalid:
1820 case eAddressClassUnknown:
1821 case eAddressClassCode:
1822 case eAddressClassCodeAlternateISA:
1823 case eAddressClassRuntime:
1824 opcode_addr &= ~(1ull);
1825 break;
1826 }
1827 break;
1828
1829 default:
1830 break;
1831 }
1832 return opcode_addr;
1833}
1834
Jim Ingham9575d842011-03-11 03:53:59 +00001835lldb::user_id_t
1836Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1837{
1838 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Claytone1cd1be2012-01-29 20:56:30 +00001839 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Ingham9575d842011-03-11 03:53:59 +00001840 m_stop_hooks[new_uid] = new_hook_sp;
1841 return new_uid;
1842}
1843
1844bool
1845Target::RemoveStopHookByID (lldb::user_id_t user_id)
1846{
1847 size_t num_removed;
1848 num_removed = m_stop_hooks.erase (user_id);
1849 if (num_removed == 0)
1850 return false;
1851 else
1852 return true;
1853}
1854
1855void
1856Target::RemoveAllStopHooks ()
1857{
1858 m_stop_hooks.clear();
1859}
1860
1861Target::StopHookSP
1862Target::GetStopHookByID (lldb::user_id_t user_id)
1863{
1864 StopHookSP found_hook;
1865
1866 StopHookCollection::iterator specified_hook_iter;
1867 specified_hook_iter = m_stop_hooks.find (user_id);
1868 if (specified_hook_iter != m_stop_hooks.end())
1869 found_hook = (*specified_hook_iter).second;
1870 return found_hook;
1871}
1872
1873bool
1874Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1875{
1876 StopHookCollection::iterator specified_hook_iter;
1877 specified_hook_iter = m_stop_hooks.find (user_id);
1878 if (specified_hook_iter == m_stop_hooks.end())
1879 return false;
1880
1881 (*specified_hook_iter).second->SetIsActive (active_state);
1882 return true;
1883}
1884
1885void
1886Target::SetAllStopHooksActiveState (bool active_state)
1887{
1888 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1889 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1890 {
1891 (*pos).second->SetIsActive (active_state);
1892 }
1893}
1894
1895void
1896Target::RunStopHooks ()
1897{
Jim Ingham6026ca32011-05-12 02:06:14 +00001898 if (m_suppress_stop_hooks)
1899 return;
1900
Jim Ingham9575d842011-03-11 03:53:59 +00001901 if (!m_process_sp)
1902 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00001903
1904 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1905 // since in that case we do not want to run the stop-hooks
1906 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1907 return;
1908
Jim Ingham9575d842011-03-11 03:53:59 +00001909 if (m_stop_hooks.empty())
1910 return;
1911
1912 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1913
1914 // If there aren't any active stop hooks, don't bother either:
1915 bool any_active_hooks = false;
1916 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1917 {
1918 if ((*pos).second->IsActive())
1919 {
1920 any_active_hooks = true;
1921 break;
1922 }
1923 }
1924 if (!any_active_hooks)
1925 return;
1926
1927 CommandReturnObject result;
1928
1929 std::vector<ExecutionContext> exc_ctx_with_reasons;
1930 std::vector<SymbolContext> sym_ctx_with_reasons;
1931
1932 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1933 size_t num_threads = cur_threadlist.GetSize();
1934 for (size_t i = 0; i < num_threads; i++)
1935 {
1936 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1937 if (cur_thread_sp->ThreadStoppedForAReason())
1938 {
1939 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1940 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1941 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1942 }
1943 }
1944
1945 // If no threads stopped for a reason, don't run the stop-hooks.
1946 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1947 if (num_exe_ctx == 0)
1948 return;
1949
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001950 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1951 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00001952
1953 bool keep_going = true;
1954 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00001955 bool print_hook_header;
1956 bool print_thread_header;
1957
1958 if (num_exe_ctx == 1)
1959 print_thread_header = false;
1960 else
1961 print_thread_header = true;
1962
1963 if (m_stop_hooks.size() == 1)
1964 print_hook_header = false;
1965 else
1966 print_hook_header = true;
1967
Jim Ingham9575d842011-03-11 03:53:59 +00001968 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1969 {
1970 // result.Clear();
1971 StopHookSP cur_hook_sp = (*pos).second;
1972 if (!cur_hook_sp->IsActive())
1973 continue;
1974
1975 bool any_thread_matched = false;
1976 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1977 {
1978 if ((cur_hook_sp->GetSpecifier () == NULL
1979 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1980 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00001981 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00001982 {
1983 if (!hooks_ran)
1984 {
Jim Ingham9575d842011-03-11 03:53:59 +00001985 hooks_ran = true;
1986 }
Jim Ingham381e25b2011-03-22 01:47:27 +00001987 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00001988 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00001989 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1990 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1991 NULL);
1992 if (cmd)
1993 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1994 else
1995 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00001996 any_thread_matched = true;
1997 }
1998
Jim Ingham381e25b2011-03-22 01:47:27 +00001999 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002000 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:59 +00002001
2002 bool stop_on_continue = true;
2003 bool stop_on_error = true;
2004 bool echo_commands = false;
2005 bool print_results = true;
2006 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:51 +00002007 &exc_ctx_with_reasons[i],
2008 stop_on_continue,
2009 stop_on_error,
2010 echo_commands,
Enrico Granata5f5ab602012-05-31 01:09:06 +00002011 print_results,
2012 eLazyBoolNo,
Greg Clayton32e0a752011-03-30 18:16:51 +00002013 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002014
2015 // If the command started the target going again, we should bag out of
2016 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002017 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2018 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002019 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002020 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002021 keep_going = false;
2022 }
2023 }
2024 }
2025 }
Jason Molenda879cf772011-09-23 00:42:55 +00002026
Caroline Tice969ed3d2011-05-02 20:41:46 +00002027 result.GetImmediateOutputStream()->Flush();
2028 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002029}
2030
Greg Clayton7b242382011-07-08 00:48:09 +00002031
Jim Ingham9575d842011-03-11 03:53:59 +00002032//--------------------------------------------------------------
2033// class Target::StopHook
2034//--------------------------------------------------------------
2035
2036
2037Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2038 UserID (uid),
2039 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002040 m_commands (),
2041 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002042 m_thread_spec_ap(NULL),
2043 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002044{
2045}
2046
2047Target::StopHook::StopHook (const StopHook &rhs) :
2048 UserID (rhs.GetID()),
2049 m_target_sp (rhs.m_target_sp),
2050 m_commands (rhs.m_commands),
2051 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002052 m_thread_spec_ap (NULL),
2053 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002054{
2055 if (rhs.m_thread_spec_ap.get() != NULL)
2056 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2057}
2058
2059
2060Target::StopHook::~StopHook ()
2061{
2062}
2063
2064void
2065Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2066{
2067 m_thread_spec_ap.reset (specifier);
2068}
2069
2070
2071void
2072Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2073{
2074 int indent_level = s->GetIndentLevel();
2075
2076 s->SetIndentLevel(indent_level + 2);
2077
Greg Clayton81c22f62011-10-19 18:09:39 +00002078 s->Printf ("Hook: %llu\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002079 if (m_active)
2080 s->Indent ("State: enabled\n");
2081 else
2082 s->Indent ("State: disabled\n");
2083
2084 if (m_specifier_sp)
2085 {
2086 s->Indent();
2087 s->PutCString ("Specifier:\n");
2088 s->SetIndentLevel (indent_level + 4);
2089 m_specifier_sp->GetDescription (s, level);
2090 s->SetIndentLevel (indent_level + 2);
2091 }
2092
2093 if (m_thread_spec_ap.get() != NULL)
2094 {
2095 StreamString tmp;
2096 s->Indent("Thread:\n");
2097 m_thread_spec_ap->GetDescription (&tmp, level);
2098 s->SetIndentLevel (indent_level + 4);
2099 s->Indent (tmp.GetData());
2100 s->PutCString ("\n");
2101 s->SetIndentLevel (indent_level + 2);
2102 }
2103
2104 s->Indent ("Commands: \n");
2105 s->SetIndentLevel (indent_level + 4);
2106 uint32_t num_commands = m_commands.GetSize();
2107 for (uint32_t i = 0; i < num_commands; i++)
2108 {
2109 s->Indent(m_commands.GetStringAtIndex(i));
2110 s->PutCString ("\n");
2111 }
2112 s->SetIndentLevel (indent_level);
2113}
2114
Greg Clayton67cc0632012-08-22 17:17:09 +00002115//--------------------------------------------------------------
2116// class TargetProperties
2117//--------------------------------------------------------------
2118
2119OptionEnumValueElement
2120lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002121{
Greg Clayton67cc0632012-08-22 17:17:09 +00002122 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2123 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2124 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2125 { 0, NULL, NULL }
2126};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002127
Greg Clayton1f746072012-08-29 21:13:06 +00002128static OptionEnumValueElement
2129g_inline_breakpoint_enums[] =
2130{
2131 { 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."},
2132 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2133 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2134 { 0, NULL, NULL }
2135};
2136
Greg Clayton67cc0632012-08-22 17:17:09 +00002137static PropertyDefinition
2138g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002139{
Greg Clayton67cc0632012-08-22 17:17:09 +00002140 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2141 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2142 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2143 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2144 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2145 { "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 "
2146 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2147 "some part (starting at the root) of the path to the file when it was built, "
2148 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2149 "Each element of the array is checked in order and the first one that results in a match wins." },
2150 { "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." },
2151 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2152 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2153 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
2154 { "run-args" , OptionValue::eTypeArgs , false, 0 , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run." },
2155 { "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." },
2156 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2157 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2158 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2159 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2160 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2161 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Disable stdin/stdout for process (e.g. for a GUI application)" },
Greg Clayton1f746072012-08-29 21:13:06 +00002162 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2163 "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. "
2164 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2165 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2166 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2167 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2168 "file and line breakpoints." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002169 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2170};
2171enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002172{
Greg Clayton67cc0632012-08-22 17:17:09 +00002173 ePropertyDefaultArch,
2174 ePropertyExprPrefix,
2175 ePropertyPreferDynamic,
2176 ePropertyEnableSynthetic,
2177 ePropertySkipPrologue,
2178 ePropertySourceMap,
2179 ePropertyExecutableSearchPaths,
2180 ePropertyMaxChildrenCount,
2181 ePropertyMaxSummaryLength,
2182 ePropertyBreakpointUseAvoidList,
2183 ePropertyRunArgs,
2184 ePropertyEnvVars,
2185 ePropertyInheritEnv,
2186 ePropertyInputPath,
2187 ePropertyOutputPath,
2188 ePropertyErrorPath,
2189 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002190 ePropertyDisableSTDIO,
2191 ePropertyInlineStrategy
Greg Clayton67cc0632012-08-22 17:17:09 +00002192};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002193
Caroline Ticedaccaa92010-09-20 20:44:43 +00002194
Greg Clayton67cc0632012-08-22 17:17:09 +00002195class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002196{
Greg Clayton67cc0632012-08-22 17:17:09 +00002197public:
2198 TargetOptionValueProperties (const ConstString &name) :
2199 OptionValueProperties (name),
2200 m_target (NULL),
2201 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002202 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002203 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002204
Greg Clayton67cc0632012-08-22 17:17:09 +00002205 // This constructor is used when creating TargetOptionValueProperties when it
2206 // is part of a new lldb_private::Target instance. It will copy all current
2207 // global property values as needed
2208 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2209 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2210 m_target (target),
2211 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002212 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002213 }
2214
2215 virtual const Property *
2216 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2217 {
2218 // When gettings the value for a key from the target options, we will always
2219 // try and grab the setting from the current target if there is one. Else we just
2220 // use the one from this instance.
2221 if (idx == ePropertyEnvVars)
2222 GetHostEnvironmentIfNeeded ();
2223
2224 if (exe_ctx)
2225 {
2226 Target *target = exe_ctx->GetTargetPtr();
2227 if (target)
2228 {
2229 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2230 if (this != target_properties)
2231 return target_properties->ProtectedGetPropertyAtIndex (idx);
2232 }
2233 }
2234 return ProtectedGetPropertyAtIndex (idx);
2235 }
2236protected:
2237
2238 void
2239 GetHostEnvironmentIfNeeded () const
2240 {
2241 if (!m_got_host_env)
2242 {
2243 if (m_target)
2244 {
2245 m_got_host_env = true;
2246 const uint32_t idx = ePropertyInheritEnv;
2247 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2248 {
2249 PlatformSP platform_sp (m_target->GetPlatform());
2250 if (platform_sp)
2251 {
2252 StringList env;
2253 if (platform_sp->GetEnvironment(env))
2254 {
2255 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2256 if (env_dict)
2257 {
2258 const bool can_replace = false;
2259 const size_t envc = env.GetSize();
2260 for (size_t idx=0; idx<envc; idx++)
2261 {
2262 const char *env_entry = env.GetStringAtIndex (idx);
2263 if (env_entry)
2264 {
2265 const char *equal_pos = ::strchr(env_entry, '=');
2266 ConstString key;
2267 // It is ok to have environment variables with no values
2268 const char *value = NULL;
2269 if (equal_pos)
2270 {
2271 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2272 if (equal_pos[1])
2273 value = equal_pos + 1;
2274 }
2275 else
2276 {
2277 key.SetCString(env_entry);
2278 }
2279 // Don't allow existing keys to be replaced with ones we get from the platform environment
2280 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2281 }
2282 }
2283 }
2284 }
2285 }
2286 }
2287 }
2288 }
2289 }
2290 Target *m_target;
2291 mutable bool m_got_host_env;
2292};
2293
2294TargetProperties::TargetProperties (Target *target) :
2295 Properties ()
2296{
2297 if (target)
2298 {
2299 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Ticedaccaa92010-09-20 20:44:43 +00002300 }
2301 else
Greg Clayton67cc0632012-08-22 17:17:09 +00002302 {
2303 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2304 m_collection_sp->Initialize(g_properties);
2305 m_collection_sp->AppendProperty(ConstString("process"),
2306 ConstString("Settings specify to processes."),
2307 true,
2308 Process::GetGlobalProperties()->GetValueProperties());
2309 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002310}
2311
Greg Clayton67cc0632012-08-22 17:17:09 +00002312TargetProperties::~TargetProperties ()
2313{
2314}
2315ArchSpec
2316TargetProperties::GetDefaultArchitecture () const
2317{
2318 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2319 if (value)
2320 return value->GetCurrentValue();
2321 return ArchSpec();
2322}
2323
2324void
2325TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2326{
2327 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2328 if (value)
2329 return value->SetCurrentValue(arch, true);
2330}
2331
2332lldb::DynamicValueType
2333TargetProperties::GetPreferDynamicValue() const
2334{
2335 const uint32_t idx = ePropertyPreferDynamic;
2336 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2337}
2338
2339bool
2340TargetProperties::GetDisableASLR () const
2341{
2342 const uint32_t idx = ePropertyDisableASLR;
2343 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2344}
2345
2346void
2347TargetProperties::SetDisableASLR (bool b)
2348{
2349 const uint32_t idx = ePropertyDisableASLR;
2350 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2351}
2352
2353bool
2354TargetProperties::GetDisableSTDIO () const
2355{
2356 const uint32_t idx = ePropertyDisableSTDIO;
2357 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2358}
2359
2360void
2361TargetProperties::SetDisableSTDIO (bool b)
2362{
2363 const uint32_t idx = ePropertyDisableSTDIO;
2364 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2365}
2366
Greg Clayton1f746072012-08-29 21:13:06 +00002367InlineStrategy
2368TargetProperties::GetInlineStrategy () const
2369{
2370 const uint32_t idx = ePropertyInlineStrategy;
2371 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2372}
2373
Greg Clayton67cc0632012-08-22 17:17:09 +00002374bool
2375TargetProperties::GetRunArguments (Args &args) const
2376{
2377 const uint32_t idx = ePropertyRunArgs;
2378 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2379}
2380
2381void
2382TargetProperties::SetRunArguments (const Args &args)
2383{
2384 const uint32_t idx = ePropertyRunArgs;
2385 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2386}
2387
2388size_t
2389TargetProperties::GetEnvironmentAsArgs (Args &env) const
2390{
2391 const uint32_t idx = ePropertyEnvVars;
2392 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2393}
2394
2395bool
2396TargetProperties::GetSkipPrologue() const
2397{
2398 const uint32_t idx = ePropertySkipPrologue;
2399 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2400}
2401
2402PathMappingList &
2403TargetProperties::GetSourcePathMap () const
2404{
2405 const uint32_t idx = ePropertySourceMap;
2406 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2407 assert(option_value);
2408 return option_value->GetCurrentValue();
2409}
2410
2411FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00002412TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00002413{
2414 const uint32_t idx = ePropertyExecutableSearchPaths;
2415 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2416 assert(option_value);
2417 return option_value->GetCurrentValue();
2418}
2419
2420bool
2421TargetProperties::GetEnableSyntheticValue () const
2422{
2423 const uint32_t idx = ePropertyEnableSynthetic;
2424 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2425}
2426
2427uint32_t
2428TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2429{
2430 const uint32_t idx = ePropertyMaxChildrenCount;
2431 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2432}
2433
2434uint32_t
2435TargetProperties::GetMaximumSizeOfStringSummary() const
2436{
2437 const uint32_t idx = ePropertyMaxSummaryLength;
2438 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2439}
2440
2441FileSpec
2442TargetProperties::GetStandardInputPath () const
2443{
2444 const uint32_t idx = ePropertyInputPath;
2445 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2446}
2447
2448void
2449TargetProperties::SetStandardInputPath (const char *p)
2450{
2451 const uint32_t idx = ePropertyInputPath;
2452 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2453}
2454
2455FileSpec
2456TargetProperties::GetStandardOutputPath () const
2457{
2458 const uint32_t idx = ePropertyOutputPath;
2459 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2460}
2461
2462void
2463TargetProperties::SetStandardOutputPath (const char *p)
2464{
2465 const uint32_t idx = ePropertyOutputPath;
2466 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2467}
2468
2469FileSpec
2470TargetProperties::GetStandardErrorPath () const
2471{
2472 const uint32_t idx = ePropertyErrorPath;
2473 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2474}
2475
Greg Clayton6920b522012-08-22 18:39:03 +00002476const char *
2477TargetProperties::GetExpressionPrefixContentsAsCString ()
2478{
2479 const uint32_t idx = ePropertyExprPrefix;
2480 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2481 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00002482 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00002483 const bool null_terminate = true;
2484 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00002485 if (data_sp)
2486 return (const char *) data_sp->GetBytes();
2487 }
Greg Clayton6920b522012-08-22 18:39:03 +00002488 return NULL;
2489}
2490
Greg Clayton67cc0632012-08-22 17:17:09 +00002491void
2492TargetProperties::SetStandardErrorPath (const char *p)
2493{
2494 const uint32_t idx = ePropertyErrorPath;
2495 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2496}
2497
2498bool
2499TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2500{
2501 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2502 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2503}
2504
2505const TargetPropertiesSP &
2506Target::GetGlobalProperties()
2507{
2508 static TargetPropertiesSP g_settings_sp;
2509 if (!g_settings_sp)
2510 {
2511 g_settings_sp.reset (new TargetProperties (NULL));
2512 }
2513 return g_settings_sp;
2514}
2515
Jim Ingham4bddaeb2012-02-16 06:50:00 +00002516const ConstString &
2517Target::TargetEventData::GetFlavorString ()
2518{
2519 static ConstString g_flavor ("Target::TargetEventData");
2520 return g_flavor;
2521}
2522
2523const ConstString &
2524Target::TargetEventData::GetFlavor () const
2525{
2526 return TargetEventData::GetFlavorString ();
2527}
2528
2529Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2530 EventData(),
2531 m_target_sp (new_target_sp)
2532{
2533}
2534
2535Target::TargetEventData::~TargetEventData()
2536{
2537
2538}
2539
2540void
2541Target::TargetEventData::Dump (Stream *s) const
2542{
2543
2544}
2545
2546const TargetSP
2547Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2548{
2549 TargetSP target_sp;
2550
2551 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2552 if (data)
2553 target_sp = data->m_target_sp;
2554
2555 return target_sp;
2556}
2557
2558const Target::TargetEventData *
2559Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2560{
2561 if (event_ptr)
2562 {
2563 const EventData *event_data = event_ptr->GetData();
2564 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2565 return static_cast <const TargetEventData *> (event_ptr->GetData());
2566 }
2567 return NULL;
2568}
2569