blob: fd9626a5de8dffb809e8bb8e2d368dd191bfe259 [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"
Jason Molendab57e4a12013-11-04 09:33:30 +000045#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000046#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000047#include "lldb/Target/Thread.h"
48#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
50using namespace lldb;
51using namespace lldb_private;
52
Jim Ingham4bddaeb2012-02-16 06:50:00 +000053ConstString &
54Target::GetStaticBroadcasterClass ()
55{
56 static ConstString class_name ("lldb.target");
57 return class_name;
58}
59
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060//----------------------------------------------------------------------
61// Target constructor
62//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:51 +000063Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +000064 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000065 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000066 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000067 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000068 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000069 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000070 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000071 m_images (this),
Greg Claytonf5e56de2010-09-14 23:36:40 +000072 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073 m_breakpoint_list (false),
74 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000075 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000076 m_process_sp (),
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +000077 m_valid (true),
Greg Clayton32e0a752011-03-30 18:16:51 +000078 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000080 m_scratch_ast_context_ap (),
81 m_scratch_ast_source_ap (),
82 m_ast_importer_ap (),
Jim Ingham9575d842011-03-11 03:53:59 +000083 m_persistent_variables (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000084 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000085 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000086 m_stop_hook_next_id (0),
Enrico Granata5d5f60c2013-09-24 22:58:37 +000087 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000089 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
90 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
91 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000092 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +000093 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Jim Ingham4bddaeb2012-02-16 06:50:00 +000094
95 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +000096
Greg Clayton5160ce52013-03-27 23:08:40 +000097 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 if (log)
99 log->Printf ("%p Target::Target()", this);
Jason Molendae1b68ad2012-12-05 00:25:49 +0000100 if (m_arch.IsValid())
101 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000102 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::Target created with architecture %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jason Molendae1b68ad2012-12-05 00:25:49 +0000103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104}
105
106//----------------------------------------------------------------------
107// Destructor
108//----------------------------------------------------------------------
109Target::~Target()
110{
Greg Clayton5160ce52013-03-27 23:08:40 +0000111 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 if (log)
113 log->Printf ("%p Target::~Target()", this);
114 DeleteCurrentProcess ();
115}
116
117void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000118Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119{
Greg Clayton89411422010-10-08 00:21:05 +0000120// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000121 if (description_level != lldb::eDescriptionLevelBrief)
122 {
123 s->Indent();
124 s->PutCString("Target\n");
125 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000126 m_images.Dump(s);
127 m_breakpoint_list.Dump(s);
128 m_internal_breakpoint_list.Dump(s);
129 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000130 }
131 else
132 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000133 Module *exe_module = GetExecutableModulePointer();
134 if (exe_module)
135 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000136 else
137 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000138 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
141void
Greg Clayton90ba8112012-12-05 00:16:59 +0000142Target::CleanupProcess ()
143{
144 // Do any cleanup of the target we need to do between process instances.
145 // NB It is better to do this before destroying the process in case the
146 // clean up needs some help from the process.
147 m_breakpoint_list.ClearAllBreakpointSites();
148 m_internal_breakpoint_list.ClearAllBreakpointSites();
149 // Disable watchpoints just on the debugger side.
150 Mutex::Locker locker;
151 this->GetWatchpointList().GetListMutex(locker);
152 DisableAllWatchpoints(false);
153 ClearAllWatchpointHitCounts();
154}
155
156void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157Target::DeleteCurrentProcess ()
158{
159 if (m_process_sp.get())
160 {
Greg Clayton17f69202010-09-14 23:52:43 +0000161 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 if (m_process_sp->IsAlive())
163 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000164
165 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166
Greg Clayton90ba8112012-12-05 00:16:59 +0000167 CleanupProcess ();
168
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 m_process_sp.reset();
170 }
171}
172
173const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000174Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175{
176 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000177 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 return m_process_sp;
179}
180
181const lldb::ProcessSP &
182Target::GetProcessSP () const
183{
184 return m_process_sp;
185}
186
Greg Clayton3418c852011-08-10 02:10:13 +0000187void
188Target::Destroy()
189{
190 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000191 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000192 DeleteCurrentProcess ();
193 m_platform_sp.reset();
194 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000195 ClearModules(true);
Greg Clayton3418c852011-08-10 02:10:13 +0000196 m_section_load_list.Clear();
197 const bool notify = false;
198 m_breakpoint_list.RemoveAll(notify);
199 m_internal_breakpoint_list.RemoveAll(notify);
200 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000201 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000202 m_search_filter_sp.reset();
203 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000204 m_persistent_variables.Clear();
205 m_stop_hooks.clear();
206 m_stop_hook_next_id = 0;
207 m_suppress_stop_hooks = false;
208}
209
210
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211BreakpointList &
212Target::GetBreakpointList(bool internal)
213{
214 if (internal)
215 return m_internal_breakpoint_list;
216 else
217 return m_breakpoint_list;
218}
219
220const BreakpointList &
221Target::GetBreakpointList(bool internal) const
222{
223 if (internal)
224 return m_internal_breakpoint_list;
225 else
226 return m_breakpoint_list;
227}
228
229BreakpointSP
230Target::GetBreakpointByID (break_id_t break_id)
231{
232 BreakpointSP bp_sp;
233
234 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
235 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
236 else
237 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
238
239 return bp_sp;
240}
241
242BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000243Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000244 const FileSpecList *source_file_spec_list,
245 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000246 bool internal,
247 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248{
Jim Ingham87df91b2011-09-23 00:54:11 +0000249 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
250 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Greg Claytoneb023e72013-10-11 19:48:25 +0000251 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +0000252}
253
254
255BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000256Target::CreateBreakpoint (const FileSpecList *containingModules,
257 const FileSpec &file,
258 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000259 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000260 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000261 bool internal,
262 bool hardware)
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));
Greg Claytoneb023e72013-10-11 19:48:25 +0000305 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306}
307
308
309BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000310Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
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 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000323 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 return bp_sp;
325}
326
327BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000328Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329{
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));
Greg Claytoneb023e72013-10-11 19:48:25 +0000332 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333}
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,
Greg Claytoneb023e72013-10-11 19:48:25 +0000341 bool internal,
342 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000344 BreakpointSP bp_sp;
345 if (func_name)
346 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000347 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000348
349 if (skip_prologue == eLazyBoolCalculate)
350 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
351
Greg Claytond16e1e52011-07-12 17:06:17 +0000352 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
353 func_name,
354 func_name_type_mask,
355 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000356 skip_prologue));
Greg Claytoneb023e72013-10-11 19:48:25 +0000357 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000358 }
359 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360}
361
Jim Inghamfab10e82012-03-06 00:37:27 +0000362lldb::BreakpointSP
363Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000364 const FileSpecList *containingSourceFiles,
365 const std::vector<std::string> &func_names,
366 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000367 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000368 bool internal,
369 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000370{
371 BreakpointSP bp_sp;
372 size_t num_names = func_names.size();
373 if (num_names > 0)
374 {
375 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000376
377 if (skip_prologue == eLazyBoolCalculate)
378 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
379
380 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000381 func_names,
382 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000383 skip_prologue));
Greg Claytoneb023e72013-10-11 19:48:25 +0000384 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +0000385 }
386 return bp_sp;
387}
388
Jim Ingham133e0fb2012-03-03 02:05:11 +0000389BreakpointSP
390Target::CreateBreakpoint (const FileSpecList *containingModules,
391 const FileSpecList *containingSourceFiles,
392 const char *func_names[],
393 size_t num_names,
394 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000395 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000396 bool internal,
397 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000398{
399 BreakpointSP bp_sp;
400 if (num_names > 0)
401 {
402 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
403
Greg Clayton03da4cc2013-04-19 21:31:16 +0000404 if (skip_prologue == eLazyBoolCalculate)
405 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
406
407 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000408 func_names,
409 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000410 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000411 skip_prologue));
Greg Claytoneb023e72013-10-11 19:48:25 +0000412 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000413 }
414 return bp_sp;
415}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416
417SearchFilterSP
418Target::GetSearchFilterForModule (const FileSpec *containingModule)
419{
420 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 if (containingModule != NULL)
422 {
423 // TODO: We should look into sharing module based search filters
424 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000425 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 }
427 else
428 {
429 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000430 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 filter_sp = m_search_filter_sp;
432 }
433 return filter_sp;
434}
435
Jim Ingham969795f2011-09-21 01:17:13 +0000436SearchFilterSP
437Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
438{
439 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000440 if (containingModules && containingModules->GetSize() != 0)
441 {
442 // TODO: We should look into sharing module based search filters
443 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000444 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000445 }
446 else
447 {
448 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000449 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000450 filter_sp = m_search_filter_sp;
451 }
452 return filter_sp;
453}
454
Jim Ingham87df91b2011-09-23 00:54:11 +0000455SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000456Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
457 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000458{
459 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
460 return GetSearchFilterForModuleList(containingModules);
461
462 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000463 if (containingModules == NULL)
464 {
465 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
466 // but that will take a little reworking.
467
Greg Claytone1cd1be2012-01-29 20:56:30 +0000468 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000469 }
470 else
471 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000472 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000473 }
474 return filter_sp;
475}
476
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000478Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000479 const FileSpecList *containingSourceFiles,
480 RegularExpression &func_regex,
481 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000482 bool internal,
483 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484{
Jim Ingham87df91b2011-09-23 00:54:11 +0000485 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17 +0000486 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
487 func_regex,
488 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489
Greg Claytoneb023e72013-10-11 19:48:25 +0000490 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491}
492
Jim Ingham219ba192012-03-05 04:47:34 +0000493lldb::BreakpointSP
494Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
495{
496 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
497}
498
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000500Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501{
502 BreakpointSP bp_sp;
503 if (filter_sp && resolver_sp)
504 {
Greg Claytoneb023e72013-10-11 19:48:25 +0000505 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 resolver_sp->SetBreakpoint (bp_sp.get());
507
508 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000509 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510 else
Greg Clayton9fed0d82010-07-23 23:33:17 +0000511 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512
Greg Clayton5160ce52013-03-27 23:08:40 +0000513 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 if (log)
515 {
516 StreamString s;
517 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
518 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
519 }
520
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 bp_sp->ResolveBreakpoint();
522 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000523
524 if (!internal && bp_sp)
525 {
526 m_last_created_breakpoint = bp_sp;
527 }
528
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529 return bp_sp;
530}
531
Johnny Chen86364b42011-09-20 23:28:55 +0000532bool
533Target::ProcessIsValid()
534{
535 return (m_process_sp && m_process_sp->IsAlive());
536}
537
Johnny Chenb90827e2012-06-04 23:19:54 +0000538static bool
539CheckIfWatchpointsExhausted(Target *target, Error &error)
540{
541 uint32_t num_supported_hardware_watchpoints;
542 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
543 if (rc.Success())
544 {
545 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
546 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
547 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
548 num_supported_hardware_watchpoints);
549 }
550 return false;
551}
552
Johnny Chen01a67862011-10-14 00:42:25 +0000553// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000554// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000555WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000556Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000557{
Greg Clayton5160ce52013-03-27 23:08:40 +0000558 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000559 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000560 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000561 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000562
Johnny Chen01a67862011-10-14 00:42:25 +0000563 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000564 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000565 {
566 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000567 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000568 }
Jim Inghamc6462312013-06-18 21:52:48 +0000569
Johnny Chen45e541f2011-09-14 22:20:15 +0000570 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000571 {
572 if (size == 0)
573 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
574 else
Daniel Malead01b2952012-11-29 21:49:15 +0000575 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000576 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000577 }
Jim Inghamc6462312013-06-18 21:52:48 +0000578
579 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
580 {
581 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
582 }
Johnny Chen7313a642011-09-13 01:15:36 +0000583
Johnny Chen01a67862011-10-14 00:42:25 +0000584 // Currently we only support one watchpoint per address, with total number
585 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000586
587 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000588 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 +0000589 Mutex::Locker locker;
590 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000591 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000592 if (matched_sp)
593 {
Johnny Chen0c406372011-09-14 20:23:45 +0000594 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000595 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000596 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
597 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000598 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000599 if (size == old_size && kind == old_type)
600 {
Johnny Chen01a67862011-10-14 00:42:25 +0000601 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000602 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000603 }
604 else
605 {
Johnny Chen01a67862011-10-14 00:42:25 +0000606 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000607 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
608 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000609 }
Johnny Chen3c532582011-09-13 23:29:31 +0000610 }
611
Jason Molenda727e3922012-12-05 23:07:34 +0000612 if (!wp_sp)
613 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000614 wp_sp.reset(new Watchpoint(*this, addr, size, type));
615 wp_sp->SetWatchpointType(kind, notify);
616 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000617 }
Johnny Chen0c406372011-09-14 20:23:45 +0000618
Jim Ingham1b5792e2012-12-18 02:03:49 +0000619 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000620 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000621 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
622 __FUNCTION__,
623 error.Success() ? "succeeded" : "failed",
624 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000625
Jason Molenda727e3922012-12-05 23:07:34 +0000626 if (error.Fail())
627 {
Johnny Chen41b77262012-03-26 22:00:10 +0000628 // Enabling the watchpoint on the device side failed.
629 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000630 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000631 // See if we could provide more helpful error message.
632 if (!CheckIfWatchpointsExhausted(this, error))
633 {
634 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Sylvestre Ledru779f9212013-10-31 23:55:19 +0000635 error.SetErrorStringWithFormat("watch size of %zu is not supported", size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000636 }
Johnny Chen01a67862011-10-14 00:42:25 +0000637 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000638 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000639 else
Johnny Chen01a67862011-10-14 00:42:25 +0000640 m_last_created_watchpoint = wp_sp;
641 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000642}
643
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644void
645Target::RemoveAllBreakpoints (bool internal_also)
646{
Greg Clayton5160ce52013-03-27 23:08:40 +0000647 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 if (log)
649 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
650
Greg Clayton9fed0d82010-07-23 23:33:17 +0000651 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000653 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000654
655 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656}
657
658void
659Target::DisableAllBreakpoints (bool internal_also)
660{
Greg Clayton5160ce52013-03-27 23:08:40 +0000661 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 if (log)
663 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
664
665 m_breakpoint_list.SetEnabledAll (false);
666 if (internal_also)
667 m_internal_breakpoint_list.SetEnabledAll (false);
668}
669
670void
671Target::EnableAllBreakpoints (bool internal_also)
672{
Greg Clayton5160ce52013-03-27 23:08:40 +0000673 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 if (log)
675 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
676
677 m_breakpoint_list.SetEnabledAll (true);
678 if (internal_also)
679 m_internal_breakpoint_list.SetEnabledAll (true);
680}
681
682bool
683Target::RemoveBreakpointByID (break_id_t break_id)
684{
Greg Clayton5160ce52013-03-27 23:08:40 +0000685 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686 if (log)
687 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
688
689 if (DisableBreakpointByID (break_id))
690 {
691 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000692 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000694 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000695 if (m_last_created_breakpoint)
696 {
697 if (m_last_created_breakpoint->GetID() == break_id)
698 m_last_created_breakpoint.reset();
699 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000700 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000701 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 return true;
703 }
704 return false;
705}
706
707bool
708Target::DisableBreakpointByID (break_id_t break_id)
709{
Greg Clayton5160ce52013-03-27 23:08:40 +0000710 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 if (log)
712 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
713
714 BreakpointSP bp_sp;
715
716 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
717 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
718 else
719 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
720 if (bp_sp)
721 {
722 bp_sp->SetEnabled (false);
723 return true;
724 }
725 return false;
726}
727
728bool
729Target::EnableBreakpointByID (break_id_t break_id)
730{
Greg Clayton5160ce52013-03-27 23:08:40 +0000731 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 if (log)
733 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
734 __FUNCTION__,
735 break_id,
736 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
737
738 BreakpointSP bp_sp;
739
740 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
741 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
742 else
743 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
744
745 if (bp_sp)
746 {
747 bp_sp->SetEnabled (true);
748 return true;
749 }
750 return false;
751}
752
Johnny Chenedf50372011-09-23 21:21:43 +0000753// The flag 'end_to_end', default to true, signifies that the operation is
754// performed end to end, for both the debugger and the debuggee.
755
Johnny Chen01a67862011-10-14 00:42:25 +0000756// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
757// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000758bool
Johnny Chen01a67862011-10-14 00:42:25 +0000759Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000760{
Greg Clayton5160ce52013-03-27 23:08:40 +0000761 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000762 if (log)
763 log->Printf ("Target::%s\n", __FUNCTION__);
764
Johnny Chenedf50372011-09-23 21:21:43 +0000765 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000766 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000767 return true;
768 }
769
770 // Otherwise, it's an end to end operation.
771
Johnny Chen86364b42011-09-20 23:28:55 +0000772 if (!ProcessIsValid())
773 return false;
774
Johnny Chen01a67862011-10-14 00:42:25 +0000775 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000776 for (size_t i = 0; i < num_watchpoints; ++i)
777 {
Johnny Chen01a67862011-10-14 00:42:25 +0000778 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
779 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000780 return false;
781
Johnny Chen01a67862011-10-14 00:42:25 +0000782 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000783 if (rc.Fail())
784 return false;
785 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000786 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000787 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000788 return true; // Success!
789}
790
Johnny Chen01a67862011-10-14 00:42:25 +0000791// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
792// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000793bool
Johnny Chen01a67862011-10-14 00:42:25 +0000794Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000795{
Greg Clayton5160ce52013-03-27 23:08:40 +0000796 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000797 if (log)
798 log->Printf ("Target::%s\n", __FUNCTION__);
799
Johnny Chenedf50372011-09-23 21:21:43 +0000800 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000801 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000802 return true;
803 }
804
805 // Otherwise, it's an end to end operation.
806
Johnny Chen86364b42011-09-20 23:28:55 +0000807 if (!ProcessIsValid())
808 return false;
809
Johnny Chen01a67862011-10-14 00:42:25 +0000810 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000811 for (size_t i = 0; i < num_watchpoints; ++i)
812 {
Johnny Chen01a67862011-10-14 00:42:25 +0000813 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
814 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000815 return false;
816
Johnny Chen01a67862011-10-14 00:42:25 +0000817 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000818 if (rc.Fail())
819 return false;
820 }
Johnny Chen86364b42011-09-20 23:28:55 +0000821 return true; // Success!
822}
823
Johnny Chen01a67862011-10-14 00:42:25 +0000824// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
825// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000826bool
Johnny Chen01a67862011-10-14 00:42:25 +0000827Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000828{
Greg Clayton5160ce52013-03-27 23:08:40 +0000829 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000830 if (log)
831 log->Printf ("Target::%s\n", __FUNCTION__);
832
Johnny Chenedf50372011-09-23 21:21:43 +0000833 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000834 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000835 return true;
836 }
837
838 // Otherwise, it's an end to end operation.
839
Johnny Chen86364b42011-09-20 23:28:55 +0000840 if (!ProcessIsValid())
841 return false;
842
Johnny Chen01a67862011-10-14 00:42:25 +0000843 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000844 for (size_t i = 0; i < num_watchpoints; ++i)
845 {
Johnny Chen01a67862011-10-14 00:42:25 +0000846 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
847 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000848 return false;
849
Johnny Chen01a67862011-10-14 00:42:25 +0000850 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000851 if (rc.Fail())
852 return false;
853 }
Johnny Chen86364b42011-09-20 23:28:55 +0000854 return true; // Success!
855}
856
Johnny Chena4d6bc92012-02-25 06:44:30 +0000857// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
858bool
859Target::ClearAllWatchpointHitCounts ()
860{
Greg Clayton5160ce52013-03-27 23:08:40 +0000861 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000862 if (log)
863 log->Printf ("Target::%s\n", __FUNCTION__);
864
865 size_t num_watchpoints = m_watchpoint_list.GetSize();
866 for (size_t i = 0; i < num_watchpoints; ++i)
867 {
868 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
869 if (!wp_sp)
870 return false;
871
872 wp_sp->ResetHitCount();
873 }
874 return true; // Success!
875}
876
Johnny Chen01a67862011-10-14 00:42:25 +0000877// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000878// during these operations.
879bool
Johnny Chen01a67862011-10-14 00:42:25 +0000880Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000881{
Greg Clayton5160ce52013-03-27 23:08:40 +0000882 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000883 if (log)
884 log->Printf ("Target::%s\n", __FUNCTION__);
885
886 if (!ProcessIsValid())
887 return false;
888
Johnny Chen01a67862011-10-14 00:42:25 +0000889 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000890 for (size_t i = 0; i < num_watchpoints; ++i)
891 {
Johnny Chen01a67862011-10-14 00:42:25 +0000892 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
893 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000894 return false;
895
Johnny Chen01a67862011-10-14 00:42:25 +0000896 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000897 }
898 return true; // Success!
899}
900
Johnny Chen01a67862011-10-14 00:42:25 +0000901// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000902bool
Johnny Chen01a67862011-10-14 00:42:25 +0000903Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000904{
Greg Clayton5160ce52013-03-27 23:08:40 +0000905 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000906 if (log)
907 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
908
909 if (!ProcessIsValid())
910 return false;
911
Johnny Chen01a67862011-10-14 00:42:25 +0000912 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
913 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000914 {
Johnny Chen01a67862011-10-14 00:42:25 +0000915 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000916 if (rc.Success())
917 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000918
Johnny Chenf04ee932011-09-22 18:04:58 +0000919 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000920 }
921 return false;
922}
923
Johnny Chen01a67862011-10-14 00:42:25 +0000924// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000925bool
Johnny Chen01a67862011-10-14 00:42:25 +0000926Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000927{
Greg Clayton5160ce52013-03-27 23:08:40 +0000928 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000929 if (log)
930 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
931
932 if (!ProcessIsValid())
933 return false;
934
Johnny Chen01a67862011-10-14 00:42:25 +0000935 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
936 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000937 {
Johnny Chen01a67862011-10-14 00:42:25 +0000938 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000939 if (rc.Success())
940 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000941
Johnny Chenf04ee932011-09-22 18:04:58 +0000942 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000943 }
944 return false;
945}
946
Johnny Chen01a67862011-10-14 00:42:25 +0000947// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000948bool
Johnny Chen01a67862011-10-14 00:42:25 +0000949Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000950{
Greg Clayton5160ce52013-03-27 23:08:40 +0000951 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000952 if (log)
953 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
954
Jim Inghamb0b45132013-07-02 02:09:46 +0000955 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
956 if (watch_to_remove_sp == m_last_created_watchpoint)
957 m_last_created_watchpoint.reset();
958
Johnny Chen01a67862011-10-14 00:42:25 +0000959 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +0000960 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000961 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +0000962 return true;
963 }
964 return false;
965}
966
Johnny Chen01a67862011-10-14 00:42:25 +0000967// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +0000968bool
Johnny Chen01a67862011-10-14 00:42:25 +0000969Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000970{
Greg Clayton5160ce52013-03-27 23:08:40 +0000971 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000972 if (log)
973 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
974
975 if (!ProcessIsValid())
976 return false;
977
Johnny Chen01a67862011-10-14 00:42:25 +0000978 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
979 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000980 {
Johnny Chen01a67862011-10-14 00:42:25 +0000981 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000982 return true;
983 }
984 return false;
985}
986
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987ModuleSP
988Target::GetExecutableModule ()
989{
Greg Claytonaa149cb2011-08-11 02:48:45 +0000990 return m_images.GetModuleAtIndex(0);
991}
992
993Module*
994Target::GetExecutableModulePointer ()
995{
996 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997}
998
Enrico Granata17598482012-11-08 02:22:02 +0000999static void
1000LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1001{
1002 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001003 StreamString feedback_stream;
1004 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001005 {
Enrico Granata97303392013-05-21 00:00:30 +00001006 if (error.AsCString())
1007 target->GetDebugger().GetErrorStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
1008 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1009 error.AsCString());
1010 if (feedback_stream.GetSize())
1011 target->GetDebugger().GetOutputStream().Printf("%s\n",
1012 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001013 }
1014}
1015
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016void
Greg Claytonb35db632013-11-09 00:03:31 +00001017Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018{
Greg Claytonb35db632013-11-09 00:03:31 +00001019 ModulesDidUnload (m_images, delete_locations);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001020 GetSectionLoadList().Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021 m_images.Clear();
1022 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001023 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001024 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001025}
1026
1027void
Greg Claytonb35db632013-11-09 00:03:31 +00001028Target::DidExec ()
1029{
1030 // When a process exec's we need to know about it so we can do some cleanup.
1031 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1032 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1033}
1034
1035void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001036Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1037{
1038 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001039 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040
1041 if (executable_sp.get())
1042 {
1043 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001044 "Target::SetExecutableModule (executable = '%s')",
1045 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046
1047 m_images.Append(executable_sp); // The first image is our exectuable file
1048
Jim Ingham5aee1622010-08-09 23:31:02 +00001049 // 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 +00001050 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001051 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001052 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001053 if (log)
1054 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1055 }
1056
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001058 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001060 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061 {
1062 executable_objfile->GetDependentModules(dependent_files);
1063 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1064 {
Greg Claytonded470d2011-03-19 01:12:21 +00001065 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1066 FileSpec platform_dependent_file_spec;
1067 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55 +00001068 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001069 else
1070 platform_dependent_file_spec = dependent_file_spec;
1071
Greg Claytonb9a01b32012-02-26 05:51:37 +00001072 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1073 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074 if (image_module_sp.get())
1075 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 ObjectFile *objfile = image_module_sp->GetObjectFile();
1077 if (objfile)
1078 objfile->GetDependentModules(dependent_files);
1079 }
1080 }
1081 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082 }
1083}
1084
1085
Jim Ingham5aee1622010-08-09 23:31:02 +00001086bool
1087Target::SetArchitecture (const ArchSpec &arch_spec)
1088{
Greg Clayton5160ce52013-03-27 23:08:40 +00001089 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001090 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001091 {
Greg Clayton70512312012-05-08 01:45:38 +00001092 // If we haven't got a valid arch spec, or the architectures are
1093 // compatible, so just update the architecture. Architectures can be
1094 // equal, yet the triple OS and vendor might change, so we need to do
1095 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001096 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001097 if (log)
1098 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 +00001099 return true;
1100 }
1101 else
1102 {
1103 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001104 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001105 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 +00001106 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001107 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001108
Greg Claytonb35db632013-11-09 00:03:31 +00001109 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001110 // Need to do something about unsetting breakpoints.
1111
1112 if (executable_sp)
1113 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001114 if (log)
1115 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 +00001116 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1117 Error error = ModuleList::GetSharedModule (module_spec,
1118 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001119 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001120 NULL,
1121 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001122
1123 if (!error.Fail() && executable_sp)
1124 {
1125 SetExecutableModule (executable_sp, true);
1126 return true;
1127 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001128 }
1129 }
Greg Clayton70512312012-05-08 01:45:38 +00001130 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001131}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133void
Enrico Granataefe637d2012-11-08 19:16:03 +00001134Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001135{
1136}
1137
1138void
Enrico Granataefe637d2012-11-08 19:16:03 +00001139Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140{
1141 // A module is being added to this target for the first time
Enrico Granataefe637d2012-11-08 19:16:03 +00001142 ModuleList my_module_list;
1143 my_module_list.Append(module_sp);
Enrico Granata17598482012-11-08 02:22:02 +00001144 LoadScriptingResourceForModule(module_sp, this);
Enrico Granataefe637d2012-11-08 19:16:03 +00001145 ModulesDidLoad (my_module_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146}
1147
1148void
Enrico Granataefe637d2012-11-08 19:16:03 +00001149Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001150{
1151 // A module is being added to this target for the first time
Enrico Granataefe637d2012-11-08 19:16:03 +00001152 ModuleList my_module_list;
1153 my_module_list.Append(module_sp);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001154 ModulesDidUnload (my_module_list, false);
Enrico Granata17598482012-11-08 02:22:02 +00001155}
1156
1157void
Enrico Granataefe637d2012-11-08 19:16:03 +00001158Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159{
Jim Inghame716ae02011-08-03 01:00:06 +00001160 // A module is replacing an already added module
Jim Ingham4a94c912012-05-17 18:38:42 +00001161 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162}
1163
1164void
1165Target::ModulesDidLoad (ModuleList &module_list)
1166{
Enrico Granata17598482012-11-08 02:22:02 +00001167 if (module_list.GetSize())
1168 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001169 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001170 if (m_process_sp)
1171 {
1172 SystemRuntime *sys_runtime = m_process_sp->GetSystemRuntime();
1173 if (sys_runtime)
1174 {
1175 sys_runtime->ModulesDidLoad (module_list);
1176 }
1177 }
Enrico Granata17598482012-11-08 02:22:02 +00001178 // TODO: make event data that packages up the module_list
1179 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1180 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181}
1182
1183void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001184Target::SymbolsDidLoad (ModuleList &module_list)
1185{
Jim Ingham31caf982013-06-04 23:01:35 +00001186 if (module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001187 {
Jim Ingham31caf982013-06-04 23:01:35 +00001188 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001189 {
Jim Ingham31caf982013-06-04 23:01:35 +00001190 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1191 if (runtime)
1192 {
1193 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1194 objc_runtime->SymbolsDidLoad(module_list);
1195 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001196 }
Jim Ingham31caf982013-06-04 23:01:35 +00001197
Greg Clayton095eeaa2013-11-05 23:28:00 +00001198 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jim Ingham31caf982013-06-04 23:01:35 +00001199 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001200 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001201}
1202
1203void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001204Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205{
Enrico Granata17598482012-11-08 02:22:02 +00001206 if (module_list.GetSize())
1207 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001208 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Enrico Granata17598482012-11-08 02:22:02 +00001209 // TODO: make event data that packages up the module_list
1210 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1211 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212}
1213
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001214bool
Greg Claytonb9a01b32012-02-26 05:51:37 +00001215Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001216{
Greg Clayton67cc0632012-08-22 17:17:09 +00001217 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001218 {
1219 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001220 ModuleSpec module_spec (module_file_spec);
1221 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001222
1223 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1224 // black list.
1225 if (num_modules > 0)
1226 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001227 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001228 {
1229 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1230 return false;
1231 }
1232 return true;
1233 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001234 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001235 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001236}
1237
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001238bool
Jim Inghamc6674fd2011-10-28 23:14:11 +00001239Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1240{
Greg Clayton67cc0632012-08-22 17:17:09 +00001241 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001242 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001243 if (m_platform_sp)
1244 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001245 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001246 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001247}
1248
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249size_t
Greg Claytondb598232011-01-07 01:57:07 +00001250Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1251{
Greg Claytone72dfb32012-02-24 01:59:29 +00001252 SectionSP section_sp (addr.GetSection());
1253 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001254 {
Jason Molenda216d91f2012-04-25 00:06:56 +00001255 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1256 if (section_sp->IsEncrypted())
1257 {
Greg Clayton57f06302012-05-25 17:05:55 +00001258 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001259 return 0;
1260 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001261 ModuleSP module_sp (section_sp->GetModule());
1262 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001263 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001264 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1265 if (objfile)
1266 {
1267 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1268 addr.GetOffset(),
1269 dst,
1270 dst_len);
1271 if (bytes_read > 0)
1272 return bytes_read;
1273 else
1274 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1275 }
Greg Claytondb598232011-01-07 01:57:07 +00001276 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001277 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001278 }
1279 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001280 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001281 }
1282 else
Greg Claytondb598232011-01-07 01:57:07 +00001283 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001284
Greg Claytondb598232011-01-07 01:57:07 +00001285 return 0;
1286}
1287
1288size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001289Target::ReadMemory (const Address& addr,
1290 bool prefer_file_cache,
1291 void *dst,
1292 size_t dst_len,
1293 Error &error,
1294 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001297
Enrico Granata9128ee22011-09-06 19:20:51 +00001298 // if we end up reading this from process memory, we will fill this
1299 // with the actual load address
1300 if (load_addr_ptr)
1301 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1302
Greg Claytondb598232011-01-07 01:57:07 +00001303 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001304
1305 addr_t load_addr = LLDB_INVALID_ADDRESS;
1306 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001307 Address resolved_addr;
1308 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001309 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001310 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001311 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001312 // No sections are loaded, so we must assume we are not running
1313 // yet and anything we are given is a file address.
1314 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1315 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001316 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001317 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001318 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001319 // We have at least one section loaded. This can be becuase
1320 // we have manually loaded some sections with "target modules load ..."
1321 // or because we have have a live process that has sections loaded
1322 // through the dynamic loader
1323 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1324 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001325 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001326 }
Greg Clayton357132e2011-03-26 19:14:58 +00001327 if (!resolved_addr.IsValid())
1328 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001329
Greg Claytonc749eb82011-07-11 05:12:02 +00001330
Greg Claytondb598232011-01-07 01:57:07 +00001331 if (prefer_file_cache)
1332 {
1333 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1334 if (bytes_read > 0)
1335 return bytes_read;
1336 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001337
Johnny Chen86364b42011-09-20 23:28:55 +00001338 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001339 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001340 if (load_addr == LLDB_INVALID_ADDRESS)
1341 load_addr = resolved_addr.GetLoadAddress (this);
1342
Greg Claytondda4f7b2010-06-30 23:03:03 +00001343 if (load_addr == LLDB_INVALID_ADDRESS)
1344 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001345 ModuleSP addr_module_sp (resolved_addr.GetModule());
1346 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001347 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Claytone72dfb32012-02-24 01:59:29 +00001348 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08 +00001349 resolved_addr.GetFileAddress(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001350 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001351 else
Daniel Malead01b2952012-11-29 21:49:15 +00001352 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001353 }
1354 else
1355 {
Greg Claytondb598232011-01-07 01:57:07 +00001356 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357 if (bytes_read != dst_len)
1358 {
1359 if (error.Success())
1360 {
1361 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001362 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363 else
Daniel Malead01b2952012-11-29 21:49:15 +00001364 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 +00001365 }
1366 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001367 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001368 {
1369 if (load_addr_ptr)
1370 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001371 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001372 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001373 // If the address is not section offset we have an address that
1374 // doesn't resolve to any address in any currently loaded shared
1375 // libaries and we failed to read memory so there isn't anything
1376 // more we can do. If it is section offset, we might be able to
1377 // read cached memory from the object file.
1378 if (!resolved_addr.IsSectionOffset())
1379 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001381 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001382
Greg Claytonc749eb82011-07-11 05:12:02 +00001383 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001384 {
Greg Claytondb598232011-01-07 01:57:07 +00001385 // If we didn't already try and read from the object file cache, then
1386 // try it after failing to read from the process.
1387 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001388 }
1389 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390}
1391
Greg Claytond16e1e52011-07-12 17:06:17 +00001392size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001393Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1394{
1395 char buf[256];
1396 out_str.clear();
1397 addr_t curr_addr = addr.GetLoadAddress(this);
1398 Address address(addr);
1399 while (1)
1400 {
1401 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1402 if (length == 0)
1403 break;
1404 out_str.append(buf, length);
1405 // If we got "length - 1" bytes, we didn't get the whole C string, we
1406 // need to read some more characters
1407 if (length == sizeof(buf) - 1)
1408 curr_addr += length;
1409 else
1410 break;
1411 address = Address(curr_addr);
1412 }
1413 return out_str.size();
1414}
1415
1416
1417size_t
1418Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1419{
1420 size_t total_cstr_len = 0;
1421 if (dst && dst_max_len)
1422 {
1423 result_error.Clear();
1424 // NULL out everything just to be safe
1425 memset (dst, 0, dst_max_len);
1426 Error error;
1427 addr_t curr_addr = addr.GetLoadAddress(this);
1428 Address address(addr);
1429 const size_t cache_line_size = 512;
1430 size_t bytes_left = dst_max_len - 1;
1431 char *curr_dst = dst;
1432
1433 while (bytes_left > 0)
1434 {
1435 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1436 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1437 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1438
1439 if (bytes_read == 0)
1440 {
1441 result_error = error;
1442 dst[total_cstr_len] = '\0';
1443 break;
1444 }
1445 const size_t len = strlen(curr_dst);
1446
1447 total_cstr_len += len;
1448
1449 if (len < bytes_to_read)
1450 break;
1451
1452 curr_dst += bytes_read;
1453 curr_addr += bytes_read;
1454 bytes_left -= bytes_read;
1455 address = Address(curr_addr);
1456 }
1457 }
1458 else
1459 {
1460 if (dst == NULL)
1461 result_error.SetErrorString("invalid arguments");
1462 else
1463 result_error.Clear();
1464 }
1465 return total_cstr_len;
1466}
1467
1468size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001469Target::ReadScalarIntegerFromMemory (const Address& addr,
1470 bool prefer_file_cache,
1471 uint32_t byte_size,
1472 bool is_signed,
1473 Scalar &scalar,
1474 Error &error)
1475{
1476 uint64_t uval;
1477
1478 if (byte_size <= sizeof(uval))
1479 {
1480 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1481 if (bytes_read == byte_size)
1482 {
1483 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001484 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001485 if (byte_size <= 4)
1486 scalar = data.GetMaxU32 (&offset, byte_size);
1487 else
1488 scalar = data.GetMaxU64 (&offset, byte_size);
1489
1490 if (is_signed)
1491 scalar.SignExtend(byte_size * 8);
1492 return bytes_read;
1493 }
1494 }
1495 else
1496 {
1497 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1498 }
1499 return 0;
1500}
1501
1502uint64_t
1503Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1504 bool prefer_file_cache,
1505 size_t integer_byte_size,
1506 uint64_t fail_value,
1507 Error &error)
1508{
1509 Scalar scalar;
1510 if (ReadScalarIntegerFromMemory (addr,
1511 prefer_file_cache,
1512 integer_byte_size,
1513 false,
1514 scalar,
1515 error))
1516 return scalar.ULongLong(fail_value);
1517 return fail_value;
1518}
1519
1520bool
1521Target::ReadPointerFromMemory (const Address& addr,
1522 bool prefer_file_cache,
1523 Error &error,
1524 Address &pointer_addr)
1525{
1526 Scalar scalar;
1527 if (ReadScalarIntegerFromMemory (addr,
1528 prefer_file_cache,
1529 m_arch.GetAddressByteSize(),
1530 false,
1531 scalar,
1532 error))
1533 {
1534 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1535 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1536 {
1537 if (m_section_load_list.IsEmpty())
1538 {
1539 // No sections are loaded, so we must assume we are not running
1540 // yet and anything we are given is a file address.
1541 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1542 }
1543 else
1544 {
1545 // We have at least one section loaded. This can be becuase
1546 // we have manually loaded some sections with "target modules load ..."
1547 // or because we have have a live process that has sections loaded
1548 // through the dynamic loader
1549 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1550 }
1551 // We weren't able to resolve the pointer value, so just return
1552 // an address with no section
1553 if (!pointer_addr.IsValid())
1554 pointer_addr.SetOffset (pointer_vm_addr);
1555 return true;
1556
1557 }
1558 }
1559 return false;
1560}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561
1562ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001563Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001565 ModuleSP module_sp;
1566
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567 Error error;
1568
Jim Ingham4a94c912012-05-17 18:38:42 +00001569 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1570 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001571
Jim Ingham4a94c912012-05-17 18:38:42 +00001572 if (module_spec.GetUUID().IsValid())
1573 module_sp = m_images.FindFirstModule(module_spec);
1574
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001575 if (!module_sp)
1576 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001577 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1578 bool did_create_module = false;
1579
1580 // If there are image search path entries, try to use them first to acquire a suitable image.
1581 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001582 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001583 ModuleSpec transformed_spec (module_spec);
1584 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1585 {
1586 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1587 error = ModuleList::GetSharedModule (transformed_spec,
1588 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001589 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001590 &old_module_sp,
1591 &did_create_module);
1592 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001593 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001594
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001595 if (!module_sp)
1596 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001597 // If we have a UUID, we can check our global shared module list in case
1598 // we already have it. If we don't have a valid UUID, then we can't since
1599 // the path in "module_spec" will be a platform path, and we will need to
1600 // let the platform find that file. For example, we could be asking for
1601 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1602 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1603 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1604 // cache.
1605 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001606 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001607 // We have a UUID, it is OK to check the global module list...
1608 error = ModuleList::GetSharedModule (module_spec,
1609 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001610 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001611 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001612 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001613 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001614
1615 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001616 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001617 // The platform is responsible for finding and caching an appropriate
1618 // module in the shared module cache.
1619 if (m_platform_sp)
1620 {
1621 FileSpec platform_file_spec;
1622 error = m_platform_sp->GetSharedModule (module_spec,
1623 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001624 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001625 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001626 &did_create_module);
1627 }
1628 else
1629 {
1630 error.SetErrorString("no platform is currently set");
1631 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001632 }
1633 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634
Jim Ingham4a94c912012-05-17 18:38:42 +00001635 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1636 // module in the list already, and if there was, let's remove it.
1637 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001639 ObjectFile *objfile = module_sp->GetObjectFile();
1640 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001641 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001642 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001643 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001644 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1645 case ObjectFile::eTypeExecutable: /// A normal executable
1646 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1647 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1648 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1649 break;
1650 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1651 if (error_ptr)
1652 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1653 return ModuleSP();
1654 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1655 if (error_ptr)
1656 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1657 return ModuleSP();
1658 default:
1659 if (error_ptr)
1660 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1661 return ModuleSP();
1662 }
1663 // GetSharedModule is not guaranteed to find the old shared module, for instance
1664 // in the common case where you pass in the UUID, it is only going to find the one
1665 // module matching the UUID. In fact, it has no good way to know what the "old module"
1666 // relevant to this target is, since there might be many copies of a module with this file spec
1667 // in various running debug sessions, but only one of them will belong to this target.
1668 // So let's remove the UUID from the module list, and look in the target's module list.
1669 // Only do this if there is SOMETHING else in the module spec...
1670 if (!old_module_sp)
1671 {
1672 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001673 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001674 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1675 module_spec_copy.GetUUID().Clear();
1676
1677 ModuleList found_modules;
1678 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1679 if (num_found == 1)
1680 {
1681 old_module_sp = found_modules.GetModuleAtIndex(0);
1682 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001683 }
1684 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001685
1686 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1687 {
1688 m_images.ReplaceModule(old_module_sp, module_sp);
1689 Module *old_module_ptr = old_module_sp.get();
1690 old_module_sp.reset();
1691 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1692 }
1693 else
1694 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001695 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001696 }
1697 }
1698 if (error_ptr)
1699 *error_ptr = error;
1700 return module_sp;
1701}
1702
1703
Greg Claytond9e416c2012-02-18 05:35:26 +00001704TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001705Target::CalculateTarget ()
1706{
Greg Claytond9e416c2012-02-18 05:35:26 +00001707 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001708}
1709
Greg Claytond9e416c2012-02-18 05:35:26 +00001710ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001711Target::CalculateProcess ()
1712{
Greg Claytond9e416c2012-02-18 05:35:26 +00001713 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001714}
1715
Greg Claytond9e416c2012-02-18 05:35:26 +00001716ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001717Target::CalculateThread ()
1718{
Greg Claytond9e416c2012-02-18 05:35:26 +00001719 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720}
1721
Jason Molendab57e4a12013-11-04 09:33:30 +00001722StackFrameSP
1723Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001724{
Jason Molendab57e4a12013-11-04 09:33:30 +00001725 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726}
1727
1728void
Greg Clayton0603aa92010-10-04 01:05:56 +00001729Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730{
Greg Claytonc14ee322011-09-22 04:58:26 +00001731 exe_ctx.Clear();
1732 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733}
1734
1735PathMappingList &
1736Target::GetImageSearchPathList ()
1737{
1738 return m_image_search_paths;
1739}
1740
1741void
1742Target::ImageSearchPathsChanged
1743(
1744 const PathMappingList &path_list,
1745 void *baton
1746)
1747{
1748 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001749 ModuleSP exe_module_sp (target->GetExecutableModule());
1750 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001751 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752}
1753
1754ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001755Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756{
Greg Clayton73da2442011-08-03 01:23:55 +00001757 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001758 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001759 {
Greg Clayton73da2442011-08-03 01:23:55 +00001760 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001761 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001762 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1763 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1764 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1765 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001766 return m_scratch_ast_context_ap.get();
1767}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001768
Sean Callanan686b2312011-11-16 18:20:47 +00001769ClangASTImporter *
1770Target::GetClangASTImporter()
1771{
1772 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1773
1774 if (!ast_importer)
1775 {
1776 ast_importer = new ClangASTImporter();
1777 m_ast_importer_ap.reset(ast_importer);
1778 }
1779
1780 return ast_importer;
1781}
1782
Greg Clayton99d0faf2010-11-18 23:32:35 +00001783void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001784Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001785{
Greg Clayton6920b522012-08-22 18:39:03 +00001786 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001787}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001788
Greg Clayton99d0faf2010-11-18 23:32:35 +00001789void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001790Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001791{
Greg Clayton6920b522012-08-22 18:39:03 +00001792 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001793}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001794
Greg Claytonc859e2d2012-02-13 23:10:39 +00001795FileSpecList
1796Target::GetDefaultExecutableSearchPaths ()
1797{
Greg Clayton67cc0632012-08-22 17:17:09 +00001798 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1799 if (properties_sp)
1800 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001801 return FileSpecList();
1802}
1803
Michael Sartaina7499c92013-07-01 19:45:50 +00001804FileSpecList
1805Target::GetDefaultDebugFileSearchPaths ()
1806{
1807 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1808 if (properties_sp)
1809 return properties_sp->GetDebugFileSearchPaths();
1810 return FileSpecList();
1811}
1812
Caroline Ticedaccaa92010-09-20 20:44:43 +00001813ArchSpec
1814Target::GetDefaultArchitecture ()
1815{
Greg Clayton67cc0632012-08-22 17:17:09 +00001816 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1817 if (properties_sp)
1818 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001819 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001820}
1821
1822void
Greg Clayton67cc0632012-08-22 17:17:09 +00001823Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001824{
Greg Clayton67cc0632012-08-22 17:17:09 +00001825 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1826 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001827 {
1828 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 +00001829 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001830 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001831}
1832
Greg Clayton0603aa92010-10-04 01:05:56 +00001833Target *
1834Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1835{
1836 // The target can either exist in the "process" of ExecutionContext, or in
1837 // the "target_sp" member of SymbolContext. This accessor helper function
1838 // will get the target from one of these locations.
1839
1840 Target *target = NULL;
1841 if (sc_ptr != NULL)
1842 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001843 if (target == NULL && exe_ctx_ptr)
1844 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001845 return target;
1846}
1847
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001848ExecutionResults
1849Target::EvaluateExpression
1850(
1851 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001852 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001853 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001854 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001855)
1856{
Enrico Granata97fca502012-09-18 17:43:16 +00001857 result_valobj_sp.reset();
1858
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001859 ExecutionResults execution_results = eExecutionSetupError;
1860
Greg Claytond1767f02011-12-08 02:13:16 +00001861 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1862 return execution_results;
1863
Jim Ingham6026ca32011-05-12 02:06:14 +00001864 // We shouldn't run stop hooks in expressions.
1865 // Be sure to reset this if you return anywhere within this function.
1866 bool old_suppress_value = m_suppress_stop_hooks;
1867 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001868
1869 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001870
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001871 if (frame)
1872 {
1873 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001874 }
1875 else if (m_process_sp)
1876 {
1877 m_process_sp->CalculateExecutionContext(exe_ctx);
1878 }
1879 else
1880 {
1881 CalculateExecutionContext(exe_ctx);
1882 }
1883
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001884 // Make sure we aren't just trying to see the value of a persistent
1885 // variable (something like "$0")
1886 lldb::ClangExpressionVariableSP persistent_var_sp;
1887 // Only check for persistent variables the expression starts with a '$'
1888 if (expr_cstr[0] == '$')
1889 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1890
1891 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001892 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001893 result_valobj_sp = persistent_var_sp->GetValueObject ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001894 execution_results = eExecutionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001895 }
1896 else
1897 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001898 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00001899 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001900 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001901 options,
1902 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001903 prefix,
1904 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001905 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001906 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001907
1908 m_suppress_stop_hooks = old_suppress_value;
1909
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001910 return execution_results;
1911}
1912
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001913lldb::addr_t
1914Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1915{
1916 addr_t code_addr = load_addr;
1917 switch (m_arch.GetMachine())
1918 {
1919 case llvm::Triple::arm:
1920 case llvm::Triple::thumb:
1921 switch (addr_class)
1922 {
1923 case eAddressClassData:
1924 case eAddressClassDebug:
1925 return LLDB_INVALID_ADDRESS;
1926
1927 case eAddressClassUnknown:
1928 case eAddressClassInvalid:
1929 case eAddressClassCode:
1930 case eAddressClassCodeAlternateISA:
1931 case eAddressClassRuntime:
1932 // Check if bit zero it no set?
1933 if ((code_addr & 1ull) == 0)
1934 {
1935 // Bit zero isn't set, check if the address is a multiple of 2?
1936 if (code_addr & 2ull)
1937 {
1938 // The address is a multiple of 2 so it must be thumb, set bit zero
1939 code_addr |= 1ull;
1940 }
1941 else if (addr_class == eAddressClassCodeAlternateISA)
1942 {
1943 // We checked the address and the address claims to be the alternate ISA
1944 // which means thumb, so set bit zero.
1945 code_addr |= 1ull;
1946 }
1947 }
1948 break;
1949 }
1950 break;
1951
1952 default:
1953 break;
1954 }
1955 return code_addr;
1956}
1957
1958lldb::addr_t
1959Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1960{
1961 addr_t opcode_addr = load_addr;
1962 switch (m_arch.GetMachine())
1963 {
1964 case llvm::Triple::arm:
1965 case llvm::Triple::thumb:
1966 switch (addr_class)
1967 {
1968 case eAddressClassData:
1969 case eAddressClassDebug:
1970 return LLDB_INVALID_ADDRESS;
1971
1972 case eAddressClassInvalid:
1973 case eAddressClassUnknown:
1974 case eAddressClassCode:
1975 case eAddressClassCodeAlternateISA:
1976 case eAddressClassRuntime:
1977 opcode_addr &= ~(1ull);
1978 break;
1979 }
1980 break;
1981
1982 default:
1983 break;
1984 }
1985 return opcode_addr;
1986}
1987
Greg Clayton9585fbf2013-03-19 00:20:55 +00001988SourceManager &
1989Target::GetSourceManager ()
1990{
1991 if (m_source_manager_ap.get() == NULL)
1992 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
1993 return *m_source_manager_ap;
1994}
1995
1996
Jim Ingham9575d842011-03-11 03:53:59 +00001997lldb::user_id_t
1998Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1999{
2000 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Claytone1cd1be2012-01-29 20:56:30 +00002001 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Ingham9575d842011-03-11 03:53:59 +00002002 m_stop_hooks[new_uid] = new_hook_sp;
2003 return new_uid;
2004}
2005
2006bool
2007Target::RemoveStopHookByID (lldb::user_id_t user_id)
2008{
2009 size_t num_removed;
2010 num_removed = m_stop_hooks.erase (user_id);
2011 if (num_removed == 0)
2012 return false;
2013 else
2014 return true;
2015}
2016
2017void
2018Target::RemoveAllStopHooks ()
2019{
2020 m_stop_hooks.clear();
2021}
2022
2023Target::StopHookSP
2024Target::GetStopHookByID (lldb::user_id_t user_id)
2025{
2026 StopHookSP found_hook;
2027
2028 StopHookCollection::iterator specified_hook_iter;
2029 specified_hook_iter = m_stop_hooks.find (user_id);
2030 if (specified_hook_iter != m_stop_hooks.end())
2031 found_hook = (*specified_hook_iter).second;
2032 return found_hook;
2033}
2034
2035bool
2036Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2037{
2038 StopHookCollection::iterator specified_hook_iter;
2039 specified_hook_iter = m_stop_hooks.find (user_id);
2040 if (specified_hook_iter == m_stop_hooks.end())
2041 return false;
2042
2043 (*specified_hook_iter).second->SetIsActive (active_state);
2044 return true;
2045}
2046
2047void
2048Target::SetAllStopHooksActiveState (bool active_state)
2049{
2050 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2051 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2052 {
2053 (*pos).second->SetIsActive (active_state);
2054 }
2055}
2056
2057void
2058Target::RunStopHooks ()
2059{
Jim Ingham6026ca32011-05-12 02:06:14 +00002060 if (m_suppress_stop_hooks)
2061 return;
2062
Jim Ingham9575d842011-03-11 03:53:59 +00002063 if (!m_process_sp)
2064 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002065
2066 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2067 // since in that case we do not want to run the stop-hooks
2068 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2069 return;
2070
Jim Ingham9575d842011-03-11 03:53:59 +00002071 if (m_stop_hooks.empty())
2072 return;
2073
2074 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2075
2076 // If there aren't any active stop hooks, don't bother either:
2077 bool any_active_hooks = false;
2078 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2079 {
2080 if ((*pos).second->IsActive())
2081 {
2082 any_active_hooks = true;
2083 break;
2084 }
2085 }
2086 if (!any_active_hooks)
2087 return;
2088
2089 CommandReturnObject result;
2090
2091 std::vector<ExecutionContext> exc_ctx_with_reasons;
2092 std::vector<SymbolContext> sym_ctx_with_reasons;
2093
2094 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2095 size_t num_threads = cur_threadlist.GetSize();
2096 for (size_t i = 0; i < num_threads; i++)
2097 {
2098 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2099 if (cur_thread_sp->ThreadStoppedForAReason())
2100 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002101 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002102 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2103 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2104 }
2105 }
2106
2107 // If no threads stopped for a reason, don't run the stop-hooks.
2108 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2109 if (num_exe_ctx == 0)
2110 return;
2111
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002112 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2113 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002114
2115 bool keep_going = true;
2116 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002117 bool print_hook_header;
2118 bool print_thread_header;
2119
2120 if (num_exe_ctx == 1)
2121 print_thread_header = false;
2122 else
2123 print_thread_header = true;
2124
2125 if (m_stop_hooks.size() == 1)
2126 print_hook_header = false;
2127 else
2128 print_hook_header = true;
2129
Jim Ingham9575d842011-03-11 03:53:59 +00002130 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2131 {
2132 // result.Clear();
2133 StopHookSP cur_hook_sp = (*pos).second;
2134 if (!cur_hook_sp->IsActive())
2135 continue;
2136
2137 bool any_thread_matched = false;
2138 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2139 {
2140 if ((cur_hook_sp->GetSpecifier () == NULL
2141 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2142 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002143 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002144 {
2145 if (!hooks_ran)
2146 {
Jim Ingham9575d842011-03-11 03:53:59 +00002147 hooks_ran = true;
2148 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002149 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002150 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002151 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2152 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2153 NULL);
2154 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002155 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002156 else
Daniel Malead01b2952012-11-29 21:49:15 +00002157 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002158 any_thread_matched = true;
2159 }
2160
Jim Ingham381e25b2011-03-22 01:47:27 +00002161 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002162 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:59 +00002163
2164 bool stop_on_continue = true;
2165 bool stop_on_error = true;
2166 bool echo_commands = false;
2167 bool print_results = true;
2168 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:51 +00002169 &exc_ctx_with_reasons[i],
2170 stop_on_continue,
2171 stop_on_error,
2172 echo_commands,
Enrico Granata5f5ab602012-05-31 01:09:06 +00002173 print_results,
2174 eLazyBoolNo,
Greg Clayton32e0a752011-03-30 18:16:51 +00002175 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002176
2177 // If the command started the target going again, we should bag out of
2178 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002179 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2180 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002181 {
Daniel Malead01b2952012-11-29 21:49:15 +00002182 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002183 keep_going = false;
2184 }
2185 }
2186 }
2187 }
Jason Molenda879cf772011-09-23 00:42:55 +00002188
Caroline Tice969ed3d2011-05-02 20:41:46 +00002189 result.GetImmediateOutputStream()->Flush();
2190 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002191}
2192
Greg Claytonfbb76342013-11-20 21:07:01 +00002193const TargetPropertiesSP &
2194Target::GetGlobalProperties()
2195{
2196 static TargetPropertiesSP g_settings_sp;
2197 if (!g_settings_sp)
2198 {
2199 g_settings_sp.reset (new TargetProperties (NULL));
2200 }
2201 return g_settings_sp;
2202}
2203
2204Error
2205Target::Install (ProcessLaunchInfo *launch_info)
2206{
2207 Error error;
2208 PlatformSP platform_sp (GetPlatform());
2209 if (platform_sp)
2210 {
2211 if (platform_sp->IsRemote())
2212 {
2213 if (platform_sp->IsConnected())
2214 {
2215 // Install all files that have an install path, and always install the
2216 // main executable when connected to a remote platform
2217 const ModuleList& modules = GetImages();
2218 const size_t num_images = modules.GetSize();
2219 for (size_t idx = 0; idx < num_images; ++idx)
2220 {
2221 const bool is_main_executable = idx == 0;
2222 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2223 if (module_sp)
2224 {
2225 FileSpec local_file (module_sp->GetFileSpec());
2226 if (local_file)
2227 {
2228 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2229 if (!remote_file)
2230 {
2231 if (is_main_executable) // TODO: add setting for always installing main executable???
2232 {
2233 // Always install the main executable
2234 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2235 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2236 }
2237 }
2238 if (remote_file)
2239 {
2240 error = platform_sp->Install(local_file, remote_file);
2241 if (error.Success())
2242 {
2243 module_sp->SetPlatformFileSpec(remote_file);
2244 if (is_main_executable)
2245 {
2246 if (launch_info)
2247 launch_info->SetExecutableFile(remote_file, false);
2248 }
2249 }
2250 else
2251 break;
2252 }
2253 }
2254 }
2255 }
2256 }
2257 }
2258 }
2259 return error;
2260}
Greg Clayton7b242382011-07-08 00:48:09 +00002261
Jim Ingham9575d842011-03-11 03:53:59 +00002262//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002263// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002264//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002265Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2266 UserID (uid),
2267 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002268 m_commands (),
2269 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002270 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002271 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002272{
2273}
2274
2275Target::StopHook::StopHook (const StopHook &rhs) :
2276 UserID (rhs.GetID()),
2277 m_target_sp (rhs.m_target_sp),
2278 m_commands (rhs.m_commands),
2279 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002280 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002281 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002282{
2283 if (rhs.m_thread_spec_ap.get() != NULL)
2284 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2285}
2286
2287
2288Target::StopHook::~StopHook ()
2289{
2290}
2291
2292void
2293Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2294{
2295 m_thread_spec_ap.reset (specifier);
2296}
2297
2298
2299void
2300Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2301{
2302 int indent_level = s->GetIndentLevel();
2303
2304 s->SetIndentLevel(indent_level + 2);
2305
Daniel Malead01b2952012-11-29 21:49:15 +00002306 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002307 if (m_active)
2308 s->Indent ("State: enabled\n");
2309 else
2310 s->Indent ("State: disabled\n");
2311
2312 if (m_specifier_sp)
2313 {
2314 s->Indent();
2315 s->PutCString ("Specifier:\n");
2316 s->SetIndentLevel (indent_level + 4);
2317 m_specifier_sp->GetDescription (s, level);
2318 s->SetIndentLevel (indent_level + 2);
2319 }
2320
2321 if (m_thread_spec_ap.get() != NULL)
2322 {
2323 StreamString tmp;
2324 s->Indent("Thread:\n");
2325 m_thread_spec_ap->GetDescription (&tmp, level);
2326 s->SetIndentLevel (indent_level + 4);
2327 s->Indent (tmp.GetData());
2328 s->PutCString ("\n");
2329 s->SetIndentLevel (indent_level + 2);
2330 }
2331
2332 s->Indent ("Commands: \n");
2333 s->SetIndentLevel (indent_level + 4);
2334 uint32_t num_commands = m_commands.GetSize();
2335 for (uint32_t i = 0; i < num_commands; i++)
2336 {
2337 s->Indent(m_commands.GetStringAtIndex(i));
2338 s->PutCString ("\n");
2339 }
2340 s->SetIndentLevel (indent_level);
2341}
2342
Greg Clayton67cc0632012-08-22 17:17:09 +00002343//--------------------------------------------------------------
2344// class TargetProperties
2345//--------------------------------------------------------------
2346
2347OptionEnumValueElement
2348lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002349{
Greg Clayton67cc0632012-08-22 17:17:09 +00002350 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2351 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2352 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2353 { 0, NULL, NULL }
2354};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002355
Greg Clayton1f746072012-08-29 21:13:06 +00002356static OptionEnumValueElement
2357g_inline_breakpoint_enums[] =
2358{
2359 { 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."},
2360 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2361 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2362 { 0, NULL, NULL }
2363};
2364
Jim Ingham0f063ba2013-03-02 00:26:47 +00002365typedef enum x86DisassemblyFlavor
2366{
2367 eX86DisFlavorDefault,
2368 eX86DisFlavorIntel,
2369 eX86DisFlavorATT
2370} x86DisassemblyFlavor;
2371
2372static OptionEnumValueElement
2373g_x86_dis_flavor_value_types[] =
2374{
2375 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2376 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2377 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2378 { 0, NULL, NULL }
2379};
2380
Enrico Granata397ddd52013-05-21 20:13:34 +00002381static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002382g_hex_immediate_style_values[] =
2383{
2384 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2385 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2386 { 0, NULL, NULL }
2387};
2388
2389static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002390g_load_script_from_sym_file_values[] =
2391{
2392 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2393 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2394 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2395 { 0, NULL, NULL }
2396};
2397
Greg Claytonfd814c52013-08-13 01:42:25 +00002398
2399static OptionEnumValueElement
2400g_memory_module_load_level_values[] =
2401{
Greg Clayton86eac942013-08-13 21:32:34 +00002402 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002403 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2404 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2405 { 0, NULL, NULL }
2406};
2407
Greg Clayton67cc0632012-08-22 17:17:09 +00002408static PropertyDefinition
2409g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002410{
Greg Clayton67cc0632012-08-22 17:17:09 +00002411 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2412 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2413 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2414 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2415 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2416 { "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 "
2417 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2418 "some part (starting at the root) of the path to the file when it was built, "
2419 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2420 "Each element of the array is checked in order and the first one that results in a match wins." },
2421 { "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 +00002422 { "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 +00002423 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2424 { "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 +00002425 { "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 +00002426 { "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 +00002427 { "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." },
2428 { "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 +00002429 { "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." },
2430 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2431 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2432 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2433 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2434 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2435 { "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 +00002436 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2437 "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. "
2438 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2439 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2440 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2441 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2442 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002443 // 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.
2444 { "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 +00002445 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2446 { "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 +00002447 { "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 +00002448 { "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 +00002449 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2450 "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. "
2451 "This setting helps users control how much information gets loaded when loading modules from memory."
2452 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2453 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2454 "'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 +00002455 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2456};
2457enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002458{
Greg Clayton67cc0632012-08-22 17:17:09 +00002459 ePropertyDefaultArch,
2460 ePropertyExprPrefix,
2461 ePropertyPreferDynamic,
2462 ePropertyEnableSynthetic,
2463 ePropertySkipPrologue,
2464 ePropertySourceMap,
2465 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002466 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002467 ePropertyMaxChildrenCount,
2468 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002469 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002470 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002471 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002472 ePropertyRunArgs,
2473 ePropertyEnvVars,
2474 ePropertyInheritEnv,
2475 ePropertyInputPath,
2476 ePropertyOutputPath,
2477 ePropertyErrorPath,
2478 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002479 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002480 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00002481 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00002482 ePropertyUseHexImmediates,
2483 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002484 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00002485 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfd814c52013-08-13 01:42:25 +00002486 ePropertyMemoryModuleLoadLevel
Greg Clayton67cc0632012-08-22 17:17:09 +00002487};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002488
Caroline Ticedaccaa92010-09-20 20:44:43 +00002489
Greg Clayton67cc0632012-08-22 17:17:09 +00002490class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002491{
Greg Clayton67cc0632012-08-22 17:17:09 +00002492public:
2493 TargetOptionValueProperties (const ConstString &name) :
2494 OptionValueProperties (name),
2495 m_target (NULL),
2496 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002497 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002498 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002499
Greg Clayton67cc0632012-08-22 17:17:09 +00002500 // This constructor is used when creating TargetOptionValueProperties when it
2501 // is part of a new lldb_private::Target instance. It will copy all current
2502 // global property values as needed
2503 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2504 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2505 m_target (target),
2506 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002507 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002508 }
2509
2510 virtual const Property *
2511 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2512 {
2513 // When gettings the value for a key from the target options, we will always
2514 // try and grab the setting from the current target if there is one. Else we just
2515 // use the one from this instance.
2516 if (idx == ePropertyEnvVars)
2517 GetHostEnvironmentIfNeeded ();
2518
2519 if (exe_ctx)
2520 {
2521 Target *target = exe_ctx->GetTargetPtr();
2522 if (target)
2523 {
2524 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2525 if (this != target_properties)
2526 return target_properties->ProtectedGetPropertyAtIndex (idx);
2527 }
2528 }
2529 return ProtectedGetPropertyAtIndex (idx);
2530 }
Enrico Granata84a53df2013-05-20 22:29:23 +00002531
2532 lldb::TargetSP
2533 GetTargetSP ()
2534 {
2535 return m_target->shared_from_this();
2536 }
2537
Greg Clayton67cc0632012-08-22 17:17:09 +00002538protected:
2539
2540 void
2541 GetHostEnvironmentIfNeeded () const
2542 {
2543 if (!m_got_host_env)
2544 {
2545 if (m_target)
2546 {
2547 m_got_host_env = true;
2548 const uint32_t idx = ePropertyInheritEnv;
2549 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2550 {
2551 PlatformSP platform_sp (m_target->GetPlatform());
2552 if (platform_sp)
2553 {
2554 StringList env;
2555 if (platform_sp->GetEnvironment(env))
2556 {
2557 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2558 if (env_dict)
2559 {
2560 const bool can_replace = false;
2561 const size_t envc = env.GetSize();
2562 for (size_t idx=0; idx<envc; idx++)
2563 {
2564 const char *env_entry = env.GetStringAtIndex (idx);
2565 if (env_entry)
2566 {
2567 const char *equal_pos = ::strchr(env_entry, '=');
2568 ConstString key;
2569 // It is ok to have environment variables with no values
2570 const char *value = NULL;
2571 if (equal_pos)
2572 {
2573 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2574 if (equal_pos[1])
2575 value = equal_pos + 1;
2576 }
2577 else
2578 {
2579 key.SetCString(env_entry);
2580 }
2581 // Don't allow existing keys to be replaced with ones we get from the platform environment
2582 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2583 }
2584 }
2585 }
2586 }
2587 }
2588 }
2589 }
2590 }
2591 }
2592 Target *m_target;
2593 mutable bool m_got_host_env;
2594};
2595
Greg Claytonfbb76342013-11-20 21:07:01 +00002596//----------------------------------------------------------------------
2597// TargetProperties
2598//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00002599TargetProperties::TargetProperties (Target *target) :
2600 Properties ()
2601{
2602 if (target)
2603 {
2604 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Ticedaccaa92010-09-20 20:44:43 +00002605 }
2606 else
Greg Clayton67cc0632012-08-22 17:17:09 +00002607 {
2608 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2609 m_collection_sp->Initialize(g_properties);
2610 m_collection_sp->AppendProperty(ConstString("process"),
2611 ConstString("Settings specify to processes."),
2612 true,
2613 Process::GetGlobalProperties()->GetValueProperties());
2614 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002615}
2616
Greg Clayton67cc0632012-08-22 17:17:09 +00002617TargetProperties::~TargetProperties ()
2618{
2619}
2620ArchSpec
2621TargetProperties::GetDefaultArchitecture () const
2622{
2623 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2624 if (value)
2625 return value->GetCurrentValue();
2626 return ArchSpec();
2627}
2628
2629void
2630TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2631{
2632 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2633 if (value)
2634 return value->SetCurrentValue(arch, true);
2635}
2636
2637lldb::DynamicValueType
2638TargetProperties::GetPreferDynamicValue() const
2639{
2640 const uint32_t idx = ePropertyPreferDynamic;
2641 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2642}
2643
2644bool
2645TargetProperties::GetDisableASLR () const
2646{
2647 const uint32_t idx = ePropertyDisableASLR;
2648 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2649}
2650
2651void
2652TargetProperties::SetDisableASLR (bool b)
2653{
2654 const uint32_t idx = ePropertyDisableASLR;
2655 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2656}
2657
2658bool
2659TargetProperties::GetDisableSTDIO () const
2660{
2661 const uint32_t idx = ePropertyDisableSTDIO;
2662 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2663}
2664
2665void
2666TargetProperties::SetDisableSTDIO (bool b)
2667{
2668 const uint32_t idx = ePropertyDisableSTDIO;
2669 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2670}
2671
Jim Ingham0f063ba2013-03-02 00:26:47 +00002672const char *
2673TargetProperties::GetDisassemblyFlavor () const
2674{
2675 const uint32_t idx = ePropertyDisassemblyFlavor;
2676 const char *return_value;
2677
2678 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2679 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
2680 return return_value;
2681}
2682
Greg Clayton1f746072012-08-29 21:13:06 +00002683InlineStrategy
2684TargetProperties::GetInlineStrategy () const
2685{
2686 const uint32_t idx = ePropertyInlineStrategy;
2687 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2688}
2689
Greg Clayton45392552012-10-17 22:57:12 +00002690const char *
2691TargetProperties::GetArg0 () const
2692{
2693 const uint32_t idx = ePropertyArg0;
2694 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2695}
2696
2697void
2698TargetProperties::SetArg0 (const char *arg)
2699{
2700 const uint32_t idx = ePropertyArg0;
2701 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2702}
2703
Greg Clayton67cc0632012-08-22 17:17:09 +00002704bool
2705TargetProperties::GetRunArguments (Args &args) const
2706{
2707 const uint32_t idx = ePropertyRunArgs;
2708 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2709}
2710
2711void
2712TargetProperties::SetRunArguments (const Args &args)
2713{
2714 const uint32_t idx = ePropertyRunArgs;
2715 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2716}
2717
2718size_t
2719TargetProperties::GetEnvironmentAsArgs (Args &env) const
2720{
2721 const uint32_t idx = ePropertyEnvVars;
2722 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2723}
2724
2725bool
2726TargetProperties::GetSkipPrologue() const
2727{
2728 const uint32_t idx = ePropertySkipPrologue;
2729 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2730}
2731
2732PathMappingList &
2733TargetProperties::GetSourcePathMap () const
2734{
2735 const uint32_t idx = ePropertySourceMap;
2736 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2737 assert(option_value);
2738 return option_value->GetCurrentValue();
2739}
2740
2741FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00002742TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00002743{
2744 const uint32_t idx = ePropertyExecutableSearchPaths;
2745 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2746 assert(option_value);
2747 return option_value->GetCurrentValue();
2748}
2749
Michael Sartaina7499c92013-07-01 19:45:50 +00002750FileSpecList &
2751TargetProperties::GetDebugFileSearchPaths ()
2752{
2753 const uint32_t idx = ePropertyDebugFileSearchPaths;
2754 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2755 assert(option_value);
2756 return option_value->GetCurrentValue();
2757}
2758
Greg Clayton67cc0632012-08-22 17:17:09 +00002759bool
2760TargetProperties::GetEnableSyntheticValue () const
2761{
2762 const uint32_t idx = ePropertyEnableSynthetic;
2763 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2764}
2765
2766uint32_t
2767TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2768{
2769 const uint32_t idx = ePropertyMaxChildrenCount;
2770 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2771}
2772
2773uint32_t
2774TargetProperties::GetMaximumSizeOfStringSummary() const
2775{
2776 const uint32_t idx = ePropertyMaxSummaryLength;
2777 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2778}
2779
Enrico Granatad325bf92013-06-04 22:54:16 +00002780uint32_t
2781TargetProperties::GetMaximumMemReadSize () const
2782{
2783 const uint32_t idx = ePropertyMaxMemReadSize;
2784 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2785}
2786
Greg Clayton67cc0632012-08-22 17:17:09 +00002787FileSpec
2788TargetProperties::GetStandardInputPath () const
2789{
2790 const uint32_t idx = ePropertyInputPath;
2791 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2792}
2793
2794void
2795TargetProperties::SetStandardInputPath (const char *p)
2796{
2797 const uint32_t idx = ePropertyInputPath;
2798 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2799}
2800
2801FileSpec
2802TargetProperties::GetStandardOutputPath () const
2803{
2804 const uint32_t idx = ePropertyOutputPath;
2805 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2806}
2807
2808void
2809TargetProperties::SetStandardOutputPath (const char *p)
2810{
2811 const uint32_t idx = ePropertyOutputPath;
2812 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2813}
2814
2815FileSpec
2816TargetProperties::GetStandardErrorPath () const
2817{
2818 const uint32_t idx = ePropertyErrorPath;
2819 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2820}
2821
Greg Clayton6920b522012-08-22 18:39:03 +00002822const char *
2823TargetProperties::GetExpressionPrefixContentsAsCString ()
2824{
2825 const uint32_t idx = ePropertyExprPrefix;
2826 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2827 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00002828 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00002829 const bool null_terminate = true;
2830 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00002831 if (data_sp)
2832 return (const char *) data_sp->GetBytes();
2833 }
Greg Clayton6920b522012-08-22 18:39:03 +00002834 return NULL;
2835}
2836
Greg Clayton67cc0632012-08-22 17:17:09 +00002837void
2838TargetProperties::SetStandardErrorPath (const char *p)
2839{
2840 const uint32_t idx = ePropertyErrorPath;
2841 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2842}
2843
2844bool
2845TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2846{
2847 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2848 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2849}
2850
Jim Ingham17d023f2013-03-13 17:58:04 +00002851bool
Daniel Malead79ae052013-08-07 21:54:09 +00002852TargetProperties::GetUseHexImmediates () const
2853{
2854 const uint32_t idx = ePropertyUseHexImmediates;
2855 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2856}
2857
2858bool
Jim Ingham17d023f2013-03-13 17:58:04 +00002859TargetProperties::GetUseFastStepping () const
2860{
2861 const uint32_t idx = ePropertyUseFastStepping;
2862 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2863}
2864
Enrico Granata397ddd52013-05-21 20:13:34 +00002865LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002866TargetProperties::GetLoadScriptFromSymbolFile () const
2867{
2868 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00002869 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002870}
2871
Daniel Malead79ae052013-08-07 21:54:09 +00002872Disassembler::HexImmediateStyle
2873TargetProperties::GetHexImmediateStyle () const
2874{
2875 const uint32_t idx = ePropertyHexImmediateStyle;
2876 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
2877}
2878
Greg Claytonfd814c52013-08-13 01:42:25 +00002879MemoryModuleLoadLevel
2880TargetProperties::GetMemoryModuleLoadLevel() const
2881{
2882 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
2883 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
2884}
2885
2886
Greg Clayton67cc0632012-08-22 17:17:09 +00002887
Greg Claytonfbb76342013-11-20 21:07:01 +00002888//----------------------------------------------------------------------
2889// Target::TargetEventData
2890//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +00002891const ConstString &
2892Target::TargetEventData::GetFlavorString ()
2893{
2894 static ConstString g_flavor ("Target::TargetEventData");
2895 return g_flavor;
2896}
2897
2898const ConstString &
2899Target::TargetEventData::GetFlavor () const
2900{
2901 return TargetEventData::GetFlavorString ();
2902}
2903
2904Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2905 EventData(),
2906 m_target_sp (new_target_sp)
2907{
2908}
2909
2910Target::TargetEventData::~TargetEventData()
2911{
2912
2913}
2914
2915void
2916Target::TargetEventData::Dump (Stream *s) const
2917{
2918
2919}
2920
2921const TargetSP
2922Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2923{
2924 TargetSP target_sp;
2925
2926 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2927 if (data)
2928 target_sp = data->m_target_sp;
2929
2930 return target_sp;
2931}
2932
2933const Target::TargetEventData *
2934Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2935{
2936 if (event_ptr)
2937 {
2938 const EventData *event_data = event_ptr->GetData();
2939 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2940 return static_cast <const TargetEventData *> (event_ptr->GetData());
2941 }
2942 return NULL;
2943}
2944