blob: 02125f9c0625452a0d3672fff118e8b2ee06ed74 [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"
Greg Claytonb09c5382013-12-13 17:20:18 +000031#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000032#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000034#include "lldb/Core/Timer.h"
35#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000036#include "lldb/Expression/ClangASTSource.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000037#include "lldb/Expression/ClangUserExpression.h"
Zachary Turner10687b02014-10-20 17:46:43 +000038#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000040#include "lldb/Interpreter/CommandInterpreter.h"
41#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000042#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000043#include "lldb/Interpreter/OptionValues.h"
44#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/lldb-private-log.h"
46#include "lldb/Symbol/ObjectFile.h"
47#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000048#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000049#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000050#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000051#include "lldb/Target/Thread.h"
52#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54using namespace lldb;
55using namespace lldb_private;
56
Jim Ingham4bddaeb2012-02-16 06:50:00 +000057ConstString &
58Target::GetStaticBroadcasterClass ()
59{
60 static ConstString class_name ("lldb.target");
61 return class_name;
62}
63
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064//----------------------------------------------------------------------
65// Target constructor
66//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000067Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000068 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000069 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000070 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000071 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000072 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000073 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000074 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000075 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000076 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077 m_breakpoint_list (false),
78 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000079 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000080 m_process_sp (),
81 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000083 m_scratch_ast_context_ap (),
84 m_scratch_ast_source_ap (),
85 m_ast_importer_ap (),
Jim Ingham9575d842011-03-11 03:53:59 +000086 m_persistent_variables (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000087 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000088 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000089 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000090 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000091 m_suppress_stop_hooks (false),
92 m_is_dummy_target(is_dummy_target)
93
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000095 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
96 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
97 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000098 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +000099 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000100
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000101 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000102
Jim Ingham893c9322014-11-22 01:42:44 +0000103 if (!m_is_dummy_target)
104 PrimeFromDummyTarget(m_debugger.GetDummyTarget());
105
Greg Clayton5160ce52013-03-27 23:08:40 +0000106 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000108 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000109 if (m_arch.IsValid())
110 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000111 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 +0000112 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113}
114
Jim Ingham893c9322014-11-22 01:42:44 +0000115void
116Target::PrimeFromDummyTarget(Target *target)
117{
118 if (!target)
119 return;
120
121 m_stop_hooks = target->m_stop_hooks;
122
123}
124
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125//----------------------------------------------------------------------
126// Destructor
127//----------------------------------------------------------------------
128Target::~Target()
129{
Greg Clayton5160ce52013-03-27 23:08:40 +0000130 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000132 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133 DeleteCurrentProcess ();
134}
135
136void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000137Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138{
Greg Clayton89411422010-10-08 00:21:05 +0000139// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000140 if (description_level != lldb::eDescriptionLevelBrief)
141 {
142 s->Indent();
143 s->PutCString("Target\n");
144 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000145 m_images.Dump(s);
146 m_breakpoint_list.Dump(s);
147 m_internal_breakpoint_list.Dump(s);
148 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000149 }
150 else
151 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000152 Module *exe_module = GetExecutableModulePointer();
153 if (exe_module)
154 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000155 else
156 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000157 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
160void
Greg Clayton90ba8112012-12-05 00:16:59 +0000161Target::CleanupProcess ()
162{
163 // Do any cleanup of the target we need to do between process instances.
164 // NB It is better to do this before destroying the process in case the
165 // clean up needs some help from the process.
166 m_breakpoint_list.ClearAllBreakpointSites();
167 m_internal_breakpoint_list.ClearAllBreakpointSites();
168 // Disable watchpoints just on the debugger side.
169 Mutex::Locker locker;
170 this->GetWatchpointList().GetListMutex(locker);
171 DisableAllWatchpoints(false);
172 ClearAllWatchpointHitCounts();
173}
174
175void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176Target::DeleteCurrentProcess ()
177{
178 if (m_process_sp.get())
179 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000180 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 if (m_process_sp->IsAlive())
182 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000183
184 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185
Greg Clayton90ba8112012-12-05 00:16:59 +0000186 CleanupProcess ();
187
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 m_process_sp.reset();
189 }
190}
191
192const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000193Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194{
195 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000196 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197 return m_process_sp;
198}
199
200const lldb::ProcessSP &
201Target::GetProcessSP () const
202{
203 return m_process_sp;
204}
205
Greg Clayton3418c852011-08-10 02:10:13 +0000206void
207Target::Destroy()
208{
209 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000210 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000211 DeleteCurrentProcess ();
212 m_platform_sp.reset();
213 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000214 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000215 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000216 const bool notify = false;
217 m_breakpoint_list.RemoveAll(notify);
218 m_internal_breakpoint_list.RemoveAll(notify);
219 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000220 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000221 m_search_filter_sp.reset();
222 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000223 m_persistent_variables.Clear();
224 m_stop_hooks.clear();
225 m_stop_hook_next_id = 0;
226 m_suppress_stop_hooks = false;
227}
228
229
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230BreakpointList &
231Target::GetBreakpointList(bool internal)
232{
233 if (internal)
234 return m_internal_breakpoint_list;
235 else
236 return m_breakpoint_list;
237}
238
239const BreakpointList &
240Target::GetBreakpointList(bool internal) const
241{
242 if (internal)
243 return m_internal_breakpoint_list;
244 else
245 return m_breakpoint_list;
246}
247
248BreakpointSP
249Target::GetBreakpointByID (break_id_t break_id)
250{
251 BreakpointSP bp_sp;
252
253 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
254 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
255 else
256 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
257
258 return bp_sp;
259}
260
261BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000262Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000263 const FileSpecList *source_file_spec_list,
264 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000265 bool internal,
266 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267{
Jim Ingham87df91b2011-09-23 00:54:11 +0000268 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
269 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000270 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000271}
272
273
274BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000275Target::CreateBreakpoint (const FileSpecList *containingModules,
276 const FileSpec &file,
277 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000278 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000279 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000280 bool internal,
281 bool hardware)
Jim Ingham969795f2011-09-21 01:17:13 +0000282{
Greg Clayton1f746072012-08-29 21:13:06 +0000283 if (check_inlines == eLazyBoolCalculate)
284 {
285 const InlineStrategy inline_strategy = GetInlineStrategy();
286 switch (inline_strategy)
287 {
288 case eInlineBreakpointsNever:
289 check_inlines = eLazyBoolNo;
290 break;
291
292 case eInlineBreakpointsHeaders:
293 if (file.IsSourceImplementationFile())
294 check_inlines = eLazyBoolNo;
295 else
296 check_inlines = eLazyBoolYes;
297 break;
298
299 case eInlineBreakpointsAlways:
300 check_inlines = eLazyBoolYes;
301 break;
302 }
303 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000304 SearchFilterSP filter_sp;
305 if (check_inlines == eLazyBoolNo)
306 {
307 // Not checking for inlines, we are looking only for matching compile units
308 FileSpecList compile_unit_list;
309 compile_unit_list.Append (file);
310 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
311 }
312 else
313 {
314 filter_sp = GetSearchFilterForModuleList (containingModules);
315 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000316 if (skip_prologue == eLazyBoolCalculate)
317 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
318
Greg Clayton1f746072012-08-29 21:13:06 +0000319 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
320 file,
321 line_no,
322 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000323 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000324 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325}
326
327
328BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000329Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 Address so_addr;
332 // Attempt to resolve our load address if possible, though it is ok if
333 // it doesn't resolve to section/offset.
334
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000335 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000336 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000337 if (!so_addr.IsValid())
338 {
339 // The address didn't resolve, so just set this as an absolute address
340 so_addr.SetOffset (addr);
341 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000342 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 return bp_sp;
344}
345
346BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000347Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000349 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000351 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352}
353
354BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000355Target::CreateBreakpoint (const FileSpecList *containingModules,
356 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000357 const char *func_name,
358 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000359 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000360 bool internal,
361 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000363 BreakpointSP bp_sp;
364 if (func_name)
365 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000366 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000367
368 if (skip_prologue == eLazyBoolCalculate)
369 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
370
Greg Claytond16e1e52011-07-12 17:06:17 +0000371 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
372 func_name,
373 func_name_type_mask,
374 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000375 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000376 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000377 }
378 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379}
380
Jim Inghamfab10e82012-03-06 00:37:27 +0000381lldb::BreakpointSP
382Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000383 const FileSpecList *containingSourceFiles,
384 const std::vector<std::string> &func_names,
385 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000386 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000387 bool internal,
388 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000389{
390 BreakpointSP bp_sp;
391 size_t num_names = func_names.size();
392 if (num_names > 0)
393 {
394 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000395
396 if (skip_prologue == eLazyBoolCalculate)
397 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
398
399 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000400 func_names,
401 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000402 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000403 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000404 }
405 return bp_sp;
406}
407
Jim Ingham133e0fb2012-03-03 02:05:11 +0000408BreakpointSP
409Target::CreateBreakpoint (const FileSpecList *containingModules,
410 const FileSpecList *containingSourceFiles,
411 const char *func_names[],
412 size_t num_names,
413 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000414 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000415 bool internal,
416 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000417{
418 BreakpointSP bp_sp;
419 if (num_names > 0)
420 {
421 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
422
Greg Clayton03da4cc2013-04-19 21:31:16 +0000423 if (skip_prologue == eLazyBoolCalculate)
424 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
425
426 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000427 func_names,
428 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000429 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000430 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000431 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000432 }
433 return bp_sp;
434}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435
436SearchFilterSP
437Target::GetSearchFilterForModule (const FileSpec *containingModule)
438{
439 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 if (containingModule != NULL)
441 {
442 // TODO: We should look into sharing module based search filters
443 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000444 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 }
446 else
447 {
448 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000449 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 filter_sp = m_search_filter_sp;
451 }
452 return filter_sp;
453}
454
Jim Ingham969795f2011-09-21 01:17:13 +0000455SearchFilterSP
456Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
457{
458 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000459 if (containingModules && containingModules->GetSize() != 0)
460 {
461 // TODO: We should look into sharing module based search filters
462 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000463 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000464 }
465 else
466 {
467 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000468 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000469 filter_sp = m_search_filter_sp;
470 }
471 return filter_sp;
472}
473
Jim Ingham87df91b2011-09-23 00:54:11 +0000474SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000475Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
476 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000477{
478 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
479 return GetSearchFilterForModuleList(containingModules);
480
481 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000482 if (containingModules == NULL)
483 {
484 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
485 // but that will take a little reworking.
486
Greg Claytone1cd1be2012-01-29 20:56:30 +0000487 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000488 }
489 else
490 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000491 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000492 }
493 return filter_sp;
494}
495
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000497Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000498 const FileSpecList *containingSourceFiles,
499 RegularExpression &func_regex,
500 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000501 bool internal,
502 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503{
Jim Ingham87df91b2011-09-23 00:54:11 +0000504 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000505 bool skip =
506 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
507 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000508 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
509 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000510 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511
Jim Ingham1460e4b2014-01-10 23:46:59 +0000512 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513}
514
Jim Ingham219ba192012-03-05 04:47:34 +0000515lldb::BreakpointSP
516Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
517{
518 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
519}
520
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000522Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523{
524 BreakpointSP bp_sp;
525 if (filter_sp && resolver_sp)
526 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000527 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528 resolver_sp->SetBreakpoint (bp_sp.get());
529
530 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000531 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 else
Greg Clayton9fed0d82010-07-23 23:33:17 +0000533 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534
Greg Clayton5160ce52013-03-27 23:08:40 +0000535 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 if (log)
537 {
538 StreamString s;
539 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
540 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
541 }
542
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000543 bp_sp->ResolveBreakpoint();
544 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000545
546 if (!internal && bp_sp)
547 {
548 m_last_created_breakpoint = bp_sp;
549 }
550
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551 return bp_sp;
552}
553
Johnny Chen86364b42011-09-20 23:28:55 +0000554bool
555Target::ProcessIsValid()
556{
557 return (m_process_sp && m_process_sp->IsAlive());
558}
559
Johnny Chenb90827e2012-06-04 23:19:54 +0000560static bool
561CheckIfWatchpointsExhausted(Target *target, Error &error)
562{
563 uint32_t num_supported_hardware_watchpoints;
564 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
565 if (rc.Success())
566 {
567 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
568 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
569 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
570 num_supported_hardware_watchpoints);
571 }
572 return false;
573}
574
Johnny Chen01a67862011-10-14 00:42:25 +0000575// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000576// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000577WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000578Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000579{
Greg Clayton5160ce52013-03-27 23:08:40 +0000580 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000581 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000582 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000583 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000584
Johnny Chen01a67862011-10-14 00:42:25 +0000585 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000586 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000587 {
588 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000589 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000590 }
Jim Inghamc6462312013-06-18 21:52:48 +0000591
Johnny Chen45e541f2011-09-14 22:20:15 +0000592 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000593 {
594 if (size == 0)
595 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
596 else
Daniel Malead01b2952012-11-29 21:49:15 +0000597 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000598 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000599 }
Jim Inghamc6462312013-06-18 21:52:48 +0000600
601 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
602 {
603 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
604 }
Johnny Chen7313a642011-09-13 01:15:36 +0000605
Johnny Chen01a67862011-10-14 00:42:25 +0000606 // Currently we only support one watchpoint per address, with total number
607 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000608
609 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000610 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 +0000611 Mutex::Locker locker;
612 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000613 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000614 if (matched_sp)
615 {
Johnny Chen0c406372011-09-14 20:23:45 +0000616 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000617 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000618 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
619 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000620 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000621 if (size == old_size && kind == old_type)
622 {
Johnny Chen01a67862011-10-14 00:42:25 +0000623 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000624 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000625 }
626 else
627 {
Johnny Chen01a67862011-10-14 00:42:25 +0000628 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000629 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
630 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000631 }
Johnny Chen3c532582011-09-13 23:29:31 +0000632 }
633
Jason Molenda727e3922012-12-05 23:07:34 +0000634 if (!wp_sp)
635 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000636 wp_sp.reset(new Watchpoint(*this, addr, size, type));
637 wp_sp->SetWatchpointType(kind, notify);
638 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000639 }
Johnny Chen0c406372011-09-14 20:23:45 +0000640
Jim Ingham1b5792e2012-12-18 02:03:49 +0000641 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000642 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000643 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
644 __FUNCTION__,
645 error.Success() ? "succeeded" : "failed",
646 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000647
Jason Molenda727e3922012-12-05 23:07:34 +0000648 if (error.Fail())
649 {
Johnny Chen41b77262012-03-26 22:00:10 +0000650 // Enabling the watchpoint on the device side failed.
651 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000652 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000653 // See if we could provide more helpful error message.
654 if (!CheckIfWatchpointsExhausted(this, error))
655 {
656 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000657 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000658 }
Johnny Chen01a67862011-10-14 00:42:25 +0000659 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000660 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000661 else
Johnny Chen01a67862011-10-14 00:42:25 +0000662 m_last_created_watchpoint = wp_sp;
663 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000664}
665
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666void
667Target::RemoveAllBreakpoints (bool internal_also)
668{
Greg Clayton5160ce52013-03-27 23:08:40 +0000669 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 if (log)
671 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
672
Greg Clayton9fed0d82010-07-23 23:33:17 +0000673 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000675 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000676
677 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678}
679
680void
681Target::DisableAllBreakpoints (bool internal_also)
682{
Greg Clayton5160ce52013-03-27 23:08:40 +0000683 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 if (log)
685 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
686
687 m_breakpoint_list.SetEnabledAll (false);
688 if (internal_also)
689 m_internal_breakpoint_list.SetEnabledAll (false);
690}
691
692void
693Target::EnableAllBreakpoints (bool internal_also)
694{
Greg Clayton5160ce52013-03-27 23:08:40 +0000695 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 if (log)
697 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
698
699 m_breakpoint_list.SetEnabledAll (true);
700 if (internal_also)
701 m_internal_breakpoint_list.SetEnabledAll (true);
702}
703
704bool
705Target::RemoveBreakpointByID (break_id_t break_id)
706{
Greg Clayton5160ce52013-03-27 23:08:40 +0000707 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 if (log)
709 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
710
711 if (DisableBreakpointByID (break_id))
712 {
713 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000714 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000716 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000717 if (m_last_created_breakpoint)
718 {
719 if (m_last_created_breakpoint->GetID() == break_id)
720 m_last_created_breakpoint.reset();
721 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000722 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000723 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 return true;
725 }
726 return false;
727}
728
729bool
730Target::DisableBreakpointByID (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", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
735
736 BreakpointSP bp_sp;
737
738 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
739 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
740 else
741 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
742 if (bp_sp)
743 {
744 bp_sp->SetEnabled (false);
745 return true;
746 }
747 return false;
748}
749
750bool
751Target::EnableBreakpointByID (break_id_t break_id)
752{
Greg Clayton5160ce52013-03-27 23:08:40 +0000753 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 if (log)
755 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
756 __FUNCTION__,
757 break_id,
758 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
759
760 BreakpointSP bp_sp;
761
762 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
763 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
764 else
765 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
766
767 if (bp_sp)
768 {
769 bp_sp->SetEnabled (true);
770 return true;
771 }
772 return false;
773}
774
Johnny Chenedf50372011-09-23 21:21:43 +0000775// The flag 'end_to_end', default to true, signifies that the operation is
776// performed end to end, for both the debugger and the debuggee.
777
Johnny Chen01a67862011-10-14 00:42:25 +0000778// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
779// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000780bool
Johnny Chen01a67862011-10-14 00:42:25 +0000781Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000782{
Greg Clayton5160ce52013-03-27 23:08:40 +0000783 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000784 if (log)
785 log->Printf ("Target::%s\n", __FUNCTION__);
786
Johnny Chenedf50372011-09-23 21:21:43 +0000787 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000788 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000789 return true;
790 }
791
792 // Otherwise, it's an end to end operation.
793
Johnny Chen86364b42011-09-20 23:28:55 +0000794 if (!ProcessIsValid())
795 return false;
796
Johnny Chen01a67862011-10-14 00:42:25 +0000797 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000798 for (size_t i = 0; i < num_watchpoints; ++i)
799 {
Johnny Chen01a67862011-10-14 00:42:25 +0000800 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
801 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000802 return false;
803
Johnny Chen01a67862011-10-14 00:42:25 +0000804 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000805 if (rc.Fail())
806 return false;
807 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000808 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000809 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000810 return true; // Success!
811}
812
Johnny Chen01a67862011-10-14 00:42:25 +0000813// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
814// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000815bool
Johnny Chen01a67862011-10-14 00:42:25 +0000816Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000817{
Greg Clayton5160ce52013-03-27 23:08:40 +0000818 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000819 if (log)
820 log->Printf ("Target::%s\n", __FUNCTION__);
821
Johnny Chenedf50372011-09-23 21:21:43 +0000822 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000823 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000824 return true;
825 }
826
827 // Otherwise, it's an end to end operation.
828
Johnny Chen86364b42011-09-20 23:28:55 +0000829 if (!ProcessIsValid())
830 return false;
831
Johnny Chen01a67862011-10-14 00:42:25 +0000832 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000833 for (size_t i = 0; i < num_watchpoints; ++i)
834 {
Johnny Chen01a67862011-10-14 00:42:25 +0000835 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
836 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000837 return false;
838
Johnny Chen01a67862011-10-14 00:42:25 +0000839 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000840 if (rc.Fail())
841 return false;
842 }
Johnny Chen86364b42011-09-20 23:28:55 +0000843 return true; // Success!
844}
845
Johnny Chen01a67862011-10-14 00:42:25 +0000846// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
847// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000848bool
Johnny Chen01a67862011-10-14 00:42:25 +0000849Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000850{
Greg Clayton5160ce52013-03-27 23:08:40 +0000851 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000852 if (log)
853 log->Printf ("Target::%s\n", __FUNCTION__);
854
Johnny Chenedf50372011-09-23 21:21:43 +0000855 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000856 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000857 return true;
858 }
859
860 // Otherwise, it's an end to end operation.
861
Johnny Chen86364b42011-09-20 23:28:55 +0000862 if (!ProcessIsValid())
863 return false;
864
Johnny Chen01a67862011-10-14 00:42:25 +0000865 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000866 for (size_t i = 0; i < num_watchpoints; ++i)
867 {
Johnny Chen01a67862011-10-14 00:42:25 +0000868 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
869 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000870 return false;
871
Johnny Chen01a67862011-10-14 00:42:25 +0000872 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000873 if (rc.Fail())
874 return false;
875 }
Johnny Chen86364b42011-09-20 23:28:55 +0000876 return true; // Success!
877}
878
Johnny Chena4d6bc92012-02-25 06:44:30 +0000879// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
880bool
881Target::ClearAllWatchpointHitCounts ()
882{
Greg Clayton5160ce52013-03-27 23:08:40 +0000883 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000884 if (log)
885 log->Printf ("Target::%s\n", __FUNCTION__);
886
887 size_t num_watchpoints = m_watchpoint_list.GetSize();
888 for (size_t i = 0; i < num_watchpoints; ++i)
889 {
890 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
891 if (!wp_sp)
892 return false;
893
894 wp_sp->ResetHitCount();
895 }
896 return true; // Success!
897}
898
Johnny Chen01a67862011-10-14 00:42:25 +0000899// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000900// during these operations.
901bool
Johnny Chen01a67862011-10-14 00:42:25 +0000902Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000903{
Greg Clayton5160ce52013-03-27 23:08:40 +0000904 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000905 if (log)
906 log->Printf ("Target::%s\n", __FUNCTION__);
907
908 if (!ProcessIsValid())
909 return false;
910
Johnny Chen01a67862011-10-14 00:42:25 +0000911 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000912 for (size_t i = 0; i < num_watchpoints; ++i)
913 {
Johnny Chen01a67862011-10-14 00:42:25 +0000914 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
915 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000916 return false;
917
Johnny Chen01a67862011-10-14 00:42:25 +0000918 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000919 }
920 return true; // Success!
921}
922
Johnny Chen01a67862011-10-14 00:42:25 +0000923// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000924bool
Johnny Chen01a67862011-10-14 00:42:25 +0000925Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000926{
Greg Clayton5160ce52013-03-27 23:08:40 +0000927 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000928 if (log)
929 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
930
931 if (!ProcessIsValid())
932 return false;
933
Johnny Chen01a67862011-10-14 00:42:25 +0000934 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
935 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000936 {
Johnny Chen01a67862011-10-14 00:42:25 +0000937 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000938 if (rc.Success())
939 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000940
Johnny Chenf04ee932011-09-22 18:04:58 +0000941 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000942 }
943 return false;
944}
945
Johnny Chen01a67862011-10-14 00:42:25 +0000946// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000947bool
Johnny Chen01a67862011-10-14 00:42:25 +0000948Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000949{
Greg Clayton5160ce52013-03-27 23:08:40 +0000950 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000951 if (log)
952 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
953
954 if (!ProcessIsValid())
955 return false;
956
Johnny Chen01a67862011-10-14 00:42:25 +0000957 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
958 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000959 {
Johnny Chen01a67862011-10-14 00:42:25 +0000960 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000961 if (rc.Success())
962 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000963
Johnny Chenf04ee932011-09-22 18:04:58 +0000964 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000965 }
966 return false;
967}
968
Johnny Chen01a67862011-10-14 00:42:25 +0000969// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000970bool
Johnny Chen01a67862011-10-14 00:42:25 +0000971Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000972{
Greg Clayton5160ce52013-03-27 23:08:40 +0000973 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000974 if (log)
975 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
976
Jim Inghamb0b45132013-07-02 02:09:46 +0000977 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
978 if (watch_to_remove_sp == m_last_created_watchpoint)
979 m_last_created_watchpoint.reset();
980
Johnny Chen01a67862011-10-14 00:42:25 +0000981 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +0000982 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000983 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +0000984 return true;
985 }
986 return false;
987}
988
Johnny Chen01a67862011-10-14 00:42:25 +0000989// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +0000990bool
Johnny Chen01a67862011-10-14 00:42:25 +0000991Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000992{
Greg Clayton5160ce52013-03-27 23:08:40 +0000993 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000994 if (log)
995 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
996
997 if (!ProcessIsValid())
998 return false;
999
Johnny Chen01a67862011-10-14 00:42:25 +00001000 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1001 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001002 {
Johnny Chen01a67862011-10-14 00:42:25 +00001003 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001004 return true;
1005 }
1006 return false;
1007}
1008
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009ModuleSP
1010Target::GetExecutableModule ()
1011{
Greg Claytonaa149cb2011-08-11 02:48:45 +00001012 return m_images.GetModuleAtIndex(0);
1013}
1014
1015Module*
1016Target::GetExecutableModulePointer ()
1017{
1018 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019}
1020
Enrico Granata17598482012-11-08 02:22:02 +00001021static void
1022LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1023{
1024 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001025 StreamString feedback_stream;
1026 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001027 {
Enrico Granata97303392013-05-21 00:00:30 +00001028 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001029 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001030 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1031 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001032 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001033 if (feedback_stream.GetSize())
1034 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1035 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001036}
1037
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038void
Greg Claytonb35db632013-11-09 00:03:31 +00001039Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040{
Greg Claytonb35db632013-11-09 00:03:31 +00001041 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001042 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001043 m_images.Clear();
1044 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001045 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001046 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001047}
1048
1049void
Greg Claytonb35db632013-11-09 00:03:31 +00001050Target::DidExec ()
1051{
1052 // When a process exec's we need to know about it so we can do some cleanup.
1053 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1054 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1055}
1056
1057void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001058Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1059{
1060 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001061 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062
1063 if (executable_sp.get())
1064 {
1065 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001066 "Target::SetExecutableModule (executable = '%s')",
1067 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001069 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070
Jim Ingham5aee1622010-08-09 23:31:02 +00001071 // 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 +00001072 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001073 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001074 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001075 if (log)
1076 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1077 }
1078
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001080 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001082 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 {
1084 executable_objfile->GetDependentModules(dependent_files);
1085 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1086 {
Greg Claytonded470d2011-03-19 01:12:21 +00001087 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1088 FileSpec platform_dependent_file_spec;
1089 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001090 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001091 else
1092 platform_dependent_file_spec = dependent_file_spec;
1093
Greg Claytonb9a01b32012-02-26 05:51:37 +00001094 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1095 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096 if (image_module_sp.get())
1097 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098 ObjectFile *objfile = image_module_sp->GetObjectFile();
1099 if (objfile)
1100 objfile->GetDependentModules(dependent_files);
1101 }
1102 }
1103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104 }
1105}
1106
1107
Jim Ingham5aee1622010-08-09 23:31:02 +00001108bool
1109Target::SetArchitecture (const ArchSpec &arch_spec)
1110{
Greg Clayton5160ce52013-03-27 23:08:40 +00001111 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001112 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001113 {
Greg Clayton70512312012-05-08 01:45:38 +00001114 // If we haven't got a valid arch spec, or the architectures are
1115 // compatible, so just update the architecture. Architectures can be
1116 // equal, yet the triple OS and vendor might change, so we need to do
1117 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001118 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001119 if (log)
1120 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 +00001121 return true;
1122 }
1123 else
1124 {
1125 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001126 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001127 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 +00001128 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001129 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001130
Greg Claytonb35db632013-11-09 00:03:31 +00001131 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001132 // Need to do something about unsetting breakpoints.
1133
1134 if (executable_sp)
1135 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001136 if (log)
1137 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 +00001138 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1139 Error error = ModuleList::GetSharedModule (module_spec,
1140 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001141 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001142 NULL,
1143 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001144
1145 if (!error.Fail() && executable_sp)
1146 {
1147 SetExecutableModule (executable_sp, true);
1148 return true;
1149 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001150 }
1151 }
Greg Clayton70512312012-05-08 01:45:38 +00001152 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001153}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155void
Enrico Granataefe637d2012-11-08 19:16:03 +00001156Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001157{
1158}
1159
1160void
Enrico Granataefe637d2012-11-08 19:16:03 +00001161Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162{
1163 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001164 if (m_valid)
1165 {
1166 ModuleList my_module_list;
1167 my_module_list.Append(module_sp);
1168 LoadScriptingResourceForModule(module_sp, this);
1169 ModulesDidLoad (my_module_list);
1170 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171}
1172
1173void
Enrico Granataefe637d2012-11-08 19:16:03 +00001174Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001175{
1176 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001177 if (m_valid)
1178 {
1179 ModuleList my_module_list;
1180 my_module_list.Append(module_sp);
1181 ModulesDidUnload (my_module_list, false);
1182 }
Enrico Granata17598482012-11-08 02:22:02 +00001183}
1184
1185void
Enrico Granataefe637d2012-11-08 19:16:03 +00001186Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187{
Jim Inghame716ae02011-08-03 01:00:06 +00001188 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001189 if (m_valid)
1190 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191}
1192
1193void
1194Target::ModulesDidLoad (ModuleList &module_list)
1195{
Greg Clayton23f8c952014-03-24 23:10:19 +00001196 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001197 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001198 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001199 if (m_process_sp)
1200 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001201 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001202 }
Enrico Granata17598482012-11-08 02:22:02 +00001203 // TODO: make event data that packages up the module_list
1204 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1205 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206}
1207
1208void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001209Target::SymbolsDidLoad (ModuleList &module_list)
1210{
Greg Clayton23f8c952014-03-24 23:10:19 +00001211 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001212 {
Jim Ingham31caf982013-06-04 23:01:35 +00001213 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001214 {
Jim Ingham31caf982013-06-04 23:01:35 +00001215 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1216 if (runtime)
1217 {
1218 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1219 objc_runtime->SymbolsDidLoad(module_list);
1220 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001221 }
Jim Ingham31caf982013-06-04 23:01:35 +00001222
Greg Clayton095eeaa2013-11-05 23:28:00 +00001223 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jim Ingham31caf982013-06-04 23:01:35 +00001224 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001225 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001226}
1227
1228void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001229Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230{
Greg Clayton23f8c952014-03-24 23:10:19 +00001231 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001232 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001233 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001234 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Enrico Granata17598482012-11-08 02:22:02 +00001235 // TODO: make event data that packages up the module_list
1236 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1237 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238}
1239
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001240bool
Greg Claytonb9a01b32012-02-26 05:51:37 +00001241Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001242{
Greg Clayton67cc0632012-08-22 17:17:09 +00001243 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001244 {
1245 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001246 ModuleSpec module_spec (module_file_spec);
1247 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001248
1249 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1250 // black list.
1251 if (num_modules > 0)
1252 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001253 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001254 {
1255 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1256 return false;
1257 }
1258 return true;
1259 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001260 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001261 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001262}
1263
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001264bool
Jim Inghamc6674fd2011-10-28 23:14:11 +00001265Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1266{
Greg Clayton67cc0632012-08-22 17:17:09 +00001267 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001268 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001269 if (m_platform_sp)
1270 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001271 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001272 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001273}
1274
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275size_t
Greg Claytondb598232011-01-07 01:57:07 +00001276Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1277{
Greg Claytone72dfb32012-02-24 01:59:29 +00001278 SectionSP section_sp (addr.GetSection());
1279 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001280 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001281 // If the contents of this section are encrypted, the on-disk file is unusable. Read only from live memory.
Jason Molenda216d91f2012-04-25 00:06:56 +00001282 if (section_sp->IsEncrypted())
1283 {
Greg Clayton57f06302012-05-25 17:05:55 +00001284 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001285 return 0;
1286 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001287 ModuleSP module_sp (section_sp->GetModule());
1288 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001289 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001290 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1291 if (objfile)
1292 {
1293 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1294 addr.GetOffset(),
1295 dst,
1296 dst_len);
1297 if (bytes_read > 0)
1298 return bytes_read;
1299 else
1300 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1301 }
Greg Claytondb598232011-01-07 01:57:07 +00001302 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001303 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001304 }
1305 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001306 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001307 }
1308 else
Greg Claytondb598232011-01-07 01:57:07 +00001309 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001310
Greg Claytondb598232011-01-07 01:57:07 +00001311 return 0;
1312}
1313
1314size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001315Target::ReadMemory (const Address& addr,
1316 bool prefer_file_cache,
1317 void *dst,
1318 size_t dst_len,
1319 Error &error,
1320 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001323
Enrico Granata9128ee22011-09-06 19:20:51 +00001324 // if we end up reading this from process memory, we will fill this
1325 // with the actual load address
1326 if (load_addr_ptr)
1327 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1328
Greg Claytondb598232011-01-07 01:57:07 +00001329 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001330
1331 addr_t load_addr = LLDB_INVALID_ADDRESS;
1332 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001333 Address resolved_addr;
1334 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001335 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001336 SectionLoadList &section_load_list = GetSectionLoadList();
1337 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001338 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001339 // No sections are loaded, so we must assume we are not running
1340 // yet and anything we are given is a file address.
1341 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1342 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001343 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001344 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001345 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001346 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001347 // we have manually loaded some sections with "target modules load ..."
1348 // or because we have have a live process that has sections loaded
1349 // through the dynamic loader
1350 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001351 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001352 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001353 }
Greg Clayton357132e2011-03-26 19:14:58 +00001354 if (!resolved_addr.IsValid())
1355 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001356
Greg Claytonc749eb82011-07-11 05:12:02 +00001357
Greg Claytondb598232011-01-07 01:57:07 +00001358 if (prefer_file_cache)
1359 {
1360 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1361 if (bytes_read > 0)
1362 return bytes_read;
1363 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001364
Johnny Chen86364b42011-09-20 23:28:55 +00001365 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001366 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001367 if (load_addr == LLDB_INVALID_ADDRESS)
1368 load_addr = resolved_addr.GetLoadAddress (this);
1369
Greg Claytondda4f7b2010-06-30 23:03:03 +00001370 if (load_addr == LLDB_INVALID_ADDRESS)
1371 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001372 ModuleSP addr_module_sp (resolved_addr.GetModule());
1373 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001374 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Claytone72dfb32012-02-24 01:59:29 +00001375 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08 +00001376 resolved_addr.GetFileAddress(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001377 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001378 else
Daniel Malead01b2952012-11-29 21:49:15 +00001379 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001380 }
1381 else
1382 {
Greg Claytondb598232011-01-07 01:57:07 +00001383 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384 if (bytes_read != dst_len)
1385 {
1386 if (error.Success())
1387 {
1388 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001389 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390 else
Daniel Malead01b2952012-11-29 21:49:15 +00001391 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 +00001392 }
1393 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001394 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001395 {
1396 if (load_addr_ptr)
1397 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001398 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001399 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001400 // If the address is not section offset we have an address that
1401 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001402 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001403 // more we can do. If it is section offset, we might be able to
1404 // read cached memory from the object file.
1405 if (!resolved_addr.IsSectionOffset())
1406 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001409
Greg Claytonc749eb82011-07-11 05:12:02 +00001410 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001411 {
Greg Claytondb598232011-01-07 01:57:07 +00001412 // If we didn't already try and read from the object file cache, then
1413 // try it after failing to read from the process.
1414 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001415 }
1416 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001417}
1418
Greg Claytond16e1e52011-07-12 17:06:17 +00001419size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001420Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1421{
1422 char buf[256];
1423 out_str.clear();
1424 addr_t curr_addr = addr.GetLoadAddress(this);
1425 Address address(addr);
1426 while (1)
1427 {
1428 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1429 if (length == 0)
1430 break;
1431 out_str.append(buf, length);
1432 // If we got "length - 1" bytes, we didn't get the whole C string, we
1433 // need to read some more characters
1434 if (length == sizeof(buf) - 1)
1435 curr_addr += length;
1436 else
1437 break;
1438 address = Address(curr_addr);
1439 }
1440 return out_str.size();
1441}
1442
1443
1444size_t
1445Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1446{
1447 size_t total_cstr_len = 0;
1448 if (dst && dst_max_len)
1449 {
1450 result_error.Clear();
1451 // NULL out everything just to be safe
1452 memset (dst, 0, dst_max_len);
1453 Error error;
1454 addr_t curr_addr = addr.GetLoadAddress(this);
1455 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001456
1457 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1458 // think this really needs to be tied to the memory cache subsystem's
1459 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001460 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001461
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001462 size_t bytes_left = dst_max_len - 1;
1463 char *curr_dst = dst;
1464
1465 while (bytes_left > 0)
1466 {
1467 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1468 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1469 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1470
1471 if (bytes_read == 0)
1472 {
1473 result_error = error;
1474 dst[total_cstr_len] = '\0';
1475 break;
1476 }
1477 const size_t len = strlen(curr_dst);
1478
1479 total_cstr_len += len;
1480
1481 if (len < bytes_to_read)
1482 break;
1483
1484 curr_dst += bytes_read;
1485 curr_addr += bytes_read;
1486 bytes_left -= bytes_read;
1487 address = Address(curr_addr);
1488 }
1489 }
1490 else
1491 {
1492 if (dst == NULL)
1493 result_error.SetErrorString("invalid arguments");
1494 else
1495 result_error.Clear();
1496 }
1497 return total_cstr_len;
1498}
1499
1500size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001501Target::ReadScalarIntegerFromMemory (const Address& addr,
1502 bool prefer_file_cache,
1503 uint32_t byte_size,
1504 bool is_signed,
1505 Scalar &scalar,
1506 Error &error)
1507{
1508 uint64_t uval;
1509
1510 if (byte_size <= sizeof(uval))
1511 {
1512 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1513 if (bytes_read == byte_size)
1514 {
1515 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001516 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001517 if (byte_size <= 4)
1518 scalar = data.GetMaxU32 (&offset, byte_size);
1519 else
1520 scalar = data.GetMaxU64 (&offset, byte_size);
1521
1522 if (is_signed)
1523 scalar.SignExtend(byte_size * 8);
1524 return bytes_read;
1525 }
1526 }
1527 else
1528 {
1529 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1530 }
1531 return 0;
1532}
1533
1534uint64_t
1535Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1536 bool prefer_file_cache,
1537 size_t integer_byte_size,
1538 uint64_t fail_value,
1539 Error &error)
1540{
1541 Scalar scalar;
1542 if (ReadScalarIntegerFromMemory (addr,
1543 prefer_file_cache,
1544 integer_byte_size,
1545 false,
1546 scalar,
1547 error))
1548 return scalar.ULongLong(fail_value);
1549 return fail_value;
1550}
1551
1552bool
1553Target::ReadPointerFromMemory (const Address& addr,
1554 bool prefer_file_cache,
1555 Error &error,
1556 Address &pointer_addr)
1557{
1558 Scalar scalar;
1559 if (ReadScalarIntegerFromMemory (addr,
1560 prefer_file_cache,
1561 m_arch.GetAddressByteSize(),
1562 false,
1563 scalar,
1564 error))
1565 {
1566 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1567 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1568 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001569 SectionLoadList &section_load_list = GetSectionLoadList();
1570 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001571 {
1572 // No sections are loaded, so we must assume we are not running
1573 // yet and anything we are given is a file address.
1574 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1575 }
1576 else
1577 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001578 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001579 // we have manually loaded some sections with "target modules load ..."
1580 // or because we have have a live process that has sections loaded
1581 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001582 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001583 }
1584 // We weren't able to resolve the pointer value, so just return
1585 // an address with no section
1586 if (!pointer_addr.IsValid())
1587 pointer_addr.SetOffset (pointer_vm_addr);
1588 return true;
1589
1590 }
1591 }
1592 return false;
1593}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001594
1595ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001596Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001597{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001598 ModuleSP module_sp;
1599
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001600 Error error;
1601
Jim Ingham4a94c912012-05-17 18:38:42 +00001602 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1603 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001604
Jim Ingham4a94c912012-05-17 18:38:42 +00001605 if (module_spec.GetUUID().IsValid())
1606 module_sp = m_images.FindFirstModule(module_spec);
1607
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001608 if (!module_sp)
1609 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001610 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1611 bool did_create_module = false;
1612
1613 // If there are image search path entries, try to use them first to acquire a suitable image.
1614 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001615 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001616 ModuleSpec transformed_spec (module_spec);
1617 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1618 {
1619 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1620 error = ModuleList::GetSharedModule (transformed_spec,
1621 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001622 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001623 &old_module_sp,
1624 &did_create_module);
1625 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001626 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001627
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001628 if (!module_sp)
1629 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001630 // If we have a UUID, we can check our global shared module list in case
1631 // we already have it. If we don't have a valid UUID, then we can't since
1632 // the path in "module_spec" will be a platform path, and we will need to
1633 // let the platform find that file. For example, we could be asking for
1634 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1635 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1636 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1637 // cache.
1638 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001639 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001640 // We have a UUID, it is OK to check the global module list...
1641 error = ModuleList::GetSharedModule (module_spec,
1642 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001643 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001644 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001645 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001646 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001647
1648 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001649 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001650 // The platform is responsible for finding and caching an appropriate
1651 // module in the shared module cache.
1652 if (m_platform_sp)
1653 {
1654 FileSpec platform_file_spec;
1655 error = m_platform_sp->GetSharedModule (module_spec,
1656 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001657 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001658 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001659 &did_create_module);
1660 }
1661 else
1662 {
1663 error.SetErrorString("no platform is currently set");
1664 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001665 }
1666 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001667
Jim Ingham4a94c912012-05-17 18:38:42 +00001668 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1669 // module in the list already, and if there was, let's remove it.
1670 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001672 ObjectFile *objfile = module_sp->GetObjectFile();
1673 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001674 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001675 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001676 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001677 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1678 case ObjectFile::eTypeExecutable: /// A normal executable
1679 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1680 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1681 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1682 break;
1683 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1684 if (error_ptr)
1685 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1686 return ModuleSP();
1687 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1688 if (error_ptr)
1689 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1690 return ModuleSP();
1691 default:
1692 if (error_ptr)
1693 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1694 return ModuleSP();
1695 }
1696 // GetSharedModule is not guaranteed to find the old shared module, for instance
1697 // in the common case where you pass in the UUID, it is only going to find the one
1698 // module matching the UUID. In fact, it has no good way to know what the "old module"
1699 // relevant to this target is, since there might be many copies of a module with this file spec
1700 // in various running debug sessions, but only one of them will belong to this target.
1701 // So let's remove the UUID from the module list, and look in the target's module list.
1702 // Only do this if there is SOMETHING else in the module spec...
1703 if (!old_module_sp)
1704 {
1705 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001706 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001707 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1708 module_spec_copy.GetUUID().Clear();
1709
1710 ModuleList found_modules;
1711 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1712 if (num_found == 1)
1713 {
1714 old_module_sp = found_modules.GetModuleAtIndex(0);
1715 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001716 }
1717 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001718
1719 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1720 {
1721 m_images.ReplaceModule(old_module_sp, module_sp);
1722 Module *old_module_ptr = old_module_sp.get();
1723 old_module_sp.reset();
1724 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1725 }
1726 else
1727 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001728 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001729 else
1730 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001731 }
1732 }
1733 if (error_ptr)
1734 *error_ptr = error;
1735 return module_sp;
1736}
1737
1738
Greg Claytond9e416c2012-02-18 05:35:26 +00001739TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001740Target::CalculateTarget ()
1741{
Greg Claytond9e416c2012-02-18 05:35:26 +00001742 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001743}
1744
Greg Claytond9e416c2012-02-18 05:35:26 +00001745ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746Target::CalculateProcess ()
1747{
Greg Claytond9e416c2012-02-18 05:35:26 +00001748 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749}
1750
Greg Claytond9e416c2012-02-18 05:35:26 +00001751ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752Target::CalculateThread ()
1753{
Greg Claytond9e416c2012-02-18 05:35:26 +00001754 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001755}
1756
Jason Molendab57e4a12013-11-04 09:33:30 +00001757StackFrameSP
1758Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001759{
Jason Molendab57e4a12013-11-04 09:33:30 +00001760 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761}
1762
1763void
Greg Clayton0603aa92010-10-04 01:05:56 +00001764Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001765{
Greg Claytonc14ee322011-09-22 04:58:26 +00001766 exe_ctx.Clear();
1767 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768}
1769
1770PathMappingList &
1771Target::GetImageSearchPathList ()
1772{
1773 return m_image_search_paths;
1774}
1775
1776void
1777Target::ImageSearchPathsChanged
1778(
1779 const PathMappingList &path_list,
1780 void *baton
1781)
1782{
1783 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001784 ModuleSP exe_module_sp (target->GetExecutableModule());
1785 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001786 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001787}
1788
1789ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001790Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001791{
Greg Clayton73da2442011-08-03 01:23:55 +00001792 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001793 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001794 {
Greg Clayton73da2442011-08-03 01:23:55 +00001795 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001796 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001797 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001798 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001799 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1800 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001801 return m_scratch_ast_context_ap.get();
1802}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001803
Sean Callanan686b2312011-11-16 18:20:47 +00001804ClangASTImporter *
1805Target::GetClangASTImporter()
1806{
1807 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1808
1809 if (!ast_importer)
1810 {
1811 ast_importer = new ClangASTImporter();
1812 m_ast_importer_ap.reset(ast_importer);
1813 }
1814
1815 return ast_importer;
1816}
1817
Greg Clayton99d0faf2010-11-18 23:32:35 +00001818void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001819Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001820{
Greg Clayton6920b522012-08-22 18:39:03 +00001821 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001822}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001823
Greg Clayton99d0faf2010-11-18 23:32:35 +00001824void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001825Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001826{
Greg Clayton6920b522012-08-22 18:39:03 +00001827 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001828}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001829
Greg Claytonc859e2d2012-02-13 23:10:39 +00001830FileSpecList
1831Target::GetDefaultExecutableSearchPaths ()
1832{
Greg Clayton67cc0632012-08-22 17:17:09 +00001833 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1834 if (properties_sp)
1835 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001836 return FileSpecList();
1837}
1838
Michael Sartaina7499c92013-07-01 19:45:50 +00001839FileSpecList
1840Target::GetDefaultDebugFileSearchPaths ()
1841{
1842 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1843 if (properties_sp)
1844 return properties_sp->GetDebugFileSearchPaths();
1845 return FileSpecList();
1846}
1847
Caroline Ticedaccaa92010-09-20 20:44:43 +00001848ArchSpec
1849Target::GetDefaultArchitecture ()
1850{
Greg Clayton67cc0632012-08-22 17:17:09 +00001851 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1852 if (properties_sp)
1853 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001854 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001855}
1856
1857void
Greg Clayton67cc0632012-08-22 17:17:09 +00001858Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001859{
Greg Clayton67cc0632012-08-22 17:17:09 +00001860 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1861 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001862 {
1863 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 +00001864 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001865 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001866}
1867
Greg Clayton0603aa92010-10-04 01:05:56 +00001868Target *
1869Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1870{
1871 // The target can either exist in the "process" of ExecutionContext, or in
1872 // the "target_sp" member of SymbolContext. This accessor helper function
1873 // will get the target from one of these locations.
1874
1875 Target *target = NULL;
1876 if (sc_ptr != NULL)
1877 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001878 if (target == NULL && exe_ctx_ptr)
1879 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001880 return target;
1881}
1882
Jim Ingham1624a2d2014-05-05 02:26:40 +00001883ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001884Target::EvaluateExpression
1885(
1886 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001887 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001888 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001889 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001890)
1891{
Enrico Granata97fca502012-09-18 17:43:16 +00001892 result_valobj_sp.reset();
1893
Jim Ingham8646d3c2014-05-05 02:47:44 +00001894 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001895
Greg Claytond1767f02011-12-08 02:13:16 +00001896 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1897 return execution_results;
1898
Jim Ingham6026ca32011-05-12 02:06:14 +00001899 // We shouldn't run stop hooks in expressions.
1900 // Be sure to reset this if you return anywhere within this function.
1901 bool old_suppress_value = m_suppress_stop_hooks;
1902 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001903
1904 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001905
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001906 if (frame)
1907 {
1908 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001909 }
1910 else if (m_process_sp)
1911 {
1912 m_process_sp->CalculateExecutionContext(exe_ctx);
1913 }
1914 else
1915 {
1916 CalculateExecutionContext(exe_ctx);
1917 }
1918
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001919 // Make sure we aren't just trying to see the value of a persistent
1920 // variable (something like "$0")
1921 lldb::ClangExpressionVariableSP persistent_var_sp;
1922 // Only check for persistent variables the expression starts with a '$'
1923 if (expr_cstr[0] == '$')
1924 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1925
1926 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001927 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001928 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00001929 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001930 }
1931 else
1932 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001933 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00001934 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001935 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001936 options,
1937 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001938 prefix,
1939 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001940 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001941 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001942
1943 m_suppress_stop_hooks = old_suppress_value;
1944
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001945 return execution_results;
1946}
1947
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001948lldb::addr_t
1949Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1950{
1951 addr_t code_addr = load_addr;
1952 switch (m_arch.GetMachine())
1953 {
1954 case llvm::Triple::arm:
1955 case llvm::Triple::thumb:
1956 switch (addr_class)
1957 {
1958 case eAddressClassData:
1959 case eAddressClassDebug:
1960 return LLDB_INVALID_ADDRESS;
1961
1962 case eAddressClassUnknown:
1963 case eAddressClassInvalid:
1964 case eAddressClassCode:
1965 case eAddressClassCodeAlternateISA:
1966 case eAddressClassRuntime:
1967 // Check if bit zero it no set?
1968 if ((code_addr & 1ull) == 0)
1969 {
1970 // Bit zero isn't set, check if the address is a multiple of 2?
1971 if (code_addr & 2ull)
1972 {
1973 // The address is a multiple of 2 so it must be thumb, set bit zero
1974 code_addr |= 1ull;
1975 }
1976 else if (addr_class == eAddressClassCodeAlternateISA)
1977 {
1978 // We checked the address and the address claims to be the alternate ISA
1979 // which means thumb, so set bit zero.
1980 code_addr |= 1ull;
1981 }
1982 }
1983 break;
1984 }
1985 break;
1986
1987 default:
1988 break;
1989 }
1990 return code_addr;
1991}
1992
1993lldb::addr_t
1994Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1995{
1996 addr_t opcode_addr = load_addr;
1997 switch (m_arch.GetMachine())
1998 {
1999 case llvm::Triple::arm:
2000 case llvm::Triple::thumb:
2001 switch (addr_class)
2002 {
2003 case eAddressClassData:
2004 case eAddressClassDebug:
2005 return LLDB_INVALID_ADDRESS;
2006
2007 case eAddressClassInvalid:
2008 case eAddressClassUnknown:
2009 case eAddressClassCode:
2010 case eAddressClassCodeAlternateISA:
2011 case eAddressClassRuntime:
2012 opcode_addr &= ~(1ull);
2013 break;
2014 }
2015 break;
2016
2017 default:
2018 break;
2019 }
2020 return opcode_addr;
2021}
2022
Greg Clayton9585fbf2013-03-19 00:20:55 +00002023SourceManager &
2024Target::GetSourceManager ()
2025{
2026 if (m_source_manager_ap.get() == NULL)
2027 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2028 return *m_source_manager_ap;
2029}
2030
Sean Callanan9998acd2014-12-05 01:21:59 +00002031ClangModulesDeclVendor *
2032Target::GetClangModulesDeclVendor ()
2033{
2034 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2035
2036 {
2037 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2038
2039 if (!m_clang_modules_decl_vendor_ap)
2040 {
2041 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2042 }
2043 }
2044
2045 return m_clang_modules_decl_vendor_ap.get();
2046}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002047
Greg Clayton44d93782014-01-27 23:43:24 +00002048Target::StopHookSP
2049Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002050{
2051 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002052 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2053 m_stop_hooks[new_uid] = stop_hook_sp;
2054 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002055}
2056
2057bool
2058Target::RemoveStopHookByID (lldb::user_id_t user_id)
2059{
2060 size_t num_removed;
2061 num_removed = m_stop_hooks.erase (user_id);
2062 if (num_removed == 0)
2063 return false;
2064 else
2065 return true;
2066}
2067
2068void
2069Target::RemoveAllStopHooks ()
2070{
2071 m_stop_hooks.clear();
2072}
2073
2074Target::StopHookSP
2075Target::GetStopHookByID (lldb::user_id_t user_id)
2076{
2077 StopHookSP found_hook;
2078
2079 StopHookCollection::iterator specified_hook_iter;
2080 specified_hook_iter = m_stop_hooks.find (user_id);
2081 if (specified_hook_iter != m_stop_hooks.end())
2082 found_hook = (*specified_hook_iter).second;
2083 return found_hook;
2084}
2085
2086bool
2087Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2088{
2089 StopHookCollection::iterator specified_hook_iter;
2090 specified_hook_iter = m_stop_hooks.find (user_id);
2091 if (specified_hook_iter == m_stop_hooks.end())
2092 return false;
2093
2094 (*specified_hook_iter).second->SetIsActive (active_state);
2095 return true;
2096}
2097
2098void
2099Target::SetAllStopHooksActiveState (bool active_state)
2100{
2101 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2102 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2103 {
2104 (*pos).second->SetIsActive (active_state);
2105 }
2106}
2107
2108void
2109Target::RunStopHooks ()
2110{
Jim Ingham6026ca32011-05-12 02:06:14 +00002111 if (m_suppress_stop_hooks)
2112 return;
2113
Jim Ingham9575d842011-03-11 03:53:59 +00002114 if (!m_process_sp)
2115 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002116
2117 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2118 // since in that case we do not want to run the stop-hooks
2119 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2120 return;
2121
Jim Ingham9575d842011-03-11 03:53:59 +00002122 if (m_stop_hooks.empty())
2123 return;
2124
2125 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2126
2127 // If there aren't any active stop hooks, don't bother either:
2128 bool any_active_hooks = false;
2129 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2130 {
2131 if ((*pos).second->IsActive())
2132 {
2133 any_active_hooks = true;
2134 break;
2135 }
2136 }
2137 if (!any_active_hooks)
2138 return;
2139
2140 CommandReturnObject result;
2141
2142 std::vector<ExecutionContext> exc_ctx_with_reasons;
2143 std::vector<SymbolContext> sym_ctx_with_reasons;
2144
2145 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2146 size_t num_threads = cur_threadlist.GetSize();
2147 for (size_t i = 0; i < num_threads; i++)
2148 {
2149 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2150 if (cur_thread_sp->ThreadStoppedForAReason())
2151 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002152 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002153 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2154 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2155 }
2156 }
2157
2158 // If no threads stopped for a reason, don't run the stop-hooks.
2159 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2160 if (num_exe_ctx == 0)
2161 return;
2162
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002163 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2164 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002165
2166 bool keep_going = true;
2167 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002168 bool print_hook_header;
2169 bool print_thread_header;
2170
2171 if (num_exe_ctx == 1)
2172 print_thread_header = false;
2173 else
2174 print_thread_header = true;
2175
2176 if (m_stop_hooks.size() == 1)
2177 print_hook_header = false;
2178 else
2179 print_hook_header = true;
2180
Jim Ingham9575d842011-03-11 03:53:59 +00002181 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2182 {
2183 // result.Clear();
2184 StopHookSP cur_hook_sp = (*pos).second;
2185 if (!cur_hook_sp->IsActive())
2186 continue;
2187
2188 bool any_thread_matched = false;
2189 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2190 {
2191 if ((cur_hook_sp->GetSpecifier () == NULL
2192 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2193 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002194 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002195 {
2196 if (!hooks_ran)
2197 {
Jim Ingham9575d842011-03-11 03:53:59 +00002198 hooks_ran = true;
2199 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002200 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002201 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002202 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2203 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2204 NULL);
2205 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002206 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002207 else
Daniel Malead01b2952012-11-29 21:49:15 +00002208 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002209 any_thread_matched = true;
2210 }
2211
Jim Ingham381e25b2011-03-22 01:47:27 +00002212 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002213 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002214
2215 CommandInterpreterRunOptions options;
2216 options.SetStopOnContinue (true);
2217 options.SetStopOnError (true);
2218 options.SetEchoCommands (false);
2219 options.SetPrintResults (true);
2220 options.SetAddToHistory (false);
2221
2222 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2223 &exc_ctx_with_reasons[i],
2224 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002225 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002226
2227 // If the command started the target going again, we should bag out of
2228 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002229 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2230 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002231 {
Daniel Malead01b2952012-11-29 21:49:15 +00002232 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002233 keep_going = false;
2234 }
2235 }
2236 }
2237 }
Jason Molenda879cf772011-09-23 00:42:55 +00002238
Caroline Tice969ed3d2011-05-02 20:41:46 +00002239 result.GetImmediateOutputStream()->Flush();
2240 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002241}
2242
Greg Claytonfbb76342013-11-20 21:07:01 +00002243const TargetPropertiesSP &
2244Target::GetGlobalProperties()
2245{
2246 static TargetPropertiesSP g_settings_sp;
2247 if (!g_settings_sp)
2248 {
2249 g_settings_sp.reset (new TargetProperties (NULL));
2250 }
2251 return g_settings_sp;
2252}
2253
2254Error
2255Target::Install (ProcessLaunchInfo *launch_info)
2256{
2257 Error error;
2258 PlatformSP platform_sp (GetPlatform());
2259 if (platform_sp)
2260 {
2261 if (platform_sp->IsRemote())
2262 {
2263 if (platform_sp->IsConnected())
2264 {
2265 // Install all files that have an install path, and always install the
2266 // main executable when connected to a remote platform
2267 const ModuleList& modules = GetImages();
2268 const size_t num_images = modules.GetSize();
2269 for (size_t idx = 0; idx < num_images; ++idx)
2270 {
2271 const bool is_main_executable = idx == 0;
2272 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2273 if (module_sp)
2274 {
2275 FileSpec local_file (module_sp->GetFileSpec());
2276 if (local_file)
2277 {
2278 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2279 if (!remote_file)
2280 {
2281 if (is_main_executable) // TODO: add setting for always installing main executable???
2282 {
2283 // Always install the main executable
2284 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2285 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2286 }
2287 }
2288 if (remote_file)
2289 {
2290 error = platform_sp->Install(local_file, remote_file);
2291 if (error.Success())
2292 {
2293 module_sp->SetPlatformFileSpec(remote_file);
2294 if (is_main_executable)
2295 {
2296 if (launch_info)
2297 launch_info->SetExecutableFile(remote_file, false);
2298 }
2299 }
2300 else
2301 break;
2302 }
2303 }
2304 }
2305 }
2306 }
2307 }
2308 }
2309 return error;
2310}
Greg Clayton7b242382011-07-08 00:48:09 +00002311
Greg Claytond5944cd2013-12-06 01:12:00 +00002312bool
2313Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2314{
2315 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2316}
2317
2318bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002319Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2320{
2321 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2322}
2323
2324bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002325Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2326{
2327 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2328 if (old_section_load_addr != new_section_load_addr)
2329 {
2330 uint32_t stop_id = 0;
2331 ProcessSP process_sp(GetProcessSP());
2332 if (process_sp)
2333 stop_id = process_sp->GetStopID();
2334 else
2335 stop_id = m_section_load_history.GetLastStopID();
2336 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2337 return true; // Return true if the section load address was changed...
2338 }
2339 return false; // Return false to indicate nothing changed
2340
2341}
2342
Greg Clayton8012cad2014-11-17 19:39:20 +00002343size_t
2344Target::UnloadModuleSections (const ModuleList &module_list)
2345{
2346 size_t section_unload_count = 0;
2347 size_t num_modules = module_list.GetSize();
2348 for (size_t i=0; i<num_modules; ++i)
2349 {
2350 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2351 }
2352 return section_unload_count;
2353}
2354
2355size_t
2356Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2357{
2358 uint32_t stop_id = 0;
2359 ProcessSP process_sp(GetProcessSP());
2360 if (process_sp)
2361 stop_id = process_sp->GetStopID();
2362 else
2363 stop_id = m_section_load_history.GetLastStopID();
2364 SectionList *sections = module_sp->GetSectionList();
2365 size_t section_unload_count = 0;
2366 if (sections)
2367 {
2368 const uint32_t num_sections = sections->GetNumSections(0);
2369 for (uint32_t i = 0; i < num_sections; ++i)
2370 {
2371 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2372 }
2373 }
2374 return section_unload_count;
2375}
2376
Greg Claytond5944cd2013-12-06 01:12:00 +00002377bool
2378Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2379{
2380 uint32_t stop_id = 0;
2381 ProcessSP process_sp(GetProcessSP());
2382 if (process_sp)
2383 stop_id = process_sp->GetStopID();
2384 else
2385 stop_id = m_section_load_history.GetLastStopID();
2386 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2387}
2388
2389bool
2390Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2391{
2392 uint32_t stop_id = 0;
2393 ProcessSP process_sp(GetProcessSP());
2394 if (process_sp)
2395 stop_id = process_sp->GetStopID();
2396 else
2397 stop_id = m_section_load_history.GetLastStopID();
2398 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2399}
2400
2401void
2402Target::ClearAllLoadedSections ()
2403{
2404 m_section_load_history.Clear();
2405}
2406
Greg Claytonb09c5382013-12-13 17:20:18 +00002407
2408Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002409Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002410{
2411 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002412 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2413
2414 if (log)
2415 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2416
Greg Claytonb09c5382013-12-13 17:20:18 +00002417 StateType state = eStateInvalid;
2418
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002419 // Scope to temporarily get the process state in case someone has manually
2420 // remotely connected already to a process and we can skip the platform
2421 // launching.
2422 {
2423 ProcessSP process_sp (GetProcessSP());
2424
2425 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002426 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002427 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002428 if (log)
2429 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2430 }
2431 else
2432 {
2433 if (log)
2434 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2435 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002436 }
2437
Greg Claytonb09c5382013-12-13 17:20:18 +00002438 launch_info.GetFlags().Set (eLaunchFlagDebug);
2439
2440 // Get the value of synchronous execution here. If you wait till after you have started to
2441 // run, then you could have hit a breakpoint, whose command might switch the value, and
2442 // then you'll pick up that incorrect value.
2443 Debugger &debugger = GetDebugger();
2444 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2445
2446 PlatformSP platform_sp (GetPlatform());
2447
2448 // Finalize the file actions, and if none were given, default to opening
2449 // up a pseudo terminal
2450 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002451 if (log)
2452 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2453 __FUNCTION__,
2454 platform_sp ? "true" : "false",
2455 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2456 default_to_use_pty ? "true" : "false");
2457
Greg Claytonb09c5382013-12-13 17:20:18 +00002458 launch_info.FinalizeFileActions (this, default_to_use_pty);
2459
2460 if (state == eStateConnected)
2461 {
2462 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2463 {
2464 error.SetErrorString("can't launch in tty when launching through a remote connection");
2465 return error;
2466 }
2467 }
2468
2469 if (!launch_info.GetArchitecture().IsValid())
2470 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002471
2472 // If we're not already connected to the process, and if we have a platform that can launch a process for debugging, go ahead and do that here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002473 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2474 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002475 if (log)
2476 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2477
Greg Claytonb09c5382013-12-13 17:20:18 +00002478 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2479 debugger,
2480 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002481 error);
2482 }
2483 else
2484 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002485 if (log)
2486 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2487
Greg Claytonb09c5382013-12-13 17:20:18 +00002488 if (state == eStateConnected)
2489 {
2490 assert(m_process_sp);
2491 }
2492 else
2493 {
Todd Fiala015d8182014-07-22 23:41:36 +00002494 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002495 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002496 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002497 }
Todd Fiala015d8182014-07-22 23:41:36 +00002498
2499 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002500 if (m_process_sp)
2501 error = m_process_sp->Launch (launch_info);
2502 }
2503
2504 if (!m_process_sp)
2505 {
2506 if (error.Success())
2507 error.SetErrorString("failed to launch or debug process");
2508 return error;
2509 }
2510
2511 if (error.Success())
2512 {
2513 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
2514 {
Greg Clayton44d93782014-01-27 23:43:24 +00002515 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00002516
Greg Claytondc6224e2014-10-21 01:00:42 +00002517 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002518
2519 if (state == eStateStopped)
2520 {
Greg Clayton44d93782014-01-27 23:43:24 +00002521 if (!synchronous_execution)
2522 m_process_sp->RestoreProcessEvents ();
2523
2524 error = m_process_sp->PrivateResume();
Todd Fialaa3b89e22014-08-12 14:33:19 +00002525
Greg Claytonb09c5382013-12-13 17:20:18 +00002526 if (error.Success())
2527 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00002528 // there is a race condition where this thread will return up the call stack to the main command
2529 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
2530 // a chance to call PushProcessIOHandler()
2531 m_process_sp->SyncIOHandler(2000);
2532
Greg Claytonb09c5382013-12-13 17:20:18 +00002533 if (synchronous_execution)
2534 {
Greg Claytondc6224e2014-10-21 01:00:42 +00002535 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
Greg Claytonb09c5382013-12-13 17:20:18 +00002536 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2537 if (!StateIsStoppedState(state, must_be_alive))
2538 {
Greg Clayton44d93782014-01-27 23:43:24 +00002539 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
Greg Claytonb09c5382013-12-13 17:20:18 +00002540 }
2541 }
2542 }
2543 else
2544 {
Greg Clayton44d93782014-01-27 23:43:24 +00002545 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002546 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002547 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002548 }
2549 }
Greg Clayton40286e02014-04-30 20:29:09 +00002550 else if (state == eStateExited)
2551 {
Zachary Turner10687b02014-10-20 17:46:43 +00002552 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002553 const int exit_status = m_process_sp->GetExitStatus();
2554 const char *exit_desc = m_process_sp->GetExitDescription();
2555#define LAUNCH_SHELL_MESSAGE "\n'r' and 'run' are aliases that default to launching through a shell.\nTry launching without going through a shell by using 'process launch'."
2556 if (exit_desc && exit_desc[0])
2557 {
2558 if (with_shell)
2559 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2560 else
2561 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2562 }
2563 else
2564 {
2565 if (with_shell)
2566 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2567 else
2568 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2569 }
2570 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002571 else
2572 {
2573 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2574 }
2575 }
Greg Clayton44d93782014-01-27 23:43:24 +00002576 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002577 }
2578 else
2579 {
Greg Clayton44d93782014-01-27 23:43:24 +00002580 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002581 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002582 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002583 }
2584 return error;
2585}
Jim Ingham9575d842011-03-11 03:53:59 +00002586//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002587// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002588//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002589Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2590 UserID (uid),
2591 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002592 m_commands (),
2593 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002594 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002595 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002596{
2597}
2598
2599Target::StopHook::StopHook (const StopHook &rhs) :
2600 UserID (rhs.GetID()),
2601 m_target_sp (rhs.m_target_sp),
2602 m_commands (rhs.m_commands),
2603 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002604 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002605 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002606{
2607 if (rhs.m_thread_spec_ap.get() != NULL)
2608 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2609}
2610
2611
2612Target::StopHook::~StopHook ()
2613{
2614}
2615
2616void
2617Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2618{
2619 m_thread_spec_ap.reset (specifier);
2620}
2621
2622
2623void
2624Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2625{
2626 int indent_level = s->GetIndentLevel();
2627
2628 s->SetIndentLevel(indent_level + 2);
2629
Daniel Malead01b2952012-11-29 21:49:15 +00002630 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002631 if (m_active)
2632 s->Indent ("State: enabled\n");
2633 else
2634 s->Indent ("State: disabled\n");
2635
2636 if (m_specifier_sp)
2637 {
2638 s->Indent();
2639 s->PutCString ("Specifier:\n");
2640 s->SetIndentLevel (indent_level + 4);
2641 m_specifier_sp->GetDescription (s, level);
2642 s->SetIndentLevel (indent_level + 2);
2643 }
2644
2645 if (m_thread_spec_ap.get() != NULL)
2646 {
2647 StreamString tmp;
2648 s->Indent("Thread:\n");
2649 m_thread_spec_ap->GetDescription (&tmp, level);
2650 s->SetIndentLevel (indent_level + 4);
2651 s->Indent (tmp.GetData());
2652 s->PutCString ("\n");
2653 s->SetIndentLevel (indent_level + 2);
2654 }
2655
2656 s->Indent ("Commands: \n");
2657 s->SetIndentLevel (indent_level + 4);
2658 uint32_t num_commands = m_commands.GetSize();
2659 for (uint32_t i = 0; i < num_commands; i++)
2660 {
2661 s->Indent(m_commands.GetStringAtIndex(i));
2662 s->PutCString ("\n");
2663 }
2664 s->SetIndentLevel (indent_level);
2665}
2666
Greg Clayton67cc0632012-08-22 17:17:09 +00002667//--------------------------------------------------------------
2668// class TargetProperties
2669//--------------------------------------------------------------
2670
2671OptionEnumValueElement
2672lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002673{
Greg Clayton67cc0632012-08-22 17:17:09 +00002674 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2675 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2676 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2677 { 0, NULL, NULL }
2678};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002679
Greg Clayton1f746072012-08-29 21:13:06 +00002680static OptionEnumValueElement
2681g_inline_breakpoint_enums[] =
2682{
2683 { 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."},
2684 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2685 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2686 { 0, NULL, NULL }
2687};
2688
Jim Ingham0f063ba2013-03-02 00:26:47 +00002689typedef enum x86DisassemblyFlavor
2690{
2691 eX86DisFlavorDefault,
2692 eX86DisFlavorIntel,
2693 eX86DisFlavorATT
2694} x86DisassemblyFlavor;
2695
2696static OptionEnumValueElement
2697g_x86_dis_flavor_value_types[] =
2698{
2699 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2700 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2701 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2702 { 0, NULL, NULL }
2703};
2704
Enrico Granata397ddd52013-05-21 20:13:34 +00002705static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002706g_hex_immediate_style_values[] =
2707{
2708 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2709 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2710 { 0, NULL, NULL }
2711};
2712
2713static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002714g_load_script_from_sym_file_values[] =
2715{
2716 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2717 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2718 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2719 { 0, NULL, NULL }
2720};
2721
Greg Claytonfd814c52013-08-13 01:42:25 +00002722
2723static OptionEnumValueElement
2724g_memory_module_load_level_values[] =
2725{
Greg Clayton86eac942013-08-13 21:32:34 +00002726 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002727 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2728 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2729 { 0, NULL, NULL }
2730};
2731
Greg Clayton67cc0632012-08-22 17:17:09 +00002732static PropertyDefinition
2733g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002734{
Greg Clayton67cc0632012-08-22 17:17:09 +00002735 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2736 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2737 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2738 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2739 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2740 { "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 "
2741 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2742 "some part (starting at the root) of the path to the file when it was built, "
2743 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2744 "Each element of the array is checked in order and the first one that results in a match wins." },
2745 { "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 +00002746 { "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 +00002747 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2748 { "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 +00002749 { "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 +00002750 { "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 +00002751 { "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." },
2752 { "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 +00002753 { "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." },
2754 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2755 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2756 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2757 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
Jim Ingham106d0282014-06-25 02:32:56 +00002758 { "detach-on-error" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "debugserver will detach (rather than killing) a process if it loses connection with lldb." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002759 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2760 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Disable stdin/stdout for process (e.g. for a GUI application)" },
Todd Fialaad6eee62014-09-24 19:59:13 +00002761 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsAlways , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
Greg Clayton1f746072012-08-29 21:13:06 +00002762 "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. "
2763 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2764 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002765 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2766 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2767 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002768 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002769 // 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.
2770 { "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 +00002771 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2772 { "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 +00002773 { "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 +00002774 { "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 +00002775 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2776 "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. "
2777 "This setting helps users control how much information gets loaded when loading modules from memory."
2778 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2779 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2780 "'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 Claytonfb6621e2013-12-06 21:59:52 +00002781 { "display-expression-in-crashlogs" , OptionValue::eTypeBoolean , false, false, NULL, NULL, "Expressions that crash will show up in crash logs if the host system supports executable specific crash log strings and this setting is set to true." },
Jason Molendaa4bea722014-02-14 05:06:49 +00002782 { "trap-handler-names" , OptionValue::eTypeArray , true, OptionValue::eTypeString, NULL, NULL, "A list of trap handler function names, e.g. a common Unix user process one is _sigtramp." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002783 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2784};
2785enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002786{
Greg Clayton67cc0632012-08-22 17:17:09 +00002787 ePropertyDefaultArch,
2788 ePropertyExprPrefix,
2789 ePropertyPreferDynamic,
2790 ePropertyEnableSynthetic,
2791 ePropertySkipPrologue,
2792 ePropertySourceMap,
2793 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002794 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002795 ePropertyMaxChildrenCount,
2796 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002797 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002798 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002799 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002800 ePropertyRunArgs,
2801 ePropertyEnvVars,
2802 ePropertyInheritEnv,
2803 ePropertyInputPath,
2804 ePropertyOutputPath,
2805 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00002806 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00002807 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002808 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002809 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00002810 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00002811 ePropertyUseHexImmediates,
2812 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002813 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00002814 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00002815 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00002816 ePropertyDisplayExpressionsInCrashlogs,
2817 ePropertyTrapHandlerNames
Greg Clayton67cc0632012-08-22 17:17:09 +00002818};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002819
Caroline Ticedaccaa92010-09-20 20:44:43 +00002820
Greg Clayton67cc0632012-08-22 17:17:09 +00002821class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002822{
Greg Clayton67cc0632012-08-22 17:17:09 +00002823public:
2824 TargetOptionValueProperties (const ConstString &name) :
2825 OptionValueProperties (name),
2826 m_target (NULL),
2827 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002828 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002829 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002830
Greg Clayton67cc0632012-08-22 17:17:09 +00002831 // This constructor is used when creating TargetOptionValueProperties when it
2832 // is part of a new lldb_private::Target instance. It will copy all current
2833 // global property values as needed
2834 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2835 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2836 m_target (target),
2837 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002838 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002839 }
2840
2841 virtual const Property *
2842 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2843 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002844 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00002845 // try and grab the setting from the current target if there is one. Else we just
2846 // use the one from this instance.
2847 if (idx == ePropertyEnvVars)
2848 GetHostEnvironmentIfNeeded ();
2849
2850 if (exe_ctx)
2851 {
2852 Target *target = exe_ctx->GetTargetPtr();
2853 if (target)
2854 {
2855 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2856 if (this != target_properties)
2857 return target_properties->ProtectedGetPropertyAtIndex (idx);
2858 }
2859 }
2860 return ProtectedGetPropertyAtIndex (idx);
2861 }
Enrico Granata84a53df2013-05-20 22:29:23 +00002862
2863 lldb::TargetSP
2864 GetTargetSP ()
2865 {
2866 return m_target->shared_from_this();
2867 }
2868
Greg Clayton67cc0632012-08-22 17:17:09 +00002869protected:
2870
2871 void
2872 GetHostEnvironmentIfNeeded () const
2873 {
2874 if (!m_got_host_env)
2875 {
2876 if (m_target)
2877 {
2878 m_got_host_env = true;
2879 const uint32_t idx = ePropertyInheritEnv;
2880 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2881 {
2882 PlatformSP platform_sp (m_target->GetPlatform());
2883 if (platform_sp)
2884 {
2885 StringList env;
2886 if (platform_sp->GetEnvironment(env))
2887 {
2888 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2889 if (env_dict)
2890 {
2891 const bool can_replace = false;
2892 const size_t envc = env.GetSize();
2893 for (size_t idx=0; idx<envc; idx++)
2894 {
2895 const char *env_entry = env.GetStringAtIndex (idx);
2896 if (env_entry)
2897 {
2898 const char *equal_pos = ::strchr(env_entry, '=');
2899 ConstString key;
2900 // It is ok to have environment variables with no values
2901 const char *value = NULL;
2902 if (equal_pos)
2903 {
2904 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2905 if (equal_pos[1])
2906 value = equal_pos + 1;
2907 }
2908 else
2909 {
2910 key.SetCString(env_entry);
2911 }
2912 // Don't allow existing keys to be replaced with ones we get from the platform environment
2913 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2914 }
2915 }
2916 }
2917 }
2918 }
2919 }
2920 }
2921 }
2922 }
2923 Target *m_target;
2924 mutable bool m_got_host_env;
2925};
2926
Greg Claytonfbb76342013-11-20 21:07:01 +00002927//----------------------------------------------------------------------
2928// TargetProperties
2929//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00002930TargetProperties::TargetProperties (Target *target) :
2931 Properties ()
2932{
2933 if (target)
2934 {
2935 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Ticedaccaa92010-09-20 20:44:43 +00002936 }
2937 else
Greg Clayton67cc0632012-08-22 17:17:09 +00002938 {
2939 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2940 m_collection_sp->Initialize(g_properties);
2941 m_collection_sp->AppendProperty(ConstString("process"),
2942 ConstString("Settings specify to processes."),
2943 true,
2944 Process::GetGlobalProperties()->GetValueProperties());
2945 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002946}
2947
Greg Clayton67cc0632012-08-22 17:17:09 +00002948TargetProperties::~TargetProperties ()
2949{
2950}
2951ArchSpec
2952TargetProperties::GetDefaultArchitecture () const
2953{
2954 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2955 if (value)
2956 return value->GetCurrentValue();
2957 return ArchSpec();
2958}
2959
2960void
2961TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2962{
2963 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2964 if (value)
2965 return value->SetCurrentValue(arch, true);
2966}
2967
2968lldb::DynamicValueType
2969TargetProperties::GetPreferDynamicValue() const
2970{
2971 const uint32_t idx = ePropertyPreferDynamic;
2972 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2973}
2974
2975bool
2976TargetProperties::GetDisableASLR () const
2977{
2978 const uint32_t idx = ePropertyDisableASLR;
2979 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2980}
2981
2982void
2983TargetProperties::SetDisableASLR (bool b)
2984{
2985 const uint32_t idx = ePropertyDisableASLR;
2986 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2987}
2988
2989bool
Jim Ingham106d0282014-06-25 02:32:56 +00002990TargetProperties::GetDetachOnError () const
2991{
2992 const uint32_t idx = ePropertyDetachOnError;
2993 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2994}
2995
2996void
2997TargetProperties::SetDetachOnError (bool b)
2998{
2999 const uint32_t idx = ePropertyDetachOnError;
3000 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3001}
3002
3003bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003004TargetProperties::GetDisableSTDIO () const
3005{
3006 const uint32_t idx = ePropertyDisableSTDIO;
3007 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3008}
3009
3010void
3011TargetProperties::SetDisableSTDIO (bool b)
3012{
3013 const uint32_t idx = ePropertyDisableSTDIO;
3014 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3015}
3016
Jim Ingham0f063ba2013-03-02 00:26:47 +00003017const char *
3018TargetProperties::GetDisassemblyFlavor () const
3019{
3020 const uint32_t idx = ePropertyDisassemblyFlavor;
3021 const char *return_value;
3022
3023 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3024 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3025 return return_value;
3026}
3027
Greg Clayton1f746072012-08-29 21:13:06 +00003028InlineStrategy
3029TargetProperties::GetInlineStrategy () const
3030{
3031 const uint32_t idx = ePropertyInlineStrategy;
3032 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3033}
3034
Greg Clayton45392552012-10-17 22:57:12 +00003035const char *
3036TargetProperties::GetArg0 () const
3037{
3038 const uint32_t idx = ePropertyArg0;
3039 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3040}
3041
3042void
3043TargetProperties::SetArg0 (const char *arg)
3044{
3045 const uint32_t idx = ePropertyArg0;
3046 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
3047}
3048
Greg Clayton67cc0632012-08-22 17:17:09 +00003049bool
3050TargetProperties::GetRunArguments (Args &args) const
3051{
3052 const uint32_t idx = ePropertyRunArgs;
3053 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3054}
3055
3056void
3057TargetProperties::SetRunArguments (const Args &args)
3058{
3059 const uint32_t idx = ePropertyRunArgs;
3060 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3061}
3062
3063size_t
3064TargetProperties::GetEnvironmentAsArgs (Args &env) const
3065{
3066 const uint32_t idx = ePropertyEnvVars;
3067 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3068}
3069
3070bool
3071TargetProperties::GetSkipPrologue() const
3072{
3073 const uint32_t idx = ePropertySkipPrologue;
3074 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3075}
3076
3077PathMappingList &
3078TargetProperties::GetSourcePathMap () const
3079{
3080 const uint32_t idx = ePropertySourceMap;
3081 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3082 assert(option_value);
3083 return option_value->GetCurrentValue();
3084}
3085
3086FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003087TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003088{
3089 const uint32_t idx = ePropertyExecutableSearchPaths;
3090 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3091 assert(option_value);
3092 return option_value->GetCurrentValue();
3093}
3094
Michael Sartaina7499c92013-07-01 19:45:50 +00003095FileSpecList &
3096TargetProperties::GetDebugFileSearchPaths ()
3097{
3098 const uint32_t idx = ePropertyDebugFileSearchPaths;
3099 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3100 assert(option_value);
3101 return option_value->GetCurrentValue();
3102}
3103
Greg Clayton67cc0632012-08-22 17:17:09 +00003104bool
3105TargetProperties::GetEnableSyntheticValue () const
3106{
3107 const uint32_t idx = ePropertyEnableSynthetic;
3108 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3109}
3110
3111uint32_t
3112TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3113{
3114 const uint32_t idx = ePropertyMaxChildrenCount;
3115 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3116}
3117
3118uint32_t
3119TargetProperties::GetMaximumSizeOfStringSummary() const
3120{
3121 const uint32_t idx = ePropertyMaxSummaryLength;
3122 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3123}
3124
Enrico Granatad325bf92013-06-04 22:54:16 +00003125uint32_t
3126TargetProperties::GetMaximumMemReadSize () const
3127{
3128 const uint32_t idx = ePropertyMaxMemReadSize;
3129 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3130}
3131
Greg Clayton67cc0632012-08-22 17:17:09 +00003132FileSpec
3133TargetProperties::GetStandardInputPath () const
3134{
3135 const uint32_t idx = ePropertyInputPath;
3136 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3137}
3138
3139void
3140TargetProperties::SetStandardInputPath (const char *p)
3141{
3142 const uint32_t idx = ePropertyInputPath;
3143 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3144}
3145
3146FileSpec
3147TargetProperties::GetStandardOutputPath () const
3148{
3149 const uint32_t idx = ePropertyOutputPath;
3150 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3151}
3152
3153void
3154TargetProperties::SetStandardOutputPath (const char *p)
3155{
3156 const uint32_t idx = ePropertyOutputPath;
3157 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3158}
3159
3160FileSpec
3161TargetProperties::GetStandardErrorPath () const
3162{
3163 const uint32_t idx = ePropertyErrorPath;
3164 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3165}
3166
Greg Clayton6920b522012-08-22 18:39:03 +00003167const char *
3168TargetProperties::GetExpressionPrefixContentsAsCString ()
3169{
3170 const uint32_t idx = ePropertyExprPrefix;
3171 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3172 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003173 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003174 const bool null_terminate = true;
3175 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003176 if (data_sp)
3177 return (const char *) data_sp->GetBytes();
3178 }
Greg Clayton6920b522012-08-22 18:39:03 +00003179 return NULL;
3180}
3181
Greg Clayton67cc0632012-08-22 17:17:09 +00003182void
3183TargetProperties::SetStandardErrorPath (const char *p)
3184{
3185 const uint32_t idx = ePropertyErrorPath;
3186 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3187}
3188
3189bool
3190TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3191{
3192 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3193 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3194}
3195
Jim Ingham17d023f2013-03-13 17:58:04 +00003196bool
Daniel Malead79ae052013-08-07 21:54:09 +00003197TargetProperties::GetUseHexImmediates () const
3198{
3199 const uint32_t idx = ePropertyUseHexImmediates;
3200 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3201}
3202
3203bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003204TargetProperties::GetUseFastStepping () const
3205{
3206 const uint32_t idx = ePropertyUseFastStepping;
3207 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3208}
3209
Greg Claytonfb6621e2013-12-06 21:59:52 +00003210bool
3211TargetProperties::GetDisplayExpressionsInCrashlogs () const
3212{
3213 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3214 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3215}
3216
Enrico Granata397ddd52013-05-21 20:13:34 +00003217LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003218TargetProperties::GetLoadScriptFromSymbolFile () const
3219{
3220 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003221 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003222}
3223
Daniel Malead79ae052013-08-07 21:54:09 +00003224Disassembler::HexImmediateStyle
3225TargetProperties::GetHexImmediateStyle () const
3226{
3227 const uint32_t idx = ePropertyHexImmediateStyle;
3228 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3229}
3230
Greg Claytonfd814c52013-08-13 01:42:25 +00003231MemoryModuleLoadLevel
3232TargetProperties::GetMemoryModuleLoadLevel() const
3233{
3234 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3235 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3236}
3237
Jason Molendaa4bea722014-02-14 05:06:49 +00003238bool
3239TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3240{
3241 const uint32_t idx = ePropertyTrapHandlerNames;
3242 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3243}
Greg Claytonfd814c52013-08-13 01:42:25 +00003244
Jason Molendaa4bea722014-02-14 05:06:49 +00003245void
3246TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3247{
3248 const uint32_t idx = ePropertyTrapHandlerNames;
3249 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3250}
Greg Clayton67cc0632012-08-22 17:17:09 +00003251
Greg Claytonfbb76342013-11-20 21:07:01 +00003252//----------------------------------------------------------------------
3253// Target::TargetEventData
3254//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +00003255const ConstString &
3256Target::TargetEventData::GetFlavorString ()
3257{
3258 static ConstString g_flavor ("Target::TargetEventData");
3259 return g_flavor;
3260}
3261
3262const ConstString &
3263Target::TargetEventData::GetFlavor () const
3264{
3265 return TargetEventData::GetFlavorString ();
3266}
3267
3268Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
3269 EventData(),
3270 m_target_sp (new_target_sp)
3271{
3272}
3273
3274Target::TargetEventData::~TargetEventData()
3275{
3276
3277}
3278
3279void
3280Target::TargetEventData::Dump (Stream *s) const
3281{
3282
3283}
3284
3285const TargetSP
3286Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
3287{
3288 TargetSP target_sp;
3289
3290 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
3291 if (data)
3292 target_sp = data->m_target_sp;
3293
3294 return target_sp;
3295}
3296
3297const Target::TargetEventData *
3298Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3299{
3300 if (event_ptr)
3301 {
3302 const EventData *event_data = event_ptr->GetData();
3303 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3304 return static_cast <const TargetEventData *> (event_ptr->GetData());
3305 }
3306 return NULL;
3307}
3308