blob: 2da8c19e85fffacf7f62ab5b542d0c827322ab7b [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000030#include "lldb/Core/SourceManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000034#include "lldb/Expression/ClangASTSource.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000035#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000037#include "lldb/Interpreter/CommandInterpreter.h"
38#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000039#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000040#include "lldb/Interpreter/OptionValues.h"
41#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/lldb-private-log.h"
43#include "lldb/Symbol/ObjectFile.h"
44#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000045#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:59 +000046#include "lldb/Target/Thread.h"
47#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
49using namespace lldb;
50using namespace lldb_private;
51
Jim Ingham4bddaeb2012-02-16 06:50:00 +000052ConstString &
53Target::GetStaticBroadcasterClass ()
54{
55 static ConstString class_name ("lldb.target");
56 return class_name;
57}
58
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059//----------------------------------------------------------------------
60// Target constructor
61//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:51 +000062Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +000063 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000064 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000065 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000066 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000067 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000068 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000069 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000070 m_images (this),
Greg Claytonf5e56de2010-09-14 23:36:40 +000071 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 m_breakpoint_list (false),
73 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000074 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000075 m_process_sp (),
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +000076 m_valid (true),
Greg Clayton32e0a752011-03-30 18:16:51 +000077 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000079 m_scratch_ast_context_ap (),
80 m_scratch_ast_source_ap (),
81 m_ast_importer_ap (),
Jim Ingham9575d842011-03-11 03:53:59 +000082 m_persistent_variables (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000083 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000084 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000085 m_stop_hook_next_id (0),
Enrico Granata5d5f60c2013-09-24 22:58:37 +000086 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000088 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
89 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
90 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000091 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +000092 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Jim Ingham4bddaeb2012-02-16 06:50:00 +000093
94 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +000095
Greg Clayton5160ce52013-03-27 23:08:40 +000096 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 if (log)
98 log->Printf ("%p Target::Target()", this);
Jason Molendae1b68ad2012-12-05 00:25:49 +000099 if (m_arch.IsValid())
100 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000101 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::Target created with architecture %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jason Molendae1b68ad2012-12-05 00:25:49 +0000102 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103}
104
105//----------------------------------------------------------------------
106// Destructor
107//----------------------------------------------------------------------
108Target::~Target()
109{
Greg Clayton5160ce52013-03-27 23:08:40 +0000110 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 if (log)
112 log->Printf ("%p Target::~Target()", this);
113 DeleteCurrentProcess ();
114}
115
116void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000117Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118{
Greg Clayton89411422010-10-08 00:21:05 +0000119// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000120 if (description_level != lldb::eDescriptionLevelBrief)
121 {
122 s->Indent();
123 s->PutCString("Target\n");
124 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000125 m_images.Dump(s);
126 m_breakpoint_list.Dump(s);
127 m_internal_breakpoint_list.Dump(s);
128 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000129 }
130 else
131 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000132 Module *exe_module = GetExecutableModulePointer();
133 if (exe_module)
134 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000135 else
136 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000137 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138}
139
140void
Greg Clayton90ba8112012-12-05 00:16:59 +0000141Target::CleanupProcess ()
142{
143 // Do any cleanup of the target we need to do between process instances.
144 // NB It is better to do this before destroying the process in case the
145 // clean up needs some help from the process.
146 m_breakpoint_list.ClearAllBreakpointSites();
147 m_internal_breakpoint_list.ClearAllBreakpointSites();
148 // Disable watchpoints just on the debugger side.
149 Mutex::Locker locker;
150 this->GetWatchpointList().GetListMutex(locker);
151 DisableAllWatchpoints(false);
152 ClearAllWatchpointHitCounts();
153}
154
155void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156Target::DeleteCurrentProcess ()
157{
158 if (m_process_sp.get())
159 {
Greg Clayton17f69202010-09-14 23:52:43 +0000160 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 if (m_process_sp->IsAlive())
162 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000163
164 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165
Greg Clayton90ba8112012-12-05 00:16:59 +0000166 CleanupProcess ();
167
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 m_process_sp.reset();
169 }
170}
171
172const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000173Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174{
175 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000176 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 return m_process_sp;
178}
179
180const lldb::ProcessSP &
181Target::GetProcessSP () const
182{
183 return m_process_sp;
184}
185
Greg Clayton3418c852011-08-10 02:10:13 +0000186void
187Target::Destroy()
188{
189 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000190 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000191 DeleteCurrentProcess ();
192 m_platform_sp.reset();
193 m_arch.Clear();
194 m_images.Clear();
195 m_section_load_list.Clear();
196 const bool notify = false;
197 m_breakpoint_list.RemoveAll(notify);
198 m_internal_breakpoint_list.RemoveAll(notify);
199 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000200 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000201 m_search_filter_sp.reset();
202 m_image_search_paths.Clear(notify);
203 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +0000204 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +0000205 m_ast_importer_ap.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000206 m_persistent_variables.Clear();
207 m_stop_hooks.clear();
208 m_stop_hook_next_id = 0;
209 m_suppress_stop_hooks = false;
210}
211
212
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213BreakpointList &
214Target::GetBreakpointList(bool internal)
215{
216 if (internal)
217 return m_internal_breakpoint_list;
218 else
219 return m_breakpoint_list;
220}
221
222const BreakpointList &
223Target::GetBreakpointList(bool internal) const
224{
225 if (internal)
226 return m_internal_breakpoint_list;
227 else
228 return m_breakpoint_list;
229}
230
231BreakpointSP
232Target::GetBreakpointByID (break_id_t break_id)
233{
234 BreakpointSP bp_sp;
235
236 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
237 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
238 else
239 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
240
241 return bp_sp;
242}
243
244BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000245Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000246 const FileSpecList *source_file_spec_list,
247 RegularExpression &source_regex,
248 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249{
Jim Ingham87df91b2011-09-23 00:54:11 +0000250 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
251 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham969795f2011-09-21 01:17:13 +0000252 return CreateBreakpoint (filter_sp, resolver_sp, internal);
253}
254
255
256BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000257Target::CreateBreakpoint (const FileSpecList *containingModules,
258 const FileSpec &file,
259 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000260 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000261 LazyBool skip_prologue,
262 bool internal)
Jim Ingham969795f2011-09-21 01:17:13 +0000263{
Greg Clayton1f746072012-08-29 21:13:06 +0000264 if (check_inlines == eLazyBoolCalculate)
265 {
266 const InlineStrategy inline_strategy = GetInlineStrategy();
267 switch (inline_strategy)
268 {
269 case eInlineBreakpointsNever:
270 check_inlines = eLazyBoolNo;
271 break;
272
273 case eInlineBreakpointsHeaders:
274 if (file.IsSourceImplementationFile())
275 check_inlines = eLazyBoolNo;
276 else
277 check_inlines = eLazyBoolYes;
278 break;
279
280 case eInlineBreakpointsAlways:
281 check_inlines = eLazyBoolYes;
282 break;
283 }
284 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000285 SearchFilterSP filter_sp;
286 if (check_inlines == eLazyBoolNo)
287 {
288 // Not checking for inlines, we are looking only for matching compile units
289 FileSpecList compile_unit_list;
290 compile_unit_list.Append (file);
291 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
292 }
293 else
294 {
295 filter_sp = GetSearchFilterForModuleList (containingModules);
296 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000297 if (skip_prologue == eLazyBoolCalculate)
298 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
299
Greg Clayton1f746072012-08-29 21:13:06 +0000300 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
301 file,
302 line_no,
303 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000304 skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 return CreateBreakpoint (filter_sp, resolver_sp, internal);
306}
307
308
309BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000310Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312 Address so_addr;
313 // Attempt to resolve our load address if possible, though it is ok if
314 // it doesn't resolve to section/offset.
315
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000316 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40 +0000317 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000318 if (!so_addr.IsValid())
319 {
320 // The address didn't resolve, so just set this as an absolute address
321 so_addr.SetOffset (addr);
322 }
323 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 return bp_sp;
325}
326
327BreakpointSP
328Target::CreateBreakpoint (Address &addr, bool internal)
329{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000330 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
332 return CreateBreakpoint (filter_sp, resolver_sp, internal);
333}
334
335BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000336Target::CreateBreakpoint (const FileSpecList *containingModules,
337 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000338 const char *func_name,
339 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000340 LazyBool skip_prologue,
341 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000343 BreakpointSP bp_sp;
344 if (func_name)
345 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000346 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000347
348 if (skip_prologue == eLazyBoolCalculate)
349 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
350
Greg Claytond16e1e52011-07-12 17:06:17 +0000351 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
352 func_name,
353 func_name_type_mask,
354 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000355 skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43 +0000356 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
357 }
358 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359}
360
Jim Inghamfab10e82012-03-06 00:37:27 +0000361lldb::BreakpointSP
362Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000363 const FileSpecList *containingSourceFiles,
364 const std::vector<std::string> &func_names,
365 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000366 LazyBool skip_prologue,
367 bool internal)
Jim Inghamfab10e82012-03-06 00:37:27 +0000368{
369 BreakpointSP bp_sp;
370 size_t num_names = func_names.size();
371 if (num_names > 0)
372 {
373 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000374
375 if (skip_prologue == eLazyBoolCalculate)
376 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
377
378 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000379 func_names,
380 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000381 skip_prologue));
Jim Inghamfab10e82012-03-06 00:37:27 +0000382 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
383 }
384 return bp_sp;
385}
386
Jim Ingham133e0fb2012-03-03 02:05:11 +0000387BreakpointSP
388Target::CreateBreakpoint (const FileSpecList *containingModules,
389 const FileSpecList *containingSourceFiles,
390 const char *func_names[],
391 size_t num_names,
392 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000393 LazyBool skip_prologue,
394 bool internal)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000395{
396 BreakpointSP bp_sp;
397 if (num_names > 0)
398 {
399 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
400
Greg Clayton03da4cc2013-04-19 21:31:16 +0000401 if (skip_prologue == eLazyBoolCalculate)
402 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
403
404 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000405 func_names,
406 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000407 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000408 skip_prologue));
Jim Ingham133e0fb2012-03-03 02:05:11 +0000409 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
410 }
411 return bp_sp;
412}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413
414SearchFilterSP
415Target::GetSearchFilterForModule (const FileSpec *containingModule)
416{
417 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 if (containingModule != NULL)
419 {
420 // TODO: We should look into sharing module based search filters
421 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000422 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 }
424 else
425 {
426 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000427 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 filter_sp = m_search_filter_sp;
429 }
430 return filter_sp;
431}
432
Jim Ingham969795f2011-09-21 01:17:13 +0000433SearchFilterSP
434Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
435{
436 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000437 if (containingModules && containingModules->GetSize() != 0)
438 {
439 // TODO: We should look into sharing module based search filters
440 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000441 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000442 }
443 else
444 {
445 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000446 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000447 filter_sp = m_search_filter_sp;
448 }
449 return filter_sp;
450}
451
Jim Ingham87df91b2011-09-23 00:54:11 +0000452SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000453Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
454 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000455{
456 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
457 return GetSearchFilterForModuleList(containingModules);
458
459 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000460 if (containingModules == NULL)
461 {
462 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
463 // but that will take a little reworking.
464
Greg Claytone1cd1be2012-01-29 20:56:30 +0000465 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000466 }
467 else
468 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000469 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000470 }
471 return filter_sp;
472}
473
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000475Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000476 const FileSpecList *containingSourceFiles,
477 RegularExpression &func_regex,
478 LazyBool skip_prologue,
479 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480{
Jim Ingham87df91b2011-09-23 00:54:11 +0000481 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17 +0000482 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
483 func_regex,
484 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485
486 return CreateBreakpoint (filter_sp, resolver_sp, internal);
487}
488
Jim Ingham219ba192012-03-05 04:47:34 +0000489lldb::BreakpointSP
490Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
491{
492 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
493}
494
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495BreakpointSP
496Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
497{
498 BreakpointSP bp_sp;
499 if (filter_sp && resolver_sp)
500 {
501 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
502 resolver_sp->SetBreakpoint (bp_sp.get());
503
504 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000505 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 else
Greg Clayton9fed0d82010-07-23 23:33:17 +0000507 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508
Greg Clayton5160ce52013-03-27 23:08:40 +0000509 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510 if (log)
511 {
512 StreamString s;
513 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
514 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
515 }
516
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 bp_sp->ResolveBreakpoint();
518 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000519
520 if (!internal && bp_sp)
521 {
522 m_last_created_breakpoint = bp_sp;
523 }
524
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 return bp_sp;
526}
527
Johnny Chen86364b42011-09-20 23:28:55 +0000528bool
529Target::ProcessIsValid()
530{
531 return (m_process_sp && m_process_sp->IsAlive());
532}
533
Johnny Chenb90827e2012-06-04 23:19:54 +0000534static bool
535CheckIfWatchpointsExhausted(Target *target, Error &error)
536{
537 uint32_t num_supported_hardware_watchpoints;
538 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
539 if (rc.Success())
540 {
541 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
542 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
543 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
544 num_supported_hardware_watchpoints);
545 }
546 return false;
547}
548
Johnny Chen01a67862011-10-14 00:42:25 +0000549// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000550// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000551WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000552Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000553{
Greg Clayton5160ce52013-03-27 23:08:40 +0000554 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000555 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000556 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000557 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000558
Johnny Chen01a67862011-10-14 00:42:25 +0000559 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000560 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000561 {
562 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000563 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000564 }
Jim Inghamc6462312013-06-18 21:52:48 +0000565
Johnny Chen45e541f2011-09-14 22:20:15 +0000566 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000567 {
568 if (size == 0)
569 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
570 else
Daniel Malead01b2952012-11-29 21:49:15 +0000571 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000572 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000573 }
Jim Inghamc6462312013-06-18 21:52:48 +0000574
575 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
576 {
577 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
578 }
Johnny Chen7313a642011-09-13 01:15:36 +0000579
Johnny Chen01a67862011-10-14 00:42:25 +0000580 // Currently we only support one watchpoint per address, with total number
581 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000582
583 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000584 const bool notify = false; // Don't notify about all the state changes we do on creating the watchpoint.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000585 Mutex::Locker locker;
586 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000587 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000588 if (matched_sp)
589 {
Johnny Chen0c406372011-09-14 20:23:45 +0000590 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000591 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000592 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
593 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000594 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000595 if (size == old_size && kind == old_type)
596 {
Johnny Chen01a67862011-10-14 00:42:25 +0000597 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000598 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000599 }
600 else
601 {
Johnny Chen01a67862011-10-14 00:42:25 +0000602 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000603 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
604 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000605 }
Johnny Chen3c532582011-09-13 23:29:31 +0000606 }
607
Jason Molenda727e3922012-12-05 23:07:34 +0000608 if (!wp_sp)
609 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000610 wp_sp.reset(new Watchpoint(*this, addr, size, type));
611 wp_sp->SetWatchpointType(kind, notify);
612 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000613 }
Johnny Chen0c406372011-09-14 20:23:45 +0000614
Jim Ingham1b5792e2012-12-18 02:03:49 +0000615 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000616 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000617 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
618 __FUNCTION__,
619 error.Success() ? "succeeded" : "failed",
620 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000621
Jason Molenda727e3922012-12-05 23:07:34 +0000622 if (error.Fail())
623 {
Johnny Chen41b77262012-03-26 22:00:10 +0000624 // Enabling the watchpoint on the device side failed.
625 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000626 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000627 // See if we could provide more helpful error message.
628 if (!CheckIfWatchpointsExhausted(this, error))
629 {
630 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
631 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
632 }
Johnny Chen01a67862011-10-14 00:42:25 +0000633 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000634 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000635 else
Johnny Chen01a67862011-10-14 00:42:25 +0000636 m_last_created_watchpoint = wp_sp;
637 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000638}
639
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640void
641Target::RemoveAllBreakpoints (bool internal_also)
642{
Greg Clayton5160ce52013-03-27 23:08:40 +0000643 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 if (log)
645 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
646
Greg Clayton9fed0d82010-07-23 23:33:17 +0000647 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000649 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000650
651 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652}
653
654void
655Target::DisableAllBreakpoints (bool internal_also)
656{
Greg Clayton5160ce52013-03-27 23:08:40 +0000657 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 if (log)
659 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
660
661 m_breakpoint_list.SetEnabledAll (false);
662 if (internal_also)
663 m_internal_breakpoint_list.SetEnabledAll (false);
664}
665
666void
667Target::EnableAllBreakpoints (bool internal_also)
668{
Greg Clayton5160ce52013-03-27 23:08:40 +0000669 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 if (log)
671 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
672
673 m_breakpoint_list.SetEnabledAll (true);
674 if (internal_also)
675 m_internal_breakpoint_list.SetEnabledAll (true);
676}
677
678bool
679Target::RemoveBreakpointByID (break_id_t break_id)
680{
Greg Clayton5160ce52013-03-27 23:08:40 +0000681 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 if (log)
683 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
684
685 if (DisableBreakpointByID (break_id))
686 {
687 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000688 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000690 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000691 if (m_last_created_breakpoint)
692 {
693 if (m_last_created_breakpoint->GetID() == break_id)
694 m_last_created_breakpoint.reset();
695 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000696 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000697 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 return true;
699 }
700 return false;
701}
702
703bool
704Target::DisableBreakpointByID (break_id_t break_id)
705{
Greg Clayton5160ce52013-03-27 23:08:40 +0000706 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 if (log)
708 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
709
710 BreakpointSP bp_sp;
711
712 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
713 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
714 else
715 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
716 if (bp_sp)
717 {
718 bp_sp->SetEnabled (false);
719 return true;
720 }
721 return false;
722}
723
724bool
725Target::EnableBreakpointByID (break_id_t break_id)
726{
Greg Clayton5160ce52013-03-27 23:08:40 +0000727 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 if (log)
729 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
730 __FUNCTION__,
731 break_id,
732 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
733
734 BreakpointSP bp_sp;
735
736 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
737 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
738 else
739 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
740
741 if (bp_sp)
742 {
743 bp_sp->SetEnabled (true);
744 return true;
745 }
746 return false;
747}
748
Johnny Chenedf50372011-09-23 21:21:43 +0000749// The flag 'end_to_end', default to true, signifies that the operation is
750// performed end to end, for both the debugger and the debuggee.
751
Johnny Chen01a67862011-10-14 00:42:25 +0000752// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
753// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000754bool
Johnny Chen01a67862011-10-14 00:42:25 +0000755Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000756{
Greg Clayton5160ce52013-03-27 23:08:40 +0000757 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000758 if (log)
759 log->Printf ("Target::%s\n", __FUNCTION__);
760
Johnny Chenedf50372011-09-23 21:21:43 +0000761 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000762 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000763 return true;
764 }
765
766 // Otherwise, it's an end to end operation.
767
Johnny Chen86364b42011-09-20 23:28:55 +0000768 if (!ProcessIsValid())
769 return false;
770
Johnny Chen01a67862011-10-14 00:42:25 +0000771 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000772 for (size_t i = 0; i < num_watchpoints; ++i)
773 {
Johnny Chen01a67862011-10-14 00:42:25 +0000774 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
775 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000776 return false;
777
Johnny Chen01a67862011-10-14 00:42:25 +0000778 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000779 if (rc.Fail())
780 return false;
781 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000782 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000783 m_last_created_watchpoint.reset();
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::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000791{
Greg Clayton5160ce52013-03-27 23:08:40 +0000792 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000793 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(false);
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->DisableWatchpoint(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 Chen01a67862011-10-14 00:42:25 +0000820// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
821// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000822bool
Johnny Chen01a67862011-10-14 00:42:25 +0000823Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000824{
Greg Clayton5160ce52013-03-27 23:08:40 +0000825 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000826 if (log)
827 log->Printf ("Target::%s\n", __FUNCTION__);
828
Johnny Chenedf50372011-09-23 21:21:43 +0000829 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000830 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000831 return true;
832 }
833
834 // Otherwise, it's an end to end operation.
835
Johnny Chen86364b42011-09-20 23:28:55 +0000836 if (!ProcessIsValid())
837 return false;
838
Johnny Chen01a67862011-10-14 00:42:25 +0000839 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000840 for (size_t i = 0; i < num_watchpoints; ++i)
841 {
Johnny Chen01a67862011-10-14 00:42:25 +0000842 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
843 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000844 return false;
845
Johnny Chen01a67862011-10-14 00:42:25 +0000846 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000847 if (rc.Fail())
848 return false;
849 }
Johnny Chen86364b42011-09-20 23:28:55 +0000850 return true; // Success!
851}
852
Johnny Chena4d6bc92012-02-25 06:44:30 +0000853// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
854bool
855Target::ClearAllWatchpointHitCounts ()
856{
Greg Clayton5160ce52013-03-27 23:08:40 +0000857 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000858 if (log)
859 log->Printf ("Target::%s\n", __FUNCTION__);
860
861 size_t num_watchpoints = m_watchpoint_list.GetSize();
862 for (size_t i = 0; i < num_watchpoints; ++i)
863 {
864 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
865 if (!wp_sp)
866 return false;
867
868 wp_sp->ResetHitCount();
869 }
870 return true; // Success!
871}
872
Johnny Chen01a67862011-10-14 00:42:25 +0000873// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000874// during these operations.
875bool
Johnny Chen01a67862011-10-14 00:42:25 +0000876Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000877{
Greg Clayton5160ce52013-03-27 23:08:40 +0000878 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000879 if (log)
880 log->Printf ("Target::%s\n", __FUNCTION__);
881
882 if (!ProcessIsValid())
883 return false;
884
Johnny Chen01a67862011-10-14 00:42:25 +0000885 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000886 for (size_t i = 0; i < num_watchpoints; ++i)
887 {
Johnny Chen01a67862011-10-14 00:42:25 +0000888 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
889 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000890 return false;
891
Johnny Chen01a67862011-10-14 00:42:25 +0000892 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000893 }
894 return true; // Success!
895}
896
Johnny Chen01a67862011-10-14 00:42:25 +0000897// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000898bool
Johnny Chen01a67862011-10-14 00:42:25 +0000899Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000900{
Greg Clayton5160ce52013-03-27 23:08:40 +0000901 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000902 if (log)
903 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
904
905 if (!ProcessIsValid())
906 return false;
907
Johnny Chen01a67862011-10-14 00:42:25 +0000908 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
909 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000910 {
Johnny Chen01a67862011-10-14 00:42:25 +0000911 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000912 if (rc.Success())
913 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000914
Johnny Chenf04ee932011-09-22 18:04:58 +0000915 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000916 }
917 return false;
918}
919
Johnny Chen01a67862011-10-14 00:42:25 +0000920// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000921bool
Johnny Chen01a67862011-10-14 00:42:25 +0000922Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000923{
Greg Clayton5160ce52013-03-27 23:08:40 +0000924 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000925 if (log)
926 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
927
928 if (!ProcessIsValid())
929 return false;
930
Johnny Chen01a67862011-10-14 00:42:25 +0000931 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
932 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000933 {
Johnny Chen01a67862011-10-14 00:42:25 +0000934 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000935 if (rc.Success())
936 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000937
Johnny Chenf04ee932011-09-22 18:04:58 +0000938 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000939 }
940 return false;
941}
942
Johnny Chen01a67862011-10-14 00:42:25 +0000943// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000944bool
Johnny Chen01a67862011-10-14 00:42:25 +0000945Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000946{
Greg Clayton5160ce52013-03-27 23:08:40 +0000947 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000948 if (log)
949 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
950
Jim Inghamb0b45132013-07-02 02:09:46 +0000951 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
952 if (watch_to_remove_sp == m_last_created_watchpoint)
953 m_last_created_watchpoint.reset();
954
Johnny Chen01a67862011-10-14 00:42:25 +0000955 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +0000956 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000957 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +0000958 return true;
959 }
960 return false;
961}
962
Johnny Chen01a67862011-10-14 00:42:25 +0000963// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +0000964bool
Johnny Chen01a67862011-10-14 00:42:25 +0000965Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000966{
Greg Clayton5160ce52013-03-27 23:08:40 +0000967 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000968 if (log)
969 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
970
971 if (!ProcessIsValid())
972 return false;
973
Johnny Chen01a67862011-10-14 00:42:25 +0000974 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
975 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000976 {
Johnny Chen01a67862011-10-14 00:42:25 +0000977 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000978 return true;
979 }
980 return false;
981}
982
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983ModuleSP
984Target::GetExecutableModule ()
985{
Greg Claytonaa149cb2011-08-11 02:48:45 +0000986 return m_images.GetModuleAtIndex(0);
987}
988
989Module*
990Target::GetExecutableModulePointer ()
991{
992 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993}
994
Enrico Granata17598482012-11-08 02:22:02 +0000995static void
996LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
997{
998 Error error;
Enrico Granata97303392013-05-21 00:00:30 +0000999 StreamString feedback_stream;
1000 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001001 {
Enrico Granata97303392013-05-21 00:00:30 +00001002 if (error.AsCString())
1003 target->GetDebugger().GetErrorStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
1004 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1005 error.AsCString());
1006 if (feedback_stream.GetSize())
1007 target->GetDebugger().GetOutputStream().Printf("%s\n",
1008 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001009 }
1010}
1011
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012void
1013Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1014{
Greg Clayton5160ce52013-03-27 23:08:40 +00001015 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016 m_images.Clear();
1017 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001018 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001019 m_ast_importer_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020
1021 if (executable_sp.get())
1022 {
1023 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001024 "Target::SetExecutableModule (executable = '%s')",
1025 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026
1027 m_images.Append(executable_sp); // The first image is our exectuable file
1028
Jim Ingham5aee1622010-08-09 23:31:02 +00001029 // 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 +00001030 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001031 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001032 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001033 if (log)
1034 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1035 }
1036
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001038 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001040 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 {
1042 executable_objfile->GetDependentModules(dependent_files);
1043 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1044 {
Greg Claytonded470d2011-03-19 01:12:21 +00001045 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1046 FileSpec platform_dependent_file_spec;
1047 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55 +00001048 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001049 else
1050 platform_dependent_file_spec = dependent_file_spec;
1051
Greg Claytonb9a01b32012-02-26 05:51:37 +00001052 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1053 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 if (image_module_sp.get())
1055 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056 ObjectFile *objfile = image_module_sp->GetObjectFile();
1057 if (objfile)
1058 objfile->GetDependentModules(dependent_files);
1059 }
1060 }
1061 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062 }
1063}
1064
1065
Jim Ingham5aee1622010-08-09 23:31:02 +00001066bool
1067Target::SetArchitecture (const ArchSpec &arch_spec)
1068{
Greg Clayton5160ce52013-03-27 23:08:40 +00001069 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001070 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001071 {
Greg Clayton70512312012-05-08 01:45:38 +00001072 // If we haven't got a valid arch spec, or the architectures are
1073 // compatible, so just update the architecture. Architectures can be
1074 // equal, yet the triple OS and vendor might change, so we need to do
1075 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001076 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001077 if (log)
1078 log->Printf ("Target::SetArchitecture setting architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Jim Ingham5aee1622010-08-09 23:31:02 +00001079 return true;
1080 }
1081 else
1082 {
1083 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001084 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001085 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00001086 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001087 ModuleSP executable_sp = GetExecutableModule ();
1088 m_images.Clear();
1089 m_scratch_ast_context_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001090 m_scratch_ast_source_ap.reset();
1091 m_ast_importer_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00001092 // Need to do something about unsetting breakpoints.
1093
1094 if (executable_sp)
1095 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001096 if (log)
1097 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Greg Claytonb9a01b32012-02-26 05:51:37 +00001098 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1099 Error error = ModuleList::GetSharedModule (module_spec,
1100 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001101 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001102 NULL,
1103 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001104
1105 if (!error.Fail() && executable_sp)
1106 {
1107 SetExecutableModule (executable_sp, true);
1108 return true;
1109 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001110 }
1111 }
Greg Clayton70512312012-05-08 01:45:38 +00001112 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001113}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115void
Enrico Granataefe637d2012-11-08 19:16:03 +00001116Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001117{
1118}
1119
1120void
Enrico Granataefe637d2012-11-08 19:16:03 +00001121Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122{
1123 // A module is being added to this target for the first time
Enrico Granataefe637d2012-11-08 19:16:03 +00001124 ModuleList my_module_list;
1125 my_module_list.Append(module_sp);
Enrico Granata17598482012-11-08 02:22:02 +00001126 LoadScriptingResourceForModule(module_sp, this);
Enrico Granataefe637d2012-11-08 19:16:03 +00001127 ModulesDidLoad (my_module_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128}
1129
1130void
Enrico Granataefe637d2012-11-08 19:16:03 +00001131Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001132{
1133 // A module is being added to this target for the first time
Enrico Granataefe637d2012-11-08 19:16:03 +00001134 ModuleList my_module_list;
1135 my_module_list.Append(module_sp);
1136 ModulesDidUnload (my_module_list);
Enrico Granata17598482012-11-08 02:22:02 +00001137}
1138
1139void
Enrico Granataefe637d2012-11-08 19:16:03 +00001140Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141{
Jim Inghame716ae02011-08-03 01:00:06 +00001142 // A module is replacing an already added module
Jim Ingham4a94c912012-05-17 18:38:42 +00001143 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144}
1145
1146void
1147Target::ModulesDidLoad (ModuleList &module_list)
1148{
Enrico Granata17598482012-11-08 02:22:02 +00001149 if (module_list.GetSize())
1150 {
1151 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1152 // TODO: make event data that packages up the module_list
1153 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1154 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155}
1156
1157void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001158Target::SymbolsDidLoad (ModuleList &module_list)
1159{
Jim Ingham31caf982013-06-04 23:01:35 +00001160 if (module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001161 {
Jim Ingham31caf982013-06-04 23:01:35 +00001162 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001163 {
Jim Ingham31caf982013-06-04 23:01:35 +00001164 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1165 if (runtime)
1166 {
1167 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1168 objc_runtime->SymbolsDidLoad(module_list);
1169 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001170 }
Jim Ingham31caf982013-06-04 23:01:35 +00001171
1172 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1173 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001174 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001175}
1176
1177void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178Target::ModulesDidUnload (ModuleList &module_list)
1179{
Enrico Granata17598482012-11-08 02:22:02 +00001180 if (module_list.GetSize())
1181 {
1182 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1183 // TODO: make event data that packages up the module_list
1184 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1185 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186}
1187
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001188bool
Greg Claytonb9a01b32012-02-26 05:51:37 +00001189Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001190{
Greg Clayton67cc0632012-08-22 17:17:09 +00001191 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001192 {
1193 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001194 ModuleSpec module_spec (module_file_spec);
1195 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001196
1197 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1198 // black list.
1199 if (num_modules > 0)
1200 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001201 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001202 {
1203 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1204 return false;
1205 }
1206 return true;
1207 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001208 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001209 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001210}
1211
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001212bool
Jim Inghamc6674fd2011-10-28 23:14:11 +00001213Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1214{
Greg Clayton67cc0632012-08-22 17:17:09 +00001215 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001216 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001217 if (m_platform_sp)
1218 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001219 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001220 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001221}
1222
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223size_t
Greg Claytondb598232011-01-07 01:57:07 +00001224Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1225{
Greg Claytone72dfb32012-02-24 01:59:29 +00001226 SectionSP section_sp (addr.GetSection());
1227 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001228 {
Jason Molenda216d91f2012-04-25 00:06:56 +00001229 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1230 if (section_sp->IsEncrypted())
1231 {
Greg Clayton57f06302012-05-25 17:05:55 +00001232 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001233 return 0;
1234 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001235 ModuleSP module_sp (section_sp->GetModule());
1236 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001237 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001238 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1239 if (objfile)
1240 {
1241 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1242 addr.GetOffset(),
1243 dst,
1244 dst_len);
1245 if (bytes_read > 0)
1246 return bytes_read;
1247 else
1248 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1249 }
Greg Claytondb598232011-01-07 01:57:07 +00001250 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001251 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001252 }
1253 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001254 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001255 }
1256 else
Greg Claytondb598232011-01-07 01:57:07 +00001257 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001258
Greg Claytondb598232011-01-07 01:57:07 +00001259 return 0;
1260}
1261
1262size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001263Target::ReadMemory (const Address& addr,
1264 bool prefer_file_cache,
1265 void *dst,
1266 size_t dst_len,
1267 Error &error,
1268 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001271
Enrico Granata9128ee22011-09-06 19:20:51 +00001272 // if we end up reading this from process memory, we will fill this
1273 // with the actual load address
1274 if (load_addr_ptr)
1275 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1276
Greg Claytondb598232011-01-07 01:57:07 +00001277 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001278
1279 addr_t load_addr = LLDB_INVALID_ADDRESS;
1280 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001281 Address resolved_addr;
1282 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001283 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001284 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001285 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001286 // No sections are loaded, so we must assume we are not running
1287 // yet and anything we are given is a file address.
1288 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1289 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001290 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001291 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001292 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001293 // We have at least one section loaded. This can be becuase
1294 // we have manually loaded some sections with "target modules load ..."
1295 // or because we have have a live process that has sections loaded
1296 // through the dynamic loader
1297 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1298 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001299 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001300 }
Greg Clayton357132e2011-03-26 19:14:58 +00001301 if (!resolved_addr.IsValid())
1302 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001303
Greg Claytonc749eb82011-07-11 05:12:02 +00001304
Greg Claytondb598232011-01-07 01:57:07 +00001305 if (prefer_file_cache)
1306 {
1307 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1308 if (bytes_read > 0)
1309 return bytes_read;
1310 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001311
Johnny Chen86364b42011-09-20 23:28:55 +00001312 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001313 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001314 if (load_addr == LLDB_INVALID_ADDRESS)
1315 load_addr = resolved_addr.GetLoadAddress (this);
1316
Greg Claytondda4f7b2010-06-30 23:03:03 +00001317 if (load_addr == LLDB_INVALID_ADDRESS)
1318 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001319 ModuleSP addr_module_sp (resolved_addr.GetModule());
1320 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001321 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Claytone72dfb32012-02-24 01:59:29 +00001322 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08 +00001323 resolved_addr.GetFileAddress(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001324 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001325 else
Daniel Malead01b2952012-11-29 21:49:15 +00001326 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001327 }
1328 else
1329 {
Greg Claytondb598232011-01-07 01:57:07 +00001330 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331 if (bytes_read != dst_len)
1332 {
1333 if (error.Success())
1334 {
1335 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001336 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337 else
Daniel Malead01b2952012-11-29 21:49:15 +00001338 error.SetErrorStringWithFormat("only %" PRIu64 " of %" PRIu64 " bytes were read from memory at 0x%" PRIx64, (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339 }
1340 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001341 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001342 {
1343 if (load_addr_ptr)
1344 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001345 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001346 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001347 // If the address is not section offset we have an address that
1348 // doesn't resolve to any address in any currently loaded shared
1349 // libaries and we failed to read memory so there isn't anything
1350 // more we can do. If it is section offset, we might be able to
1351 // read cached memory from the object file.
1352 if (!resolved_addr.IsSectionOffset())
1353 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001356
Greg Claytonc749eb82011-07-11 05:12:02 +00001357 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001358 {
Greg Claytondb598232011-01-07 01:57:07 +00001359 // If we didn't already try and read from the object file cache, then
1360 // try it after failing to read from the process.
1361 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001362 }
1363 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364}
1365
Greg Claytond16e1e52011-07-12 17:06:17 +00001366size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001367Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1368{
1369 char buf[256];
1370 out_str.clear();
1371 addr_t curr_addr = addr.GetLoadAddress(this);
1372 Address address(addr);
1373 while (1)
1374 {
1375 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1376 if (length == 0)
1377 break;
1378 out_str.append(buf, length);
1379 // If we got "length - 1" bytes, we didn't get the whole C string, we
1380 // need to read some more characters
1381 if (length == sizeof(buf) - 1)
1382 curr_addr += length;
1383 else
1384 break;
1385 address = Address(curr_addr);
1386 }
1387 return out_str.size();
1388}
1389
1390
1391size_t
1392Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1393{
1394 size_t total_cstr_len = 0;
1395 if (dst && dst_max_len)
1396 {
1397 result_error.Clear();
1398 // NULL out everything just to be safe
1399 memset (dst, 0, dst_max_len);
1400 Error error;
1401 addr_t curr_addr = addr.GetLoadAddress(this);
1402 Address address(addr);
1403 const size_t cache_line_size = 512;
1404 size_t bytes_left = dst_max_len - 1;
1405 char *curr_dst = dst;
1406
1407 while (bytes_left > 0)
1408 {
1409 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1410 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1411 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1412
1413 if (bytes_read == 0)
1414 {
1415 result_error = error;
1416 dst[total_cstr_len] = '\0';
1417 break;
1418 }
1419 const size_t len = strlen(curr_dst);
1420
1421 total_cstr_len += len;
1422
1423 if (len < bytes_to_read)
1424 break;
1425
1426 curr_dst += bytes_read;
1427 curr_addr += bytes_read;
1428 bytes_left -= bytes_read;
1429 address = Address(curr_addr);
1430 }
1431 }
1432 else
1433 {
1434 if (dst == NULL)
1435 result_error.SetErrorString("invalid arguments");
1436 else
1437 result_error.Clear();
1438 }
1439 return total_cstr_len;
1440}
1441
1442size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001443Target::ReadScalarIntegerFromMemory (const Address& addr,
1444 bool prefer_file_cache,
1445 uint32_t byte_size,
1446 bool is_signed,
1447 Scalar &scalar,
1448 Error &error)
1449{
1450 uint64_t uval;
1451
1452 if (byte_size <= sizeof(uval))
1453 {
1454 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1455 if (bytes_read == byte_size)
1456 {
1457 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001458 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001459 if (byte_size <= 4)
1460 scalar = data.GetMaxU32 (&offset, byte_size);
1461 else
1462 scalar = data.GetMaxU64 (&offset, byte_size);
1463
1464 if (is_signed)
1465 scalar.SignExtend(byte_size * 8);
1466 return bytes_read;
1467 }
1468 }
1469 else
1470 {
1471 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1472 }
1473 return 0;
1474}
1475
1476uint64_t
1477Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1478 bool prefer_file_cache,
1479 size_t integer_byte_size,
1480 uint64_t fail_value,
1481 Error &error)
1482{
1483 Scalar scalar;
1484 if (ReadScalarIntegerFromMemory (addr,
1485 prefer_file_cache,
1486 integer_byte_size,
1487 false,
1488 scalar,
1489 error))
1490 return scalar.ULongLong(fail_value);
1491 return fail_value;
1492}
1493
1494bool
1495Target::ReadPointerFromMemory (const Address& addr,
1496 bool prefer_file_cache,
1497 Error &error,
1498 Address &pointer_addr)
1499{
1500 Scalar scalar;
1501 if (ReadScalarIntegerFromMemory (addr,
1502 prefer_file_cache,
1503 m_arch.GetAddressByteSize(),
1504 false,
1505 scalar,
1506 error))
1507 {
1508 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1509 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1510 {
1511 if (m_section_load_list.IsEmpty())
1512 {
1513 // No sections are loaded, so we must assume we are not running
1514 // yet and anything we are given is a file address.
1515 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1516 }
1517 else
1518 {
1519 // We have at least one section loaded. This can be becuase
1520 // we have manually loaded some sections with "target modules load ..."
1521 // or because we have have a live process that has sections loaded
1522 // through the dynamic loader
1523 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1524 }
1525 // We weren't able to resolve the pointer value, so just return
1526 // an address with no section
1527 if (!pointer_addr.IsValid())
1528 pointer_addr.SetOffset (pointer_vm_addr);
1529 return true;
1530
1531 }
1532 }
1533 return false;
1534}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535
1536ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001537Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001539 ModuleSP module_sp;
1540
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001541 Error error;
1542
Jim Ingham4a94c912012-05-17 18:38:42 +00001543 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1544 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001545
Jim Ingham4a94c912012-05-17 18:38:42 +00001546 if (module_spec.GetUUID().IsValid())
1547 module_sp = m_images.FindFirstModule(module_spec);
1548
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001549 if (!module_sp)
1550 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001551 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1552 bool did_create_module = false;
1553
1554 // If there are image search path entries, try to use them first to acquire a suitable image.
1555 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001556 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001557 ModuleSpec transformed_spec (module_spec);
1558 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1559 {
1560 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1561 error = ModuleList::GetSharedModule (transformed_spec,
1562 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001563 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001564 &old_module_sp,
1565 &did_create_module);
1566 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001567 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001568
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001569 if (!module_sp)
1570 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001571 // If we have a UUID, we can check our global shared module list in case
1572 // we already have it. If we don't have a valid UUID, then we can't since
1573 // the path in "module_spec" will be a platform path, and we will need to
1574 // let the platform find that file. For example, we could be asking for
1575 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1576 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1577 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1578 // cache.
1579 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001580 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001581 // We have a UUID, it is OK to check the global module list...
1582 error = ModuleList::GetSharedModule (module_spec,
1583 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001584 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001585 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001586 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001587 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001588
1589 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001590 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001591 // The platform is responsible for finding and caching an appropriate
1592 // module in the shared module cache.
1593 if (m_platform_sp)
1594 {
1595 FileSpec platform_file_spec;
1596 error = m_platform_sp->GetSharedModule (module_spec,
1597 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001598 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001599 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001600 &did_create_module);
1601 }
1602 else
1603 {
1604 error.SetErrorString("no platform is currently set");
1605 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001606 }
1607 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001608
Jim Ingham4a94c912012-05-17 18:38:42 +00001609 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1610 // module in the list already, and if there was, let's remove it.
1611 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001612 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001613 ObjectFile *objfile = module_sp->GetObjectFile();
1614 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001615 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001616 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001617 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001618 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1619 case ObjectFile::eTypeExecutable: /// A normal executable
1620 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1621 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1622 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1623 break;
1624 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1625 if (error_ptr)
1626 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1627 return ModuleSP();
1628 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1629 if (error_ptr)
1630 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1631 return ModuleSP();
1632 default:
1633 if (error_ptr)
1634 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1635 return ModuleSP();
1636 }
1637 // GetSharedModule is not guaranteed to find the old shared module, for instance
1638 // in the common case where you pass in the UUID, it is only going to find the one
1639 // module matching the UUID. In fact, it has no good way to know what the "old module"
1640 // relevant to this target is, since there might be many copies of a module with this file spec
1641 // in various running debug sessions, but only one of them will belong to this target.
1642 // So let's remove the UUID from the module list, and look in the target's module list.
1643 // Only do this if there is SOMETHING else in the module spec...
1644 if (!old_module_sp)
1645 {
1646 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001647 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001648 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1649 module_spec_copy.GetUUID().Clear();
1650
1651 ModuleList found_modules;
1652 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1653 if (num_found == 1)
1654 {
1655 old_module_sp = found_modules.GetModuleAtIndex(0);
1656 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001657 }
1658 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001659
1660 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1661 {
1662 m_images.ReplaceModule(old_module_sp, module_sp);
1663 Module *old_module_ptr = old_module_sp.get();
1664 old_module_sp.reset();
1665 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1666 }
1667 else
1668 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001669 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001670 }
1671 }
1672 if (error_ptr)
1673 *error_ptr = error;
1674 return module_sp;
1675}
1676
1677
Greg Claytond9e416c2012-02-18 05:35:26 +00001678TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001679Target::CalculateTarget ()
1680{
Greg Claytond9e416c2012-02-18 05:35:26 +00001681 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682}
1683
Greg Claytond9e416c2012-02-18 05:35:26 +00001684ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685Target::CalculateProcess ()
1686{
Greg Claytond9e416c2012-02-18 05:35:26 +00001687 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688}
1689
Greg Claytond9e416c2012-02-18 05:35:26 +00001690ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691Target::CalculateThread ()
1692{
Greg Claytond9e416c2012-02-18 05:35:26 +00001693 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001694}
1695
Greg Claytond9e416c2012-02-18 05:35:26 +00001696StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001697Target::CalculateStackFrame ()
1698{
Greg Claytond9e416c2012-02-18 05:35:26 +00001699 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700}
1701
1702void
Greg Clayton0603aa92010-10-04 01:05:56 +00001703Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704{
Greg Claytonc14ee322011-09-22 04:58:26 +00001705 exe_ctx.Clear();
1706 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707}
1708
1709PathMappingList &
1710Target::GetImageSearchPathList ()
1711{
1712 return m_image_search_paths;
1713}
1714
1715void
1716Target::ImageSearchPathsChanged
1717(
1718 const PathMappingList &path_list,
1719 void *baton
1720)
1721{
1722 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001723 ModuleSP exe_module_sp (target->GetExecutableModule());
1724 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001725 {
Greg Claytonaa149cb2011-08-11 02:48:45 +00001726 target->m_images.Clear();
1727 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001728 }
1729}
1730
1731ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001732Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733{
Greg Clayton73da2442011-08-03 01:23:55 +00001734 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001735 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001736 {
Greg Clayton73da2442011-08-03 01:23:55 +00001737 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001738 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001739 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1740 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1741 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1742 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001743 return m_scratch_ast_context_ap.get();
1744}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001745
Sean Callanan686b2312011-11-16 18:20:47 +00001746ClangASTImporter *
1747Target::GetClangASTImporter()
1748{
1749 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1750
1751 if (!ast_importer)
1752 {
1753 ast_importer = new ClangASTImporter();
1754 m_ast_importer_ap.reset(ast_importer);
1755 }
1756
1757 return ast_importer;
1758}
1759
Greg Clayton99d0faf2010-11-18 23:32:35 +00001760void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001761Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001762{
Greg Clayton6920b522012-08-22 18:39:03 +00001763 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001764}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001765
Greg Clayton99d0faf2010-11-18 23:32:35 +00001766void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001767Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001768{
Greg Clayton6920b522012-08-22 18:39:03 +00001769 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001770}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001771
Greg Claytonc859e2d2012-02-13 23:10:39 +00001772FileSpecList
1773Target::GetDefaultExecutableSearchPaths ()
1774{
Greg Clayton67cc0632012-08-22 17:17:09 +00001775 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1776 if (properties_sp)
1777 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001778 return FileSpecList();
1779}
1780
Michael Sartaina7499c92013-07-01 19:45:50 +00001781FileSpecList
1782Target::GetDefaultDebugFileSearchPaths ()
1783{
1784 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1785 if (properties_sp)
1786 return properties_sp->GetDebugFileSearchPaths();
1787 return FileSpecList();
1788}
1789
Caroline Ticedaccaa92010-09-20 20:44:43 +00001790ArchSpec
1791Target::GetDefaultArchitecture ()
1792{
Greg Clayton67cc0632012-08-22 17:17:09 +00001793 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1794 if (properties_sp)
1795 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001796 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001797}
1798
1799void
Greg Clayton67cc0632012-08-22 17:17:09 +00001800Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001801{
Greg Clayton67cc0632012-08-22 17:17:09 +00001802 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1803 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001804 {
1805 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::SetDefaultArchitecture setting target's default architecture to %s (%s)", arch.GetArchitectureName(), arch.GetTriple().getTriple().c_str());
Greg Clayton67cc0632012-08-22 17:17:09 +00001806 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001807 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001808}
1809
Greg Clayton0603aa92010-10-04 01:05:56 +00001810Target *
1811Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1812{
1813 // The target can either exist in the "process" of ExecutionContext, or in
1814 // the "target_sp" member of SymbolContext. This accessor helper function
1815 // will get the target from one of these locations.
1816
1817 Target *target = NULL;
1818 if (sc_ptr != NULL)
1819 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001820 if (target == NULL && exe_ctx_ptr)
1821 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001822 return target;
1823}
1824
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001825ExecutionResults
1826Target::EvaluateExpression
1827(
1828 const char *expr_cstr,
1829 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001830 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001831 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001832)
1833{
Enrico Granata97fca502012-09-18 17:43:16 +00001834 result_valobj_sp.reset();
1835
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001836 ExecutionResults execution_results = eExecutionSetupError;
1837
Greg Claytond1767f02011-12-08 02:13:16 +00001838 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1839 return execution_results;
1840
Jim Ingham6026ca32011-05-12 02:06:14 +00001841 // We shouldn't run stop hooks in expressions.
1842 // Be sure to reset this if you return anywhere within this function.
1843 bool old_suppress_value = m_suppress_stop_hooks;
1844 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001845
1846 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001847
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001848 if (frame)
1849 {
1850 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001851 }
1852 else if (m_process_sp)
1853 {
1854 m_process_sp->CalculateExecutionContext(exe_ctx);
1855 }
1856 else
1857 {
1858 CalculateExecutionContext(exe_ctx);
1859 }
1860
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001861 // Make sure we aren't just trying to see the value of a persistent
1862 // variable (something like "$0")
1863 lldb::ClangExpressionVariableSP persistent_var_sp;
1864 // Only check for persistent variables the expression starts with a '$'
1865 if (expr_cstr[0] == '$')
1866 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1867
1868 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001869 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001870 result_valobj_sp = persistent_var_sp->GetValueObject ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001871 execution_results = eExecutionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001872 }
1873 else
1874 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001875 const char *prefix = GetExpressionPrefixContentsAsCString();
1876
1877 execution_results = ClangUserExpression::Evaluate (exe_ctx,
1878 options.GetExecutionPolicy(),
1879 lldb::eLanguageTypeUnknown,
1880 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1881 options.DoesUnwindOnError(),
Jim Ingham184e9812013-01-15 02:47:48 +00001882 options.DoesIgnoreBreakpoints(),
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001883 expr_cstr,
1884 prefix,
1885 result_valobj_sp,
1886 options.GetRunOthers(),
1887 options.GetTimeoutUsec());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001888 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001889
1890 m_suppress_stop_hooks = old_suppress_value;
1891
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001892 return execution_results;
1893}
1894
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001895lldb::addr_t
1896Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1897{
1898 addr_t code_addr = load_addr;
1899 switch (m_arch.GetMachine())
1900 {
1901 case llvm::Triple::arm:
1902 case llvm::Triple::thumb:
1903 switch (addr_class)
1904 {
1905 case eAddressClassData:
1906 case eAddressClassDebug:
1907 return LLDB_INVALID_ADDRESS;
1908
1909 case eAddressClassUnknown:
1910 case eAddressClassInvalid:
1911 case eAddressClassCode:
1912 case eAddressClassCodeAlternateISA:
1913 case eAddressClassRuntime:
1914 // Check if bit zero it no set?
1915 if ((code_addr & 1ull) == 0)
1916 {
1917 // Bit zero isn't set, check if the address is a multiple of 2?
1918 if (code_addr & 2ull)
1919 {
1920 // The address is a multiple of 2 so it must be thumb, set bit zero
1921 code_addr |= 1ull;
1922 }
1923 else if (addr_class == eAddressClassCodeAlternateISA)
1924 {
1925 // We checked the address and the address claims to be the alternate ISA
1926 // which means thumb, so set bit zero.
1927 code_addr |= 1ull;
1928 }
1929 }
1930 break;
1931 }
1932 break;
1933
1934 default:
1935 break;
1936 }
1937 return code_addr;
1938}
1939
1940lldb::addr_t
1941Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1942{
1943 addr_t opcode_addr = load_addr;
1944 switch (m_arch.GetMachine())
1945 {
1946 case llvm::Triple::arm:
1947 case llvm::Triple::thumb:
1948 switch (addr_class)
1949 {
1950 case eAddressClassData:
1951 case eAddressClassDebug:
1952 return LLDB_INVALID_ADDRESS;
1953
1954 case eAddressClassInvalid:
1955 case eAddressClassUnknown:
1956 case eAddressClassCode:
1957 case eAddressClassCodeAlternateISA:
1958 case eAddressClassRuntime:
1959 opcode_addr &= ~(1ull);
1960 break;
1961 }
1962 break;
1963
1964 default:
1965 break;
1966 }
1967 return opcode_addr;
1968}
1969
Greg Clayton9585fbf2013-03-19 00:20:55 +00001970SourceManager &
1971Target::GetSourceManager ()
1972{
1973 if (m_source_manager_ap.get() == NULL)
1974 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
1975 return *m_source_manager_ap;
1976}
1977
1978
Jim Ingham9575d842011-03-11 03:53:59 +00001979lldb::user_id_t
1980Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1981{
1982 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Claytone1cd1be2012-01-29 20:56:30 +00001983 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Ingham9575d842011-03-11 03:53:59 +00001984 m_stop_hooks[new_uid] = new_hook_sp;
1985 return new_uid;
1986}
1987
1988bool
1989Target::RemoveStopHookByID (lldb::user_id_t user_id)
1990{
1991 size_t num_removed;
1992 num_removed = m_stop_hooks.erase (user_id);
1993 if (num_removed == 0)
1994 return false;
1995 else
1996 return true;
1997}
1998
1999void
2000Target::RemoveAllStopHooks ()
2001{
2002 m_stop_hooks.clear();
2003}
2004
2005Target::StopHookSP
2006Target::GetStopHookByID (lldb::user_id_t user_id)
2007{
2008 StopHookSP found_hook;
2009
2010 StopHookCollection::iterator specified_hook_iter;
2011 specified_hook_iter = m_stop_hooks.find (user_id);
2012 if (specified_hook_iter != m_stop_hooks.end())
2013 found_hook = (*specified_hook_iter).second;
2014 return found_hook;
2015}
2016
2017bool
2018Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2019{
2020 StopHookCollection::iterator specified_hook_iter;
2021 specified_hook_iter = m_stop_hooks.find (user_id);
2022 if (specified_hook_iter == m_stop_hooks.end())
2023 return false;
2024
2025 (*specified_hook_iter).second->SetIsActive (active_state);
2026 return true;
2027}
2028
2029void
2030Target::SetAllStopHooksActiveState (bool active_state)
2031{
2032 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2033 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2034 {
2035 (*pos).second->SetIsActive (active_state);
2036 }
2037}
2038
2039void
2040Target::RunStopHooks ()
2041{
Jim Ingham6026ca32011-05-12 02:06:14 +00002042 if (m_suppress_stop_hooks)
2043 return;
2044
Jim Ingham9575d842011-03-11 03:53:59 +00002045 if (!m_process_sp)
2046 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002047
2048 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2049 // since in that case we do not want to run the stop-hooks
2050 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2051 return;
2052
Jim Ingham9575d842011-03-11 03:53:59 +00002053 if (m_stop_hooks.empty())
2054 return;
2055
2056 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2057
2058 // If there aren't any active stop hooks, don't bother either:
2059 bool any_active_hooks = false;
2060 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2061 {
2062 if ((*pos).second->IsActive())
2063 {
2064 any_active_hooks = true;
2065 break;
2066 }
2067 }
2068 if (!any_active_hooks)
2069 return;
2070
2071 CommandReturnObject result;
2072
2073 std::vector<ExecutionContext> exc_ctx_with_reasons;
2074 std::vector<SymbolContext> sym_ctx_with_reasons;
2075
2076 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2077 size_t num_threads = cur_threadlist.GetSize();
2078 for (size_t i = 0; i < num_threads; i++)
2079 {
2080 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2081 if (cur_thread_sp->ThreadStoppedForAReason())
2082 {
2083 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2084 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2085 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2086 }
2087 }
2088
2089 // If no threads stopped for a reason, don't run the stop-hooks.
2090 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2091 if (num_exe_ctx == 0)
2092 return;
2093
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002094 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2095 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002096
2097 bool keep_going = true;
2098 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002099 bool print_hook_header;
2100 bool print_thread_header;
2101
2102 if (num_exe_ctx == 1)
2103 print_thread_header = false;
2104 else
2105 print_thread_header = true;
2106
2107 if (m_stop_hooks.size() == 1)
2108 print_hook_header = false;
2109 else
2110 print_hook_header = true;
2111
Jim Ingham9575d842011-03-11 03:53:59 +00002112 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2113 {
2114 // result.Clear();
2115 StopHookSP cur_hook_sp = (*pos).second;
2116 if (!cur_hook_sp->IsActive())
2117 continue;
2118
2119 bool any_thread_matched = false;
2120 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2121 {
2122 if ((cur_hook_sp->GetSpecifier () == NULL
2123 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2124 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002125 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002126 {
2127 if (!hooks_ran)
2128 {
Jim Ingham9575d842011-03-11 03:53:59 +00002129 hooks_ran = true;
2130 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002131 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002132 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002133 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2134 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2135 NULL);
2136 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002137 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002138 else
Daniel Malead01b2952012-11-29 21:49:15 +00002139 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002140 any_thread_matched = true;
2141 }
2142
Jim Ingham381e25b2011-03-22 01:47:27 +00002143 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002144 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:59 +00002145
2146 bool stop_on_continue = true;
2147 bool stop_on_error = true;
2148 bool echo_commands = false;
2149 bool print_results = true;
2150 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:51 +00002151 &exc_ctx_with_reasons[i],
2152 stop_on_continue,
2153 stop_on_error,
2154 echo_commands,
Enrico Granata5f5ab602012-05-31 01:09:06 +00002155 print_results,
2156 eLazyBoolNo,
Greg Clayton32e0a752011-03-30 18:16:51 +00002157 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002158
2159 // If the command started the target going again, we should bag out of
2160 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002161 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2162 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002163 {
Daniel Malead01b2952012-11-29 21:49:15 +00002164 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002165 keep_going = false;
2166 }
2167 }
2168 }
2169 }
Jason Molenda879cf772011-09-23 00:42:55 +00002170
Caroline Tice969ed3d2011-05-02 20:41:46 +00002171 result.GetImmediateOutputStream()->Flush();
2172 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002173}
2174
Greg Clayton7b242382011-07-08 00:48:09 +00002175
Jim Ingham9575d842011-03-11 03:53:59 +00002176//--------------------------------------------------------------
2177// class Target::StopHook
2178//--------------------------------------------------------------
2179
2180
2181Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2182 UserID (uid),
2183 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002184 m_commands (),
2185 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002186 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002187 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002188{
2189}
2190
2191Target::StopHook::StopHook (const StopHook &rhs) :
2192 UserID (rhs.GetID()),
2193 m_target_sp (rhs.m_target_sp),
2194 m_commands (rhs.m_commands),
2195 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002196 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002197 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002198{
2199 if (rhs.m_thread_spec_ap.get() != NULL)
2200 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2201}
2202
2203
2204Target::StopHook::~StopHook ()
2205{
2206}
2207
2208void
2209Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2210{
2211 m_thread_spec_ap.reset (specifier);
2212}
2213
2214
2215void
2216Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2217{
2218 int indent_level = s->GetIndentLevel();
2219
2220 s->SetIndentLevel(indent_level + 2);
2221
Daniel Malead01b2952012-11-29 21:49:15 +00002222 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002223 if (m_active)
2224 s->Indent ("State: enabled\n");
2225 else
2226 s->Indent ("State: disabled\n");
2227
2228 if (m_specifier_sp)
2229 {
2230 s->Indent();
2231 s->PutCString ("Specifier:\n");
2232 s->SetIndentLevel (indent_level + 4);
2233 m_specifier_sp->GetDescription (s, level);
2234 s->SetIndentLevel (indent_level + 2);
2235 }
2236
2237 if (m_thread_spec_ap.get() != NULL)
2238 {
2239 StreamString tmp;
2240 s->Indent("Thread:\n");
2241 m_thread_spec_ap->GetDescription (&tmp, level);
2242 s->SetIndentLevel (indent_level + 4);
2243 s->Indent (tmp.GetData());
2244 s->PutCString ("\n");
2245 s->SetIndentLevel (indent_level + 2);
2246 }
2247
2248 s->Indent ("Commands: \n");
2249 s->SetIndentLevel (indent_level + 4);
2250 uint32_t num_commands = m_commands.GetSize();
2251 for (uint32_t i = 0; i < num_commands; i++)
2252 {
2253 s->Indent(m_commands.GetStringAtIndex(i));
2254 s->PutCString ("\n");
2255 }
2256 s->SetIndentLevel (indent_level);
2257}
2258
Greg Clayton67cc0632012-08-22 17:17:09 +00002259//--------------------------------------------------------------
2260// class TargetProperties
2261//--------------------------------------------------------------
2262
2263OptionEnumValueElement
2264lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002265{
Greg Clayton67cc0632012-08-22 17:17:09 +00002266 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2267 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2268 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2269 { 0, NULL, NULL }
2270};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002271
Greg Clayton1f746072012-08-29 21:13:06 +00002272static OptionEnumValueElement
2273g_inline_breakpoint_enums[] =
2274{
2275 { 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."},
2276 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2277 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2278 { 0, NULL, NULL }
2279};
2280
Jim Ingham0f063ba2013-03-02 00:26:47 +00002281typedef enum x86DisassemblyFlavor
2282{
2283 eX86DisFlavorDefault,
2284 eX86DisFlavorIntel,
2285 eX86DisFlavorATT
2286} x86DisassemblyFlavor;
2287
2288static OptionEnumValueElement
2289g_x86_dis_flavor_value_types[] =
2290{
2291 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2292 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2293 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2294 { 0, NULL, NULL }
2295};
2296
Enrico Granata397ddd52013-05-21 20:13:34 +00002297static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002298g_hex_immediate_style_values[] =
2299{
2300 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2301 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2302 { 0, NULL, NULL }
2303};
2304
2305static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002306g_load_script_from_sym_file_values[] =
2307{
2308 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2309 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2310 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2311 { 0, NULL, NULL }
2312};
2313
Greg Claytonfd814c52013-08-13 01:42:25 +00002314
2315static OptionEnumValueElement
2316g_memory_module_load_level_values[] =
2317{
Greg Clayton86eac942013-08-13 21:32:34 +00002318 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002319 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2320 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2321 { 0, NULL, NULL }
2322};
2323
Greg Clayton67cc0632012-08-22 17:17:09 +00002324static PropertyDefinition
2325g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002326{
Greg Clayton67cc0632012-08-22 17:17:09 +00002327 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2328 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2329 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2330 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2331 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2332 { "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 "
2333 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2334 "some part (starting at the root) of the path to the file when it was built, "
2335 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2336 "Each element of the array is checked in order and the first one that results in a match wins." },
2337 { "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." },
Michael Sartaina7499c92013-07-01 19:45:50 +00002338 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating debug symbol files." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002339 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2340 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
Enrico Granatad325bf92013-06-04 22:54:16 +00002341 { "max-memory-read-size" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of bytes that 'memory read' will fetch before --force must be specified." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002342 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
Greg Clayton45392552012-10-17 22:57:12 +00002343 { "arg0" , OptionValue::eTypeString , false, 0 , NULL, NULL, "The first argument passed to the program in the argument array which can be different from the executable itself." },
2344 { "run-args" , OptionValue::eTypeArgs , false, 0 , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run. Note that this does NOT include the argv[0] which is in target.arg0." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002345 { "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." },
2346 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2347 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2348 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2349 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2350 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2351 { "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 +00002352 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2353 "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. "
2354 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2355 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2356 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2357 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2358 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002359 // FIXME: This is the wrong way to do per-architecture settings, but we don't have a general per architecture settings system in place yet.
2360 { "x86-disassembly-flavor" , OptionValue::eTypeEnum , false, eX86DisFlavorDefault, NULL, g_x86_dis_flavor_value_types, "The default disassembly flavor to use for x86 or x86-64 targets." },
Daniel Malead79ae052013-08-07 21:54:09 +00002361 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2362 { "hex-immediate-style" , OptionValue::eTypeEnum , false, Disassembler::eHexStyleC, NULL, g_hex_immediate_style_values, "Which style to use for printing hexadecimal disassembly values." },
Jim Ingham34951272013-04-04 01:38:54 +00002363 { "use-fast-stepping" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Use a fast stepping algorithm based on running from branch to branch rather than instruction single-stepping." },
Enrico Granata397ddd52013-05-21 20:13:34 +00002364 { "load-script-from-symbol-file" , OptionValue::eTypeEnum , false, eLoadScriptFromSymFileWarn, NULL, g_load_script_from_sym_file_values, "Allow LLDB to load scripting resources embedded in symbol files when available." },
Greg Clayton86eac942013-08-13 21:32:34 +00002365 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2366 "Loading modules from memory can be slow as reading the symbol tables and other data can take a long time depending on your connection to the debug target. "
2367 "This setting helps users control how much information gets loaded when loading modules from memory."
2368 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2369 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2370 "'minimal' is the fastest setting and will load section data with no symbols, but should rarely be used as stack frames in these memory regions will be inaccurate and not provide any context (fastest). " },
Greg Clayton67cc0632012-08-22 17:17:09 +00002371 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2372};
2373enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002374{
Greg Clayton67cc0632012-08-22 17:17:09 +00002375 ePropertyDefaultArch,
2376 ePropertyExprPrefix,
2377 ePropertyPreferDynamic,
2378 ePropertyEnableSynthetic,
2379 ePropertySkipPrologue,
2380 ePropertySourceMap,
2381 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002382 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002383 ePropertyMaxChildrenCount,
2384 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002385 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002386 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002387 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002388 ePropertyRunArgs,
2389 ePropertyEnvVars,
2390 ePropertyInheritEnv,
2391 ePropertyInputPath,
2392 ePropertyOutputPath,
2393 ePropertyErrorPath,
2394 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002395 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002396 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00002397 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00002398 ePropertyUseHexImmediates,
2399 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002400 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00002401 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfd814c52013-08-13 01:42:25 +00002402 ePropertyMemoryModuleLoadLevel
Greg Clayton67cc0632012-08-22 17:17:09 +00002403};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002404
Caroline Ticedaccaa92010-09-20 20:44:43 +00002405
Greg Clayton67cc0632012-08-22 17:17:09 +00002406class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002407{
Greg Clayton67cc0632012-08-22 17:17:09 +00002408public:
2409 TargetOptionValueProperties (const ConstString &name) :
2410 OptionValueProperties (name),
2411 m_target (NULL),
2412 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002413 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002414 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002415
Greg Clayton67cc0632012-08-22 17:17:09 +00002416 // This constructor is used when creating TargetOptionValueProperties when it
2417 // is part of a new lldb_private::Target instance. It will copy all current
2418 // global property values as needed
2419 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2420 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2421 m_target (target),
2422 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002423 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002424 }
2425
2426 virtual const Property *
2427 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2428 {
2429 // When gettings the value for a key from the target options, we will always
2430 // try and grab the setting from the current target if there is one. Else we just
2431 // use the one from this instance.
2432 if (idx == ePropertyEnvVars)
2433 GetHostEnvironmentIfNeeded ();
2434
2435 if (exe_ctx)
2436 {
2437 Target *target = exe_ctx->GetTargetPtr();
2438 if (target)
2439 {
2440 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2441 if (this != target_properties)
2442 return target_properties->ProtectedGetPropertyAtIndex (idx);
2443 }
2444 }
2445 return ProtectedGetPropertyAtIndex (idx);
2446 }
Enrico Granata84a53df2013-05-20 22:29:23 +00002447
2448 lldb::TargetSP
2449 GetTargetSP ()
2450 {
2451 return m_target->shared_from_this();
2452 }
2453
Greg Clayton67cc0632012-08-22 17:17:09 +00002454protected:
2455
2456 void
2457 GetHostEnvironmentIfNeeded () const
2458 {
2459 if (!m_got_host_env)
2460 {
2461 if (m_target)
2462 {
2463 m_got_host_env = true;
2464 const uint32_t idx = ePropertyInheritEnv;
2465 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2466 {
2467 PlatformSP platform_sp (m_target->GetPlatform());
2468 if (platform_sp)
2469 {
2470 StringList env;
2471 if (platform_sp->GetEnvironment(env))
2472 {
2473 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2474 if (env_dict)
2475 {
2476 const bool can_replace = false;
2477 const size_t envc = env.GetSize();
2478 for (size_t idx=0; idx<envc; idx++)
2479 {
2480 const char *env_entry = env.GetStringAtIndex (idx);
2481 if (env_entry)
2482 {
2483 const char *equal_pos = ::strchr(env_entry, '=');
2484 ConstString key;
2485 // It is ok to have environment variables with no values
2486 const char *value = NULL;
2487 if (equal_pos)
2488 {
2489 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2490 if (equal_pos[1])
2491 value = equal_pos + 1;
2492 }
2493 else
2494 {
2495 key.SetCString(env_entry);
2496 }
2497 // Don't allow existing keys to be replaced with ones we get from the platform environment
2498 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2499 }
2500 }
2501 }
2502 }
2503 }
2504 }
2505 }
2506 }
2507 }
2508 Target *m_target;
2509 mutable bool m_got_host_env;
2510};
2511
2512TargetProperties::TargetProperties (Target *target) :
2513 Properties ()
2514{
2515 if (target)
2516 {
2517 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Ticedaccaa92010-09-20 20:44:43 +00002518 }
2519 else
Greg Clayton67cc0632012-08-22 17:17:09 +00002520 {
2521 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2522 m_collection_sp->Initialize(g_properties);
2523 m_collection_sp->AppendProperty(ConstString("process"),
2524 ConstString("Settings specify to processes."),
2525 true,
2526 Process::GetGlobalProperties()->GetValueProperties());
2527 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002528}
2529
Greg Clayton67cc0632012-08-22 17:17:09 +00002530TargetProperties::~TargetProperties ()
2531{
2532}
2533ArchSpec
2534TargetProperties::GetDefaultArchitecture () const
2535{
2536 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2537 if (value)
2538 return value->GetCurrentValue();
2539 return ArchSpec();
2540}
2541
2542void
2543TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2544{
2545 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2546 if (value)
2547 return value->SetCurrentValue(arch, true);
2548}
2549
2550lldb::DynamicValueType
2551TargetProperties::GetPreferDynamicValue() const
2552{
2553 const uint32_t idx = ePropertyPreferDynamic;
2554 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2555}
2556
2557bool
2558TargetProperties::GetDisableASLR () const
2559{
2560 const uint32_t idx = ePropertyDisableASLR;
2561 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2562}
2563
2564void
2565TargetProperties::SetDisableASLR (bool b)
2566{
2567 const uint32_t idx = ePropertyDisableASLR;
2568 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2569}
2570
2571bool
2572TargetProperties::GetDisableSTDIO () const
2573{
2574 const uint32_t idx = ePropertyDisableSTDIO;
2575 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2576}
2577
2578void
2579TargetProperties::SetDisableSTDIO (bool b)
2580{
2581 const uint32_t idx = ePropertyDisableSTDIO;
2582 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2583}
2584
Jim Ingham0f063ba2013-03-02 00:26:47 +00002585const char *
2586TargetProperties::GetDisassemblyFlavor () const
2587{
2588 const uint32_t idx = ePropertyDisassemblyFlavor;
2589 const char *return_value;
2590
2591 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2592 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
2593 return return_value;
2594}
2595
Greg Clayton1f746072012-08-29 21:13:06 +00002596InlineStrategy
2597TargetProperties::GetInlineStrategy () const
2598{
2599 const uint32_t idx = ePropertyInlineStrategy;
2600 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2601}
2602
Greg Clayton45392552012-10-17 22:57:12 +00002603const char *
2604TargetProperties::GetArg0 () const
2605{
2606 const uint32_t idx = ePropertyArg0;
2607 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2608}
2609
2610void
2611TargetProperties::SetArg0 (const char *arg)
2612{
2613 const uint32_t idx = ePropertyArg0;
2614 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2615}
2616
Greg Clayton67cc0632012-08-22 17:17:09 +00002617bool
2618TargetProperties::GetRunArguments (Args &args) const
2619{
2620 const uint32_t idx = ePropertyRunArgs;
2621 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2622}
2623
2624void
2625TargetProperties::SetRunArguments (const Args &args)
2626{
2627 const uint32_t idx = ePropertyRunArgs;
2628 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2629}
2630
2631size_t
2632TargetProperties::GetEnvironmentAsArgs (Args &env) const
2633{
2634 const uint32_t idx = ePropertyEnvVars;
2635 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2636}
2637
2638bool
2639TargetProperties::GetSkipPrologue() const
2640{
2641 const uint32_t idx = ePropertySkipPrologue;
2642 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2643}
2644
2645PathMappingList &
2646TargetProperties::GetSourcePathMap () const
2647{
2648 const uint32_t idx = ePropertySourceMap;
2649 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2650 assert(option_value);
2651 return option_value->GetCurrentValue();
2652}
2653
2654FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00002655TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00002656{
2657 const uint32_t idx = ePropertyExecutableSearchPaths;
2658 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2659 assert(option_value);
2660 return option_value->GetCurrentValue();
2661}
2662
Michael Sartaina7499c92013-07-01 19:45:50 +00002663FileSpecList &
2664TargetProperties::GetDebugFileSearchPaths ()
2665{
2666 const uint32_t idx = ePropertyDebugFileSearchPaths;
2667 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2668 assert(option_value);
2669 return option_value->GetCurrentValue();
2670}
2671
Greg Clayton67cc0632012-08-22 17:17:09 +00002672bool
2673TargetProperties::GetEnableSyntheticValue () const
2674{
2675 const uint32_t idx = ePropertyEnableSynthetic;
2676 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2677}
2678
2679uint32_t
2680TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2681{
2682 const uint32_t idx = ePropertyMaxChildrenCount;
2683 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2684}
2685
2686uint32_t
2687TargetProperties::GetMaximumSizeOfStringSummary() const
2688{
2689 const uint32_t idx = ePropertyMaxSummaryLength;
2690 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2691}
2692
Enrico Granatad325bf92013-06-04 22:54:16 +00002693uint32_t
2694TargetProperties::GetMaximumMemReadSize () const
2695{
2696 const uint32_t idx = ePropertyMaxMemReadSize;
2697 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2698}
2699
Greg Clayton67cc0632012-08-22 17:17:09 +00002700FileSpec
2701TargetProperties::GetStandardInputPath () const
2702{
2703 const uint32_t idx = ePropertyInputPath;
2704 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2705}
2706
2707void
2708TargetProperties::SetStandardInputPath (const char *p)
2709{
2710 const uint32_t idx = ePropertyInputPath;
2711 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2712}
2713
2714FileSpec
2715TargetProperties::GetStandardOutputPath () const
2716{
2717 const uint32_t idx = ePropertyOutputPath;
2718 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2719}
2720
2721void
2722TargetProperties::SetStandardOutputPath (const char *p)
2723{
2724 const uint32_t idx = ePropertyOutputPath;
2725 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2726}
2727
2728FileSpec
2729TargetProperties::GetStandardErrorPath () const
2730{
2731 const uint32_t idx = ePropertyErrorPath;
2732 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2733}
2734
Greg Clayton6920b522012-08-22 18:39:03 +00002735const char *
2736TargetProperties::GetExpressionPrefixContentsAsCString ()
2737{
2738 const uint32_t idx = ePropertyExprPrefix;
2739 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2740 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00002741 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00002742 const bool null_terminate = true;
2743 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00002744 if (data_sp)
2745 return (const char *) data_sp->GetBytes();
2746 }
Greg Clayton6920b522012-08-22 18:39:03 +00002747 return NULL;
2748}
2749
Greg Clayton67cc0632012-08-22 17:17:09 +00002750void
2751TargetProperties::SetStandardErrorPath (const char *p)
2752{
2753 const uint32_t idx = ePropertyErrorPath;
2754 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2755}
2756
2757bool
2758TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2759{
2760 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2761 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2762}
2763
Jim Ingham17d023f2013-03-13 17:58:04 +00002764bool
Daniel Malead79ae052013-08-07 21:54:09 +00002765TargetProperties::GetUseHexImmediates () const
2766{
2767 const uint32_t idx = ePropertyUseHexImmediates;
2768 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2769}
2770
2771bool
Jim Ingham17d023f2013-03-13 17:58:04 +00002772TargetProperties::GetUseFastStepping () const
2773{
2774 const uint32_t idx = ePropertyUseFastStepping;
2775 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2776}
2777
Enrico Granata397ddd52013-05-21 20:13:34 +00002778LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002779TargetProperties::GetLoadScriptFromSymbolFile () const
2780{
2781 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00002782 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002783}
2784
Daniel Malead79ae052013-08-07 21:54:09 +00002785Disassembler::HexImmediateStyle
2786TargetProperties::GetHexImmediateStyle () const
2787{
2788 const uint32_t idx = ePropertyHexImmediateStyle;
2789 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
2790}
2791
Greg Claytonfd814c52013-08-13 01:42:25 +00002792MemoryModuleLoadLevel
2793TargetProperties::GetMemoryModuleLoadLevel() const
2794{
2795 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
2796 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
2797}
2798
2799
Greg Clayton67cc0632012-08-22 17:17:09 +00002800const TargetPropertiesSP &
2801Target::GetGlobalProperties()
2802{
2803 static TargetPropertiesSP g_settings_sp;
2804 if (!g_settings_sp)
2805 {
2806 g_settings_sp.reset (new TargetProperties (NULL));
2807 }
2808 return g_settings_sp;
2809}
2810
Jim Ingham4bddaeb2012-02-16 06:50:00 +00002811const ConstString &
2812Target::TargetEventData::GetFlavorString ()
2813{
2814 static ConstString g_flavor ("Target::TargetEventData");
2815 return g_flavor;
2816}
2817
2818const ConstString &
2819Target::TargetEventData::GetFlavor () const
2820{
2821 return TargetEventData::GetFlavorString ();
2822}
2823
2824Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2825 EventData(),
2826 m_target_sp (new_target_sp)
2827{
2828}
2829
2830Target::TargetEventData::~TargetEventData()
2831{
2832
2833}
2834
2835void
2836Target::TargetEventData::Dump (Stream *s) const
2837{
2838
2839}
2840
2841const TargetSP
2842Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2843{
2844 TargetSP target_sp;
2845
2846 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2847 if (data)
2848 target_sp = data->m_target_sp;
2849
2850 return target_sp;
2851}
2852
2853const Target::TargetEventData *
2854Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2855{
2856 if (event_ptr)
2857 {
2858 const EventData *event_data = event_ptr->GetData();
2859 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2860 return static_cast <const TargetEventData *> (event_ptr->GetData());
2861 }
2862 return NULL;
2863}
2864