blob: 17e5205efde604a1f64839da067c35292887aea9 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000030#include "lldb/Core/SourceManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000034#include "lldb/Expression/ClangASTSource.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000035#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000037#include "lldb/Interpreter/CommandInterpreter.h"
38#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000039#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000040#include "lldb/Interpreter/OptionValues.h"
41#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/lldb-private-log.h"
43#include "lldb/Symbol/ObjectFile.h"
44#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000045#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000046#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000047#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000048#include "lldb/Target/Thread.h"
49#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
51using namespace lldb;
52using namespace lldb_private;
53
Jim Ingham4bddaeb2012-02-16 06:50:00 +000054ConstString &
55Target::GetStaticBroadcasterClass ()
56{
57 static ConstString class_name ("lldb.target");
58 return class_name;
59}
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061//----------------------------------------------------------------------
62// Target constructor
63//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:51 +000064Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +000065 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000066 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000067 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000068 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000069 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000070 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000071 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000072 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000073 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074 m_breakpoint_list (false),
75 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000076 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000077 m_process_sp (),
78 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),
Greg Claytond5944cd2013-12-06 01:12:00 +000087 m_valid (true),
Enrico Granata5d5f60c2013-09-24 22:58:37 +000088 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000090 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
91 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
92 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000093 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +000094 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Jim Ingham4bddaeb2012-02-16 06:50:00 +000095
96 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +000097
Greg Clayton5160ce52013-03-27 23:08:40 +000098 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099 if (log)
100 log->Printf ("%p Target::Target()", this);
Jason Molendae1b68ad2012-12-05 00:25:49 +0000101 if (m_arch.IsValid())
102 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000103 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 +0000104 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105}
106
107//----------------------------------------------------------------------
108// Destructor
109//----------------------------------------------------------------------
110Target::~Target()
111{
Greg Clayton5160ce52013-03-27 23:08:40 +0000112 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 if (log)
114 log->Printf ("%p Target::~Target()", this);
115 DeleteCurrentProcess ();
116}
117
118void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000119Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120{
Greg Clayton89411422010-10-08 00:21:05 +0000121// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000122 if (description_level != lldb::eDescriptionLevelBrief)
123 {
124 s->Indent();
125 s->PutCString("Target\n");
126 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000127 m_images.Dump(s);
128 m_breakpoint_list.Dump(s);
129 m_internal_breakpoint_list.Dump(s);
130 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000131 }
132 else
133 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000134 Module *exe_module = GetExecutableModulePointer();
135 if (exe_module)
136 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000137 else
138 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000139 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140}
141
142void
Greg Clayton90ba8112012-12-05 00:16:59 +0000143Target::CleanupProcess ()
144{
145 // Do any cleanup of the target we need to do between process instances.
146 // NB It is better to do this before destroying the process in case the
147 // clean up needs some help from the process.
148 m_breakpoint_list.ClearAllBreakpointSites();
149 m_internal_breakpoint_list.ClearAllBreakpointSites();
150 // Disable watchpoints just on the debugger side.
151 Mutex::Locker locker;
152 this->GetWatchpointList().GetListMutex(locker);
153 DisableAllWatchpoints(false);
154 ClearAllWatchpointHitCounts();
155}
156
157void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158Target::DeleteCurrentProcess ()
159{
160 if (m_process_sp.get())
161 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000162 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163 if (m_process_sp->IsAlive())
164 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000165
166 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167
Greg Clayton90ba8112012-12-05 00:16:59 +0000168 CleanupProcess ();
169
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 m_process_sp.reset();
171 }
172}
173
174const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000175Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176{
177 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000178 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 return m_process_sp;
180}
181
182const lldb::ProcessSP &
183Target::GetProcessSP () const
184{
185 return m_process_sp;
186}
187
Greg Clayton3418c852011-08-10 02:10:13 +0000188void
189Target::Destroy()
190{
191 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000192 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000193 DeleteCurrentProcess ();
194 m_platform_sp.reset();
195 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000196 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000197 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000198 const bool notify = false;
199 m_breakpoint_list.RemoveAll(notify);
200 m_internal_breakpoint_list.RemoveAll(notify);
201 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000202 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000203 m_search_filter_sp.reset();
204 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000205 m_persistent_variables.Clear();
206 m_stop_hooks.clear();
207 m_stop_hook_next_id = 0;
208 m_suppress_stop_hooks = false;
209}
210
211
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212BreakpointList &
213Target::GetBreakpointList(bool internal)
214{
215 if (internal)
216 return m_internal_breakpoint_list;
217 else
218 return m_breakpoint_list;
219}
220
221const BreakpointList &
222Target::GetBreakpointList(bool internal) const
223{
224 if (internal)
225 return m_internal_breakpoint_list;
226 else
227 return m_breakpoint_list;
228}
229
230BreakpointSP
231Target::GetBreakpointByID (break_id_t break_id)
232{
233 BreakpointSP bp_sp;
234
235 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
236 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
237 else
238 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
239
240 return bp_sp;
241}
242
243BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000244Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000245 const FileSpecList *source_file_spec_list,
246 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000247 bool internal,
248 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249{
Jim Ingham87df91b2011-09-23 00:54:11 +0000250 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
251 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Greg Claytoneb023e72013-10-11 19:48:25 +0000252 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +0000253}
254
255
256BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000257Target::CreateBreakpoint (const FileSpecList *containingModules,
258 const FileSpec &file,
259 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000260 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000261 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000262 bool internal,
263 bool hardware)
Jim Ingham969795f2011-09-21 01:17:13 +0000264{
Greg Clayton1f746072012-08-29 21:13:06 +0000265 if (check_inlines == eLazyBoolCalculate)
266 {
267 const InlineStrategy inline_strategy = GetInlineStrategy();
268 switch (inline_strategy)
269 {
270 case eInlineBreakpointsNever:
271 check_inlines = eLazyBoolNo;
272 break;
273
274 case eInlineBreakpointsHeaders:
275 if (file.IsSourceImplementationFile())
276 check_inlines = eLazyBoolNo;
277 else
278 check_inlines = eLazyBoolYes;
279 break;
280
281 case eInlineBreakpointsAlways:
282 check_inlines = eLazyBoolYes;
283 break;
284 }
285 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000286 SearchFilterSP filter_sp;
287 if (check_inlines == eLazyBoolNo)
288 {
289 // Not checking for inlines, we are looking only for matching compile units
290 FileSpecList compile_unit_list;
291 compile_unit_list.Append (file);
292 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
293 }
294 else
295 {
296 filter_sp = GetSearchFilterForModuleList (containingModules);
297 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000298 if (skip_prologue == eLazyBoolCalculate)
299 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
300
Greg Clayton1f746072012-08-29 21:13:06 +0000301 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
302 file,
303 line_no,
304 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000305 skip_prologue));
Greg Claytoneb023e72013-10-11 19:48:25 +0000306 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307}
308
309
310BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000311Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 Address so_addr;
314 // Attempt to resolve our load address if possible, though it is ok if
315 // it doesn't resolve to section/offset.
316
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000317 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000318 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000319 if (!so_addr.IsValid())
320 {
321 // The address didn't resolve, so just set this as an absolute address
322 so_addr.SetOffset (addr);
323 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000324 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 return bp_sp;
326}
327
328BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000329Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000331 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Greg Claytoneb023e72013-10-11 19:48:25 +0000333 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334}
335
336BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000337Target::CreateBreakpoint (const FileSpecList *containingModules,
338 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000339 const char *func_name,
340 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000341 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000342 bool internal,
343 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000345 BreakpointSP bp_sp;
346 if (func_name)
347 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000348 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000349
350 if (skip_prologue == eLazyBoolCalculate)
351 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
352
Greg Claytond16e1e52011-07-12 17:06:17 +0000353 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
354 func_name,
355 func_name_type_mask,
356 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000357 skip_prologue));
Greg Claytoneb023e72013-10-11 19:48:25 +0000358 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000359 }
360 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361}
362
Jim Inghamfab10e82012-03-06 00:37:27 +0000363lldb::BreakpointSP
364Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000365 const FileSpecList *containingSourceFiles,
366 const std::vector<std::string> &func_names,
367 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000368 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000369 bool internal,
370 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000371{
372 BreakpointSP bp_sp;
373 size_t num_names = func_names.size();
374 if (num_names > 0)
375 {
376 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000377
378 if (skip_prologue == eLazyBoolCalculate)
379 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
380
381 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000382 func_names,
383 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000384 skip_prologue));
Greg Claytoneb023e72013-10-11 19:48:25 +0000385 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +0000386 }
387 return bp_sp;
388}
389
Jim Ingham133e0fb2012-03-03 02:05:11 +0000390BreakpointSP
391Target::CreateBreakpoint (const FileSpecList *containingModules,
392 const FileSpecList *containingSourceFiles,
393 const char *func_names[],
394 size_t num_names,
395 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000396 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000397 bool internal,
398 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000399{
400 BreakpointSP bp_sp;
401 if (num_names > 0)
402 {
403 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
404
Greg Clayton03da4cc2013-04-19 21:31:16 +0000405 if (skip_prologue == eLazyBoolCalculate)
406 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
407
408 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000409 func_names,
410 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000411 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000412 skip_prologue));
Greg Claytoneb023e72013-10-11 19:48:25 +0000413 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000414 }
415 return bp_sp;
416}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
418SearchFilterSP
419Target::GetSearchFilterForModule (const FileSpec *containingModule)
420{
421 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422 if (containingModule != NULL)
423 {
424 // TODO: We should look into sharing module based search filters
425 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000426 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 }
428 else
429 {
430 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000431 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 filter_sp = m_search_filter_sp;
433 }
434 return filter_sp;
435}
436
Jim Ingham969795f2011-09-21 01:17:13 +0000437SearchFilterSP
438Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
439{
440 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000441 if (containingModules && containingModules->GetSize() != 0)
442 {
443 // TODO: We should look into sharing module based search filters
444 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000445 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000446 }
447 else
448 {
449 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000450 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000451 filter_sp = m_search_filter_sp;
452 }
453 return filter_sp;
454}
455
Jim Ingham87df91b2011-09-23 00:54:11 +0000456SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000457Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
458 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000459{
460 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
461 return GetSearchFilterForModuleList(containingModules);
462
463 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000464 if (containingModules == NULL)
465 {
466 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
467 // but that will take a little reworking.
468
Greg Claytone1cd1be2012-01-29 20:56:30 +0000469 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000470 }
471 else
472 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000473 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000474 }
475 return filter_sp;
476}
477
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000479Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000480 const FileSpecList *containingSourceFiles,
481 RegularExpression &func_regex,
482 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000483 bool internal,
484 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485{
Jim Ingham87df91b2011-09-23 00:54:11 +0000486 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17 +0000487 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
488 func_regex,
489 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490
Greg Claytoneb023e72013-10-11 19:48:25 +0000491 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492}
493
Jim Ingham219ba192012-03-05 04:47:34 +0000494lldb::BreakpointSP
495Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
496{
497 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
498}
499
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000501Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502{
503 BreakpointSP bp_sp;
504 if (filter_sp && resolver_sp)
505 {
Greg Claytoneb023e72013-10-11 19:48:25 +0000506 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507 resolver_sp->SetBreakpoint (bp_sp.get());
508
509 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000510 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 else
Greg Clayton9fed0d82010-07-23 23:33:17 +0000512 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513
Greg Clayton5160ce52013-03-27 23:08:40 +0000514 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515 if (log)
516 {
517 StreamString s;
518 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
519 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
520 }
521
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 bp_sp->ResolveBreakpoint();
523 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000524
525 if (!internal && bp_sp)
526 {
527 m_last_created_breakpoint = bp_sp;
528 }
529
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 return bp_sp;
531}
532
Johnny Chen86364b42011-09-20 23:28:55 +0000533bool
534Target::ProcessIsValid()
535{
536 return (m_process_sp && m_process_sp->IsAlive());
537}
538
Johnny Chenb90827e2012-06-04 23:19:54 +0000539static bool
540CheckIfWatchpointsExhausted(Target *target, Error &error)
541{
542 uint32_t num_supported_hardware_watchpoints;
543 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
544 if (rc.Success())
545 {
546 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
547 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
548 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
549 num_supported_hardware_watchpoints);
550 }
551 return false;
552}
553
Johnny Chen01a67862011-10-14 00:42:25 +0000554// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000555// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000556WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000557Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000558{
Greg Clayton5160ce52013-03-27 23:08:40 +0000559 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000560 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000561 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000562 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000563
Johnny Chen01a67862011-10-14 00:42:25 +0000564 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000565 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000566 {
567 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000568 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000569 }
Jim Inghamc6462312013-06-18 21:52:48 +0000570
Johnny Chen45e541f2011-09-14 22:20:15 +0000571 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000572 {
573 if (size == 0)
574 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
575 else
Daniel Malead01b2952012-11-29 21:49:15 +0000576 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000577 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000578 }
Jim Inghamc6462312013-06-18 21:52:48 +0000579
580 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
581 {
582 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
583 }
Johnny Chen7313a642011-09-13 01:15:36 +0000584
Johnny Chen01a67862011-10-14 00:42:25 +0000585 // Currently we only support one watchpoint per address, with total number
586 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000587
588 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000589 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 +0000590 Mutex::Locker locker;
591 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000592 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000593 if (matched_sp)
594 {
Johnny Chen0c406372011-09-14 20:23:45 +0000595 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000596 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000597 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
598 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000599 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000600 if (size == old_size && kind == old_type)
601 {
Johnny Chen01a67862011-10-14 00:42:25 +0000602 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000603 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000604 }
605 else
606 {
Johnny Chen01a67862011-10-14 00:42:25 +0000607 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000608 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
609 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000610 }
Johnny Chen3c532582011-09-13 23:29:31 +0000611 }
612
Jason Molenda727e3922012-12-05 23:07:34 +0000613 if (!wp_sp)
614 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000615 wp_sp.reset(new Watchpoint(*this, addr, size, type));
616 wp_sp->SetWatchpointType(kind, notify);
617 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000618 }
Johnny Chen0c406372011-09-14 20:23:45 +0000619
Jim Ingham1b5792e2012-12-18 02:03:49 +0000620 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000621 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000622 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
623 __FUNCTION__,
624 error.Success() ? "succeeded" : "failed",
625 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000626
Jason Molenda727e3922012-12-05 23:07:34 +0000627 if (error.Fail())
628 {
Johnny Chen41b77262012-03-26 22:00:10 +0000629 // Enabling the watchpoint on the device side failed.
630 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000631 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000632 // See if we could provide more helpful error message.
633 if (!CheckIfWatchpointsExhausted(this, error))
634 {
635 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Sylvestre Ledru779f9212013-10-31 23:55:19 +0000636 error.SetErrorStringWithFormat("watch size of %zu is not supported", size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000637 }
Johnny Chen01a67862011-10-14 00:42:25 +0000638 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000639 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000640 else
Johnny Chen01a67862011-10-14 00:42:25 +0000641 m_last_created_watchpoint = wp_sp;
642 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000643}
644
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645void
646Target::RemoveAllBreakpoints (bool internal_also)
647{
Greg Clayton5160ce52013-03-27 23:08:40 +0000648 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649 if (log)
650 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
651
Greg Clayton9fed0d82010-07-23 23:33:17 +0000652 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000654 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000655
656 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657}
658
659void
660Target::DisableAllBreakpoints (bool internal_also)
661{
Greg Clayton5160ce52013-03-27 23:08:40 +0000662 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 if (log)
664 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
665
666 m_breakpoint_list.SetEnabledAll (false);
667 if (internal_also)
668 m_internal_breakpoint_list.SetEnabledAll (false);
669}
670
671void
672Target::EnableAllBreakpoints (bool internal_also)
673{
Greg Clayton5160ce52013-03-27 23:08:40 +0000674 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 if (log)
676 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
677
678 m_breakpoint_list.SetEnabledAll (true);
679 if (internal_also)
680 m_internal_breakpoint_list.SetEnabledAll (true);
681}
682
683bool
684Target::RemoveBreakpointByID (break_id_t break_id)
685{
Greg Clayton5160ce52013-03-27 23:08:40 +0000686 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 if (log)
688 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
689
690 if (DisableBreakpointByID (break_id))
691 {
692 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000693 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000695 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000696 if (m_last_created_breakpoint)
697 {
698 if (m_last_created_breakpoint->GetID() == break_id)
699 m_last_created_breakpoint.reset();
700 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000701 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000702 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 return true;
704 }
705 return false;
706}
707
708bool
709Target::DisableBreakpointByID (break_id_t break_id)
710{
Greg Clayton5160ce52013-03-27 23:08:40 +0000711 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 if (log)
713 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
714
715 BreakpointSP bp_sp;
716
717 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
718 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
719 else
720 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
721 if (bp_sp)
722 {
723 bp_sp->SetEnabled (false);
724 return true;
725 }
726 return false;
727}
728
729bool
730Target::EnableBreakpointByID (break_id_t break_id)
731{
Greg Clayton5160ce52013-03-27 23:08:40 +0000732 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 if (log)
734 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
735 __FUNCTION__,
736 break_id,
737 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
738
739 BreakpointSP bp_sp;
740
741 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
742 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
743 else
744 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
745
746 if (bp_sp)
747 {
748 bp_sp->SetEnabled (true);
749 return true;
750 }
751 return false;
752}
753
Johnny Chenedf50372011-09-23 21:21:43 +0000754// The flag 'end_to_end', default to true, signifies that the operation is
755// performed end to end, for both the debugger and the debuggee.
756
Johnny Chen01a67862011-10-14 00:42:25 +0000757// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
758// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000759bool
Johnny Chen01a67862011-10-14 00:42:25 +0000760Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000761{
Greg Clayton5160ce52013-03-27 23:08:40 +0000762 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000763 if (log)
764 log->Printf ("Target::%s\n", __FUNCTION__);
765
Johnny Chenedf50372011-09-23 21:21:43 +0000766 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000767 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000768 return true;
769 }
770
771 // Otherwise, it's an end to end operation.
772
Johnny Chen86364b42011-09-20 23:28:55 +0000773 if (!ProcessIsValid())
774 return false;
775
Johnny Chen01a67862011-10-14 00:42:25 +0000776 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000777 for (size_t i = 0; i < num_watchpoints; ++i)
778 {
Johnny Chen01a67862011-10-14 00:42:25 +0000779 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
780 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000781 return false;
782
Johnny Chen01a67862011-10-14 00:42:25 +0000783 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000784 if (rc.Fail())
785 return false;
786 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000787 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000788 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000789 return true; // Success!
790}
791
Johnny Chen01a67862011-10-14 00:42:25 +0000792// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
793// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000794bool
Johnny Chen01a67862011-10-14 00:42:25 +0000795Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000796{
Greg Clayton5160ce52013-03-27 23:08:40 +0000797 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000798 if (log)
799 log->Printf ("Target::%s\n", __FUNCTION__);
800
Johnny Chenedf50372011-09-23 21:21:43 +0000801 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000802 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000803 return true;
804 }
805
806 // Otherwise, it's an end to end operation.
807
Johnny Chen86364b42011-09-20 23:28:55 +0000808 if (!ProcessIsValid())
809 return false;
810
Johnny Chen01a67862011-10-14 00:42:25 +0000811 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000812 for (size_t i = 0; i < num_watchpoints; ++i)
813 {
Johnny Chen01a67862011-10-14 00:42:25 +0000814 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
815 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000816 return false;
817
Johnny Chen01a67862011-10-14 00:42:25 +0000818 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000819 if (rc.Fail())
820 return false;
821 }
Johnny Chen86364b42011-09-20 23:28:55 +0000822 return true; // Success!
823}
824
Johnny Chen01a67862011-10-14 00:42:25 +0000825// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
826// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000827bool
Johnny Chen01a67862011-10-14 00:42:25 +0000828Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000829{
Greg Clayton5160ce52013-03-27 23:08:40 +0000830 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000831 if (log)
832 log->Printf ("Target::%s\n", __FUNCTION__);
833
Johnny Chenedf50372011-09-23 21:21:43 +0000834 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000835 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000836 return true;
837 }
838
839 // Otherwise, it's an end to end operation.
840
Johnny Chen86364b42011-09-20 23:28:55 +0000841 if (!ProcessIsValid())
842 return false;
843
Johnny Chen01a67862011-10-14 00:42:25 +0000844 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000845 for (size_t i = 0; i < num_watchpoints; ++i)
846 {
Johnny Chen01a67862011-10-14 00:42:25 +0000847 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
848 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000849 return false;
850
Johnny Chen01a67862011-10-14 00:42:25 +0000851 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000852 if (rc.Fail())
853 return false;
854 }
Johnny Chen86364b42011-09-20 23:28:55 +0000855 return true; // Success!
856}
857
Johnny Chena4d6bc92012-02-25 06:44:30 +0000858// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
859bool
860Target::ClearAllWatchpointHitCounts ()
861{
Greg Clayton5160ce52013-03-27 23:08:40 +0000862 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000863 if (log)
864 log->Printf ("Target::%s\n", __FUNCTION__);
865
866 size_t num_watchpoints = m_watchpoint_list.GetSize();
867 for (size_t i = 0; i < num_watchpoints; ++i)
868 {
869 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
870 if (!wp_sp)
871 return false;
872
873 wp_sp->ResetHitCount();
874 }
875 return true; // Success!
876}
877
Johnny Chen01a67862011-10-14 00:42:25 +0000878// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000879// during these operations.
880bool
Johnny Chen01a67862011-10-14 00:42:25 +0000881Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000882{
Greg Clayton5160ce52013-03-27 23:08:40 +0000883 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000884 if (log)
885 log->Printf ("Target::%s\n", __FUNCTION__);
886
887 if (!ProcessIsValid())
888 return false;
889
Johnny Chen01a67862011-10-14 00:42:25 +0000890 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000891 for (size_t i = 0; i < num_watchpoints; ++i)
892 {
Johnny Chen01a67862011-10-14 00:42:25 +0000893 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
894 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000895 return false;
896
Johnny Chen01a67862011-10-14 00:42:25 +0000897 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000898 }
899 return true; // Success!
900}
901
Johnny Chen01a67862011-10-14 00:42:25 +0000902// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000903bool
Johnny Chen01a67862011-10-14 00:42:25 +0000904Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000905{
Greg Clayton5160ce52013-03-27 23:08:40 +0000906 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000907 if (log)
908 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
909
910 if (!ProcessIsValid())
911 return false;
912
Johnny Chen01a67862011-10-14 00:42:25 +0000913 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
914 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000915 {
Johnny Chen01a67862011-10-14 00:42:25 +0000916 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000917 if (rc.Success())
918 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000919
Johnny Chenf04ee932011-09-22 18:04:58 +0000920 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000921 }
922 return false;
923}
924
Johnny Chen01a67862011-10-14 00:42:25 +0000925// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000926bool
Johnny Chen01a67862011-10-14 00:42:25 +0000927Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000928{
Greg Clayton5160ce52013-03-27 23:08:40 +0000929 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000930 if (log)
931 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
932
933 if (!ProcessIsValid())
934 return false;
935
Johnny Chen01a67862011-10-14 00:42:25 +0000936 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
937 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000938 {
Johnny Chen01a67862011-10-14 00:42:25 +0000939 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000940 if (rc.Success())
941 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000942
Johnny Chenf04ee932011-09-22 18:04:58 +0000943 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000944 }
945 return false;
946}
947
Johnny Chen01a67862011-10-14 00:42:25 +0000948// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000949bool
Johnny Chen01a67862011-10-14 00:42:25 +0000950Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000951{
Greg Clayton5160ce52013-03-27 23:08:40 +0000952 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000953 if (log)
954 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
955
Jim Inghamb0b45132013-07-02 02:09:46 +0000956 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
957 if (watch_to_remove_sp == m_last_created_watchpoint)
958 m_last_created_watchpoint.reset();
959
Johnny Chen01a67862011-10-14 00:42:25 +0000960 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +0000961 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000962 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +0000963 return true;
964 }
965 return false;
966}
967
Johnny Chen01a67862011-10-14 00:42:25 +0000968// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +0000969bool
Johnny Chen01a67862011-10-14 00:42:25 +0000970Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000971{
Greg Clayton5160ce52013-03-27 23:08:40 +0000972 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000973 if (log)
974 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
975
976 if (!ProcessIsValid())
977 return false;
978
Johnny Chen01a67862011-10-14 00:42:25 +0000979 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
980 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000981 {
Johnny Chen01a67862011-10-14 00:42:25 +0000982 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000983 return true;
984 }
985 return false;
986}
987
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988ModuleSP
989Target::GetExecutableModule ()
990{
Greg Claytonaa149cb2011-08-11 02:48:45 +0000991 return m_images.GetModuleAtIndex(0);
992}
993
994Module*
995Target::GetExecutableModulePointer ()
996{
997 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998}
999
Enrico Granata17598482012-11-08 02:22:02 +00001000static void
1001LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1002{
1003 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001004 StreamString feedback_stream;
1005 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001006 {
Enrico Granata97303392013-05-21 00:00:30 +00001007 if (error.AsCString())
1008 target->GetDebugger().GetErrorStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
1009 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1010 error.AsCString());
1011 if (feedback_stream.GetSize())
1012 target->GetDebugger().GetOutputStream().Printf("%s\n",
1013 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001014 }
1015}
1016
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017void
Greg Claytonb35db632013-11-09 00:03:31 +00001018Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019{
Greg Claytonb35db632013-11-09 00:03:31 +00001020 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001021 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 m_images.Clear();
1023 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001024 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001025 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001026}
1027
1028void
Greg Claytonb35db632013-11-09 00:03:31 +00001029Target::DidExec ()
1030{
1031 // When a process exec's we need to know about it so we can do some cleanup.
1032 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1033 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1034}
1035
1036void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001037Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1038{
1039 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001040 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041
1042 if (executable_sp.get())
1043 {
1044 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001045 "Target::SetExecutableModule (executable = '%s')",
1046 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047
1048 m_images.Append(executable_sp); // The first image is our exectuable file
1049
Jim Ingham5aee1622010-08-09 23:31:02 +00001050 // 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 +00001051 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001052 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001053 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001054 if (log)
1055 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1056 }
1057
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001059 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001061 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062 {
1063 executable_objfile->GetDependentModules(dependent_files);
1064 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1065 {
Greg Claytonded470d2011-03-19 01:12:21 +00001066 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1067 FileSpec platform_dependent_file_spec;
1068 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55 +00001069 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001070 else
1071 platform_dependent_file_spec = dependent_file_spec;
1072
Greg Claytonb9a01b32012-02-26 05:51:37 +00001073 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1074 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 if (image_module_sp.get())
1076 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077 ObjectFile *objfile = image_module_sp->GetObjectFile();
1078 if (objfile)
1079 objfile->GetDependentModules(dependent_files);
1080 }
1081 }
1082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 }
1084}
1085
1086
Jim Ingham5aee1622010-08-09 23:31:02 +00001087bool
1088Target::SetArchitecture (const ArchSpec &arch_spec)
1089{
Greg Clayton5160ce52013-03-27 23:08:40 +00001090 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001091 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001092 {
Greg Clayton70512312012-05-08 01:45:38 +00001093 // If we haven't got a valid arch spec, or the architectures are
1094 // compatible, so just update the architecture. Architectures can be
1095 // equal, yet the triple OS and vendor might change, so we need to do
1096 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001097 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001098 if (log)
1099 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 +00001100 return true;
1101 }
1102 else
1103 {
1104 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001105 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001106 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 +00001107 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001108 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001109
Greg Claytonb35db632013-11-09 00:03:31 +00001110 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001111 // Need to do something about unsetting breakpoints.
1112
1113 if (executable_sp)
1114 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001115 if (log)
1116 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 +00001117 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1118 Error error = ModuleList::GetSharedModule (module_spec,
1119 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001120 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001121 NULL,
1122 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001123
1124 if (!error.Fail() && executable_sp)
1125 {
1126 SetExecutableModule (executable_sp, true);
1127 return true;
1128 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001129 }
1130 }
Greg Clayton70512312012-05-08 01:45:38 +00001131 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001132}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134void
Enrico Granataefe637d2012-11-08 19:16:03 +00001135Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001136{
1137}
1138
1139void
Enrico Granataefe637d2012-11-08 19:16:03 +00001140Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141{
1142 // A module is being added to this target for the first time
Enrico Granataefe637d2012-11-08 19:16:03 +00001143 ModuleList my_module_list;
1144 my_module_list.Append(module_sp);
Enrico Granata17598482012-11-08 02:22:02 +00001145 LoadScriptingResourceForModule(module_sp, this);
Enrico Granataefe637d2012-11-08 19:16:03 +00001146 ModulesDidLoad (my_module_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147}
1148
1149void
Enrico Granataefe637d2012-11-08 19:16:03 +00001150Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001151{
1152 // A module is being added to this target for the first time
Enrico Granataefe637d2012-11-08 19:16:03 +00001153 ModuleList my_module_list;
1154 my_module_list.Append(module_sp);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001155 ModulesDidUnload (my_module_list, false);
Enrico Granata17598482012-11-08 02:22:02 +00001156}
1157
1158void
Enrico Granataefe637d2012-11-08 19:16:03 +00001159Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160{
Jim Inghame716ae02011-08-03 01:00:06 +00001161 // A module is replacing an already added module
Jim Ingham4a94c912012-05-17 18:38:42 +00001162 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163}
1164
1165void
1166Target::ModulesDidLoad (ModuleList &module_list)
1167{
Enrico Granata17598482012-11-08 02:22:02 +00001168 if (module_list.GetSize())
1169 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001170 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001171 if (m_process_sp)
1172 {
1173 SystemRuntime *sys_runtime = m_process_sp->GetSystemRuntime();
1174 if (sys_runtime)
1175 {
1176 sys_runtime->ModulesDidLoad (module_list);
1177 }
1178 }
Enrico Granata17598482012-11-08 02:22:02 +00001179 // TODO: make event data that packages up the module_list
1180 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1181 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182}
1183
1184void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001185Target::SymbolsDidLoad (ModuleList &module_list)
1186{
Jim Ingham31caf982013-06-04 23:01:35 +00001187 if (module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001188 {
Jim Ingham31caf982013-06-04 23:01:35 +00001189 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001190 {
Jim Ingham31caf982013-06-04 23:01:35 +00001191 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1192 if (runtime)
1193 {
1194 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1195 objc_runtime->SymbolsDidLoad(module_list);
1196 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001197 }
Jim Ingham31caf982013-06-04 23:01:35 +00001198
Greg Clayton095eeaa2013-11-05 23:28:00 +00001199 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jim Ingham31caf982013-06-04 23:01:35 +00001200 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001201 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001202}
1203
1204void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001205Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206{
Enrico Granata17598482012-11-08 02:22:02 +00001207 if (module_list.GetSize())
1208 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001209 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Enrico Granata17598482012-11-08 02:22:02 +00001210 // TODO: make event data that packages up the module_list
1211 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1212 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213}
1214
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001215bool
Greg Claytonb9a01b32012-02-26 05:51:37 +00001216Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001217{
Greg Clayton67cc0632012-08-22 17:17:09 +00001218 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001219 {
1220 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001221 ModuleSpec module_spec (module_file_spec);
1222 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001223
1224 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1225 // black list.
1226 if (num_modules > 0)
1227 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001228 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001229 {
1230 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1231 return false;
1232 }
1233 return true;
1234 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001235 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001236 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001237}
1238
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001239bool
Jim Inghamc6674fd2011-10-28 23:14:11 +00001240Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1241{
Greg Clayton67cc0632012-08-22 17:17:09 +00001242 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001243 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001244 if (m_platform_sp)
1245 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001246 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001247 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001248}
1249
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250size_t
Greg Claytondb598232011-01-07 01:57:07 +00001251Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1252{
Greg Claytone72dfb32012-02-24 01:59:29 +00001253 SectionSP section_sp (addr.GetSection());
1254 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001255 {
Jason Molenda216d91f2012-04-25 00:06:56 +00001256 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1257 if (section_sp->IsEncrypted())
1258 {
Greg Clayton57f06302012-05-25 17:05:55 +00001259 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001260 return 0;
1261 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001262 ModuleSP module_sp (section_sp->GetModule());
1263 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001264 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001265 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1266 if (objfile)
1267 {
1268 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1269 addr.GetOffset(),
1270 dst,
1271 dst_len);
1272 if (bytes_read > 0)
1273 return bytes_read;
1274 else
1275 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1276 }
Greg Claytondb598232011-01-07 01:57:07 +00001277 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001278 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001279 }
1280 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001281 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001282 }
1283 else
Greg Claytondb598232011-01-07 01:57:07 +00001284 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001285
Greg Claytondb598232011-01-07 01:57:07 +00001286 return 0;
1287}
1288
1289size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001290Target::ReadMemory (const Address& addr,
1291 bool prefer_file_cache,
1292 void *dst,
1293 size_t dst_len,
1294 Error &error,
1295 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001298
Enrico Granata9128ee22011-09-06 19:20:51 +00001299 // if we end up reading this from process memory, we will fill this
1300 // with the actual load address
1301 if (load_addr_ptr)
1302 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1303
Greg Claytondb598232011-01-07 01:57:07 +00001304 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001305
1306 addr_t load_addr = LLDB_INVALID_ADDRESS;
1307 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001308 Address resolved_addr;
1309 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001310 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001311 SectionLoadList &section_load_list = GetSectionLoadList();
1312 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001313 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001314 // No sections are loaded, so we must assume we are not running
1315 // yet and anything we are given is a file address.
1316 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1317 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001318 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001319 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001320 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001321 // We have at least one section loaded. This can be becuase
1322 // we have manually loaded some sections with "target modules load ..."
1323 // or because we have have a live process that has sections loaded
1324 // through the dynamic loader
1325 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001326 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001327 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001328 }
Greg Clayton357132e2011-03-26 19:14:58 +00001329 if (!resolved_addr.IsValid())
1330 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001331
Greg Claytonc749eb82011-07-11 05:12:02 +00001332
Greg Claytondb598232011-01-07 01:57:07 +00001333 if (prefer_file_cache)
1334 {
1335 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1336 if (bytes_read > 0)
1337 return bytes_read;
1338 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001339
Johnny Chen86364b42011-09-20 23:28:55 +00001340 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001341 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001342 if (load_addr == LLDB_INVALID_ADDRESS)
1343 load_addr = resolved_addr.GetLoadAddress (this);
1344
Greg Claytondda4f7b2010-06-30 23:03:03 +00001345 if (load_addr == LLDB_INVALID_ADDRESS)
1346 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001347 ModuleSP addr_module_sp (resolved_addr.GetModule());
1348 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001349 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Claytone72dfb32012-02-24 01:59:29 +00001350 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08 +00001351 resolved_addr.GetFileAddress(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001352 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001353 else
Daniel Malead01b2952012-11-29 21:49:15 +00001354 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001355 }
1356 else
1357 {
Greg Claytondb598232011-01-07 01:57:07 +00001358 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359 if (bytes_read != dst_len)
1360 {
1361 if (error.Success())
1362 {
1363 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001364 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 else
Daniel Malead01b2952012-11-29 21:49:15 +00001366 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 +00001367 }
1368 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001369 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001370 {
1371 if (load_addr_ptr)
1372 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001373 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001374 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001375 // If the address is not section offset we have an address that
1376 // doesn't resolve to any address in any currently loaded shared
1377 // libaries and we failed to read memory so there isn't anything
1378 // more we can do. If it is section offset, we might be able to
1379 // read cached memory from the object file.
1380 if (!resolved_addr.IsSectionOffset())
1381 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001382 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001384
Greg Claytonc749eb82011-07-11 05:12:02 +00001385 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001386 {
Greg Claytondb598232011-01-07 01:57:07 +00001387 // If we didn't already try and read from the object file cache, then
1388 // try it after failing to read from the process.
1389 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001390 }
1391 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392}
1393
Greg Claytond16e1e52011-07-12 17:06:17 +00001394size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001395Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1396{
1397 char buf[256];
1398 out_str.clear();
1399 addr_t curr_addr = addr.GetLoadAddress(this);
1400 Address address(addr);
1401 while (1)
1402 {
1403 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1404 if (length == 0)
1405 break;
1406 out_str.append(buf, length);
1407 // If we got "length - 1" bytes, we didn't get the whole C string, we
1408 // need to read some more characters
1409 if (length == sizeof(buf) - 1)
1410 curr_addr += length;
1411 else
1412 break;
1413 address = Address(curr_addr);
1414 }
1415 return out_str.size();
1416}
1417
1418
1419size_t
1420Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1421{
1422 size_t total_cstr_len = 0;
1423 if (dst && dst_max_len)
1424 {
1425 result_error.Clear();
1426 // NULL out everything just to be safe
1427 memset (dst, 0, dst_max_len);
1428 Error error;
1429 addr_t curr_addr = addr.GetLoadAddress(this);
1430 Address address(addr);
1431 const size_t cache_line_size = 512;
1432 size_t bytes_left = dst_max_len - 1;
1433 char *curr_dst = dst;
1434
1435 while (bytes_left > 0)
1436 {
1437 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1438 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1439 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1440
1441 if (bytes_read == 0)
1442 {
1443 result_error = error;
1444 dst[total_cstr_len] = '\0';
1445 break;
1446 }
1447 const size_t len = strlen(curr_dst);
1448
1449 total_cstr_len += len;
1450
1451 if (len < bytes_to_read)
1452 break;
1453
1454 curr_dst += bytes_read;
1455 curr_addr += bytes_read;
1456 bytes_left -= bytes_read;
1457 address = Address(curr_addr);
1458 }
1459 }
1460 else
1461 {
1462 if (dst == NULL)
1463 result_error.SetErrorString("invalid arguments");
1464 else
1465 result_error.Clear();
1466 }
1467 return total_cstr_len;
1468}
1469
1470size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001471Target::ReadScalarIntegerFromMemory (const Address& addr,
1472 bool prefer_file_cache,
1473 uint32_t byte_size,
1474 bool is_signed,
1475 Scalar &scalar,
1476 Error &error)
1477{
1478 uint64_t uval;
1479
1480 if (byte_size <= sizeof(uval))
1481 {
1482 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1483 if (bytes_read == byte_size)
1484 {
1485 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001486 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001487 if (byte_size <= 4)
1488 scalar = data.GetMaxU32 (&offset, byte_size);
1489 else
1490 scalar = data.GetMaxU64 (&offset, byte_size);
1491
1492 if (is_signed)
1493 scalar.SignExtend(byte_size * 8);
1494 return bytes_read;
1495 }
1496 }
1497 else
1498 {
1499 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1500 }
1501 return 0;
1502}
1503
1504uint64_t
1505Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1506 bool prefer_file_cache,
1507 size_t integer_byte_size,
1508 uint64_t fail_value,
1509 Error &error)
1510{
1511 Scalar scalar;
1512 if (ReadScalarIntegerFromMemory (addr,
1513 prefer_file_cache,
1514 integer_byte_size,
1515 false,
1516 scalar,
1517 error))
1518 return scalar.ULongLong(fail_value);
1519 return fail_value;
1520}
1521
1522bool
1523Target::ReadPointerFromMemory (const Address& addr,
1524 bool prefer_file_cache,
1525 Error &error,
1526 Address &pointer_addr)
1527{
1528 Scalar scalar;
1529 if (ReadScalarIntegerFromMemory (addr,
1530 prefer_file_cache,
1531 m_arch.GetAddressByteSize(),
1532 false,
1533 scalar,
1534 error))
1535 {
1536 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1537 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1538 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001539 SectionLoadList &section_load_list = GetSectionLoadList();
1540 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001541 {
1542 // No sections are loaded, so we must assume we are not running
1543 // yet and anything we are given is a file address.
1544 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1545 }
1546 else
1547 {
1548 // We have at least one section loaded. This can be becuase
1549 // we have manually loaded some sections with "target modules load ..."
1550 // or because we have have a live process that has sections loaded
1551 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001552 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001553 }
1554 // We weren't able to resolve the pointer value, so just return
1555 // an address with no section
1556 if (!pointer_addr.IsValid())
1557 pointer_addr.SetOffset (pointer_vm_addr);
1558 return true;
1559
1560 }
1561 }
1562 return false;
1563}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564
1565ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001566Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568 ModuleSP module_sp;
1569
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570 Error error;
1571
Jim Ingham4a94c912012-05-17 18:38:42 +00001572 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1573 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001574
Jim Ingham4a94c912012-05-17 18:38:42 +00001575 if (module_spec.GetUUID().IsValid())
1576 module_sp = m_images.FindFirstModule(module_spec);
1577
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001578 if (!module_sp)
1579 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001580 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1581 bool did_create_module = false;
1582
1583 // If there are image search path entries, try to use them first to acquire a suitable image.
1584 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001585 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001586 ModuleSpec transformed_spec (module_spec);
1587 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1588 {
1589 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1590 error = ModuleList::GetSharedModule (transformed_spec,
1591 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001592 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001593 &old_module_sp,
1594 &did_create_module);
1595 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001596 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001597
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001598 if (!module_sp)
1599 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001600 // If we have a UUID, we can check our global shared module list in case
1601 // we already have it. If we don't have a valid UUID, then we can't since
1602 // the path in "module_spec" will be a platform path, and we will need to
1603 // let the platform find that file. For example, we could be asking for
1604 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1605 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1606 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1607 // cache.
1608 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001609 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001610 // We have a UUID, it is OK to check the global module list...
1611 error = ModuleList::GetSharedModule (module_spec,
1612 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001613 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001614 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001615 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001616 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001617
1618 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001619 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001620 // The platform is responsible for finding and caching an appropriate
1621 // module in the shared module cache.
1622 if (m_platform_sp)
1623 {
1624 FileSpec platform_file_spec;
1625 error = m_platform_sp->GetSharedModule (module_spec,
1626 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001627 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001628 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001629 &did_create_module);
1630 }
1631 else
1632 {
1633 error.SetErrorString("no platform is currently set");
1634 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001635 }
1636 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637
Jim Ingham4a94c912012-05-17 18:38:42 +00001638 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1639 // module in the list already, and if there was, let's remove it.
1640 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001641 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001642 ObjectFile *objfile = module_sp->GetObjectFile();
1643 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001644 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001645 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001646 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001647 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1648 case ObjectFile::eTypeExecutable: /// A normal executable
1649 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1650 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1651 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1652 break;
1653 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1654 if (error_ptr)
1655 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1656 return ModuleSP();
1657 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1658 if (error_ptr)
1659 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1660 return ModuleSP();
1661 default:
1662 if (error_ptr)
1663 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1664 return ModuleSP();
1665 }
1666 // GetSharedModule is not guaranteed to find the old shared module, for instance
1667 // in the common case where you pass in the UUID, it is only going to find the one
1668 // module matching the UUID. In fact, it has no good way to know what the "old module"
1669 // relevant to this target is, since there might be many copies of a module with this file spec
1670 // in various running debug sessions, but only one of them will belong to this target.
1671 // So let's remove the UUID from the module list, and look in the target's module list.
1672 // Only do this if there is SOMETHING else in the module spec...
1673 if (!old_module_sp)
1674 {
1675 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001676 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001677 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1678 module_spec_copy.GetUUID().Clear();
1679
1680 ModuleList found_modules;
1681 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1682 if (num_found == 1)
1683 {
1684 old_module_sp = found_modules.GetModuleAtIndex(0);
1685 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001686 }
1687 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001688
1689 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1690 {
1691 m_images.ReplaceModule(old_module_sp, module_sp);
1692 Module *old_module_ptr = old_module_sp.get();
1693 old_module_sp.reset();
1694 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1695 }
1696 else
1697 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001698 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 }
1700 }
1701 if (error_ptr)
1702 *error_ptr = error;
1703 return module_sp;
1704}
1705
1706
Greg Claytond9e416c2012-02-18 05:35:26 +00001707TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001708Target::CalculateTarget ()
1709{
Greg Claytond9e416c2012-02-18 05:35:26 +00001710 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001711}
1712
Greg Claytond9e416c2012-02-18 05:35:26 +00001713ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001714Target::CalculateProcess ()
1715{
Greg Claytond9e416c2012-02-18 05:35:26 +00001716 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001717}
1718
Greg Claytond9e416c2012-02-18 05:35:26 +00001719ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720Target::CalculateThread ()
1721{
Greg Claytond9e416c2012-02-18 05:35:26 +00001722 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723}
1724
Jason Molendab57e4a12013-11-04 09:33:30 +00001725StackFrameSP
1726Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727{
Jason Molendab57e4a12013-11-04 09:33:30 +00001728 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729}
1730
1731void
Greg Clayton0603aa92010-10-04 01:05:56 +00001732Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733{
Greg Claytonc14ee322011-09-22 04:58:26 +00001734 exe_ctx.Clear();
1735 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736}
1737
1738PathMappingList &
1739Target::GetImageSearchPathList ()
1740{
1741 return m_image_search_paths;
1742}
1743
1744void
1745Target::ImageSearchPathsChanged
1746(
1747 const PathMappingList &path_list,
1748 void *baton
1749)
1750{
1751 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001752 ModuleSP exe_module_sp (target->GetExecutableModule());
1753 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001754 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001755}
1756
1757ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001758Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001759{
Greg Clayton73da2442011-08-03 01:23:55 +00001760 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001761 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001762 {
Greg Clayton73da2442011-08-03 01:23:55 +00001763 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001764 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001765 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1766 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1767 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1768 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769 return m_scratch_ast_context_ap.get();
1770}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001771
Sean Callanan686b2312011-11-16 18:20:47 +00001772ClangASTImporter *
1773Target::GetClangASTImporter()
1774{
1775 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1776
1777 if (!ast_importer)
1778 {
1779 ast_importer = new ClangASTImporter();
1780 m_ast_importer_ap.reset(ast_importer);
1781 }
1782
1783 return ast_importer;
1784}
1785
Greg Clayton99d0faf2010-11-18 23:32:35 +00001786void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001787Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001788{
Greg Clayton6920b522012-08-22 18:39:03 +00001789 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001790}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001791
Greg Clayton99d0faf2010-11-18 23:32:35 +00001792void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001793Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001794{
Greg Clayton6920b522012-08-22 18:39:03 +00001795 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001796}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001797
Greg Claytonc859e2d2012-02-13 23:10:39 +00001798FileSpecList
1799Target::GetDefaultExecutableSearchPaths ()
1800{
Greg Clayton67cc0632012-08-22 17:17:09 +00001801 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1802 if (properties_sp)
1803 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001804 return FileSpecList();
1805}
1806
Michael Sartaina7499c92013-07-01 19:45:50 +00001807FileSpecList
1808Target::GetDefaultDebugFileSearchPaths ()
1809{
1810 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1811 if (properties_sp)
1812 return properties_sp->GetDebugFileSearchPaths();
1813 return FileSpecList();
1814}
1815
Caroline Ticedaccaa92010-09-20 20:44:43 +00001816ArchSpec
1817Target::GetDefaultArchitecture ()
1818{
Greg Clayton67cc0632012-08-22 17:17:09 +00001819 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1820 if (properties_sp)
1821 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001822 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001823}
1824
1825void
Greg Clayton67cc0632012-08-22 17:17:09 +00001826Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001827{
Greg Clayton67cc0632012-08-22 17:17:09 +00001828 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1829 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001830 {
1831 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 +00001832 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001833 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001834}
1835
Greg Clayton0603aa92010-10-04 01:05:56 +00001836Target *
1837Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1838{
1839 // The target can either exist in the "process" of ExecutionContext, or in
1840 // the "target_sp" member of SymbolContext. This accessor helper function
1841 // will get the target from one of these locations.
1842
1843 Target *target = NULL;
1844 if (sc_ptr != NULL)
1845 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001846 if (target == NULL && exe_ctx_ptr)
1847 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001848 return target;
1849}
1850
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001851ExecutionResults
1852Target::EvaluateExpression
1853(
1854 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001855 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001856 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001857 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001858)
1859{
Enrico Granata97fca502012-09-18 17:43:16 +00001860 result_valobj_sp.reset();
1861
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001862 ExecutionResults execution_results = eExecutionSetupError;
1863
Greg Claytond1767f02011-12-08 02:13:16 +00001864 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1865 return execution_results;
1866
Jim Ingham6026ca32011-05-12 02:06:14 +00001867 // We shouldn't run stop hooks in expressions.
1868 // Be sure to reset this if you return anywhere within this function.
1869 bool old_suppress_value = m_suppress_stop_hooks;
1870 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001871
1872 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001873
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001874 if (frame)
1875 {
1876 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001877 }
1878 else if (m_process_sp)
1879 {
1880 m_process_sp->CalculateExecutionContext(exe_ctx);
1881 }
1882 else
1883 {
1884 CalculateExecutionContext(exe_ctx);
1885 }
1886
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001887 // Make sure we aren't just trying to see the value of a persistent
1888 // variable (something like "$0")
1889 lldb::ClangExpressionVariableSP persistent_var_sp;
1890 // Only check for persistent variables the expression starts with a '$'
1891 if (expr_cstr[0] == '$')
1892 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1893
1894 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001895 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001896 result_valobj_sp = persistent_var_sp->GetValueObject ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001897 execution_results = eExecutionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001898 }
1899 else
1900 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001901 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00001902 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001903 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001904 options,
1905 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001906 prefix,
1907 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001908 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001909 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001910
1911 m_suppress_stop_hooks = old_suppress_value;
1912
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001913 return execution_results;
1914}
1915
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001916lldb::addr_t
1917Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1918{
1919 addr_t code_addr = load_addr;
1920 switch (m_arch.GetMachine())
1921 {
1922 case llvm::Triple::arm:
1923 case llvm::Triple::thumb:
1924 switch (addr_class)
1925 {
1926 case eAddressClassData:
1927 case eAddressClassDebug:
1928 return LLDB_INVALID_ADDRESS;
1929
1930 case eAddressClassUnknown:
1931 case eAddressClassInvalid:
1932 case eAddressClassCode:
1933 case eAddressClassCodeAlternateISA:
1934 case eAddressClassRuntime:
1935 // Check if bit zero it no set?
1936 if ((code_addr & 1ull) == 0)
1937 {
1938 // Bit zero isn't set, check if the address is a multiple of 2?
1939 if (code_addr & 2ull)
1940 {
1941 // The address is a multiple of 2 so it must be thumb, set bit zero
1942 code_addr |= 1ull;
1943 }
1944 else if (addr_class == eAddressClassCodeAlternateISA)
1945 {
1946 // We checked the address and the address claims to be the alternate ISA
1947 // which means thumb, so set bit zero.
1948 code_addr |= 1ull;
1949 }
1950 }
1951 break;
1952 }
1953 break;
1954
1955 default:
1956 break;
1957 }
1958 return code_addr;
1959}
1960
1961lldb::addr_t
1962Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1963{
1964 addr_t opcode_addr = load_addr;
1965 switch (m_arch.GetMachine())
1966 {
1967 case llvm::Triple::arm:
1968 case llvm::Triple::thumb:
1969 switch (addr_class)
1970 {
1971 case eAddressClassData:
1972 case eAddressClassDebug:
1973 return LLDB_INVALID_ADDRESS;
1974
1975 case eAddressClassInvalid:
1976 case eAddressClassUnknown:
1977 case eAddressClassCode:
1978 case eAddressClassCodeAlternateISA:
1979 case eAddressClassRuntime:
1980 opcode_addr &= ~(1ull);
1981 break;
1982 }
1983 break;
1984
1985 default:
1986 break;
1987 }
1988 return opcode_addr;
1989}
1990
Greg Clayton9585fbf2013-03-19 00:20:55 +00001991SourceManager &
1992Target::GetSourceManager ()
1993{
1994 if (m_source_manager_ap.get() == NULL)
1995 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
1996 return *m_source_manager_ap;
1997}
1998
1999
Jim Ingham9575d842011-03-11 03:53:59 +00002000lldb::user_id_t
2001Target::AddStopHook (Target::StopHookSP &new_hook_sp)
2002{
2003 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Claytone1cd1be2012-01-29 20:56:30 +00002004 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Ingham9575d842011-03-11 03:53:59 +00002005 m_stop_hooks[new_uid] = new_hook_sp;
2006 return new_uid;
2007}
2008
2009bool
2010Target::RemoveStopHookByID (lldb::user_id_t user_id)
2011{
2012 size_t num_removed;
2013 num_removed = m_stop_hooks.erase (user_id);
2014 if (num_removed == 0)
2015 return false;
2016 else
2017 return true;
2018}
2019
2020void
2021Target::RemoveAllStopHooks ()
2022{
2023 m_stop_hooks.clear();
2024}
2025
2026Target::StopHookSP
2027Target::GetStopHookByID (lldb::user_id_t user_id)
2028{
2029 StopHookSP found_hook;
2030
2031 StopHookCollection::iterator specified_hook_iter;
2032 specified_hook_iter = m_stop_hooks.find (user_id);
2033 if (specified_hook_iter != m_stop_hooks.end())
2034 found_hook = (*specified_hook_iter).second;
2035 return found_hook;
2036}
2037
2038bool
2039Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2040{
2041 StopHookCollection::iterator specified_hook_iter;
2042 specified_hook_iter = m_stop_hooks.find (user_id);
2043 if (specified_hook_iter == m_stop_hooks.end())
2044 return false;
2045
2046 (*specified_hook_iter).second->SetIsActive (active_state);
2047 return true;
2048}
2049
2050void
2051Target::SetAllStopHooksActiveState (bool active_state)
2052{
2053 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2054 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2055 {
2056 (*pos).second->SetIsActive (active_state);
2057 }
2058}
2059
2060void
2061Target::RunStopHooks ()
2062{
Jim Ingham6026ca32011-05-12 02:06:14 +00002063 if (m_suppress_stop_hooks)
2064 return;
2065
Jim Ingham9575d842011-03-11 03:53:59 +00002066 if (!m_process_sp)
2067 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002068
2069 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2070 // since in that case we do not want to run the stop-hooks
2071 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2072 return;
2073
Jim Ingham9575d842011-03-11 03:53:59 +00002074 if (m_stop_hooks.empty())
2075 return;
2076
2077 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2078
2079 // If there aren't any active stop hooks, don't bother either:
2080 bool any_active_hooks = false;
2081 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2082 {
2083 if ((*pos).second->IsActive())
2084 {
2085 any_active_hooks = true;
2086 break;
2087 }
2088 }
2089 if (!any_active_hooks)
2090 return;
2091
2092 CommandReturnObject result;
2093
2094 std::vector<ExecutionContext> exc_ctx_with_reasons;
2095 std::vector<SymbolContext> sym_ctx_with_reasons;
2096
2097 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2098 size_t num_threads = cur_threadlist.GetSize();
2099 for (size_t i = 0; i < num_threads; i++)
2100 {
2101 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2102 if (cur_thread_sp->ThreadStoppedForAReason())
2103 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002104 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002105 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2106 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2107 }
2108 }
2109
2110 // If no threads stopped for a reason, don't run the stop-hooks.
2111 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2112 if (num_exe_ctx == 0)
2113 return;
2114
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002115 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2116 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002117
2118 bool keep_going = true;
2119 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002120 bool print_hook_header;
2121 bool print_thread_header;
2122
2123 if (num_exe_ctx == 1)
2124 print_thread_header = false;
2125 else
2126 print_thread_header = true;
2127
2128 if (m_stop_hooks.size() == 1)
2129 print_hook_header = false;
2130 else
2131 print_hook_header = true;
2132
Jim Ingham9575d842011-03-11 03:53:59 +00002133 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2134 {
2135 // result.Clear();
2136 StopHookSP cur_hook_sp = (*pos).second;
2137 if (!cur_hook_sp->IsActive())
2138 continue;
2139
2140 bool any_thread_matched = false;
2141 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2142 {
2143 if ((cur_hook_sp->GetSpecifier () == NULL
2144 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2145 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002146 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002147 {
2148 if (!hooks_ran)
2149 {
Jim Ingham9575d842011-03-11 03:53:59 +00002150 hooks_ran = true;
2151 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002152 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002153 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002154 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2155 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2156 NULL);
2157 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002158 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002159 else
Daniel Malead01b2952012-11-29 21:49:15 +00002160 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002161 any_thread_matched = true;
2162 }
2163
Jim Ingham381e25b2011-03-22 01:47:27 +00002164 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002165 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:59 +00002166
2167 bool stop_on_continue = true;
2168 bool stop_on_error = true;
2169 bool echo_commands = false;
2170 bool print_results = true;
2171 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:51 +00002172 &exc_ctx_with_reasons[i],
2173 stop_on_continue,
2174 stop_on_error,
2175 echo_commands,
Enrico Granata5f5ab602012-05-31 01:09:06 +00002176 print_results,
2177 eLazyBoolNo,
Greg Clayton32e0a752011-03-30 18:16:51 +00002178 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002179
2180 // If the command started the target going again, we should bag out of
2181 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002182 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2183 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002184 {
Daniel Malead01b2952012-11-29 21:49:15 +00002185 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002186 keep_going = false;
2187 }
2188 }
2189 }
2190 }
Jason Molenda879cf772011-09-23 00:42:55 +00002191
Caroline Tice969ed3d2011-05-02 20:41:46 +00002192 result.GetImmediateOutputStream()->Flush();
2193 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002194}
2195
Greg Claytonfbb76342013-11-20 21:07:01 +00002196const TargetPropertiesSP &
2197Target::GetGlobalProperties()
2198{
2199 static TargetPropertiesSP g_settings_sp;
2200 if (!g_settings_sp)
2201 {
2202 g_settings_sp.reset (new TargetProperties (NULL));
2203 }
2204 return g_settings_sp;
2205}
2206
2207Error
2208Target::Install (ProcessLaunchInfo *launch_info)
2209{
2210 Error error;
2211 PlatformSP platform_sp (GetPlatform());
2212 if (platform_sp)
2213 {
2214 if (platform_sp->IsRemote())
2215 {
2216 if (platform_sp->IsConnected())
2217 {
2218 // Install all files that have an install path, and always install the
2219 // main executable when connected to a remote platform
2220 const ModuleList& modules = GetImages();
2221 const size_t num_images = modules.GetSize();
2222 for (size_t idx = 0; idx < num_images; ++idx)
2223 {
2224 const bool is_main_executable = idx == 0;
2225 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2226 if (module_sp)
2227 {
2228 FileSpec local_file (module_sp->GetFileSpec());
2229 if (local_file)
2230 {
2231 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2232 if (!remote_file)
2233 {
2234 if (is_main_executable) // TODO: add setting for always installing main executable???
2235 {
2236 // Always install the main executable
2237 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2238 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2239 }
2240 }
2241 if (remote_file)
2242 {
2243 error = platform_sp->Install(local_file, remote_file);
2244 if (error.Success())
2245 {
2246 module_sp->SetPlatformFileSpec(remote_file);
2247 if (is_main_executable)
2248 {
2249 if (launch_info)
2250 launch_info->SetExecutableFile(remote_file, false);
2251 }
2252 }
2253 else
2254 break;
2255 }
2256 }
2257 }
2258 }
2259 }
2260 }
2261 }
2262 return error;
2263}
Greg Clayton7b242382011-07-08 00:48:09 +00002264
Greg Claytond5944cd2013-12-06 01:12:00 +00002265bool
2266Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2267{
2268 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2269}
2270
2271bool
2272Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2273{
2274 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2275 if (old_section_load_addr != new_section_load_addr)
2276 {
2277 uint32_t stop_id = 0;
2278 ProcessSP process_sp(GetProcessSP());
2279 if (process_sp)
2280 stop_id = process_sp->GetStopID();
2281 else
2282 stop_id = m_section_load_history.GetLastStopID();
2283 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2284 return true; // Return true if the section load address was changed...
2285 }
2286 return false; // Return false to indicate nothing changed
2287
2288}
2289
2290bool
2291Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2292{
2293 uint32_t stop_id = 0;
2294 ProcessSP process_sp(GetProcessSP());
2295 if (process_sp)
2296 stop_id = process_sp->GetStopID();
2297 else
2298 stop_id = m_section_load_history.GetLastStopID();
2299 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2300}
2301
2302bool
2303Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2304{
2305 uint32_t stop_id = 0;
2306 ProcessSP process_sp(GetProcessSP());
2307 if (process_sp)
2308 stop_id = process_sp->GetStopID();
2309 else
2310 stop_id = m_section_load_history.GetLastStopID();
2311 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2312}
2313
2314void
2315Target::ClearAllLoadedSections ()
2316{
2317 m_section_load_history.Clear();
2318}
2319
Jim Ingham9575d842011-03-11 03:53:59 +00002320//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002321// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002322//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002323Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2324 UserID (uid),
2325 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002326 m_commands (),
2327 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002328 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002329 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002330{
2331}
2332
2333Target::StopHook::StopHook (const StopHook &rhs) :
2334 UserID (rhs.GetID()),
2335 m_target_sp (rhs.m_target_sp),
2336 m_commands (rhs.m_commands),
2337 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002338 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002339 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002340{
2341 if (rhs.m_thread_spec_ap.get() != NULL)
2342 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2343}
2344
2345
2346Target::StopHook::~StopHook ()
2347{
2348}
2349
2350void
2351Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2352{
2353 m_thread_spec_ap.reset (specifier);
2354}
2355
2356
2357void
2358Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2359{
2360 int indent_level = s->GetIndentLevel();
2361
2362 s->SetIndentLevel(indent_level + 2);
2363
Daniel Malead01b2952012-11-29 21:49:15 +00002364 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002365 if (m_active)
2366 s->Indent ("State: enabled\n");
2367 else
2368 s->Indent ("State: disabled\n");
2369
2370 if (m_specifier_sp)
2371 {
2372 s->Indent();
2373 s->PutCString ("Specifier:\n");
2374 s->SetIndentLevel (indent_level + 4);
2375 m_specifier_sp->GetDescription (s, level);
2376 s->SetIndentLevel (indent_level + 2);
2377 }
2378
2379 if (m_thread_spec_ap.get() != NULL)
2380 {
2381 StreamString tmp;
2382 s->Indent("Thread:\n");
2383 m_thread_spec_ap->GetDescription (&tmp, level);
2384 s->SetIndentLevel (indent_level + 4);
2385 s->Indent (tmp.GetData());
2386 s->PutCString ("\n");
2387 s->SetIndentLevel (indent_level + 2);
2388 }
2389
2390 s->Indent ("Commands: \n");
2391 s->SetIndentLevel (indent_level + 4);
2392 uint32_t num_commands = m_commands.GetSize();
2393 for (uint32_t i = 0; i < num_commands; i++)
2394 {
2395 s->Indent(m_commands.GetStringAtIndex(i));
2396 s->PutCString ("\n");
2397 }
2398 s->SetIndentLevel (indent_level);
2399}
2400
Greg Clayton67cc0632012-08-22 17:17:09 +00002401//--------------------------------------------------------------
2402// class TargetProperties
2403//--------------------------------------------------------------
2404
2405OptionEnumValueElement
2406lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002407{
Greg Clayton67cc0632012-08-22 17:17:09 +00002408 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2409 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2410 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2411 { 0, NULL, NULL }
2412};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002413
Greg Clayton1f746072012-08-29 21:13:06 +00002414static OptionEnumValueElement
2415g_inline_breakpoint_enums[] =
2416{
2417 { 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."},
2418 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2419 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2420 { 0, NULL, NULL }
2421};
2422
Jim Ingham0f063ba2013-03-02 00:26:47 +00002423typedef enum x86DisassemblyFlavor
2424{
2425 eX86DisFlavorDefault,
2426 eX86DisFlavorIntel,
2427 eX86DisFlavorATT
2428} x86DisassemblyFlavor;
2429
2430static OptionEnumValueElement
2431g_x86_dis_flavor_value_types[] =
2432{
2433 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2434 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2435 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2436 { 0, NULL, NULL }
2437};
2438
Enrico Granata397ddd52013-05-21 20:13:34 +00002439static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002440g_hex_immediate_style_values[] =
2441{
2442 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2443 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2444 { 0, NULL, NULL }
2445};
2446
2447static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002448g_load_script_from_sym_file_values[] =
2449{
2450 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2451 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2452 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2453 { 0, NULL, NULL }
2454};
2455
Greg Claytonfd814c52013-08-13 01:42:25 +00002456
2457static OptionEnumValueElement
2458g_memory_module_load_level_values[] =
2459{
Greg Clayton86eac942013-08-13 21:32:34 +00002460 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002461 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2462 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2463 { 0, NULL, NULL }
2464};
2465
Greg Clayton67cc0632012-08-22 17:17:09 +00002466static PropertyDefinition
2467g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002468{
Greg Clayton67cc0632012-08-22 17:17:09 +00002469 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2470 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2471 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2472 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2473 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2474 { "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 "
2475 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2476 "some part (starting at the root) of the path to the file when it was built, "
2477 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2478 "Each element of the array is checked in order and the first one that results in a match wins." },
2479 { "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 +00002480 { "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 +00002481 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2482 { "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 +00002483 { "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 +00002484 { "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 +00002485 { "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." },
2486 { "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 +00002487 { "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." },
2488 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2489 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2490 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2491 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2492 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2493 { "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 +00002494 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2495 "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. "
2496 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2497 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2498 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2499 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2500 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002501 // 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.
2502 { "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 +00002503 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2504 { "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 +00002505 { "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 +00002506 { "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 +00002507 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2508 "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. "
2509 "This setting helps users control how much information gets loaded when loading modules from memory."
2510 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2511 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2512 "'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 +00002513 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2514};
2515enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002516{
Greg Clayton67cc0632012-08-22 17:17:09 +00002517 ePropertyDefaultArch,
2518 ePropertyExprPrefix,
2519 ePropertyPreferDynamic,
2520 ePropertyEnableSynthetic,
2521 ePropertySkipPrologue,
2522 ePropertySourceMap,
2523 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002524 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002525 ePropertyMaxChildrenCount,
2526 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002527 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002528 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002529 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002530 ePropertyRunArgs,
2531 ePropertyEnvVars,
2532 ePropertyInheritEnv,
2533 ePropertyInputPath,
2534 ePropertyOutputPath,
2535 ePropertyErrorPath,
2536 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002537 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002538 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00002539 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00002540 ePropertyUseHexImmediates,
2541 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002542 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00002543 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfd814c52013-08-13 01:42:25 +00002544 ePropertyMemoryModuleLoadLevel
Greg Clayton67cc0632012-08-22 17:17:09 +00002545};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002546
Caroline Ticedaccaa92010-09-20 20:44:43 +00002547
Greg Clayton67cc0632012-08-22 17:17:09 +00002548class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002549{
Greg Clayton67cc0632012-08-22 17:17:09 +00002550public:
2551 TargetOptionValueProperties (const ConstString &name) :
2552 OptionValueProperties (name),
2553 m_target (NULL),
2554 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002555 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002556 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002557
Greg Clayton67cc0632012-08-22 17:17:09 +00002558 // This constructor is used when creating TargetOptionValueProperties when it
2559 // is part of a new lldb_private::Target instance. It will copy all current
2560 // global property values as needed
2561 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2562 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2563 m_target (target),
2564 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002565 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002566 }
2567
2568 virtual const Property *
2569 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2570 {
2571 // When gettings the value for a key from the target options, we will always
2572 // try and grab the setting from the current target if there is one. Else we just
2573 // use the one from this instance.
2574 if (idx == ePropertyEnvVars)
2575 GetHostEnvironmentIfNeeded ();
2576
2577 if (exe_ctx)
2578 {
2579 Target *target = exe_ctx->GetTargetPtr();
2580 if (target)
2581 {
2582 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2583 if (this != target_properties)
2584 return target_properties->ProtectedGetPropertyAtIndex (idx);
2585 }
2586 }
2587 return ProtectedGetPropertyAtIndex (idx);
2588 }
Enrico Granata84a53df2013-05-20 22:29:23 +00002589
2590 lldb::TargetSP
2591 GetTargetSP ()
2592 {
2593 return m_target->shared_from_this();
2594 }
2595
Greg Clayton67cc0632012-08-22 17:17:09 +00002596protected:
2597
2598 void
2599 GetHostEnvironmentIfNeeded () const
2600 {
2601 if (!m_got_host_env)
2602 {
2603 if (m_target)
2604 {
2605 m_got_host_env = true;
2606 const uint32_t idx = ePropertyInheritEnv;
2607 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2608 {
2609 PlatformSP platform_sp (m_target->GetPlatform());
2610 if (platform_sp)
2611 {
2612 StringList env;
2613 if (platform_sp->GetEnvironment(env))
2614 {
2615 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2616 if (env_dict)
2617 {
2618 const bool can_replace = false;
2619 const size_t envc = env.GetSize();
2620 for (size_t idx=0; idx<envc; idx++)
2621 {
2622 const char *env_entry = env.GetStringAtIndex (idx);
2623 if (env_entry)
2624 {
2625 const char *equal_pos = ::strchr(env_entry, '=');
2626 ConstString key;
2627 // It is ok to have environment variables with no values
2628 const char *value = NULL;
2629 if (equal_pos)
2630 {
2631 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2632 if (equal_pos[1])
2633 value = equal_pos + 1;
2634 }
2635 else
2636 {
2637 key.SetCString(env_entry);
2638 }
2639 // Don't allow existing keys to be replaced with ones we get from the platform environment
2640 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2641 }
2642 }
2643 }
2644 }
2645 }
2646 }
2647 }
2648 }
2649 }
2650 Target *m_target;
2651 mutable bool m_got_host_env;
2652};
2653
Greg Claytonfbb76342013-11-20 21:07:01 +00002654//----------------------------------------------------------------------
2655// TargetProperties
2656//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00002657TargetProperties::TargetProperties (Target *target) :
2658 Properties ()
2659{
2660 if (target)
2661 {
2662 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Ticedaccaa92010-09-20 20:44:43 +00002663 }
2664 else
Greg Clayton67cc0632012-08-22 17:17:09 +00002665 {
2666 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2667 m_collection_sp->Initialize(g_properties);
2668 m_collection_sp->AppendProperty(ConstString("process"),
2669 ConstString("Settings specify to processes."),
2670 true,
2671 Process::GetGlobalProperties()->GetValueProperties());
2672 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002673}
2674
Greg Clayton67cc0632012-08-22 17:17:09 +00002675TargetProperties::~TargetProperties ()
2676{
2677}
2678ArchSpec
2679TargetProperties::GetDefaultArchitecture () const
2680{
2681 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2682 if (value)
2683 return value->GetCurrentValue();
2684 return ArchSpec();
2685}
2686
2687void
2688TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2689{
2690 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2691 if (value)
2692 return value->SetCurrentValue(arch, true);
2693}
2694
2695lldb::DynamicValueType
2696TargetProperties::GetPreferDynamicValue() const
2697{
2698 const uint32_t idx = ePropertyPreferDynamic;
2699 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2700}
2701
2702bool
2703TargetProperties::GetDisableASLR () const
2704{
2705 const uint32_t idx = ePropertyDisableASLR;
2706 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2707}
2708
2709void
2710TargetProperties::SetDisableASLR (bool b)
2711{
2712 const uint32_t idx = ePropertyDisableASLR;
2713 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2714}
2715
2716bool
2717TargetProperties::GetDisableSTDIO () const
2718{
2719 const uint32_t idx = ePropertyDisableSTDIO;
2720 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2721}
2722
2723void
2724TargetProperties::SetDisableSTDIO (bool b)
2725{
2726 const uint32_t idx = ePropertyDisableSTDIO;
2727 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2728}
2729
Jim Ingham0f063ba2013-03-02 00:26:47 +00002730const char *
2731TargetProperties::GetDisassemblyFlavor () const
2732{
2733 const uint32_t idx = ePropertyDisassemblyFlavor;
2734 const char *return_value;
2735
2736 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2737 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
2738 return return_value;
2739}
2740
Greg Clayton1f746072012-08-29 21:13:06 +00002741InlineStrategy
2742TargetProperties::GetInlineStrategy () const
2743{
2744 const uint32_t idx = ePropertyInlineStrategy;
2745 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2746}
2747
Greg Clayton45392552012-10-17 22:57:12 +00002748const char *
2749TargetProperties::GetArg0 () const
2750{
2751 const uint32_t idx = ePropertyArg0;
2752 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2753}
2754
2755void
2756TargetProperties::SetArg0 (const char *arg)
2757{
2758 const uint32_t idx = ePropertyArg0;
2759 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2760}
2761
Greg Clayton67cc0632012-08-22 17:17:09 +00002762bool
2763TargetProperties::GetRunArguments (Args &args) const
2764{
2765 const uint32_t idx = ePropertyRunArgs;
2766 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2767}
2768
2769void
2770TargetProperties::SetRunArguments (const Args &args)
2771{
2772 const uint32_t idx = ePropertyRunArgs;
2773 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2774}
2775
2776size_t
2777TargetProperties::GetEnvironmentAsArgs (Args &env) const
2778{
2779 const uint32_t idx = ePropertyEnvVars;
2780 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2781}
2782
2783bool
2784TargetProperties::GetSkipPrologue() const
2785{
2786 const uint32_t idx = ePropertySkipPrologue;
2787 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2788}
2789
2790PathMappingList &
2791TargetProperties::GetSourcePathMap () const
2792{
2793 const uint32_t idx = ePropertySourceMap;
2794 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2795 assert(option_value);
2796 return option_value->GetCurrentValue();
2797}
2798
2799FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00002800TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00002801{
2802 const uint32_t idx = ePropertyExecutableSearchPaths;
2803 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2804 assert(option_value);
2805 return option_value->GetCurrentValue();
2806}
2807
Michael Sartaina7499c92013-07-01 19:45:50 +00002808FileSpecList &
2809TargetProperties::GetDebugFileSearchPaths ()
2810{
2811 const uint32_t idx = ePropertyDebugFileSearchPaths;
2812 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2813 assert(option_value);
2814 return option_value->GetCurrentValue();
2815}
2816
Greg Clayton67cc0632012-08-22 17:17:09 +00002817bool
2818TargetProperties::GetEnableSyntheticValue () const
2819{
2820 const uint32_t idx = ePropertyEnableSynthetic;
2821 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2822}
2823
2824uint32_t
2825TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2826{
2827 const uint32_t idx = ePropertyMaxChildrenCount;
2828 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2829}
2830
2831uint32_t
2832TargetProperties::GetMaximumSizeOfStringSummary() const
2833{
2834 const uint32_t idx = ePropertyMaxSummaryLength;
2835 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2836}
2837
Enrico Granatad325bf92013-06-04 22:54:16 +00002838uint32_t
2839TargetProperties::GetMaximumMemReadSize () const
2840{
2841 const uint32_t idx = ePropertyMaxMemReadSize;
2842 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2843}
2844
Greg Clayton67cc0632012-08-22 17:17:09 +00002845FileSpec
2846TargetProperties::GetStandardInputPath () const
2847{
2848 const uint32_t idx = ePropertyInputPath;
2849 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2850}
2851
2852void
2853TargetProperties::SetStandardInputPath (const char *p)
2854{
2855 const uint32_t idx = ePropertyInputPath;
2856 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2857}
2858
2859FileSpec
2860TargetProperties::GetStandardOutputPath () const
2861{
2862 const uint32_t idx = ePropertyOutputPath;
2863 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2864}
2865
2866void
2867TargetProperties::SetStandardOutputPath (const char *p)
2868{
2869 const uint32_t idx = ePropertyOutputPath;
2870 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2871}
2872
2873FileSpec
2874TargetProperties::GetStandardErrorPath () const
2875{
2876 const uint32_t idx = ePropertyErrorPath;
2877 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2878}
2879
Greg Clayton6920b522012-08-22 18:39:03 +00002880const char *
2881TargetProperties::GetExpressionPrefixContentsAsCString ()
2882{
2883 const uint32_t idx = ePropertyExprPrefix;
2884 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2885 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00002886 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00002887 const bool null_terminate = true;
2888 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00002889 if (data_sp)
2890 return (const char *) data_sp->GetBytes();
2891 }
Greg Clayton6920b522012-08-22 18:39:03 +00002892 return NULL;
2893}
2894
Greg Clayton67cc0632012-08-22 17:17:09 +00002895void
2896TargetProperties::SetStandardErrorPath (const char *p)
2897{
2898 const uint32_t idx = ePropertyErrorPath;
2899 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2900}
2901
2902bool
2903TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2904{
2905 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2906 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2907}
2908
Jim Ingham17d023f2013-03-13 17:58:04 +00002909bool
Daniel Malead79ae052013-08-07 21:54:09 +00002910TargetProperties::GetUseHexImmediates () const
2911{
2912 const uint32_t idx = ePropertyUseHexImmediates;
2913 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2914}
2915
2916bool
Jim Ingham17d023f2013-03-13 17:58:04 +00002917TargetProperties::GetUseFastStepping () const
2918{
2919 const uint32_t idx = ePropertyUseFastStepping;
2920 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2921}
2922
Enrico Granata397ddd52013-05-21 20:13:34 +00002923LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002924TargetProperties::GetLoadScriptFromSymbolFile () const
2925{
2926 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00002927 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002928}
2929
Daniel Malead79ae052013-08-07 21:54:09 +00002930Disassembler::HexImmediateStyle
2931TargetProperties::GetHexImmediateStyle () const
2932{
2933 const uint32_t idx = ePropertyHexImmediateStyle;
2934 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
2935}
2936
Greg Claytonfd814c52013-08-13 01:42:25 +00002937MemoryModuleLoadLevel
2938TargetProperties::GetMemoryModuleLoadLevel() const
2939{
2940 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
2941 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
2942}
2943
2944
Greg Clayton67cc0632012-08-22 17:17:09 +00002945
Greg Claytonfbb76342013-11-20 21:07:01 +00002946//----------------------------------------------------------------------
2947// Target::TargetEventData
2948//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +00002949const ConstString &
2950Target::TargetEventData::GetFlavorString ()
2951{
2952 static ConstString g_flavor ("Target::TargetEventData");
2953 return g_flavor;
2954}
2955
2956const ConstString &
2957Target::TargetEventData::GetFlavor () const
2958{
2959 return TargetEventData::GetFlavorString ();
2960}
2961
2962Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2963 EventData(),
2964 m_target_sp (new_target_sp)
2965{
2966}
2967
2968Target::TargetEventData::~TargetEventData()
2969{
2970
2971}
2972
2973void
2974Target::TargetEventData::Dump (Stream *s) const
2975{
2976
2977}
2978
2979const TargetSP
2980Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2981{
2982 TargetSP target_sp;
2983
2984 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2985 if (data)
2986 target_sp = data->m_target_sp;
2987
2988 return target_sp;
2989}
2990
2991const Target::TargetEventData *
2992Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2993{
2994 if (event_ptr)
2995 {
2996 const EventData *event_data = event_ptr->GetData();
2997 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2998 return static_cast <const TargetEventData *> (event_ptr->GetData());
2999 }
3000 return NULL;
3001}
3002